Skip to content

Instantly share code, notes, and snippets.

@dwbapst
Last active February 4, 2018 16:29
Show Gist options
  • Save dwbapst/a004df2bdb5c97e2d52a02b4917b3490 to your computer and use it in GitHub Desktop.
Save dwbapst/a004df2bdb5c97e2d52a02b4917b3490 to your computer and use it in GitHub Desktop.
There is no good example code online of how to do forward selection with MASS::stepAIC and that's really annoying
#So here's an example of that
x<-rnorm(100)
y<-rnorm(100)
z<-rnorm(100)
xy<-x+2*y+rnorm(100)
# everything needs to be in a dataframe
data<-data.frame(xy,x,y,z)
# stepAIC is in MASS
library(MASS)
# need to define the starting model
initialModel<-lm(xy~1,data)
# need to define the model with all parameters
allModel<-lm(xy~.,data)
# now we can do stepAIC
stepAIC(initialModel,direction="forward",
scope=list(lower=initialModel,
upper=allModel)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment