Retrieving a p-value from a lapply glm fits list

I am using lapply to perform multiple glm regressions one dependent variable per one explanatory variable at a time. I'm interested in Pr(>|z|)

every independent variable right now . However, I'm not sure how to report just Pr(>|z|)

using the list from lapply.

If I were just running one model at a time: coef(summary(fit))[,"Pr(>|z|)"]

or summary(fit)$coefficients[,4]

Will work (as described here ), but trying something similar with lapply

doesn't seem to work. Can I get just p values ​​using lapply

and glm

using an accessor or from a direct call from models?

#mtcars dataset
vars <- names(mtcars)[2:8]
fits <- lapply(vars, function(x) {glm(substitute(mpg ~ i, list(i = as.name(x))), family=binomial, data = mtcars)})
lapply(fits,summary) # this works
lapply(fits, coefficients) # this works
#lapply(fits, summary(fits)$coefficients[,4])# this for example does not work

      

+3


source to share


2 answers


You want to do:

lapply(fits, function(f) summary(f)$coefficients[,4])

      



However, if each element is just a p-value, chances are you will have a vector rather than a list, so you can use sapply

instead lapply

:

sapply(fits, function(f) summary(f)$coefficients[,4])

      

+8


source


When run, lapply(fits, summary)

it creates a list of summary.glm objects, each of which is printed withprint.summary.glm

If you keep this

 summaries <- lapply(fits, summary)

      

Then you can go through and extract the coefficient matrix



 coefmat <- lapply(summaries, '[[', 'coefficients')

      

and then the 4th column

 lapply(coefmat, '[', , 4)

      

+6


source







All Articles