| wRandomforest | R Documentation |
A model selection function for fitting wRF (linear or logistic) models for complex survey data with sampling weights by a replicating weights method to select optimal tuning parameters by weighted error minimization. Detailed arguments are referred to in R-randomForest::randomForest.
wRandomforest(
data = NULL,
col.x = NULL,
y = NULL,
xtest = NULL,
ytest = NULL,
cluster = NULL,
strata = NULL,
weights = NULL,
design = NULL,
method = c("dCV", "JKn", "BRR", "split", "extrapolation"),
k = 10,
R = 1,
B = 200,
dCV.sw.test = FALSE,
train.prob = 0.7,
method.split = c("dCV", "bootstrap", "subbootstrap"),
print.rw = FALSE,
ntree = 500,
mtry = if (!is.null(y) && !is.factor(y)) max(floor(ncol(data[, col.x])/3), 1) else
floor(sqrt(ncol(data[, col.x]))),
replace = TRUE,
classwt = NULL,
cutoff,
sampsize = if (replace) nrow(data) else ceiling(0.632 * nrow(data)),
nodesize = if (!is.null(y) && !is.factor(y)) 5 else 1,
maxnodes = NULL,
importance = FALSE,
proximity = FALSE,
oob.prox = proximity,
norm.votes = TRUE,
do.trace = FALSE,
keep.forest = TRUE,
corr.bias = FALSE,
keep.inbag = FALSE,
...
)
data |
A data frame with information about independent variables, as well as sampling weights and strata and cluster indicators. It could be |
col.x |
A numeric vector indicating indices of columns for covariates or independent variables or a vector of character strings indicating names of these columns. |
y |
A vector of the response variable. If a factor, classification is assumed, otherwise, regression is assumed. If omitted, randomForest will run in a unsupervised mode. |
xtest |
A data frame or matrix containing predictors for the test set. |
ytest |
A vector of response variable for the test set. |
cluster |
A character string indicating the name of cluster identifiers. It could be |
strata |
A character string indicating the name of strata identifiers. It could be |
weights |
A character string indicating the name of sampling weights. It could be |
design |
An object of class |
method |
A character string indicating a method of replicate weights. Choose one of these: |
k |
An integer. The number of folds for the |
R |
An integer. The number of times the sample is partitioned for |
B |
Not used by |
dCV.sw.test |
A logical value indicating the method for estimating the error for |
train.prob |
A numeric between 0 and 1, indicating the proportion of clusters (for the method |
method.split |
A string of one the following replicate weights methods to be implemented under the |
print.rw |
A logical value. If |
ntree |
The number of trees to grow. This should not set to be too small a number, to ensure that every input row gets predicted at least a few times. |
mtry |
The number of variables randomly sampled as candidates at each split. Note that the default values are different for classification (sqrt(p) where p is number of variables in x) and regression (p/3) |
replace, classwt, cutoff, sampsize, nodesize, maxnodes, importance, proximity, oob.prox, norm.votes, do.trace, keep.forest, corr.bias, keep.inbag, ... |
Optional parameters to be passed to the low level function |
Unlike wElnet() and wXGBoost(), wRandomforest() does not offer method = "bootstrap"
or method = "subbootstrap" as a top-level replicate-weight resampling method. Random forests
already perform bootstrap resampling internally for every tree (bagging is the defining mechanism of
the algorithm), so layering an additional, outer bootstrap/subbootstrap replicate-weight scheme on top
would double-resample in a way that has no clear interpretation for variance/error estimation, and
would not serve the same tuning-parameter-selection purpose that dCV, JKn, BRR,
split, or extrapolation do here. If method = "bootstrap" or "subbootstrap"
is supplied, wRandomforest() stops with an informative error rather than silently proceeding.
The output object of the function wRandomforest() is an object of class w.randomforest:
mtry: A list containing information on mtry as a tuning parameter (the number of predictors sampled for splitting at each node):
all: A numeric vector of k*R mtry- values, i.e. mtry for each fold per replicate.
optimal.mtry: An optimal tuning parameter, mtry among mtry$all that minimizes the average error.
evaluation_log: A list containing information of two elements:
weighted.test.error: A vector of the average errors corresponding to tuning parameters in mtry$all.
min.weighted.test.error: An minimum error for the optimal tuning parameter from mtry$optimal.mtry to select the final model.
model: A list containing information on the fitted model by optimal.mtry: an object of class randomForest.
Note that the selected model is fitted by the original data, not one of the generated training sets by replicate weights.
data.rw: A data frame containing the original data set and the replicate weights added to define training and test sets. Only included in the output object if print.rw=TRUE.
call: The call that executes this function producing the object of w.randomforest.
final.model can leverage R-randomForest functionality for comprehensive data analysis for linear/logistic regression but will be
extensively used for other models, i.e., Cox, Poisson, etc.
randomForest::randomForest for arguments and return values in detail.
# Global options to avoid only one PSU in a stratum in a particular domain or subpopulation
options(survey.adjust.domain.lonely=TRUE)
options(survey.lonely.psu="adjust")
# For classification: y should be a factor.
# If a test set is provided, one can plug it into `xtest`, `ytest`
# just as implemented in \code{randomForest::randomForest()}.
data(nhanes2013_sbc)
dcv.rf <- wRandomforest(data = nhanes2013_sbc,
y = as.factor(nhanes2013_sbc$HBP), col.x = 2:61,
cluster = "SDMVPSU", strata = "SDMVSTRA", weights = "WTSAF2YR",
method = "dCV", k=5, R=5,importance=TRUE,proximity = TRUE)
# Or equivalently:
des <- survey::svydesign(ids=~SDMVPSU, strata = ~SDMVSTRA, weights = ~WTSAF2YR,
nest = TRUE, data =nhanes2013_sbc)
dcv.rf <- wRandomforest(y = as.factor(nhanes2013_sbc$HBP), col.x = 2:61, design =des,
importance=TRUE,proximity = TRUE,
method = "dCV", k=5, R=5)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.