Apologies if this has been answered before but I could not find the answer anywhere. I have a regression with 5 different outcome variables and 10 different explanatory variables, so I use two loops to run the model as follows:
for(i in 1:length(outcome)){
for(j in 1:length(explanatory)){
reg[[i]] <- glm(as.formula(paste(outcome[i],"~",explanatory[j])), data=mydata, family=binomial)
assign(paste0("reg", i, j), reg[[i]])
}
}
This way I have for example reg11 is the regression with the first outcome and first explanatory variable, and for example reg 310 is the regression with the third outcome and the 10th explanatory variable.
Now I want to extract the betas from each regression to create new dataframes, and I use the following:
for(i in 1:5){
for(j in 1:10){
betas <- reg[[i,j]]$"coefficients"
}
}
However, it seems that the syntax for [[i,j]] is wrong. I have tried [[i,j]], [[i]][[j]], and numerous other combinations but neither seems to work. How should I spell it so that R understands which regression I am referring to?
Thank you very much!