[R] Extracting variable names of final model in stepAIC

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Aug 5 09:07:44 CEST 2008


The 'final model' returned by stepAIC is just a model fit, so you do this 
the same way as any fit.  I'll assume you want to know for lm() fits, but 
this is fairly general.

library(MASS)
example(stepAIC)
formula(quine.stp)
attr(terms(quine.stp), "term.labels")

(You are making a habit of asking questions that your homework should have 
answered for you -- see ?stepAIC, the book it supports and the posting 
guide.)

On Tue, 5 Aug 2008, Chua Siang Li wrote:

>
>   Hello  there.   I uses the following codes for the purpose of variable
>   selection.
>   > lmModel <- lm(y~.,data.frame(y=y, x=x))
>   > step <- stepAIC(lmModel, direction="both")
>   > step$anova
>   Stepwise Model Path
>   Analysis of Deviance Table
>
>   Initial Model:
>   y ~ x.Market.Price + x.Quantity + x.Country + x.Incoterm + x.Channel +
>       x.PaymentTerm
>
>   Final Model:
>   y ~ x.Quantity + x.Country + x.Incoterm + x.PaymentTerm
>
>                 Step Df Deviance Resid. Df Resid. Dev      AIC
>   1                                   1067   852257.2 7268.945
>   2      - x.Channel  1 127.9724      1068   852385.2 7267.108
>   3 - x.Market.Price  1 858.2886      1069   853243.5 7266.200
>   question:
>   Now, $anova list the final model only on the console.  How do I retrieve the
>   variable names of final model (to be written into a text file later)?
>   Thanks.
>   ----
>   Chua Siang Li
>   Consultant - Operations Research
>   Acceval Pte Ltd
>   Tel: 6297 8740
>   Email: siang.li.chua at acceval-intl.com
>   Website: www.acceval-intl.com
>   This message and any attachments (the "message"...{{dropped:13}}
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list