【发布时间】:2013-01-28 07:41:49
【问题描述】:
我在randomForest的文档中阅读了以下内容:
strata:用于分层抽样的(因子)变量。
sampsize:要绘制的样本大小。对于分类,如果 sampsize 是一个长度为层数的向量,然后是采样 按strata分层,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, ...)
我的问题是:究竟如何使用strata 和sampsize?这是一个最小的工作示例,我想在其中测试这些参数:
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,开始讨论该主题,但没有说明如何使用这些参数就解决了。
【问题讨论】:
-
?randomForest中演示分层抽样的示例代码对您来说不够清晰吗? -
谢谢@joran。文档中提供的示例使用了
sampsize,但没有使用strata。文档只说:strata: A (factor) variable that is used for stratified sampling。在这种情况下,我不清楚"used"这个词。也许是因为我对分层抽样和 R 比较陌生。 -
如果您不提供响应变量,它可能会默认使用响应变量。如果您想要与响应变量不同的层,您可以自己提供。
标签: r