How to repeat univariate regression and extract P values?

Viewed 213

I am using lapply to perform several glm regressions on one dependent variable by one independent variable at a time. but I'm not sure how to extract the P values at a time.

There are 200 features in my dataset, but the code below only gave me the P value of feature#1. How can I get a matrix of all P values of the 200 features?

valName<- as.data.frame(colnames(repeatData))
featureName<-valName[3,]
lapply(featureName,
       function(var) {       
         formula    <- as.formula(paste("outcome ~", var))
         fit.logist <- glm(formula, data = repeatData, family = binomial)
         summary(fit.logist)
         Pvalue<-coef(summary(fit.logist))[,'Pr(>|z|)'] 
       })
1 Answers

I I simplified your code a little bit; (1) used reformulate() (not really different, just prettier) (2) returned only the p-value for the focal variable (not the intercept p-value). (If you leave out the 2, you'll get a 2-row matrix with intercept and focal-variable p-values.)

My example uses the built-in mtcars data set, with an added (fake) binomial response.

repeatData <- data.frame(outcome=rbinom(nrow(mtcars), size=1, prob=0.5), mtcars)
ff <-   function(var) {       
         formula    <- reformulate(var, response="outcome")
         fit.logist <- glm(formula, data = repeatData, family = binomial)
         coef(summary(fit.logist))[2, 'Pr(>|z|)'] 
       }
## skip first column (response variable).
sapply(names(repeatData)[-1], ff)
Related