【发布时间】:2020-08-14 09:26:26
【问题描述】:
我正在尝试将以下代码从 Stata 转换为 R:
collapse (mean) erate_total_male laborforce_male erate_total_male_1953 laborforce_male_1953 share_expellees_male share_dest_flats instrument share_agric_1939 city_state (max) occzone_occu [aw=laborforce_male], by(bundesland_id_1953 occupation_id)
我尝试在 R 中使用 collapse 包,但我不确定如何合并 Stata 代码的权重元素或最大值(尽管我可能只是生成一个新变量来解决这个问题) .
test1 <- rep_data %>%
mutate(bundesland_id_1953 =
case_when(
bundesland_id == 8 ~ 99,
bundesland_id == 9 ~ 99,
bundesland_id == 10 ~ 99,
)) %>%
group_by(bundesland_id_1953, occupation_id) %>%
select(erate_total_male, laborforce_male, erate_total_male_1953, laborforce_male_1953, share_expellees_male, share_dest_flats, instrument_male, share_agric_1939, city_state, occzone_occu) %>% fmean
我也尝试为所有变量生成均值,但在添加权重时遇到了同样的问题:
t6Data2 <- rep_data %>%
mutate(bundesland_id_1953 =
case_when(
bundesland_id == 8 ~ 99,
bundesland_id == 9 ~ 99,
bundesland_id == 10 ~ 99,
)) %>%
group_by(bundesland_id_1953, occupation_id) %>% summarise_at(vars(erate_total_male, laborforce_male, erate_total_male_1953, laborforce_male_1953, share_expellees_male, share_dest_flats, instrument_male, share_agric_1939, city_state)
最后,我尝试了一个循环,但是当我使用 lm() 运行回归时,我的变量没有出现:
test444 <- rep_data %>%
mutate(bundesland_id_1953 =
case_when(
bundesland_id == 8 ~ 99,
bundesland_id == 9 ~ 99,
bundesland_id == 10 ~ 99,
)) %>%
group_by(bundesland_id_1953, occupation_id)
t6_data_test4 <- sapply(c(test444$erate_total_male, test444$laborforce_male, test444$erate_total_male_1953, test444$laborforce_male_1953, test444$share_expellees_male, test444$share_dest_flats, test444$instrument_male, test444$share_agric_1939, test444$city_state), function(x) {
weighted.mean(x, weight = laborforce_male)
})
我不知道该怎么做,但如果能提供任何帮助,我将不胜感激。我是一个相对新手,所以对于我在代码中犯的任何明显错误,我深表歉意。
【问题讨论】:
-
如果您使用
dput共享数据并显示相同的预期输出,则更容易提供帮助。请阅读有关how to ask a good question 的信息以及如何提供reproducible example。 -
我是Stata的人,可以看出你的目标是对Stata也非常了解的R人。如果您展示一个包含几行(观察)和几列(变量)的非常小的示例数据集,并且在计算平均值时直接解释分析权重的含义,您更有可能获得详细的响应。这就是
[aw=...]语法。事实上,Stata 语法在这里可以说是无关紧要的。您可以直接询问如何在 R 中做您想做的事情。
标签: r stata mean collapse weighted