Helper function for applying predict methods to stacks of grids.

Share:

Description

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 multi.local.function or multi.focal.function.

Usage

1
2
grid.predict(fit, predfun, trafo, control.predict, predict.column, trace = 0,
  location, ...)

Arguments

fit

a model object for which prediction is desired

predfun

optional prediction function; if missing, the fit's predict method is called. In some cases it may be convenient to define a wrapper function for the predict method that may be passed as predfun argument.

trafo

an optional function(x) that takes a data.frame x and returns a data.frame with the same number of rows; this is intended to perform transformations on the input variables, e.g. derive a log-transformed variable from the raw input read from the grids, or more complex variables such as the NDVI etc.; the data.frame resulting from a call to trafo (if provided) is passed to predfun

control.predict

an optional list of arguments to be passed on to predfun; this may be e.g. type="response" to obtain probability prediction maps from a logistic regression model

predict.column

optional character string: Some predict methods (e.g. predict.lda) return a data.frame with several columns, e.g. one column per class in a classification problem. predict.column is used to pick the one that is of interest

trace

integer >=0: positive values give more (=2) or less (=1) information on predictor variables and predictions

location

optional location data received from multi.focal.function; is added to the newdata object that is passed on to predfun.

...

these arguments are provided by the calling function, usually multi.local.function or multi.focal.function. They contain the explanatory (predictor) variables required by the fit model.

Details

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.local.function (or 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 control.predict arguments.

Value

grid.predict returns the result of the call to predfun or the default predict method.

Note

Though grid.predict can in principle deal with predict methods returning factor variables, its usual caller multi.local.function / multi.focal.function cannot; classification models should be dealt with by setting a type="prob" (for rpart) or type="response" (for logistic regression and logistic additive model) argument, for example (see second Example below).

Author(s)

Alexander Brenning

References

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.

See Also

focal.function, multi.local.function, multi.focal.function

Examples

 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)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.