Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command for finding the best linear model in R

Is there a way to get R to run all possible models (with all combinations of variables in a dataset) to produce the best/most accurate linear model and then output that model?

I feel like there is a way to do this, but I am having a hard time finding the information.

like image 964
Lindsey Register Avatar asked Nov 16 '15 16:11

Lindsey Register


1 Answers

There are numerous ways this could be achieved, but for a simple way of doing this I would suggest that you have a look at the glmulti package, which is described in detail in this paper:

  • glmulti: An R Package for Easy Automated Model Selection with (Generalized) Linear Models

Alternatively, very simple example of the model selection as available on the Quick-R website:

# Stepwise Regression
library(MASS)
fit <- lm(y~x1+x2+x3,data=mydata)
step <- stepAIC(fit, direction="both")
step$anova # display results 

Or to simplify even more, you can do more manual model comparison:

fit1 <- lm(y ~ x1 + x2 + x3 + x4, data=mydata)
fit2 <- lm(y ~ x1 + x2, data=mydata)
anova(fit1, fit2) 
like image 121
Konrad Avatar answered Sep 30 '22 09:09

Konrad