了解插入符号如何计算R中的模型性能
我试图理解caret是如何计算模型性能的。 为了简化事情,我将了解插入符号如何计算R中的模型性能,r,r-caret,R,R Caret,我试图理解caret是如何计算模型性能的。 为了简化事情,我将lm方法与通常的引导(boot)结合使用 默认值显然提供了25次重采样 我无法复制重采样的R^2和RMSE。 我手动将列车数据子集,以匹配第一次重采样使用的数据,并估计相同的回归。我对选定的OB和抵制者重复了两次此测试 这些都不起作用。我无法在文档或之前的问题中找到我遗漏的内容 编辑:我成功地复制了RMSE,但仍在与其他指标作斗争 library(caret) df<-iris ##DATA PARTITIONING## pa
lm
方法与通常的引导(boot
)结合使用
默认值显然提供了25次重采样
我无法复制重采样的R^2
和RMSE
。
我手动将列车数据子集,以匹配第一次重采样使用的数据,并估计相同的回归。我对选定的OB和抵制者重复了两次此测试
这些都不起作用。我无法在文档或之前的问题中找到我遗漏的内容
编辑:我成功地复制了RMSE,但仍在与其他指标作斗争
library(caret)
df<-iris
##DATA PARTITIONING##
partition <- createDataPartition(df$Species, p = 0.5, list=FALSE ) # returns the indices of the train db.
train.db<-df[partition,]
# Linear regression
model.lm <- train(Sepal.Length~.,
data = train.db,
method="lm")
model.lm$resample[1,] # These are the metrics I'm trying to replicate
boot1<-model.lm[["control"]][["index"]][["Resample01"]]
boot1.out<-model.lm[["control"]][["indexOut"]][["Resample01"]]
boot1.db<-train.db[boot1,] # Train set
boot1.db.oob<-train.db[boot1.out,] # test set
a<-lm(Sepal.Length~., data = boot1.db)
boot1.db.oob$yhat<-predict(a,newdata =boot1.db.oob )
#rmse
rmse<-sqrt(mean((boot1.db.oob$Sepal.Length-boot1.db.oob$yhat)^2)) #RMSE
#R2
#TSS
tss<-sum((boot1.db.oob$Sepal.Length-mean(boot1.db.oob$Sepal.Length))^2)
#RSS
rss<-sum((boot1.db.oob$Sepal.Length-boot1.db.oob$yhat)^2)
r2<-1-rss/tss
#MAE
mae<-median(abs(boot1.db.oob$Sepal.Length-boot1.db.oob$yhat))
c(rmse,r2,mae)
model.lm$resample[1,] # RMSE is the same but the rest aren't
库(插入符号)
df我设法发现了所有的差异,为了其他人而在这里发帖
首先,MEA
是平均绝对误差,而不是中间绝对误差
更重要的发现是关于caret计算样本外R.sq
关于如何使用插入符号计算R2的注意事项:它采用直接的方法计算观测值和预测值(即R)之间的相关性,并将值平方。当模型较差时,这可能导致此估计值与从线性回归模型导出的更为广泛的已知估计值之间存在差异。最值得注意的是,相关性方法不会产生R2的负值(理论上无效)。这些估算值与其他估算值的比较见Kvalseth 1985
以下是更正后的代码:
library(caret)
df<-iris
##DATA PARTITIONING##
partition <- createDataPartition(df$Species, p = 0.5, list=FALSE ) # returns the indices of the train db.
train.db<-df[partition,]
# Linear regression
model.lm <- train(Sepal.Length~.,
data = train.db,
method="lm")
model.lm$resample[1,] # These are the metrics I'm trying to replicate
boot1<-model.lm[["control"]][["index"]][["Resample01"]]
boot1.out<-model.lm[["control"]][["indexOut"]][["Resample01"]]
boot1.db<-train.db[boot1,] # Train set
boot1.db.oob<-train.db[boot1.out,] # test set
a<-lm(Sepal.Length~., data = boot1.db)
boot1.db.oob$yhat<-predict(a,newdata =boot1.db.oob )
#rmse
rmse<-sqrt(mean((boot1.db.oob$Sepal.Length-boot1.db.oob$yhat)^2))
#R2
r2_caret<-cor(boot1.db.oob$Sepal.Length,boot1.db.oob$yhat)^2
#MAE
mae<-mean(abs(boot1.db.oob$Sepal.Length-boot1.db.oob$yhat))
c(rmse,r2_caret,mae)
model.lm$resample[1,]
库(插入符号)
df