I am new to R. I found the following code for doing univariate logistic regression for a set of variables. What i would like to do is run chi square test for a list of variables against the dependent variable, similar to the logistic regression code below. I found couple of them which involve creating all possible combinations of the variables, but I can't get it to work. Ideally, I want the one of the variables (X) to be the same.
Chi Square Analysis using for loop in R
lapply(c("age","sex","race","service","cancer",
"renal","inf","cpr","sys","heart","prevad",
"type","frac","po2","ph","pco2","bic","cre","loc"),
function(var) {
formula <- as.formula(paste("status ~", var))
res.logist <- glm(formula, data = icu, family = binomial)
summary(res.logist)
})
Are you sure that the strings in the vector you
lapply
over are in the column names of theicu
dataset?It works for me when I download the
icu
data:and change
status
toSTA
which is a column inicu
. Here an example for some of your variables:This gives me a list with
summary.glm
objects. Example:If you want to do a chi-square test:
or a chi-square test for all combinations of variables:
Does this work?