R - 给定来自训练集和测试集的训练模型计算测试 MSE
R - Calculate Test MSE given a trained model from a training set and a test set
给定两组简单数据:
head(training_set)
x y
1 1 2.167512
2 2 4.684017
3 3 3.702477
4 4 9.417312
5 5 9.424831
6 6 13.090983
head(test_set)
x y
1 1 2.068663
2 2 4.162103
3 3 5.080583
4 4 8.366680
5 5 8.344651
我想在训练数据上拟合线性回归线,并使用该线(或系数)计算测试数据上残差的 "test MSE" 或均方误差,一旦该线是适合那里。
model = lm(y~x,data=training_set)
train_MSE = mean(model$residuals^2)
test_MSE = ?
在这种情况下,更准确地说是MSPE (mean squared prediction error):
mean((test_set$y - predict.lm(model, test_set)) ^ 2)
这是一个更有用的衡量标准,因为所有模型都以预测为目标。我们想要一个具有最小 MSPE 的模型。
在实践中,如果我们有一个备用的测试数据集,我们可以像上面那样直接计算 MSPE。但是,我们经常没有备用数据。在统计学中,leave-one-out cross-validation 是来自训练数据集的 MSPE 估计值。
还有一些其他统计数据可用于评估预测误差,例如 Mallows's statistic and AIC。
给定两组简单数据:
head(training_set)
x y
1 1 2.167512
2 2 4.684017
3 3 3.702477
4 4 9.417312
5 5 9.424831
6 6 13.090983
head(test_set)
x y
1 1 2.068663
2 2 4.162103
3 3 5.080583
4 4 8.366680
5 5 8.344651
我想在训练数据上拟合线性回归线,并使用该线(或系数)计算测试数据上残差的 "test MSE" 或均方误差,一旦该线是适合那里。
model = lm(y~x,data=training_set)
train_MSE = mean(model$residuals^2)
test_MSE = ?
在这种情况下,更准确地说是MSPE (mean squared prediction error):
mean((test_set$y - predict.lm(model, test_set)) ^ 2)
这是一个更有用的衡量标准,因为所有模型都以预测为目标。我们想要一个具有最小 MSPE 的模型。
在实践中,如果我们有一个备用的测试数据集,我们可以像上面那样直接计算 MSPE。但是,我们经常没有备用数据。在统计学中,leave-one-out cross-validation 是来自训练数据集的 MSPE 估计值。
还有一些其他统计数据可用于评估预测误差,例如 Mallows's statistic and AIC。