I want to estimate the gradient (slope and aspect) of an undefined surface (i.e., the function is unknown). To test my methods, here is the test data:
require(raster); require(rasterVis)
set.seed(123)
x <- runif(100, min = 0, max = 1)
y <- runif(100, min = 0, max = 1)
e <- 0.5 * rnorm(100)
test <- expand.grid(sort(x),sort(y))
names(test)<-c('X','Y')
z1 <- (5 * test$X^3 + sin(3*pi*test$Y))
realy <- matrix(z1, 100, 100, byrow = F)
# And a few plots for demonstration #
persp(sort(x), sort(y), realy,
xlab = 'X', ylab = "Y", zlab = 'Z',
main = 'Real function (3d)', theta = 30,
phi = 30, ticktype = "simple", cex=1.4)
contour(sort(x), sort(y), realy,
xlab = 'X', ylab = "Y",
main = 'Real function (contours)', cex=1.4)
I convert everything into a raster and plot using rasterVis::vectorplot
. Everything looks fine. The vector field indicates the direction of the highest magnitude of change and the shading is similar to my contour plot...
test.rast <- raster(t(realy), xmn = 0, xmx = 1,
ymn = 0, ymx = 1, crs = CRS("+proj"))
vectorplot(test.rast, par.settings=RdBuTheme, narrow = 100, reverse = T)
However, I need a matrix of slope values. As I understand vectorplot, it uses the raster::terrain function:
terr.mast <- list("slope" = matrix(nrow = 100,
ncol = 100,
terrain(test.rast,
opt = "slope",
unit = "degrees",
reverse = TRUE,
neighbors = 8)@data@values,
byrow = T),
"aspect" = matrix(nrow = 100,
ncol = 100,
terrain(test.rast,
opt = "aspect",
unit = "degrees",
reverse = TRUE,
neighbors = 8)@data@values,
byrow = T))
however, the slope seem really high... ( 90 degrees would be vertical, right?!)
terr.mast$slope[2:6,2:6]
# [,1] [,2] [,3] [,4] [,5]
#[1,] 87.96546 87.96546 87.96546 87.96550 87.96551
#[2,] 84.68628 84.68628 84.68627 84.68702 84.68709
#[3,] 84.41349 84.41350 84.41349 84.41436 84.41444
#[4,] 84.71757 84.71757 84.71756 84.71830 84.71837
#[5,] 79.48740 79.48741 79.48735 79.49315 79.49367
and if I plot the slope and aspect, they don't seem to agree with the vectorplot graphic.
plot(terrain(test.rast, opt = c("slope", "aspect"), unit = "degrees",
reverse = TRUE, neighbors = 8))
My thoughts:
- Vectorplot must be smoothing the slope, but how?
- I am fairly certain that
raster::terrain
is using a roving-window method to calculate slope. Perhaps the window is too small... can this be expanded? - Am I going about this in an inappropriate fashion? How else might I estimate the slope of an undefined surface?