Take the 2-minute tour ×

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

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)
share|improve this question
Have a look at this question and answer stackoverflow.com/questions/9573893/… –  mnel Sep 24 '12 at 23:25

1 Answer 1

up vote 2 down vote accepted

You need to use as.formula to paste the appropriate columns for evaluation. I created a custom function for your case:

mysvy <- function(data, columns, ...) {
    model <- lapply(as.list(columns), function(x) {
          summary(svyglm(as.formula(paste0(names(data)[x], "~ell+meals+mobility")), 
            data = data, ...))

Then you can run your your desired columns through the function.

# To run columns 13 - 15 and get the results into a list
results <- mysvy(apistrat, 13:15, design = dstrat)
# should return a list of 3. results[[1]] to see the first
share|improve this answer

Your Answer


