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`

.

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

`fit` |
a model object for which prediction is desired |

`predfun` |
optional prediction function; if missing, the |

`trafo` |
an optional |

`control.predict` |
an optional list of arguments to be passed on to |

`predict.column` |
optional character string: Some predict methods (e.g. |

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

`location` |
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.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.

`grid.predict`

returns the result of the call to `predfun`

or the default `predict`

method.

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).

Alexander Brenning

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.

`focal.function`

, `multi.local.function`

, `multi.focal.function`

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)
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

All documentation is copyright its authors; we didn't write any of that.