本文介绍了在plyr调用中使用svyglm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这显然与R的调查包特有.我正在尝试使用plyr包中的llply
进行列表的svyglm
模型.这是一个示例:
This is clearly something idiosyncratic to R's survey package. I'm trying to use llply
from the plyr package to make a list of svyglm
models. Here's 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))
我的因变量列号列表
dvnum <- 1:3
在此样本中未显示任何聚类或地层
Indicating no clusters or strata in this sample
wd <- svydesign(ids= ~0, strata= NULL, weights= ~weights, data = foo)
一个svyglm通话有效
A single svyglm call works
svyglm(y1 ~ x1 + x2 + x3, design= wd)
然后llply
将列出基本的R glm
模型
And llply
will make a list of base R glm
models
llply(dvnum, function(i) glm(foo[,i] ~ x1 + x2 + x3, data = foo))
但是当我尝试将此方法调整为svyglm
But llply
throws 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
所以我的问题是:如何使用llply
和svyglm
?
So my question is: how do I use llply
and svyglm
?
推荐答案
DWin谈到了他对正确公式的评论.
DWin was on to something with his comment about correct formula.
reformulate
将执行此操作.
dvnum <- names(foo)[1:3]
llply(dvnum, function(i) {
svyglm(reformulate(c('x1', 'x2', 'x3'),response = i), design = wd)})
这篇关于在plyr调用中使用svyglm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!