Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding interaction terms to step AIC in R

Tags:

r

So I have a bunch of variables sitting in a data frame and I want to use the step function to select a model.

Right now I'm doing something like this

step(lm(SalePrice ~ Gr.Liv.Area + Total.Bsmt.SF + Garage.Area + Lot.Area, list= ~upper(Neighborhood + Neighborhood:Bedroom.AbvGr) .... 

How do I add multiple interaction terms without having to manually input them with the : notation?

like image 520
praks5432 Avatar asked Mar 15 '14 00:03

praks5432


Video Answer


1 Answers

Here is one way of adding interactions: Assume that all your data of interest is in dat and your dependent variable is named y. The code

init_mod <- lm(y ~ ., data = dat)
step(init_mod, scope = . ~ .^2, direction = 'forward')

will add interaction terms to your model using AIC. If you want k order interactions you can replace .^2 with .^k.

like image 69
Lars Lau Raket Avatar answered Oct 13 '22 12:10

Lars Lau Raket