在plyr调用中使用svyglm

时间:2012-03-05 20:59:37

标签: r plyr

这显然是R survey package的特质。我正在尝试使用plyr package中的llply来制作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))

我的因变量列号列表

dvnum <- 1:3

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

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

单个svyglm调用

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

llply将列出基本R glm模型

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

但是当我尝试将此方法调整为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

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

1 个答案:

答案 0 :(得分:1)

DWin对他关于正确公式的评论有所了解。

reformulate会这样做。

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

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