我有一个包含 14 个因变量(它们都很重要)和 678 个观察值的模型。我使用最佳子集回归和验证集(33% 的数据用于验证)来找出哪个统计模型的 MSE 最低(出于我的好奇心)。我得到了下图,令人惊讶的是,验证数据集的 MSE 总是低于所有模型的训练数据集(从 1 到 14 个因变量)。这是我使用的代码,
library(MASS)
set.seed(1)
train=sample(seq(678),452,replace=FALSE)
train
regfit.exh=regsubsets(HPV~. -Model.Types..code.-Year..code.,data=Mydata, nvmax=NULL,force.in = NULL, force.out = NULL, method="exhaustive")
val.errors=rep(NA,14)
x.test=model.matrix(HPV~.-Model.Types..code.-Year..code.,data=Mydata[-train,])
for(i in 1:14){
coefi=coef(regfit.exh,id=i)
pred=x.test[,names(coefi)]%*%coefi
val.errors[i]=mean((Mydata$HPV[-train]-pred)^2)
}
plot(sqrt(val.errors),ylab="Root MSE",ylim=c(3,12), pch=11, type="b")
points(sqrt(regfit.exh$rss[-1]/452),col="blue",pch=11,type="b")
legend("topright",legend=c("Training","Validation"),col=c("blue","black"),pch=11)
为什么验证根 MSE 总是能击败训练?对于任何反馈,我们都表示感谢。
