R 中是否存在类似 tab() gen() 的语法?

Does syntax like tab() gen() exist in R?

我现在正在尝试将 stata 转换为 R,我在 stata 中有以下代码:

tsset code_numeric year_numeric
sort code_numeric year_numeric
tab year, gen (yr)
tab code, gen(cd)
reg fhpolrigaug L.(fhpolrigaug lrgdpch) yr* cd* if sample==1, cluster(code)

有谁知道我应该在 R 中使用哪种语法来获得与在 stata 中相同的结果?具体来说,我指的是 tab year, gen(yr)tab code, gen(cd)reg fhpolrigaug L.(fhpolrigaug lrgdpch) yr* cd* if sample==1, cluster(code) 部分。我使用的数据集在link:https://www.openicpsr.org/openicpsr/project/113251/version/V1/view

我想这或多或少是你想要达到的目标:

setwd("your_directory/")
library(plm)
library(readxl)
df <-read_excel("Income-and-Democracy-Data-AER-adjustment.xls", sheet = "10 Year Panel")

df <- pdata.frame(df, index = c("code", "year"), drop.index = FALSE)

#produce lagged variables if needed:
#df$fhpolrigaug_lag <- lag(df$fhpolrigaug)
#df$lrgdpch_lag <- lag(df$lrgdpch)


regModel <- plm(fhpolrigaug ~ lag(fhpolrigaug) + lag(lrgdpch),
          data = subset(df, sample==1) , index = c("code","year"), model = "within", effect = "twoways")
summary(regModel)

#for more info, look here: https://philippbroniecki.com/statistics1/seminar10.html

还剩一件事;您将不得不自己找出如何对标准错误进行聚类。

PS:这是 R 和 Stata 命令的并排比较:http://rslblissett.com/wp-content/uploads/2016/09/sidebyside_130826.pdf