在计算randomForest
回归时,对象将R平方包括为“% Var explained: ...
”。
library(randomForest)
library(doSNOW)
library(foreach)
library(ggplot2)
dat <- data.frame(ggplot2::diamonds[1:1000,1:7])
rf <- randomForest(formula = carat ~ ., data = dat, ntree = 500)
rf
# Call:
# randomForest(formula = carat ~ ., data = dat, ntree = 500)
# Type of random forest: regression
# Number of trees: 500
# No. of variables tried at each split: 2
#
# Mean of squared residuals: 0.001820046
# % Var explained: 95.22
但是,当使用foreach
循环计算和combine
多个randomForest
个对象时,R平方值不可用,如?combine
中所述:
组合对象的
confusion
,err.rate
,mse
和rsq
组件(以及测试组件中的相应组件,如果存在)将为{ {1}}
NULL
由于在this question中没有真正回答:之后是否可以计算出cl <- makeCluster(8)
registerDoSNOW(cl)
rfPar <- foreach(ntree=rep(63,8),
.combine = combine,
.multicombine = T,
.packages = "randomForest") %dopar%
{
randomForest(formula = carat ~ ., data = dat, ntree = ntree)
}
stopCluster(cl)
rfPar
# Call:
# randomForest(formula = carat ~ ., data = dat, ntree = ntree)
# Type of random forest: regression
# Number of trees: 504
# No. of variables tried at each split: 2
对象的R平方(%Var解释)和平方残差平均值?
(对这种并行化的批评者可能会争论使用randomForest
或其他。但是,事实证明这需要永远。事实上,这可能对使用caret::train(... method = "parRF")
合并{{}的任何人都有用。 1}}对象......)
答案 0 :(得分:5)
是。您可以通过获取训练数据和训练模型产生的预测并将其与实际值进行比较来计算事后的R平方值:
# taking the object from the question:
actual <- dat$carat
predicted <- unname(predict(rfPar, dat))
R2 <- 1 - (sum((actual-predicted)^2)/sum((actual-mean(actual))^2))
或均方误差:
caret::RMSE(predicted,actual)