r - 如何在 R 中应用与 NIS(国家住院患者样本)相关的权重
问题描述
我正在尝试使用 R 包“调查”应用 NIS 数据给出的权重,但我没有成功。我对 R 和调查命令相当陌生。
这是我尝试过的:
# Create the unweighted dataset
d <- read.dta13(path)
# This produces the correct weighted amount of cases I need.
sum(d$DISCWT) # This produces the correct weighted amount of cases I need.
library(survey)
# Create survey object
dsvy <- svydesign(id = ~ d$HOSP_NIS, strata = ~ d$NIS_STRATUM, weights = ~ d$DISCWT, nest = TRUE, data = d)
d$count <- 1
svytotal(~d$count, dsvy)
但是,在运行调查总数后出现以下错误:
Error in onestrat(x[index, , drop = FALSE], clusters[index], nPSU[index][1], :
Stratum (1131) has only one PSU at stage 1
任何帮助将不胜感激,谢谢!
解决方案
该错误表明您指定了一个设计,其中一个层只有一个初级抽样单位。对于这样的设计,不可能获得无偏的方差估计:第 1131 层的贡献最终将是 0/0。
如您所见,R 的默认响应是给出错误,因为合理可能的解释是数据或svydesign
语句错误。有时,就像这里一样,这不是您想要的,全局选项“survey.lonely.psu”描述了其他响应方式。你想设置
options(survey.lonely.psu = "adjust")
此选项和其他选项记录在help(surveyoptions)
推荐阅读
- javascript - 基于键如何从 JavaScript 中的数组中过滤?
- mysql - 将一个 CSV 上传到 Mysql 中的两个不同表中
- typescript - 可选属性与非可选属性?
- powershell - Powershell:工作流不能使用递归
- amazon-emr - EMR 上的 Flink - 没有输出到控制台或文件
- javascript - 当我用 new Function 定义一个函数时,函数的名称返回匿名
- arrays - 斯威夫特:加入(分隔符:“\br”)
- asciidoc - Asciidoc Maven 构建 PDF 链接不起作用
- samba - Samba 不会自动创建主目录
- abap - 从数据库中获取数据时删除多余的空格