Stat.Enthus
Stat.Enthus

Reputation: 345

Extracting formula from model after using StepAIC

I am running stepAIC for R. I am trying to extract the formula of the best model , and the best model itself from the returned value. Any thoughts?

Example:

library(tidyverse)
library(MASS)
df<-mtcars 
mdl <- glm.nb(mpg ~ cyl +hp +drat+wt+qsec+gear+am +carb , data = df)
mdl <- stepAIC(mdl, trace = FALSE)

Edit I know this code retuns a lot of values, I just need the formula.

mdl$terms
mpg ~ hp + wt
attr(,"variables")
list(mpg, hp, wt)
attr(,"factors")
    hp wt
mpg  0  0
hp   1  0
wt   0  1
attr(,"term.labels")
[1] "hp" "wt"
attr(,"order")
[1] 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
attr(,"predvars")
list(mpg, hp, wt)
attr(,"dataClasses")
      mpg        hp        wt 
"numeric" "numeric" "numeric" 

Upvotes: 1

Views: 473

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226182

How about

formula(mdl)

? .....

Upvotes: 1

Related Questions