I have a dataset with 1-3 versions of the dependent variable, and 10-15 independent variables. I'd like to run a glm command for the model, but would like it to loop for ALL possible combinations of independent variables. I've never written code for a loop, and want to make sure I set it up correctly.
Below is a small subset of my data frame. The actual dataframe has an explicit name for each variable; not just "DepVar1" or "IndVar1."
dfPRAC <- structure(list(DepVar1 = c(0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1), DepVar2 = c(0, 1, 0, 0,
1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1),
IndVar1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
0, 0, 0, 1, 0, 0, 0, 1, 0), IndVar2 = c(1, 3, 9, 1, 5, 1,
1, 8, 4, 6, 3, 15, 4, 1, 1, 3, 2, 1, 10, 1, 9, 9, 11, 5),
IndVar3 = c(0.500100322564443, 1.64241601558441, 0.622735778490702,
2.42429812749226, 5.10055213237027, 1.38479786027561, 7.24663629203007,
0.5102348706939, 2.91566510995229, 3.73356170379198, 5.42003495939846,
1.29312896116503, 3.33753833987496, 0.91783513806083, 4.7735736131668,
1.17609362602233, 5.58010703426296, 5.6668754863739, 1.4377813063642,
5.07724130837643, 2.4791994535923, 2.55100067348583, 2.41043629522981,
2.14411703944206)), .Names = c("DepVar1", "DepVar2", "IndVar1",
"IndVar2", "IndVar3"), row.names = c(NA, 24L), class = "data.frame")
My current code for running a single glm model is:
RegPRAC <- glm(DepVar1 ~ IndVar1, data=dfPRAC, family=binomial("logit"))
summary(RegPRAC)
I'd like to run the model for ALL possible combinations of independent variables, with all combinations of dependent variables, but I'm not sure where to start. I was thinking something like:
for (i in dfPRAC$IndVar1:dfPRAC$IndVar3) {glm(DepVar1 ~ i, data=dfPRAC, family=binomial("logit")) }
I tried running it, but got several errors. Any suggestions would be appreciated.
See Question&Answers more detail:
os