R调查包函数svyciprop采用“似然”方法

时间:2014-11-27 01:56:45

标签: r survey

我正在尝试使用R调查包函数svyciprop和“似然”方法计算比例的置信区间。

以下是一些示例代码:

df <- data.frame(id =  c(1, 1, 1, 2, 2, 2), var = c("a", "b", "a", "b", "a", "b"))
survey_design <- svydesign(id = ~id, data = df)
svyciprop(~I(var == "a"), survey_design, method = "likelihood")

这会生成错误消息:

Error in seq.int(xmin, xmax, length.out = n) : 'from' must be finite

我在包文档中找不到任何解释如何使其工作的内容。

非常感谢!

3 个答案:

答案 0 :(得分:3)

这个例子中的问题是分母的自由度太小。调查包中的代码最终调用MASS::confint.glm来获取间隔,但间隔半宽约为10(与iid采样和大样本相比,例如1.96)。这意味着必须给MASS::confint.glm提供1.6e-25的标称单侧尾部概率。不幸的是,MASS::confint.glm希望以1-1.6e-25的形式出现这种情况,这是机器精度的1倍。

您可以使用df参数指定分母自由度:使用这些数据,您可以得到df=2的结果。

> svyciprop(~I(var == "a"), survey_design, method = "like",df=5)
                     2.5% 97.5%
I(var == "a") 0.500 0.117  0.88
> svyciprop(~I(var == "a"), survey_design, method = "like",df=4)
                      2.5% 97.5%
I(var == "a") 0.5000 0.0993   0.9
> svyciprop(~I(var == "a"), survey_design, method = "like",df=3)
                       2.5% 97.5%
I(var == "a") 0.5000 0.0696  0.93
> svyciprop(~I(var == "a"), survey_design, method = "like",df=2)
                       2.5% 97.5%
I(var == "a") 0.5000 0.0216  0.98
> svyciprop(~I(var == "a"), survey_design, method = "like",df=1)
Error in seq.int(xmin, xmax, length.out = n) : 'from' must be finite

很明显,置信区间将从近0延伸到接近1

答案 1 :(得分:2)

错误在版本3.31-4中得到修复。但是,间隔仍然报告为

 > svyciprop(~I(var == "a"), survey_design, method = "likelihood",level=0.95)
                   2.5% 97.5%
 I(var == "a") 0.5  0.0    NA

正如安东尼指出的那样,问题是真正的置信区间从接近0变为接近1.

答案 2 :(得分:1)

通过输入svyciprop或通过Google搜索?svyciprop找到了svyciprop的文档,但文档不会涵盖与错误相关的内容。

由于所有R代码都可供用户阅读,因此您可以调试正在使用的功能。调试survey:::svyciprop会引导您survey:::confint.svyglm,引导您前往MASS:::confint.glm,然后转到MASS:::confint.profile.glm,依此类推。互联网有很多关于如何在R中使用debug函数的解释。这里有很多活动部分

您从此计算中深入的glm对象中获取了一些Inf值,导致其中断。它可能与您的示例数据集有点太完美(并且不切实际)有关。 ;)

如果我从你的df中抛出一个观察,那么它就可以了。

library(survey)
df <- data.frame(id =  c(1, 1, 2, 2, 2), var = c("a", "b", "b", "a", "b"))
survey_design <- svydesign(id = ~id, data = df)
svyciprop(~I(var == "a"), survey_design, method = "likelihood")

?svyciprop底部的示例数据集也有效。

您的特殊问题是您无法提出请求的置信区间。

# watch how the confidence interval tends toward zero and one as you widen it.
svyciprop(~I(var == "a"), survey_design, method = "likelihood",level=0.8)
svyciprop(~I(var == "a"), survey_design, method = "likelihood",level=0.9)
svyciprop(~I(var == "a"), survey_design, method = "likelihood",level=0.93)
svyciprop(~I(var == "a"), survey_design, method = "likelihood",level=0.95) # this is the default