Helper function for applying predict methods to stacks of grids.
This function can be used to apply the predict method of hopefully any fitted predictive model pixel by pixel to a stack of grids representing the explanatory variables. It is intended to be called primarily by
a model object for which prediction is desired
optional prediction function; if missing, the
an optional list of arguments to be passed on to
optional character string: Some predict methods (e.g.
integer >=0: positive values give more (=2) or less (=1) information on predictor variables and predictions
optional location data received from
these arguments are provided by the calling function, usually
grid.predict is a simple wrapper function. First it binds the arguments in
... together in a
data.frame with the raw predictor variables that have been read from their grids by the caller,
multi.focal.function). Then it calls the optional
trafo function to transform or combine predictor variables (e.g. perform log transformations, ratioing, arithmetic operations such as calculating the NDVI). Finally the
predfun (or, typically, the default
predict method of
fit) is called, handing over the
fit, the predictor
data.frame, and the optional
grid.predict returns the result of the call to
predfun or the default
grid.predict can in principle deal with
predict methods returning factor variables, its usual caller
multi.focal.function cannot; classification models should be dealt with by setting a
type="response" (for logistic regression and logistic additive model) argument, for example (see second Example below).
Brenning, A. (2008): Statistical geocomputing combining R and SAGA: The example of landslide susceptibility analysis with generalized additive models. In: J. Boehner, T. Blaschke, L. Montanarella (eds.), SAGA - Seconds Out (= Hamburger Beitraege zur Physischen Geographie und Landschaftsoekologie, 19), 23-32.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
## Not run: # Assume that d is a data.frame with point observations # of a numerical response variable y and predictor variables # a, b, and c. # Fit a generalized additive model to y,a,b,c. # We want to model b and c as nonlinear terms: require(gam) fit <- gam(y ~ a + s(b) + s(c), data = d) multi.local.function(in.grids = c("a", "b", "c"), out.varnames = "pred", fun = grid.predict, fit = fit ) # Note that the 'grid.predict' uses by default the # predict method of 'fit'. # Model predictions are written to a file named pred.asc ## End(Not run) ## Not run: # A fake example of a logistic additive model: require(gam) fit <- gam(cl ~ a + s(b) + s(c), data = d, family = binomial) multi.local.function(in.grids = c("a", "b", "c"), out.varnames = "pred", fun = grid.predict, fit = fit, control.predict = list(type = "response") ) # 'control.predict' is passed on to 'grid.predict', which # dumps its contents into the arguments for 'fit''s # 'predict' method. # Model predictions are written to a file named pred.asc ## End(Not run)