0

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)
}
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
Marco M
  • 623
  • 1
  • 8
  • 15
  • Have a look at this question and answer http://stackoverflow.com/questions/9573893/using-svyglm-within-plyr-call/10712920#10712920 – mnel Sep 24 '12 at 23:25

1 Answers1

3

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, ...))
        })
    return(model)
}

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
Maiasaura
  • 32,226
  • 27
  • 104
  • 108