r – How do I get RSS from a linear model output
r – How do I get RSS from a linear model output
Here are some ways of computing the residual sum of squares (RSS) using the built-in anscombe
data set:
fm <- lm(y1 ~ x1+x2+x3, anscombe)
deviance(fm)
## [1] 13.76269
sum(resid(fm)^2)
## [1] 13.76269
anova(fm) # see the Residuals row of the Sum Sq column
## Analysis of Variance Table
##
## Response: y1
## Df Sum Sq Mean Sq F value Pr(>F)
## x1 1 27.510 27.5100 17.99 0.00217 **
## Residuals 9 13.763 1.5292
## ---
## Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
anova(fm)[Residuals, Sum Sq]
## [1] 13.76269
with(summary(fm), df[2] * sigma^2)
## [1] 13.76269
Regarding the last one, note that summary(fm)$df[2]
and summary(fm)$sigma
are shown in the summary(fm)
output in case you want to calculate RSS using only a printout from summary
. In particular, for the output shown in the question df[2] = 116 and sigma = 1.928 so RSS = df[2] * sigma^2 = 116 * 1.928^2 = 431.1933 .
As you are using glm, qpcR library can calculate the residual sum-of-squares of nls, lm, glm, drc or any other models from which residuals can be extacted. Here RSS(fit) function returns the RSS value of the model.
install.packages(qpcR)
library(qpcR)
fm <- lm(y1 ~ x1+x2+x3)
RSS(fm)
check the link to see other functions of qpcR