This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
trainCrf.Rd
93 lines (77 loc) · 2.73 KB
/
trainCrf.Rd
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/trainCrf.r
\name{trainCrf}
\alias{trainCrf}
\title{Calibrate a conditional random forest model}
\usage{
trainCrf(
data,
resp = names(data)[1],
preds = names(data)[2:ncol(data)],
family = "binomial",
w = ifelse(family == "binomial", TRUE, FALSE),
...
)
}
\arguments{
\item{data}{Data frame.}
\item{resp}{Character or integer. Name or column index of response variable. Default is to use the first column in \code{data}.}
\item{preds}{Character list or integer list. Names of columns or column indices of predictors. Default is to use the second and subsequent columns in \code{data}.}
\item{family}{Name of family for data error structure (see \code{?family}). Default is to use the 'binomial' family.}
\item{w}{Either logical in which case TRUE causes the total weight of presences to equal the total weight of absences (if \code{family='binomial'}) OR a numeric list of weights, one per row in \code{data} OR the name of the column in \code{data} that contains site weights. The default is to assign a weight of 1 to each datum.}
\item{...}{Arguments to pass to \code{\link[party]{cforest}} function.}
}
\value{
Object of class \code{RandomForest}.
}
\description{
This function trains a conditional random forest model. It is nearly identical to the \code{\link[party]{cforest}} function in the \pkg{party} package but is included for consistency with \code{\link{trainGlm}}, \code{\link{trainGam}}, and similar functions.
}
\examples{
\dontrun{
### 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...
}
}
\seealso{
\code{\link[party]{cforest}}, \code{\link[enmSdm]{trainRf}}
}