Jevgenijs Strigins
Jevgenijs Strigins

Reputation: 103

How to get values of Z - statistic from glm object?

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?

Upvotes: 11

Views: 7344

Answers (3)

Aybek Khodiev
Aybek Khodiev

Reputation: 596

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 :)

Upvotes: 0

Ben Bolker
Ben Bolker

Reputation: 226182

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)

Upvotes: 12

Jilber Urbina
Jilber Urbina

Reputation: 61154

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.

Upvotes: 1

Related Questions