Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting p-value from lapply list of glm fits

Tags:

r

lapply

glm

I am using lapply to perform several glm regressions on one dependent variable by one independent variable at a time. Right now I am specifically interested in the Pr(>|z|) of each independent variable. However, I am unsure on how to report just Pr(>|z|) using the list from lapply.

If I was just running one model at a time: coef(summary(fit))[,"Pr(>|z|)"] or summary(fit)$coefficients[,4] Would work (as described here), but trying something similar with lapply does not seem to work. Can I get just the p-values using lapply and glm with an accessor method or from directly calling from the 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
like image 208
nofunsally Avatar asked Feb 06 '13 03:02

nofunsally


People also ask

How to extract data from a fitted glmmodel?

In order to extract some data from the fitted glmmodel object, you need to figure out where that data resides (use documentation and str()for that). Some data might be available from the summary.glmobject, while more detailed data is available from the glmobject itself.

How are p-values calculated for glmer models?

For glmer models, the summary output provides p-values based on asymptotic Wald tests (P); while this is standard practice for generalized linear models, these tests make assumptions both about the shape of the log-likelihood surface and about the accuracy of a chi-squared approximation to differences in log-likelihoods.

Is fit a new variable in GLM?

$\begingroup$@Ciochi: You're welcome. Feel free to accept/upvote my answer, if it is helpful. Yes, fitis new variable that gets created and initialized with the return value of the glm()function (the return value is an object of class glm).$\endgroup$

How do I compare coefficients from GLM with more than one variable?

2 How do I compare coefficients from my glm when I have more than one factor variable in my formula? 1 r glm - Error in names(coef) <- xnames only for 2 columns in data 2 Select behavior dependant with other factors and its formalization


1 Answers

You want to do:

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

However, if each item is just a p-value, you would probably rather have a vector than a list, so you could use sapply instead of lapply:

sapply(fits, function(f) summary(f)$coefficients[,4])
like image 72
David Robinson Avatar answered Nov 13 '22 03:11

David Robinson