# NOT RUN {
data(vipers)
data(d.gen)
data(grid)
# create a matrix of distances from sample points (columns) to all
# grid pixels
rd <- geo.dist(grid, vipers[,1:2])
#interpolate with idw
result <- intgen.idw(rd, d.gen)
#plot the 12 random interpolations
layout(matrix(c(1:12), 4,3))
for (i in sample(1:nrow(vipers), 12))
{
dt <- data.frame(grid, int=result[,i])
# when samples are given with real coordinates, aspect of image
# should be maintained with asp=1 to plot properly.
image(sort(unique(grid[,1])), sort(unique(grid[,2])),
xtabs(int~x+y, dt), xlab='Longitude', ylab='Latitude',
main=colnames(result)[i])
cex <- (d.gen[,i]-min(d.gen[,i]))/(max(d.gen[,i])-min(d.gen[,i]))
points(vipers[,1:2], cex=cex+0.5)
}
# }
# NOT RUN {
# Compare 'idw' with 'intgen.idw' to generate the same results.
# NOTE: it may take a few minutes to run.
result2 <- matrix(NA, nrow=nrow(grid), ncol=nrow(vipers))
for (i in 1:nrow(vipers)) {
values <- d.gen[i,]
intpl <- idw(values, vipers[,1:2], grid)
result2[,i] <- intpl[,1]
}
colnames(result2) <- rownames(vipers)
# compare all items in the two matrices to check equality:
all(result == result2)
# }
Run the code above in your browser using DataLab