首页 > 解决方案 > 如何在 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

任何帮助将不胜感激,谢谢!

标签: rstatisticssurvey

解决方案


该错误表明您指定了一个设计,其中一个层只有一个初级抽样单位。对于这样的设计,不可能获得无偏的方差估计:第 1131 层的贡献最终将是 0/0。

如您所见,R 的默认响应是给出错误,因为合理可能的解释是数据或svydesign语句错误。有时,就像这里一样,这不是您想要的,全局选项“survey.lonely.psu”描述了其他响应方式。你想设置

options(survey.lonely.psu = "adjust")

此选项和其他选项记录在help(surveyoptions)


推荐阅读