I want to get regression coefficients and fit statistics from one dependent regressed on all combinations of two other independent factors.
What I have is data like this (Note the NA):
H<-data.frame(replicate(10,sample(0:20,10,rep=TRUE)))
H[2,3]<-NA
names(H)<-c("dep",letters[1:9])
So I want to regress "ind" on all these combinations using lm
.
apply(combn(names(H)[2:9],2), MARGIN=2, FUN=paste, collapse="*")
"axb" "axc" "axd" "axe" "axf" "axg" ... etc.
One at a time, I could get what I want like:
ab<-data.frame(ind="a*b",cbind(data.frame(glance(lm(data=H,dep~a*b))),
t(data.frame(unlist((lm(data=H,dep~a*b)[1]))))
))
names(ab)[13:16]<-c("int","coef1","coef2","coefby")
ac<-data.frame(ind="a*c",cbind(data.frame(glance(lm(data=H,dep~a*c))),
t(data.frame(unlist((lm(data=H,dep~a*c)[1]))))
))
names(ac)[13:16]<-c("int","coef1","coef2","coefby")
rbind(ab,ac)
What I want is either all these coefficients and statistics, or at least the model coefficients and r.squared.
Someone already showed how to almost the exact same thing using combn. But when I tried a modification of this using glance
instead of coefs
fun <- function(x) glance(lm(dep~paste(x, collapse="*"), data=H))[[1]][1]
combn(names(H[2:10]), 2, fun)
I get an error. I thought maybe I needed to try "dep" repeated 36 times, one for each 2 factor combination, but that didn't do it.
Error in model.frame.default(formula = dep ~ paste(x, collapse = "*"), :
variable lengths differ (found for 'paste(x, collapse = "*")')
How do I get either one coefficient at a time or all of them, for all possible dep~x*y multiple regression combination (with "dep" always being my y dependent variable)? Thanks!