如何从lm结果中获取RMSE? [英] How to obtain RMSE out of lm result?
问题描述
我知道$sigma
与均方根误差的概念之间存在微小差异.因此,我想知道从 R 中的lm
函数中获取RMSE的最简单方法是什么?
I know there is a small difference between $sigma
and the concept of root mean squared error. So, i am wondering what is the easiest way to obtain RMSE out of lm
function in R?
res<-lm(randomData$price ~randomData$carat+
randomData$cut+randomData$color+
randomData$clarity+randomData$depth+
randomData$table+randomData$x+
randomData$y+randomData$z)
length(coefficients(res))
包含24个系数,因此我无法再手动制作模型.
因此,如何根据从lm
得出的系数来评估RMSE?
contains 24 coefficient, and I cannot make my model manually anymore.
So, how can I evaluate the RMSE based on coefficients derived from lm
?
推荐答案
残差平方和:
RSS <- c(crossprod(res$residuals))
均方误差:
MSE <- RSS / length(res$residuals)
MSE根目录
RMSE <- sqrt(MSE)
Pearson估计的剩余方差(由summary.lm
返回):
Pearson estimated residual variance (as returned by summary.lm
):
sig2 <- RSS / res$df.residual
从统计上讲,MSE是残差方差的最大似然估计量,但有偏差(向下). Pearson 1是残差方差的受限最大似然估计,它是无偏的.
Statistically, MSE is the maximum likelihood estimator of residual variance, but is biased (downward). The Pearson one is the restricted maximum likelihood estimator of residual variance, which is unbiased.
备注
- 给出两个向量
x
和y
,c(crossprod(x, y))
等效于sum(x * y)
,但快得多 .c(crossprod(x))
同样比sum(x ^ 2)
快. -
sum(x) / length(x)
比mean(x)
更快.
- Given two vectors
x
andy
,c(crossprod(x, y))
is equivalent tosum(x * y)
but much faster.c(crossprod(x))
is likewise faster thansum(x ^ 2)
. sum(x) / length(x)
is also faster thanmean(x)
.
这篇关于如何从lm结果中获取RMSE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!