| trainCrf | R Documentation |
This function trains a conditional random forest model. It is nearly identical to the cforest function in the party package but is included for consistency with trainGlm, trainGam, and similar functions.
trainCrf( data, resp = names(data)[1], preds = names(data)[2:ncol(data)], family = "binomial", w = ifelse(family == "binomial", TRUE, FALSE), ... )
data |
Data frame. |
resp |
Character or integer. Name or column index of response variable. Default is to use the first column in |
preds |
Character list or integer list. Names of columns or column indices of predictors. Default is to use the second and subsequent columns in |
family |
Name of family for data error structure (see |
w |
Either logical in which case TRUE causes the total weight of presences to equal the total weight of absences (if |
... |
Arguments to pass to |
Object of class RandomForest.
cforest, trainRf
## Not run:
### model red-bellied lemurs
data(mad0)
data(lemurs)
# climate data
bios <- c(1, 5, 12, 15)
clim <- raster::getData('worldclim', var='bio', res=10)
clim <- raster::subset(clim, bios)
clim <- raster::crop(clim, mad0)
# occurrence data
occs <- lemurs[lemurs$species == 'Eulemur rubriventer', ]
occsEnv <- raster::extract(clim, occs[ , c('longitude', 'latitude')])
# background sites
bg <- 2000 # too few cells to locate 10000 background points
bgSites <- dismo::randomPoints(clim, 2000)
bgEnv <- raster::extract(clim, bgSites)
# collate
presBg <- rep(c(1, 0), c(nrow(occs), nrow(bgSites)))
env <- rbind(occsEnv, bgEnv)
env <- cbind(presBg, env)
env <- as.data.frame(env)
preds <- paste0('bio', bios)
set.seed(123)
# random forest
rf <- trainRf(
data = env,
resp = 'presBg',
preds = preds,
)
# conditional random forest
crf <- trainCrf(
data = env,
resp = 'presBg',
preds = preds,
)
plot(rf)
# prediction rasters
mapRf1 <- predict(clim, rf, type='prob') # opposite class!
mapRf2 <- 1 - predict(clim, rf, type='prob') # correct
pointsFx <- function() points(occs[ , c('longitude', 'latitude')])
plot(stack(mapRf1, mapRf2), addfun=pointsFx)
# CRFs are tricky...
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.