我很难理解RF代码在“随机森林”软件包中的输入之间的不同。此引用建议使用
## S3 method for class 'formula'
randomForest(formula, data=NULL, ..., subset, na.action=na.fail)
## Default S3 method:
randomForest(**x**, **y** =NULL, xtest=NULL, ytest=NULL, ntree=500,.
据我所知,x是带有预测因子的数据框架,y是响应变量。但是,我看到从同一张纸生成这个代码的例子是首先使用响应变量,然后是数据,
iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE,
proximity=TRUE)因此,我已经编写了我的代码有两个选项,但我不确定哪一个分类是正确的,为什么?
Here is my code:
I am basically comparing the two codes for rf.
## create data frame
n <- 199
z <- seq(-10, 10, length=n)
x<-sin(x)/x
y <- rnorm(n, 0, 0.1)
xy <- data.frame(x,y)
## create classes
xy$Y<-sample(1:2, n, replace = T)
XY<-xy
n <- nrow(XY)
p <- ncol(XY)-1
colnames(XY)[p+1]<-'Y'
## create trining and test sets
s <- sample(sample(n))
ntr <- round(ptr*n)
id.tr <- s[1:ntr]
id.te <- s[(ntr+1):n]
XY.tr <- XY[id.tr, ]
XY.te <- XY[id.te, ]
y.te <- XY[id.te, p+1]
XY.tr$Y<-as.factor(XY.tr$Y)
##run Random forest
rf1 <- randomForest(XY.tr, data=XY.tr$Y, proximity=TRUE,importance=T)
rf2<-randomForest(formula = XY.tr$Y ~ ., data=XY.tr, proximity = TRUE, importance = T) 非常感谢你对我的理解
发布于 2018-05-31 06:23:22
两人都会给你同样的答案:
data(iris) #load data在第一种方法中,您显式地提供响应向量y(但是相应地更正代码):
set.seed(131)
rf1 <- randomForest(y= iris$Species, x=iris[1:4], proximity=TRUE, importance=T) 在第二种方法中,您通过公式隐式地告知响应向量y,并提供整个数据矩阵。
set.seed(131)
rf2<-randomForest(formula = Species ~ ., data=iris, importance=TRUE, proximity=TRUE)请参阅randomForest的R文档
论点: x,公式: 数据框架或预测器矩阵,或描述模型的公式 安装(用于打印方法,randomForest对象)。
https://stackoverflow.com/questions/50617097
复制相似问题