【问题标题】:Does syntax like tab() gen() exist in R?R 中是否存在类似 tab() gen() 的语法?
【发布时间】:2020-06-09 13:04:45
【问题描述】:

我现在正在尝试将 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) 部分。我使用的数据集在链接中:https://www.openicpsr.org/openicpsr/project/113251/version/V1/view

【问题讨论】:

  • 这个可以一分为二。 tabulate 和 generate() 选项 (a) 显示指定变量的不同值的频率表 (year, 不管) (b) 为所述不同值生成指示变量。即使在 Stata (b) 中也是历史性的,因为您可以向大多数模型拟合命令发出信号,将变量视为一系列指标,也就是说(我不是 R 人),因为我理解它在 R 中也是完全标准的. (a) 可能是您寻求的任何方式,但我想在 R 中有很多方法可以做到这一点。

标签: r stata generate


【解决方案1】:

我想这或多或少是您想要实现的目标:

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

【讨论】:

  • 感谢您提供 pdf 文件!我已经尝试过你的代码,但系数的结果与统计数据不同......
  • 我也是一个本地的 Stata 用户,他只能偶尔使用 R。一般来说,代码应该复制你的结果,因为在回归中包含 yr 和 cd 虚拟变量与包含 year 和 code 相同plm 中的固定效果。可能,我错误地指定了模型。我将把找到正确模型规范的任务留给你。
  • 有关更多信息,回归模型显示为:d_it = alpha * lag(d_it) + gamma * lag(y_it) + mu_t + delta_i + u_it,其中 d_it 由 fhpolrigaug 测量; y_it 由 lrgdpch 提供; mu_t 和 delta_i 是指标变量(yr* 和 cd*); u_it 是误差项,即 i.i.d。和正态分布。 R也可以到达吗?谢谢
  • 当然,这可以通过 R 实现。请查看philippbroniecki.com/statistics1/seminar10.html,了解有关如何使用 R 对面板数据运行固定效应回归的更多详细信息。
  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-24
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多