## ----------------------------------------------------------------------------- x <- 0:7 y <- 0:7 xy <- expand.grid(x, y) colnames(xy) <- c("x", "y") head(xy) ## ----------------------------------------------------------------------------- z <- 2*xy[,1] + 3*xy[,2] zm <- matrix(z, ncol=8) ## ----------------------------------------------------------------------------- detproc <- function(x, y) { z <- 2*x + 3*y return(z) } v <- detproc(xy[,1], xy[,2]) zm <- matrix(v, ncol=8) ## ----p4-1--------------------------------------------------------------------- plot(x, y, type="n") text(xy[,1], xy[,2], z) contour(x, y, zm, add=TRUE, lty=2) ## ----p4-2, fig.width=8, fig.height=3------------------------------------------ library(terra) r <- rast(xmin=0, xmax=7, ymin=0, ymax=7, ncol=8, nrow=8) X <- init(r, "x") Y <- init(r, "y") par(mfrow=c(1,2)) plot(X, main="x") plot(Y, main="y") ## ----------------------------------------------------------------------------- Z <- 2*X + 3*Y ## ----p4-3, fig.cap="", fig.width=6, fig.height=5------------------------------ plot(Z) text(Z, cex=.75) contour(Z, add=TRUE, labcex=1, lwd=2, col="red") ## ----p4-4, fig.height=5------------------------------------------------------- set.seed(987) s <- sample(c(-1, 1), ncell(r), replace=TRUE) s[1:8] R <- setValues(r, s) plot(R) ## ----p4-5, fig.height=5------------------------------------------------------- Z <- 2*X + 3*Y + R plot(Z) text(Z, cex=.75) contour(Z, add=T, labcex=1, lwd=2, col="red") ## ----------------------------------------------------------------------------- f <- function() { s <- sample(c(-1, 1), ncell(r), replace=TRUE) S <- setValues(r, s) Z <- 2*X + 3*Y + S return(Z) } ## ----p4-6, fig.width=8, fig.height=6.5---------------------------------------- set.seed(777) par(mfrow=c(2,2), mai=c(0.5,0.5,0.5,0.5)) for (i in 1:4) { pattern <- f() plot(pattern) text(pattern, cex=.75) contour(pattern, add=TRUE, labcex=1, lwd=2, col="red") } ## ----p4-7--------------------------------------------------------------------- csr <- function(n, r=99, plot=FALSE) { x <- runif(n, max=r) y <- runif(n, max=r) if (plot) { plot(x, y, xlim=c(0,r), ylim=c(0,r)) } } ## ----p4-8, fig.width=6, fig.height=6------------------------------------------ set.seed(0) par(mfrow=c(2,2), mai=c(.5, .5, .5, .5)) for (i in 1:4) { csr(50, plot=TRUE) } ## ----------------------------------------------------------------------------- events <- 0:10 combinations <- choose(10, events) prob1 <- (1/8)^events prob2 <- (7/8)^(10-events) Pk <- combinations * prob1 * prob2 d <- data.frame(events, combinations, prob1, prob2, Pk) round(d, 8) sum(d$Pk) ## ----------------------------------------------------------------------------- b <- dbinom(0:10, 10, 1/8) round(b, 8) ## ----------------------------------------------------------------------------- set.seed(1234) x <- runif(50) * 99 y <- runif(50) * 99 ## ----------------------------------------------------------------------------- r <- rast(xmin=0, xmax=99, ymin=0, ymax=99, ncol=10, nrow=10) quads <- as.polygons(r) ## ----p4-9, fig.cap=""--------------------------------------------------------- plot(quads, border="gray", pax=list(las=1)) points(x, y, col="red", pch=20) ## ----p4-10, fig.cap="", fig.width=6, fig.height=5----------------------------- vxy <- vect(cbind(x,y)) vxy$v <- 1 p <- rasterize(vxy, r, "v", fun=length, background=0) plot(p) plot(quads, add=TRUE, border="gray") points(x, y, pch=20) ## ----p4-12, fig.cap=""-------------------------------------------------------- f <- freq(p) f barplot(p) ## ----p4-13, fig.cap=""-------------------------------------------------------- n <- 0:8 prob <- 1 / ncell(r) size <- 50 expected <- dbinom(n, size, prob) round(expected, 5) plot(n, expected, cex=2, pch="x", col="blue") ## ----p4-15, fig.cap=""-------------------------------------------------------- m <- rbind(f[,3]/100, expected[1:nrow(f)]) bp <- barplot(m, beside=T, names.arg =1:nrow(f), space=c(0.1, 0.5), ylim=c(0,0.7), col=c("red", "blue")) text(bp, m, labels=round(m, 2), pos = 3, cex = .75) legend(11, 0.7, c("Observed", "Expected"), fill=c("red", "blue")) ## ----p4-16, fig.cap=""-------------------------------------------------------- poisexp <- dpois(0:8, lambda=50/100) poisexp plot(expected, poisexp, cex=2) abline(0,1) ## ----p4-rlines1--------------------------------------------------------------- randomLineInRectangle <- function(xmn=0, xmx=0.8, ymn=0, ymx=0.6, retXY=FALSE) { x <- runif(1, xmn, xmx) y <- runif(1, ymn, ymx) angle <- runif(1, 0, 359.99999999) if (angle == 0) { # vertical line, tan is infinite if (retXY) { xy <- rbind(c(x, ymn), c(x, y), c(x, ymx)) return(xy) } return(ymx - ymn) } tang <- tan(pi*angle/180) x1 <- max(xmn, min(xmx, x - y / tang)) x2 <- max(xmn, min(xmx, x + (ymx-y) / tang)) y1 <- max(ymn, min(ymx, y - (x-x1) * tang)) y2 <- max(ymn, min(ymx, y + (x2-x) * tang)) if (retXY) { xy <- rbind(c(x1, y1), c(x, y), c(x2, y2)) return(xy) } sqrt((x2 - x1)^2 + (y2 - y1)^2) } ## ----p4-rlines2--------------------------------------------------------------- randomLineInRectangle() randomLineInRectangle() set.seed(999) plot(NA, xlim=c(0, 0.8), ylim=c(0, 0.6), xaxs="i", yaxs="i", xlab="", ylab="", las=1) for (i in 1:4) { xy <- randomLineInRectangle(retXY=TRUE) lines(xy, lwd=2, col=i) #points(xy, cex=2, pch=20, col=i) } ## ----p4-rlines3--------------------------------------------------------------- r <- replicate(10000, randomLineInRectangle()) hist(r, breaks=seq(0,1,0.01), xlab="Line length", main="", freq=FALSE) ## ----p4-seats----------------------------------------------------------------- set.seed(0) x <- replicate(10000, abs(diff(sample(1:4, 2)))) sum(x==2) / length(x) ## ----p4-19-------------------------------------------------------------------- r <- rast(xmin=0, xmax=1, ymin=0, ymax=1, ncol=8, nrow=8) p <- as.polygons(r) chess <- function() { s <- sample(c(-1, 1), 64, replace=TRUE) values(r) <- s plot(r, col=c("black", "white"), legend=FALSE, axes=FALSE, mar=c(1,1,1,1)) plot(p, add=T, border="gray") } ## ----p4-20-------------------------------------------------------------------- set.seed(0) par(mfrow=c(2,2)) for (i in 1:4) { chess() } ## ----p4-21, fig.width=6, fig.height=5----------------------------------------- r <- rast(xmin=0, xmax=1, ymin=0, ymax=1, ncol=20, nrow=20) values(r) <- rnorm(ncell(r), 0, 2) plot(r) contour(r, add=T) ## ----p4-22, fig.width=6, fig.height=5----------------------------------------- ra <- focal(r, w=matrix(1/9, nc=3, nr=3), na.rm=TRUE) ra <- focal(ra, w=matrix(1/9, nc=3, nr=3), na.rm=TRUE) plot(ra)