R中随机森林的分层抽样

时间:2013-02-12 21:21:15

标签: r

我在randomForest的文档中阅读了以下内容:

  

strata:用于分层抽样的(因子)变量。

     

sampsize:要绘制的样本的大小。对于分类,如果是sampsize             是一个长度为向量数的向量,然后是采样             由分层和sampsize的元素分层             表示从阶层中提取的数字。

作为参考,该函数的接口由:

给出
 randomForest(x, y=NULL,  xtest=NULL, ytest=NULL, ntree=500,
              mtry=if (!is.null(y) && !is.factor(y))
              max(floor(ncol(x)/3), 1) else floor(sqrt(ncol(x))),
              replace=TRUE, classwt=NULL, cutoff, strata,
              sampsize = if (replace) nrow(x) else ceiling(.632*nrow(x)),
              nodesize = if (!is.null(y) && !is.factor(y)) 5 else 1,
              maxnodes = NULL,
              importance=FALSE, localImp=FALSE, nPerm=1,
              proximity, oob.prox=proximity,
              norm.votes=TRUE, do.trace=FALSE,
              keep.forest=!is.null(y) && is.null(xtest), corr.bias=FALSE,
              keep.inbag=FALSE, ...)

我的问题是:如何使用stratasampsize?这是一个最小的工作示例,我想测试这些参数:

library(randomForest)
iris = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", sep = ",", header = FALSE)
names(iris) = c("sepal.length", "sepal.width", "petal.length", "petal.width", "iris.type")

model = randomForest(iris.type ~ sepal.length + sepal.width, data = iris)

> model
500 samples
  6 predictors
  2 classes: 'Y0', 'Y1' 

No pre-processing
Resampling: Bootstrap (7 reps) 

Summary of sample sizes: 477, 477, 477, 477, 477, 477, ... 

Resampling results across tuning parameters:

  mtry  ROC    Sens  Spec  ROC SD  Sens SD  Spec SD
  2     0.763  1     0     0.156   0        0      
  4     0.782  1     0     0.231   0        0      
  6     0.847  1     0     0.173   0        0      

ROC was used to select the optimal model using  the largest value.
The final value used for the model was mtry = 6.

我来参考这些参数,因为我希望RF能够使用自举样本,这些样本尊重我的数据中负数与负数的比例。

This other thread,开始讨论这个主题,但结果却没有说明如何使用这些参数。

1 个答案:

答案 0 :(得分:7)

这不会是这样的:

model = randomForest(iris.type ~ sepal.length + sepal.width, 
                     data = iris, 
                     sampsize=c(10,10,10), strata=iris$iris.type)

我确实尝试了..., strata=iristype..., strata='iristype',但显然代码并未编写为在'data'参数的环境中解释该值。我使用了结果变量,因为它是该数据集中唯一的因子变量,但我不认为它必须是结果变量。事实上,我认为它绝对不应该是结果变量。预计此特定模型将产生无用的输出,并且仅用于测试语法。

相关问题