【发布时间】:2020-04-24 10:16:54
【问题描述】:
我想在 R 中创建一个频率表,将另一个变量视为权重。
更准确地说,作为“分析权重”,例如在 Stata 中。根据其帮助文件,
aweights, or analytic weights, are weights that are inversely
proportional to the variance of an observation; i.e., the variance of
the jth observation is assumed to be sigma^2/w_j, where w_j are the
weights. Typically, the observations represent averages and the
weights are the number of elements that gave rise to the average.
For most Stata commands, the recorded scale of aweights is
irrelevant; Stata internally rescales them to sum to N, the number of
observations in your data, when it uses them.
stackflow 成员的宝贵贡献是:
Table_WEIGHT <- xtabs(WEIGHT ~ INTERVIEW_DAY, timeuse_2003)
> Prop <- prop.table(Table_WEIGHT)
> Cum <- cumsum(100 * Prop / sum(Prop))
> Cum
1 2 3 4 5 6 7
14.35397 29.14973 43.23935 57.31355 71.50782 85.80359 100.00000
> out <- data.frame(INTERVIEW_DAY = names(Table_WEIGHT), Freq = as.numeric(Table_WEIGHT),
+ Prop = as.numeric(Prop), Cum = as.numeric(Cum))
> out
INTERVIEW_DAY Freq Prop Cum
1 1 11803438268 0.1435397 14.35397
2 2 12166729888 0.1479576 29.14973
3 3 11586059070 0.1408962 43.23935
4 4 11573379591 0.1407420 57.31355
5 5 11672116808 0.1419427 71.50782
6 6 11755579310 0.1429577 85.80359
7 7 11673877965 0.1419641 100.00000
尽管如此,频率仍然不是我所期望的,因为我们使用第二个变量的总和作为权重,而不是上面设置的“分析权重”。
所需的表应该是:
(mean) |
interview_d |
ay | Freq. Percent Cum.
------------+-----------------------------------
1 | 2,974.1424 14.35 14.35
2 | 3,065.6819 14.80 29.15
3 | 2,919.3688 14.09 43.24
4 |2,916.17392 14.07 57.31
5 |2,941.05299 14.19 71.51
6 | 2,962.0832 14.30 85.80
7 | 2,941.4968 14.20 100.00
------------+-----------------------------------
Total | 20,720 100.00
请注意,“频率”是完全不同的。
这里是两个变量 (INTERVIEW_DATE) 和 WEIGHT(WEIGHT) 的示例,它们是调查日期和原始文章中未指定的重量。
> timeuse_2003$INTERVIEW_DATE[1:15]
[1] "2003-01-03" "2003-01-04" "2003-01-04" "2003-01-02" "2003-01-09" "2003-01-02" "2003-01-06"
[8] "2003-01-07" "2003-01-04" "2003-01-09" "2003-01-04" "2003-01-05" "2003-01-04" "2003-01-01"
[15] "2003-01-04"
> timeuse_2003$WEIGHT[1:15]
[1] 8155462.7 1735322.5 3830527.5 6622023.0 3068387.3 3455424.9 1637826.3 6574426.8 1528296.3
[10] 4277052.8 1961482.3 505227.2 2135476.8 5366309.3 1058351.1
我会感谢任何贡献。
【问题讨论】:
-
欢迎来到 Stack Overflow!你能提供一个minimal, reproducible example 你的数据吗?
-
嗨@M--,感谢您的互动。我将使用变量样本更新问题。该数据集还有 69 个其他变量。 20720 个观测值。你还需要什么吗?
-
在运行代码重现问题之前,我无法确定需要什么。如果你点击我分享的链接,它会告诉你需要什么以及如何确保你包含了所有需要的东西。干杯。
-
问题其实是stackoverflow.com/questions/59555243/…的延续。在那里我得到了频率表的支持。现在我试图弄清楚如何生成 Stata 的这种“分析权重”,但在 R 中。我将使用两张表更新问题,我拥有的一张和我需要的一张。