Using svyglm in plyr call

This is clearly something special for the R review package . I am trying to use llply from the plyr package to list svyglm models. Here is an example:

 library(survey) library(plyr) foo <- data.frame(y1 = rbinom(50, size = 1, prob=.25), y2 = rbinom(50, size = 1, prob=.5), y3 = rbinom(50, size = 1, prob=.75), x1 = rnorm(50, 0, 2), x2 = rnorm(50, 0, 2), x3 = rnorm(50, 0, 2), weights = runif(50, .5, 1.5)) 

My list of column numbers of dependent variables

 dvnum <- 1:3 

Specifying clusters or layers in this example

 wd <- svydesign(ids= ~0, strata= NULL, weights= ~weights, data = foo) 

One svyglm call is made

 svyglm(y1 ~ x1 + x2 + x3, design= wd) 

And llply will create a list of basic R glm models

 llply(dvnum, function(i) glm(foo[,i] ~ x1 + x2 + x3, data = foo)) 

But llply gives the following error when I try to adapt this method to svyglm

 llply(dvnum, function(i) svyglm(foo[,i] ~ x1 + x2 + x3, design= wd)) Error in svyglm.survey.design(foo[, i] ~ x1 + x2 + x3, design = wd) : all variables must be in design= argument 

So my question is: how to use llply and svyglm ?

+4
source share
1 answer

DWin was up to something with his comment on the correct formula.

reformulate will do that.

 dvnum <- names(foo)[1:3] llply(dvnum, function(i) { svyglm(reformulate(c('x1', 'x2', 'x3'),response = i), design = wd)}) 
+1
source

Source: https://habr.com/ru/post/1399870/


All Articles