我正在运行一个袋装的随机森林模型,我无法捕获每棵树中使用的预测变量。我总共有34个预测变量,我试图运行一个带有4个预测变量的树模型。对于生成的每个树模型,我想捕获预测变量并将它们保存到名为" lstVariableUsed的项目中。"
我遇到线路问题" lstVariablesUsed< - rbind(lstVariablesUsed,varUsed(model.bagged))。"
我已尝试过多种方法来捕获它,包括varUsed,model.bagged $ terms,Name()和attr(),但是没有运气获得每棵树中使用的4个变量。任何帮助表示赞赏。
set.seed(1)
lstVariablesUsed <- character()
maxnumpred <- 4
maxnumtrees <- 14
for(numpreds in 1:maxnumpred){
for(numtrees in 1:maxnumtrees){
train <- sample(1:nrow(df.FinalClass), nrow(df.FinalClass)/2)
FinalClass.test2 = df.FinalClass[-train,]
DidRetain.test2 = DidRetain[-train]
model.bagged <- randomForest(DidRetain~.,
data=df.FinalClass,
subset=train,
mtry=maxnumpred,
ntree=maxnumtrees,
importance = FALSE)
pred.vals.tree <- predict(model.bagged, FinalClass.test2, type="class")
t <- table(pred.vals.tree, DidRetain.test2)
acc.val <- (sum(diag(t))/sum(t))*100
#store the accuracy value in the lstAccuraccy's
lstAccuracy <- rbind(lstAccuracy, acc.val)
#store the variables used in the model
lstVariablesUsed <- rbind(lstVariablesUsed, varUsed(model.bagged))
#output the number of iterations of for loop that have passed
print(paste(" Processed trees: ", numtrees))
}
#output the number of iterations of for loop that have passed
print(paste("Processed predictors: ", numpreds))
}