R调查权重标准误差

R survey weights standard-error

我在调查中计算 SE 时遇到了一些问题。这是我想做的一个例子,我尝试在 R 中使用 survey 包。 (下例中的 fpc 等于每个层中的观察数)

生成数据的代码:

id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) 
strata = c(6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8) 
weight = c(60, 75, 85, 140, 170, 175, 270, 310, 325, 785, 1450, 3920) 
fpc = c(8, 8, 8, 7, 7, 7, 6, 6, 6, 6, 6, 6)
answer = c("2", "2", "3", "1", "2", NA, NA, 2, "3", NA, "1", NA)
df = data.frame(id, strata, weight, fpc, answer)
df <- df[complete.cases(df), ]

然后我尝试使用调查包计算均值和标准误差:

dstrat<-svydesign(id=~1,strata=~strata, weights=~weight, data=df, fpc=~fpc)
svymean(~answer, dstrat)

        mean    SE
answer1 0.60803 0.2573
answer2 0.23518 0.1755
answer3 0.15679 0.1479

我的第一个问题是:我如何考虑在我的研究中没有回答的观察结果的权重?在上面的示例中,我在 运行 函数之前删除了我的 NA 观察结果,但我想包含此信息。我假设 SE 会更大或更小,这取决于我是否对具有最大权重的观察结果有答案?

我的第二个问题是:如何计算 "net value" 的 SE? 假设:

answer1 = good  
answer2 = neutral  
answer3 = bad 

我可以将 "net value" 计算为 answer1 - answer3 = 0.60803 - 0.15679 = 0.45124。 如何获得此 "net value" 的 SE?

你的第一个问题属于 stats.stackexchange -- 但我认为答案是当数据丢失时你无法计算 SE。但这是解决第二个问题的 SE 的方法:

library(survey)
id <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) 
strata <- c(6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8) 
weight <- c(60, 75, 85, 140, 170, 175, 270, 310, 325, 785, 1450, 3920) 
fpc <- c(8, 8, 8, 7, 7, 7, 6, 6, 6, 6, 6, 6)
answer <- c("2", "2", "3", "1", "2", NA, NA, 2, "3", NA, "1", NA)
df <- data.frame(id=id, strata=strata, weight=weight, fpc=fpc, answer=answer)


# this is probably a mistake
df <- df[complete.cases(df), ]
# in most data sets, you should be using na.rm=TRUE later
# and not tossing out statements before the `svydesign` gets run

df$ones <- as.numeric( df$answer %in% 1 )

df$threes <- as.numeric( df$answer %in% 3 )

dstrat<-svydesign(id=~1,strata=~strata, weights=~weight, data=df, fpc=~fpc)

a <- svymean( ~ ones + threes , dstrat , na.rm = TRUE )

svycontrast(a, list(avg=c(0,0), diff=c(1,-1)))