Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract pvalue from glm

Tags:

r

glm

p-value

I'm running many regressions and am only interested in the effect on the coefficient and p-value of one particular variable. So, in my script, I'd like to be able to just extract the p-value from the glm summary (getting the coefficient itself is easy). The only way I know of to view the p-value is using summary(myReg). Is there some other way?

e.g.:

fit <- glm(y ~ x1 + x2, myData) x1Coeff <- fit$coefficients[2] # only returns coefficient, of course x1pValue <- ??? 

I've tried treating fit$coefficients as a matrix, but am still unable to simply extract the p-value.

Is it possible to do this?

Thanks!

like image 958
ch-pub Avatar asked May 23 '14 22:05

ch-pub


People also ask

What is p-value in GLM?

p-values are essentially hypothesis tests on the values of each coefficient. A high p-value means that a coefficient is unreliable (insignificant), while a low p-value suggests that the coefficient is statistically significant. You can request GLM or GAM to compute the p-values by enabling the compute_p_values option.

How do I know if my GLM is significant?

A significance level of 0.05 indicates a 5% risk of concluding that an association exists when there is no actual association. If the p-value is less than or equal to the significance level, you can conclude that there is a statistically significant association between the response variable and the term.

What is Pr (>| z |)?

The Pr(>|z|) column represents the p-value associated with the value in the z value column. If the p-value is less than a certain significance level (e.g. α = . 05) then this indicates that the predictor variable has a statistically significant relationship with the response variable in the model.


1 Answers

You want

coef(summary(fit))[,4] 

which extracts the column vector of p values from the tabular output shown by summary(fit). The p-values aren't actually computed until you run summary() on the model fit.

By the way, use extractor functions rather than delve into objects if you can:

fit$coefficients[2] 

should be

coef(fit)[2] 

If there aren't extractor functions, str() is your friend. It allows you to look at the structure of any object, which allows you to see what the object contains and how to extract it:

summ <- summary(fit)  > str(summ, max = 1) List of 17  $ call          : language glm(formula = counts ~ outcome + treatment, family = poisson())  $ terms         :Classes 'terms', 'formula' length 3 counts ~ outcome + treatment   .. ..- attr(*, "variables")= language list(counts, outcome, treatment)   .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1   .. .. ..- attr(*, "dimnames")=List of 2   .. ..- attr(*, "term.labels")= chr [1:2] "outcome" "treatment"   .. ..- attr(*, "order")= int [1:2] 1 1   .. ..- attr(*, "intercept")= int 1   .. ..- attr(*, "response")= int 1   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>    .. ..- attr(*, "predvars")= language list(counts, outcome, treatment)   .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "factor" "factor"   .. .. ..- attr(*, "names")= chr [1:3] "counts" "outcome" "treatment"  $ family        :List of 12   ..- attr(*, "class")= chr "family"  $ deviance      : num 5.13  $ aic           : num 56.8  $ contrasts     :List of 2  $ df.residual   : int 4  $ null.deviance : num 10.6  $ df.null       : int 8  $ iter          : int 4  $ deviance.resid: Named num [1:9] -0.671 0.963 -0.17 -0.22 -0.956 ...   ..- attr(*, "names")= chr [1:9] "1" "2" "3" "4" ...  $ coefficients  : num [1:5, 1:4] 3.04 -4.54e-01 -2.93e-01 1.34e-15 1.42e-15 ...   ..- attr(*, "dimnames")=List of 2  $ aliased       : Named logi [1:5] FALSE FALSE FALSE FALSE FALSE   ..- attr(*, "names")= chr [1:5] "(Intercept)" "outcome2" "outcome3" "treatment2" ...  $ dispersion    : num 1  $ df            : int [1:3] 5 4 5  $ cov.unscaled  : num [1:5, 1:5] 0.0292 -0.0159 -0.0159 -0.02 -0.02 ...   ..- attr(*, "dimnames")=List of 2  $ cov.scaled    : num [1:5, 1:5] 0.0292 -0.0159 -0.0159 -0.02 -0.02 ...   ..- attr(*, "dimnames")=List of 2  - attr(*, "class")= chr "summary.glm" 

Hence we note the coefficients component which we can extract using coef(), but other components don't have extractors, like null.deviance, which you can extract as summ$null.deviance.

like image 65
Gavin Simpson Avatar answered Sep 18 '22 18:09

Gavin Simpson