尝试使用 R 中的 plm 在 stata 中重现 xtreg

Trying to reproduce xtreg in stata with plm in R

如果不使用 Stata 中的 fe 选项,我似乎无法在 R 中匹配 Stata 中的 xtreg 命令。 当我进行标准回归或具有固定效应的面板模型时,Stata 和 R 中的系数相同。

示例数据:

library("plm" )
z <- Cigar[ Cigar$year %in% c( 63, 73) , ]

#saving so I can use in Stata
foreign::write.dta( z , "C:/Users/matthewr/Desktop/temp.dta")

所以我在 R 中得到了相同的系数:

coef( lm( sales ~ pop , data= z2   ) )

这在 Stata 中

use "C:/Users/matthewr/Desktop/temp.dta" , clear   
reg sales pop

当我设置面板并使用固定效果选项时它起作用了。

z2 <- pdata.frame( z , index=c("state", "year")  )    
coef( plm( sales ~ pop , data= z2  , model="within"   ) ) # matches xtreg , fe

在 Stata 中匹配这个

xtset state year
xtreg sales pop, fe

当我没有使用固定效果选项时,我不知道如何匹配 Stata 我试图在 R 中匹配这个结果,但不能 这是我想要重现的结果: 系数:-.0006838

  xtreg sales pop

Stata xtreg y x相当于xtreg y x, re,所以你要的是计算随机效应

summary(plm(sales ~ pop, data=z, model="random", index=c("state", "year")))$coe
#                  Estimate  Std. Error   z-value     Pr(>|z|)
# (Intercept)  1.311398e+02 6.499511330 20.176878 1.563130e-90
# pop         -6.837769e-04 0.001077432 -0.634636 5.256658e-01

数据:

xtreg sales pop, re

       sales |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         pop |  -.0006838   .0010774    -0.63   0.526    -.0027955     .001428
       _cons |   131.1398   6.499511    20.18   0.000      118.401    143.8787

@jay.sf 已回答您的问题。我只是添加了其他内容,尽管它可能无法直接回答您的问题。 Stata xtregR plm 都有一些选项,我觉得 RStata 包可能是一个方便的工具来尝试不同的选项并比较 Stata 和 [=15= 的结果] 直接在 RStudio 中。我认为这可能会有所帮助。 Stata 路径仅适用于我的计算机。

library("plm" )
library(RStata)
data("Cigar", package = "plm")
z <- Cigar[ Cigar$year %in% c( 63, 73) , ]

options("RStata.StataPath" = "\"C:\Program Files (x86)\Stata14\StataSE-64\"")
options("RStata.StataVersion" = 14)

# Stata fe 
stata_do1 <- '
  xtset state year
  xtreg sales pop, fe
'
stata(stata_do1, data.out = TRUE, data.in = z)
#> . 
#> .   xtset state year
#>        panel variable:  state (strongly balanced)
#>         time variable:  year, 63 to 73, but with gaps
#>                 delta:  1 unit
#> .   xtreg sales pop, fe
#> 
#> Fixed-effects (within) regression               Number of obs     =         92
#> Group variable: state                           Number of groups  =         46
#> 
#> R-sq:                                           Obs per group:
#>      within  = 0.0118                                         min =          2
#>      between = 0.0049                                         avg =        2.0
#>      overall = 0.0048                                         max =          2
#> 
#>                                                 F(1,45)           =       0.54
#> corr(u_i, Xb)  = -0.3405                        Prob > F          =     0.4676
#> 
#> ------------------------------------------------------------------------------
#>        sales |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
#> -------------+----------------------------------------------------------------
#>          pop |  -.0032108   .0043826    -0.73   0.468    -.0120378    .0056162
#>        _cons |   141.5186   18.06909     7.83   0.000     105.1256    177.9116
#> -------------+----------------------------------------------------------------
#>      sigma_u |  34.093409
#>      sigma_e |  15.183908
#>          rho |  .83448264   (fraction of variance due to u_i)
#> ------------------------------------------------------------------------------
#> F test that all u_i=0: F(45, 45) = 8.91                      Prob > F = 0.0000

# R 
z2 <- pdata.frame( z , index=c("state", "year")  )    
coef( plm( sales ~ pop , data= z2  , model="within" ) )
#>          pop 
#> -0.003210817

# Stata re
stata_do2 <- '
  xtset state year
  xtreg sales pop, re
'
stata(stata_do2, data.out = TRUE, data.in = z)
#> . 
#> .   xtset state year
#>        panel variable:  state (strongly balanced)
#>         time variable:  year, 63 to 73, but with gaps
#>                 delta:  1 unit
#> .   xtreg sales pop, re
#> 
#> Random-effects GLS regression                   Number of obs     =         92
#> Group variable: state                           Number of groups  =         46
#> 
#> R-sq:                                           Obs per group:
#>      within  = 0.0118                                         min =          2
#>      between = 0.0049                                         avg =        2.0
#>      overall = 0.0048                                         max =          2
#> 
#>                                                 Wald chi2(1)      =       0.40
#> corr(u_i, X)   = 0 (assumed)                    Prob > chi2       =     0.5257
#> 
#> ------------------------------------------------------------------------------
#>        sales |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
#> -------------+----------------------------------------------------------------
#>          pop |  -.0006838   .0010774    -0.63   0.526    -.0027955     .001428
#>        _cons |   131.1398   6.499511    20.18   0.000      118.401    143.8787
#> -------------+----------------------------------------------------------------
#>      sigma_u |  30.573218
#>      sigma_e |  15.183908
#>          rho |  .80214841   (fraction of variance due to u_i)
#> ------------------------------------------------------------------------------

# R random
coef(plm(sales ~ pop, 
            data=z, 
            model="random", 
            index=c("state", "year")))
#>   (Intercept)           pop 
#>  1.311398e+02 -6.837769e-04

reprex package (v0.3.0)

于 2020-01-27 创建