10

How can I get values of Z - statistics as a vector from glm object? For example, I have

fit <- glm(y ~ 0 + x,binomial)

How can I access the column Pr(>|z|) the same way I get estimates of coefficients with fit$coef?

Prradep
  • 5,506
  • 5
  • 43
  • 84

3 Answers3

12

I believe that

coef(summary(fit))[,"Pr(>|z|)"]

will get you what you want. (summary.glm() returns an object that has a coef() method that returns the coefficient table.) (By the way, if accessor methods exist it's better to use them than to directly access the components of the fitted model -- e.g. coef(fit) is better than fit$coef.)

pull out p-values and r-squared from a linear regression gives a similar answer.

I would suggest methods(class="summary.glm") to find available accessor methods, but it's actually a little bit trickier than that because the default methods (in this case coef.default()) may also be relevant ...

PS if you want the Z values, coef(summary(fit))[,"z value"] should do it (your question is a little bit ambiguous: usually when people say "Z statistic" they mean the want the value of the test statistic, rather than the p value)

Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Re: using accessor method better than `fit$coef`: ... especially so since fit$coef does not return any p-values. – IRTFM Sep 09 '12 at 17:45
  • Yes -- the last sentence was referring back to the OP's use of `fit$coef` (the OP is already aware that this doesn't get p-values) – Ben Bolker Sep 09 '12 at 17:48
  • Apropos not much ... I find the use of the `coef`function for use the summary.glm-objects and summary.lm-objects to be rather a stretch in terminology. p-values , std-errors and z-stats are not "coefficients" in my book. It breaks the consistency of the use of that function. – IRTFM Sep 09 '12 at 17:55
  • 1
    OK, it should be `coefTable()` ... but what can you do? Let's not get started on R inconsistencies here ... – Ben Bolker Sep 09 '12 at 17:57
1

You can access to the info you want by doing

utils::data(anorexia, package="MASS") # Some data

anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
                family = gaussian, data = anorexia)  # a glm model 
summary(anorex.1)

summary(anorex.1)$coefficients[,3] # vector of t-values
(Intercept)       Prewt   TreatCont     TreatFT 
   3.716770   -3.508689   -2.163761    2.138933 

summary(anorex.1)$coefficients[,4] # vector of p-values
 (Intercept)        Prewt    TreatCont      TreatFT 
0.0004101067 0.0008034250 0.0339993147 0.0360350847 

summary(anorex.1)$coefficients[,3:4] # matrix 
              t value     Pr(>|t|)
(Intercept)  3.716770 0.0004101067
Prewt       -3.508689 0.0008034250
TreatCont   -2.163761 0.0339993147
TreatFT      2.138933 0.0360350847

str function will show you where each element within an object is located, and [[ accessors (better than $, as pointed out by @DWin and @Ben Bolker) will extract the info for you. Try str(summary(anorex.1)) to see what this function does.

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
  • 2
    I would prefer accessors instead of extracting list elements [`coef(summary(...))` vs. `summary(...)$coefficients`], and extracting columns by name [`...[,"Pr(>|t|)"]` vs `...[,3]` ... but +1 for thorough illustration – Ben Bolker Sep 09 '12 at 17:47
0

I use the summary syntax like: summary(my_model)[[1]][[2]]. You can try to use different combinations of numbers in "[[]]" to extract the data required. Of course, if I correctly understood your question :)

Aybek Khodiev
  • 596
  • 1
  • 4
  • 10