Soyeon Kim
2011-Feb-04 16:56 UTC
[R] How to get the variables used in stepwise logistic regression?
Dear All, I used glm and then used step function for stepwise regression. Now, I want to store the variables used in the stepwise regression. The code is the following. m_logistic <- glm(y ~ . + M1:T + M2:T + M3:T+ M4:T +M5:T, family=binomial("logit"), data = data) step_glm <- step(m_logistic)> step_glm$termsy ~ M1 + T + M1:T attr(,"variables") list(y, M1, T) attr(,"factors") M1 T M1:T y 0 0 0 M1 1 0 1 T 0 1 1 attr(,"term.labels") [1] "M1" "T" "M1:T" attr(,"order") [1] 1 1 2 attr(,"intercept") [1] 1 attr(,"response") [1] 1 attr(,".Environment") <environment: R_GlobalEnv> attr(,"predvars") list(y, M1, T) attr(,"dataClasses") y M1 T "numeric" "numeric" "numeric" How can I extract only "term.labels" ? Thank you, Soyeon
David Winsemius
2011-Feb-04 17:07 UTC
[R] How to get the variables used in stepwise logistic regression?
On Feb 4, 2011, at 11:56 AM, Soyeon Kim wrote:> Dear All, > > I used glm and then used step function for stepwise regression. > Now, I want to store the variables used in the stepwise regression.I am not sure that what you ask here is actually what you eventually ask and will limit my answer to only what you ask below.> > The code is the following. > > m_logistic <- glm(y ~ . + M1:T + M2:T + M3:T+ M4:T +M5:T, > family=binomial("logit"), data = data) > step_glm <- step(m_logistic) > >> step_glm$terms > y ~ M1 + T + M1:T > attr(,"variables") > list(y, M1, T) > attr(,"factors") > M1 T M1:T > y 0 0 0 > M1 1 0 1 > T 0 1 1 > attr(,"term.labels") > [1] "M1" "T" "M1:T" > attr(,"order") > [1] 1 1 2 > attr(,"intercept") > [1] 1 > attr(,"response") > [1] 1 > attr(,".Environment") > <environment: R_GlobalEnv> > attr(,"predvars") > list(y, M1, T) > attr(,"dataClasses") > y M1 T > "numeric" "numeric" "numeric" > > How can I extract only "term.labels" ? >Have you tried what would seem to be the obvious answer (assuming that is a faithful console output): attr(step_glm$terms, "term.labels") ?attr ?attributes (And the usual caveats: stepwise methods are often invalid.) -- David Winsemius, MD West Hartford, CT
Soyeon Kim
2011-Feb-04 17:14 UTC
[R] How to get the variables used in stepwise logistic regression?
Thank you. This is what I want. I need to study about attr :). Soyeon On Fri, Feb 4, 2011 at 11:07 AM, David Winsemius <dwinsemius at comcast.net> wrote:> > On Feb 4, 2011, at 11:56 AM, Soyeon Kim wrote: > >> Dear All, >> >> I used glm and then used step function for stepwise regression. >> Now, I want to store the variables used in the stepwise regression. > > I am not sure that what you ask here is actually what you eventually ask and > will limit my answer to only what you ask below. > >> >> The code is the following. >> >> m_logistic <- glm(y ~ . + M1:T + M2:T + M3:T+ M4:T +M5:T, >> family=binomial("logit"), data = data) >> ?step_glm <- step(m_logistic) >> >>> step_glm$terms >> >> y ~ M1 + T + M1:T >> attr(,"variables") >> list(y, M1, T) >> attr(,"factors") >> ?M1 T M1:T >> y ? 0 0 ? ?0 >> M1 ?1 0 ? ?1 >> T ? 0 1 ? ?1 >> attr(,"term.labels") >> [1] "M1" ? "T" ? ?"M1:T" >> attr(,"order") >> [1] 1 1 2 >> attr(,"intercept") >> [1] 1 >> attr(,"response") >> [1] 1 >> attr(,".Environment") >> <environment: R_GlobalEnv> >> attr(,"predvars") >> list(y, M1, T) >> attr(,"dataClasses") >> ? ? ? y ? ? ? ?M1 ? ? ? ? T >> "numeric" "numeric" "numeric" >> >> How can I extract only "term.labels" ? >> > > Have you tried what would seem to be the obvious answer (assuming that is a > faithful console output): > > attr(step_glm$terms, ?"term.labels") > > ?attr > ?attributes > > (And the usual caveats: stepwise methods are often invalid.) > > -- > David Winsemius, MD > West Hartford, CT > >