18

I have run a logistic regression, the summary of which I name. "score" Accordingly, summary(score) gives me the following

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.3616  -0.9806  -0.7876   1.2563   1.9246  

                       Estimate Std. Error    z value    Pr(>|z|)
(Intercept)        -4.188286233 1.94605597 -2.1521921 0.031382230 *
Overall            -0.013407201 0.06158168 -0.2177141 0.827651866
RTN                -0.052959314 0.05015013 -1.0560154 0.290961160
Recorded            0.162863294 0.07290053  2.2340482 0.025479900 *
PV                 -0.086743611 0.02950620 -2.9398438 0.003283778 **
Expire             -0.035046322 0.04577103 -0.7656878 0.443862068
Trial               0.007220173 0.03294419  0.2191637 0.826522498
Fitness             0.056135418 0.03114687  1.8022810 0.071501212 .

---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 757.25  on 572  degrees of freedom
Residual deviance: 725.66  on 565  degrees of freedom
AIC: 741.66

Number of Fisher Scoring iterations: 4

What I am hoping to achieve is to get variables names and coefficients of those variables which have a *, **, or *** next to their Pr(>|z|) value. In other words, I want the aforementioned variables and coefficients with a Pr(>|z|) < .05.

Ideally, I'd like to get them in a data frame. Unfortunately, the following code I've tried does not work.

variable_try <-
  summary(score)$coefficients[if(summary(score)$coefficients[, 4] <= .05, 
                                 summary(score)$coefficients[, 1]),]

Error: unexpected ',' in "variable_try <-
summary(score)$coefficients[if(summary(score)$coefficients[,4] < .05,"
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Jonathan Charlton
  • 1,975
  • 6
  • 23
  • 30

1 Answers1

27

What about this:

data.frame(summary(score)$coef[summary(score)$coef[,4] <= .05, 4])
tcash21
  • 4,880
  • 4
  • 32
  • 39
  • 2
    @JonathanRossCharlton Re the renaming of the question; please **don't** do that. What use it that to anyone? – Gavin Simpson Apr 17 '13 at 22:15
  • 3
    @JonathanRossCharlton Unless a user _explicitly_ states that they down voted, you have no way to know who did it. Voting is completely anonymous. Regardless, things like "R Programming" and mentions of specific users are considered inappropriate in titles. – joran Apr 17 '13 at 22:22
  • Hey Joran - I dig the advisement. Regarding the voting, there may not be a way to know, but there may be a means of reckoning it. I figured that as we were corresponding, I saw the vote go -1. So I reckoned it. In like manner, I also voted up Gavin's advisement, as I will yours. There was no intention to break the rules; ergo, duly noted. – Jonathan Charlton Apr 18 '13 at 00:01
  • @tcash21, and what if you have factors? You won't get the real name, but the name+level. How can you overcome this? Thanks. – GabyLP Sep 29 '16 at 18:47
  • @GavinSimpson if x is the data.frame above: `stringr::str_match(rownames(x), "\\((.*)\\)")[,2]` – tcash21 Nov 25 '20 at 19:09