Obtain residual standard errors of an "mlm" object returned by `lm()`
Asked Answered
W

2

6

I've used lm() to fit multiple regression models, for multiple (~1 million) response variables in R. Eg.

allModels <- lm(t(responseVariablesMatrix ~ modelMatrix)

This returns an object of class "mlm", which is like a huge object containing all the models. I want to get the Residual Sum of Squares for each model, which I can do using:

summaries <- summary(allModels)
rss1s <- sapply(summaries, function(a) return(a$sigma))

My problem is that I think the "summary" function calculates a whole bunch of other stuff, too, and is hence quite slow. I'm wondering if there is a faster way of extracting just the Residual sum of squares for the model?

Thanks!

Waves answered 6/3, 2013 at 17:55 Comment(0)
I
5

there is a component residuals in output of lm object, so you get residual sum of squares by sum(output$residuals^2).

edit: You are actually taking sigma out of summaries, which is sqrt(sum(output$residuals^2)/output$df.residuals)

For all models use

sapply(allModels, function(a) sqrt(sum(a$residuals^2)/a$df.residuals)))

Indenture answered 6/3, 2013 at 18:24 Comment(0)
A
0

Rarely known to many, the generic function deviance can compute residual sum of squares for "lm" and "mlm" models. Let fit be your fitted model, you can do

sqrt(deviance(fit) / fit$df.residual)

There are two advantages here:

  1. the generic function is fully "vectorized" (using colSums) rather than loop-based (like the solution via sapply);
  2. the generic function knows how to deal with weighted regression case.
Abell answered 7/10, 2016 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.