wRandomforest: Weighted Random Forest prediction models (wRF) for complex...

View source: R/wrf.R

wRandomforestR Documentation

Weighted Random Forest prediction models (wRF) for complex survey data

Description

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.

Usage

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,
  ...
)

Arguments

data

A data frame with information about independent variables, as well as sampling weights and strata and cluster indicators. It could be NULL if the sampling design were plugged in the design argument.

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 NULL if the sampling design were plugged in the design argument.

strata

A character string indicating the name of strata identifiers. It could be NULL if the sampling design were plugged in the design argument.

weights

A character string indicating the name of sampling weights. It could be NULL if the sampling design were plugged in the design argument.

design

An object of class survey.design generated by survey::svydesign(). It could be NULL if information about cluster, strata, weights and data were given.

method

A character string indicating a method of replicate weights. Choose one of these: JKn, dCV, BRR, split, extrapolation. Note: bootstrap and subbootstrap are not offered here (see Details).

k

An integer. The number of folds for the dCV method. Default is k=10.

R

An integer. The number of times the sample is partitioned for dCV, split or extrapolation method. Default is R=1.

B

Not used by wRandomforest(); retained in the signature only for interface consistency with wElnet()/wXGBoost(). See Details.

dCV.sw.test

A logical value indicating the method for estimating the error for dCV method. FALSE, (the default option) estimates the error for each test set and defines the cross-validated error based on the average strategy. Option TRUE estimates the cross-validated error based on the pooling strategy

train.prob

A numeric between 0 and 1, indicating the proportion of clusters (for the method split) or strata (for the method extrapolation) to be set in the training sets. Default is train.prob = 0.7. Only applies for split and extrapolation methods.

method.split

A string of one the following replicate weights methods to be implemented under the split method in the method argument: dCV, bootstrap or subbootstrap. (This still permits bootstrap/subbootstrap as the inner resampling scheme within a split's training partition, since that usage does not conflate with randomForest's own internal bagging the way a standalone top-level bootstrap/subbootstrap replicate-weight method would – see Details.)

print.rw

A logical value. If TRUE, the data set with the replicate weights is saved in the output object. Default print.rw=FALSE.

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 randomForest::randomForest().

Details

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.

Value

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.

Note

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.

See Also

randomForest::randomForest for arguments and return values in detail.

Examples

 # 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)



MLSurvey documentation built on Nov. 18, 2026, 5:11 p.m.