How to obtain RMSE out of lm result?

后端 未结 4 1260
孤街浪徒
孤街浪徒 2021-02-01 22:06

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

4条回答
  •  旧巷少年郎
    2021-02-01 22:38

    Residual sum of squares:

    RSS <- c(crossprod(res$residuals))
    

    Mean squared error:

    MSE <- RSS / length(res$residuals)
    

    Root MSE:

    RMSE <- sqrt(MSE)
    

    Pearson estimated residual variance (as returned by summary.lm):

    sig2 <- RSS / res$df.residual
    

    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.


    Remark

    • Given two vectors x and y, c(crossprod(x, y)) is equivalent to sum(x * y) but much faster. c(crossprod(x)) is likewise faster than sum(x ^ 2).
    • sum(x) / length(x) is also faster than mean(x).

提交回复
热议问题