I was wondering if there is any command that can output the results of a lm model into a data frame in R like outest in SAS. Any ideas? I am running multiple models and I want the result to look like below -
Model | alpha | Beta | Rsquared | F | df |
model0 | 8.4 | ... | .... | ..| .. |
model1 | ... | ... | .... | ..| .. |
model2 | ... | ... | .... | ..| .. |
The data i have is 'ds' which is -
X1 | X2 | Y1 |
.. | .. | .. |
.. | .. | .. |
.. | .. | .. |
.. | .. | .. |
And my code is a simple lm code -
model0 <- lm(Y1 ~ X1, ds)
model1 <- lm(Y1 ~ 1, ds)
model2 <- lm(Y1 ~ X1 + X2, ds)
I do exactly the same thing. The difficulty here is of course if the models have different number of coefficients - then you would have different number of columns, which is impossible in data.frame. You need to have the same number of columns for each model.
I normally use it for glm (these code snippets are commented out) but I modified it for lm for you:
models <- c()
for (i in 1:10) {
y <- rnorm(100) # generate some example data for lm
x <- rnorm(100)
m <- lm(y ~ x)
# in case of glm:
#m <- glm(y ~ x, data = data, family = "quasipoisson")
#overdispersion <- 1/m$df.residual*sum((data$count-fitted(m))^2/fitted(m))
coef <- summary(m)$coef
v.coef <- c(t(coef))
names(v.coef) <- paste(rep(rownames(coef), each = 4), c("coef", "stderr", "t", "p-value"))
v.model_info <- c(r.squared = summary(m)$r.squared, F = summary(m)$fstatistic[1], df.res = summary(m)$df[2])
# in case of glm:
#v.model_info <- c(overdisp = summary(m)$dispersion, res.deviance = m$deviance, df.res = m$df.residual, null.deviance = m$null.deviance, df.null = m$df.null)
v.all <- c(v.coef, v.model_info)
models <- rbind(models, cbind(data.frame(model = paste("model", i, sep = "")), t(v.all)))
}
I prefer to take data from summary(m). To bundle the data into data.frame, you use the cbind (column bind) and rbind (row bind) functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With