
Last chance! 50% off unlimited learning
Sale ends in
xyValues
) to obtain the values to fit the model (see the example). Any type of model (e.g. glm. gam, randomforest) for which a predict method has been implemented can be used.predict(object, ...)
predict(object, model, filename='', ext=NULL, const=NULL, index=1, se.fit=FALSE, ...)
object
a RasterStack, RasterBrick or RasterLayer object
model
A fitted model
filename
Output filename for a new raster; if NA the result is not written to a file but returned with the RasterLayer object, in the data slot
ext
An Extent object to limit the prediction to a sub-region of x
const
data.frame. Can be used to add a constant for which there is no Raster object for model predictions. Particulalry useful if the constant is a character-like factor value
index
Integer. To select the column if predict.'model' returns a matrix with multiple columns
se.fit
Extract the standard error rather then the prediction (works for GLM and perhaps other methods where the prediction and se.fit are returned as a list)
...
Additional arguments to pass to the predict.'model' function
}
The following additional arguments can be passed, to replace default values
format
Character. Output file type. See writeRaster
datatype
Character. Output data type. See dataType
overwrite
Logical. If TRUE
, "filename" will be overwritten if it exists
progress
Character. Valid values are "text", "tcltk", "windows" (on that platform only) and ""
}interpolate
if your model has 'x' and 'y' as implicit independent variables (e.g., in kriging).# A simple model to predict the location of the R in the R-logo using 20 presence points
# and 50 (random) pseudo-absence points. This type of model is often used to predict species distributions
# create a RasterStack (a set of predictor rasters)
logo <- stack(system.file("pictures/Rlogo.jpg", package="rgdal"))
layerNames(logo) <- c('red', 'green', 'blue')
#get presence and absence points
presence <- matrix(c(48, 48, 48, 53, 50, 46, 54, 70, 84, 85, 74, 84, 95, 85, 66, 42, 26, 4, 19, 17, 7, 14, 26, 29, 39, 45, 51, 56, 46, 38, 31, 22, 34, 60, 70, 73, 63, 46, 43, 28), ncol=2)
# random absence
absence <- cbind(runif(50)*(xmax(logo)-xmin(logo))+xmin(logo), runif(50)*(ymax(logo)-ymin(logo))+ymin(logo))
#extract values for points from stack
xy <- rbind(cbind(1, presence), cbind(0, absence))
v <- cbind(xy[,1], xyValues(logo, xy[,2:3]))
colnames(v)[1] <- 'presabs'
#build a model, here an example with glm
model <- glm(formula=presabs~., data=data.frame(v))
#predict to a raster
r <- predict(logo, model, progress='text')
plot(r>0.3)
points(presence, bg='blue', pch=21)
points(absence, bg='red', pch=21)
## also try:
# require(randomForest)
## formula <- as.factor(presabs) ~.
# formula <- presabs ~.
# model <- randomForest(formula, data=data.frame(v))
# r2 <- predict(logo, type='response', model, progress='text')
## note the additional argument "type='response'" that is passed to predict.randomForest
Run the code above in your browser using DataLab