在 plyr 调用中使用 svyglm

tom*_*omw 5 r plyr

这显然与 R 的调查包不同。我试图使用llplyplyr包进行列表svyglm模式。下面是一个例子:

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))
Run Code Online (Sandbox Code Playgroud)

我的因变量列号列表

dvnum <- 1:3
Run Code Online (Sandbox Code Playgroud)

表示此样本中没有聚类或分层

wd <- svydesign(ids= ~0, strata= NULL, weights= ~weights, data = foo)
Run Code Online (Sandbox Code Playgroud)

单个 svyglm 调用有效

svyglm(y1 ~ x1 + x2 + x3, design= wd)
Run Code Online (Sandbox Code Playgroud)

并将llply列出基本 Rglm模型

llply(dvnum, function(i) glm(foo[,i] ~ x1 + x2 + x3, data = foo))
Run Code Online (Sandbox Code Playgroud)

但是llply当我尝试将此方法应用于时会引发以下错误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
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:我如何使用llplyand svyglm

mne*_*nel 2

DWin 对正确公式的评论是有道理的。

reformulate会这样做。

dvnum <- names(foo)[1:3]

llply(dvnum, function(i) {
    svyglm(reformulate(c('x1', 'x2', 'x3'),response = i), design = wd)})
Run Code Online (Sandbox Code Playgroud)