# 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