【问题标题】:Raking Weights on Nested Data: R Output Doesn't Match Stata Output嵌套数据的权重:R 输出与 Stata 输出不匹配
【发布时间】:2015-06-28 18:53:11
【问题描述】:

简介

我有嵌套在学校中的教师的多层次调查数据。我已经根据概率选择和响应率(oldwt 下面)手动计算了设计权重和无响应调整权重。现在我想通过在两个边际上倾斜来创建分层后权重:教师的性别(男性或女性)和就业状况(全职或非全职)。在 Statalist 好心人的帮助下(请参阅here),我似乎在 Stata 中成功地做到了这一点。但是,在尝试在 R 中复制结果时,我得出了截然不同的输出。

样本数据

#Variables
#school   : unique school id
#caseid   : unique teacher id
#oldwt    : the product of the design weight and the non-response adjustment
#gender   : male or female
#timecat  : employment status (full-time or part-time)
#scgender : a combined factor variable of school x gender
#sctime   : a combined factor variable of school x timecat
#genderp  : the school's true population for gender
#fullp    : the school's true population for timecat

#Sample Data
foo <- structure(list(caseid = 1:11, school = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), oldwt = c(1.8, 1.8, 1.8, 1.8, 1.8, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3), gender = structure(c(2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c("Female", "Male"), class = "factor"), timecat = structure(c(2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("Full-time", "Part-time"), class = "factor"), scgender = structure(c(2L, 1L, 1L, 2L, 2L, 3L, 4L, 4L, 3L, 4L, 4L), .Label = c("1.Female", "1.Male", "2.Female", "2.Male"), class = "factor"), sctime = structure(c(2L, 2L, 1L, 1L, 1L, 4L, 4L, 3L, 3L, 3L, 3L), .Label = c("1.Full-time", "1.Part-time", "2.Full-time", "2.Part-time"), class = "factor"), genderp = c(0.444, 0.556, 0.556, 0.444, 0.444, 0.25, 0.75, 0.75, 0.25, 0.75, 0.75), fullp = c(0.222, 0.222, 0.778, 0.778, 0.778, 0.375, 0.375, 0.625, 0.625, 0.625, 0.625)), .Names = c("caseid", "school", "oldwt", "gender", "timecat", "scgender", "sctime", "genderp", "fullp"), class = "data.frame", row.names = c(NA, -11L))

抽奖码

(有关在 R 中使用 anesrake 的深入示例,请参阅 herehere)。

# extract true population proportions into a vector
genderp <- c(aggregate(foo$genderp, by=list(foo$scgender), FUN=max))
fullp <- c(aggregate(foo$fullp, by=list(foo$sctime), FUN=max))
genderp <- as.vector(genderp$x)
fullp <- as.vector(fullp$x)

# align the levels/labels of the population total with the variables
names(genderp) <- c("1.Female", "1.Male", "2.Female", "2.Male")
names(fullp) <- c("1.Full-time", "1.Part-time", "2.Full-time", "2.Part-time")

# create target list of true population proportions for variables
targets <- list(genderp, fullp)
names(targets) <- c("scgender", "sctime") 

# rake
library(anesrake)
outsave <- anesrake(targets, foo, caseid = foo$caseid, weightvec = foo$oldwt, verbose = F, choosemethod = "total", type = "nolim", nlim = 2, force1 = FALSE)
outsave

与Stata输出比较

问题是 R 的输出与 Stata 的输出不匹配(即使我设置了force1 = TRUE),而且 Stata 的输出似乎是正确的,让我觉得我的马虎R 代码是错误的。是这样吗?

caseid    R      Stata 
  1     0.070    0.633
  2     0.152    1.367
  3     0.404    3.633
  4     0.187    1.683
  5     0.187    1.683
  6     0.143    1.146
  7     0.232    1.854
  8     0.173    1.382
  9     0.107    0.854
 10     0.173    1.382
 11     0.173    1.382

【问题讨论】:

  • 如果您在这里没有得到相关答案,我建议您访问 www.statalist.org,在那里您可能会找到更多关于调查方法的专家。
  • 根据@RobertoFerrer 的建议,我在Statalist 上发布(上面由@NickCox 链接)。我没有在此处重新发布该答案,而是尝试在 R 中复制它,只是发现输出中存在差异(可能是由于我的 R 技能不佳)。所以,我认为最好编辑原始问题以专注于这种不匹配。

标签: r stata multi-level weighting


【解决方案1】:

您的目标在 R 中的分布应该加起来并代表您的总体分布。看my example。我认为 force1 选项不会计算你想要的分布,至少每所学校都有相同的人口权重。这就是 force1 正在做的事情:

targets[[1]]/sum(targets[[1]]) 1.Female 1.Male 2.Female 2.Male 0.278 0.222 0.125 0.375

这是你想要的吗?

【讨论】:

  • “[Y]我们在 R 中的目标应该是一个”。我懂了。这与 Stata 不同,后者汇总为实际人口总数(例如,上面 Statalist 链接的原始样本数据显示,学校 1 的 n=9 和学校 2 的 n=8,上面显示的倾斜权重总和为 17(9 +8) 用于 Stata 输出。但你说得对,R 输出总和为 2(每所学校为 1 + 1)。我猜 Stata 的方法(即等于实际人口总数)对我来说更有意义,这就是为什么我以为我的 R 代码出错了;我想知道这些不同方法的含义是什么。
  • 您可以复制 Stata 在 R 中生成的内容,您只需根据每所学校的教师人数调整比例。我认为这只是实现上的不同,而不是不同的方法。
猜你喜欢
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
相关资源
最近更新 更多