View source: R/interpolateRasters.r
interpolateRasters | R Documentation |
This function returns a stack of rasters interpolated from a stack of rasters. For example, the input might represent rasters of a process measured at times t, t + 1, and t + 4. The rasters at t + 2 and t + 3 could be interpolated based on the values in the other rasters. Note that this function can take a lot of time and memory, even for relatively small rasters.
interpolateRasters( rasts, interpFrom, interpTo, type = "linear", onFail = NA, useRasts = FALSE, na.rm = TRUE, verbose = TRUE, ... )
rasts |
A "stack" of |
interpFrom |
Numeric vector, one value per raster in |
interpTo |
Numeric vector, values of "distances" at which to interpolate the rasters. |
type |
Character. The type of model used to do the interpolation. Note that some of these (the first few) are guaranteed to go through every point being interpolated from. The second set, however, are effectively regressions so are not guaranteed to do through any of the points. Note that some methods cannot handle cases where at least some series of cells have < a given number of non-
|
onFail |
Either |
useRasts |
Logical. If |
na.rm |
Logical, if |
verbose |
Logical. If |
... |
Other arguments passed to |
This function can be very memory-intensive for large rasters. It may speed things up (and make them possible) to do interpolations piece by piece (e.g., instead of interpolating between times t0, t1, t2, t3, ..., interpolate between t0 and t1, then t1 and t2, etc. This may give results that differ from using the entire set, however. Note that using linear and splines will often yield very similar results except that in a small number of cases splines may produce very extreme interpolated values.
A raster stack with one layer per element in interpTo
.
approxNA
, approxfun
, splinefun
, trainNs
, glm
, , bs
, smooth.spline
.
## Not run: interpFrom <- c(1, 3, 4, 8, 10, 11, 15) interpTo <- 1:15 rx <- rast(nrows=10, ncols=10) r1 <- setValues(rx, rnorm(100, 1)) r3 <- setValues(rx, rnorm(100, 3)) r4 <- setValues(rx, rnorm(100, 5)) r8 <- setValues(rx, rnorm(100, 11)) r10 <- setValues(rx, rnorm(100, 3)) r11 <- setValues(rx, rnorm(100, 5)) r15 <- setValues(rx, rnorm(100, 13)) rasts <- c(r1, r3, r4, r8, r10, r11, r15) names(rasts) <- paste0('rasts', interpFrom) linear <- interpolateRasters(rasts, interpFrom, interpTo) spline <- interpolateRasters(rasts, interpFrom, interpTo, type='spline') gam <- interpolateRasters(rasts, interpFrom, interpTo, type='gam', onFail='linear') ns <- interpolateRasters(rasts, interpFrom, interpTo, type='ns', onFail='linear', verbose=FALSE) poly <- interpolateRasters(rasts, interpFrom, interpTo, type='poly', onFail='linear') bs <- interpolateRasters(rasts, interpFrom, interpTo, type='bs', onFail='linear') ss <- interpolateRasters(rasts, interpFrom, interpTo, type='smooth.spline', onFail='linear', verbose=FALSE) # examine trends for a particular point on the landscape pts <- rbind(c(-9, 13)) linearExt <- unlist(raster::extract(linear, pts)) splineExt <- unlist(raster::extract(spline, pts)) gamExt <- unlist(raster::extract(gam, pts)) nsExt <- unlist(raster::extract(ns, pts)) polyExt <- unlist(raster::extract(poly, pts)) bsExt <- unlist(raster::extract(bs, pts)) ssExt <- unlist(raster::extract(ss, pts)) mins <- min(linearExt, splineExt, gamExt, nsExt, polyExt, bsExt, ssExt) maxs <- max(linearExt, splineExt, gamExt, nsExt, polyExt, bsExt, ssExt) plot(interpTo, linearExt, type='l', lwd=2, ylim=c(mins, maxs), ylab='Value') lines(interpTo, splineExt, col='blue') lines(interpTo, gamExt, col='green') lines(interpTo, nsExt, col='orange') lines(interpTo, polyExt, col='gray') lines(interpTo, bsExt, col='magenta') lines(interpTo, ssExt, col='cyan') ext <- c(extract(rasts, pts)) points(interpFrom, ext) legend('topleft', inset=0.01, lty=c(rep(1, 7), NA), legend=c('linear', 'spline', 'GAM', 'NS', 'polynomial', 'B-spline', 'Smooth spline', 'Observed'), col=c('black', 'blue', 'green', 'orange', 'gray', 'magenta', 'cyan'), pch=c(rep(NA, 7), 1)) ## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.