有没有办法在R中折叠加权平均数?

有没有办法在R中折叠加权平均数?,r,stata,mean,collapse,weighted,R,Stata,Mean,Collapse,Weighted,我正在尝试将以下代码从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_1

我正在尝试将以下代码从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代码的weight元素或最大值,尽管我可能只是生成一个新变量来解决这个问题

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)
}) 

我不知道该怎么办,但我希望能得到任何帮助。我是一个相对的新手,因此对于我在代码中犯的任何明显错误,我深表歉意。

这是有效的:

图书馆弹琴
d如果您使用dput共享数据并显示相同数据的预期输出,则更容易提供帮助。请阅读相关信息以及如何给出答案。我是一个Stata人,可以看出你的目标是R人,他们也非常了解Stata。如果显示一个非常小的示例数据集,其中包含几行观察值和几列变量,并直接解释计算平均值时分析权重意味着什么,则更可能得到详细的响应。这就是[aw=…]语法。事实上,Stata语法在这里可以说是无关紧要的。你可以直接问如何在R做你想做的事。