Extracting predictor names when one predictor present in regression R

58 views Asked by At

so I am currently running logistic regression and am attempting to extract summary information for individual predictors without including the intercept as follows:

zscores1<-summary(step1)$coefficients[-1,"z value"]
> zscores1
   i3        i2        i1 
0.5011802 2.7834229 2.0239975

Step1 is the model of interest here to provide context. I ultimately want to extract just the included predictors:

allpredictorsincld<-rownames(summary(step1)$coefficients[-1,])
> allpredictorsincld
[1] "i3" "i2" "i1"

The issue I am having is if I only use one predictor I only get the information for that predictor, but the item number is dropped as follows:

> zscores1<-summary(step1)$coefficients[-1,"z value"]
> zscores1
[1] 5.644939

Where item 1 (i1) is the only item included as a predictor. How can I make it so R gives me the value in addition to the item number? So as to have something like this:

> zscores1
   i1
5.644939 

Thank you!

1

There are 1 answers

0
costebk08 On BEST ANSWER

Another solution I discovered is converting the results to a data frame, then extracting the row names as follows:

> allpredsincld<-as.data.frame(summary(step1)$coefficients)
> allpredsincld
         Estimate Std. Error   z value     Pr(>|z|)
(Intercept) -7.998346   1.216048 -6.577327 4.789808e-11
i1           3.928425   0.695920  5.644939 1.652402e-08

then:

> allpredsincld<-allpredsincld[-1,]
> allpredsincld
Estimate Std. Error  z value     Pr(>|z|)
i1 3.928425    0.69592 5.644939 1.652402e-08

and finally:

> rownames(allpredsincld)
[1] "i1"

Though Mr. Flick's solution is much quicker.