R - 具有两个结果变量的随机森林
R - RandomForest with two Outcome Variables
在这里使用 randomForest
统计包还很陌生。
我正在尝试 运行 一个具有 2 个响应变量和 7 个预测变量的模型,但由于响应变量的长度,我似乎做不到 and/or 拟合的性质具有 2 个响应变量的模型。
假设这是我的数据和模型:
> table(data$y1)
0 1 2 3 4
23 43 75 47 21
> length(data$y1)
0 4
> table(data$y2)
0 2 3 4
104 30 46 29
> length(data$y2)
0 4
m1<-randomForest(cbind(y1,y2)~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
当我运行这个模型时,我收到这个错误:
Error in randomForest.default(m, y, ...) :
length of response must be the same as predictors
我做了一些排查,发现cbind()
这两个响应变量只是简单地将它们的值放在一起,从而使原来的长度加倍,并可能导致上述错误。例如,
length(cbind(y1,y2))
> 418
t(lapply(data, length()))
> a b c d e f g y1 y2
209 209 209 209 209 209 209 209 209
然后我尝试通过 运行ning randomForest
分别对每个响应变量解决这个问题,然后在回归模型上应用 combine()
,但遇到了这些问题:
m2<-randomForest(y1~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
m3<-randomForest(y2~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
combine(m2,m3)
Warning message:
In randomForest.default(m, y, ...) :
The response has five or fewer unique values. Are you sure you want to do regression?
然后我决定将 randomForest
模型视为分类模型,并在 运行 宁 randomForest
之前将 as.factor()
应用于两个响应变量,但后来遇到了这个新问题:
m4<-randomForest(as.factor(y1)~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
m5<-randomForest(as.factor(y2)~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
combine(m4,m5)
Error in rf$votes + ifelse(is.na(rflist[[i]]$votes), 0, rflist[[i]]$votes) :
non-conformable arrays
我的猜测是我无法 combine()
分类模型。
我希望我尝试 运行 多元随机森林模型的询问有意义。如果还有其他问题,请告诉我。我也可以回去调整。
在 randomForest 公式之外合并您的列:
data[["y3"]] <- paste0(data$y1, data$y2)
randomForest(y3~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
在这里使用 randomForest
统计包还很陌生。
我正在尝试 运行 一个具有 2 个响应变量和 7 个预测变量的模型,但由于响应变量的长度,我似乎做不到 and/or 拟合的性质具有 2 个响应变量的模型。
假设这是我的数据和模型:
> table(data$y1)
0 1 2 3 4
23 43 75 47 21
> length(data$y1)
0 4
> table(data$y2)
0 2 3 4
104 30 46 29
> length(data$y2)
0 4
m1<-randomForest(cbind(y1,y2)~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
当我运行这个模型时,我收到这个错误:
Error in randomForest.default(m, y, ...) :
length of response must be the same as predictors
我做了一些排查,发现cbind()
这两个响应变量只是简单地将它们的值放在一起,从而使原来的长度加倍,并可能导致上述错误。例如,
length(cbind(y1,y2))
> 418
t(lapply(data, length()))
> a b c d e f g y1 y2
209 209 209 209 209 209 209 209 209
然后我尝试通过 运行ning randomForest
分别对每个响应变量解决这个问题,然后在回归模型上应用 combine()
,但遇到了这些问题:
m2<-randomForest(y1~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
m3<-randomForest(y2~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
combine(m2,m3)
Warning message:
In randomForest.default(m, y, ...) :
The response has five or fewer unique values. Are you sure you want to do regression?
然后我决定将 randomForest
模型视为分类模型,并在 运行 宁 randomForest
之前将 as.factor()
应用于两个响应变量,但后来遇到了这个新问题:
m4<-randomForest(as.factor(y1)~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
m5<-randomForest(as.factor(y2)~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)
combine(m4,m5)
Error in rf$votes + ifelse(is.na(rflist[[i]]$votes), 0, rflist[[i]]$votes) :
non-conformable arrays
我的猜测是我无法 combine()
分类模型。
我希望我尝试 运行 多元随机森林模型的询问有意义。如果还有其他问题,请告诉我。我也可以回去调整。
在 randomForest 公式之外合并您的列:
data[["y3"]] <- paste0(data$y1, data$y2)
randomForest(y3~a+b+c+d+e+f+g, data, mtry=7, importance=TRUE)