【发布时间】:2018-01-15 17:54:38
【问题描述】:
我正在使用survey R 包中的“svyby”函数,并收到一个我不知道如何处理的错误。
一开始,我使用变量cntry作为分组,接下来,我使用essround作为分组,一切顺利。但是当我使用它们的组合 ~cntry+essround 时,它会返回一个错误。
我很困惑它如何可以单独为每个分组工作,但不能用于组合分组。
这在某种程度上与省略的数据有关,例如当我删除所有空单元格时(即使用 na.omit(dat) 而不是 dat 来定义调查设计)它开始工作。但我不想丢掉所有的缺失。我认为 svymean 的 na.rm 论点应该处理它。请注意,变量 cntry 和 essround 不包含任何缺失值。
library("survey")
s.w <- svydesign(ids = ~1, data = dat, weights = dat[,weight])
svyby(~ Security, by=~ essround, s.w, svymean, na.rm=T) # Works
svyby(~ Security, by=~ cntry, s.w, svymean, na.rm=T) # Also works
svyby(~ Security, by=~ essround+cntry, s.w, svymean, na.rm=T) # Gives an error
Error in tapply(1:NROW(x), list(factor(strata)), function(index) { :
arguments must have same length
所以我的问题是 - 如何让它工作?
更新。
对不起,我误读了文档。通过在svyby 函数中添加na.rm.all = TRUE 即可解决问题。
【问题讨论】:
-
我会试试:
by=~ interaction(essround,cntry) -
还是同样的错误。