【问题标题】:Using svyglm within plyr call在 plyr 调用中使用 svyglm
【发布时间】:2012-03-05 20:59:37
【问题描述】:

这显然是 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))

但是当我尝试将此方法调整为svyglm 时,llply 会引发以下错误

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

【问题讨论】:

  • 您可能需要使用循环运行此程序,为每个模型创建适当的公式对象。

标签: r plyr


【解决方案1】:

DWin 对正确公式的评论很有意义。

reformulate 会这样做。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-30
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2020-11-13
    相关资源
    最近更新 更多