I've built an R function that uses the same explanatory variables on a range of columns. I've used the glm
function, but now I need to do the same with svyglm
from the survey package. The main problem I'm having is that I can't build loops by using svyglm(Data[,i]~explanatoryVariables)
as I do in glm
, because it doesn't like column names (which are however very practical in loops).
For example if you try
library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
summary(svyglm(api00~ell+meals+mobility, design=dstrat))
everything is fine but if you want to loop through several dependent variables by using the column number (here 13), you get an error
summary(svyglm(apistrat[,13]~ell+meals+mobility,data=apistrat, design=dstrat))
Does anyone know how to get around this? To give a simple example (never mind the statistical accuracy or the link function) I need to achieve the equivalent of this in normal glm
but using svyglm
instead
for(i in (12:15)){
print(glm(apistrat[,i]~ ell+meals,data=apistrat)$aic)
}