作者: 谢雁翔 (南开大学)


目录

  • 1.简介
  • 2. R 的安装下载及 Rcall 命令的安装
  • 2.1 R 的安装下载
  • 2.2 Rcall 命令的安装
  • 3. Rcall 命令及 R 语言初识
  • 3.1 Rcall 命令基本语法
  • 3.2 R 语言基本的数据类型
  • 3.3 R 语言基本的数据结构
  • 4. Stata实例
  • 4.1 Stata 与 R 语言的数据转换
  • 4.2 在 Stata 中运行 R
  • 4.3 拓展:在 R 中运行Stata

本文主要参考如下论文,特此致谢

Haghish E F . Seamless interactive language interfacing between R and Stata[J]. Stata Journal, 2019, 19(1):61-82.[PDF]

1.简介

R 是一门免费、开源的语言,具有体积小 (R 4.0.0 为83.5 M) 、功能多 (外部包达16052个,截止 2020.07.19) 等优势,其被广泛应用于数据挖掘、机器学习、数据可视化、计量经济学和空间统计等领域。那么如何在 Stata 中运行 R 语言?  Rcall

Rcall 安装包可将 R 集成在 Stata 中并允许 R 与 Stata 之间的数据衔接。其能够将 Stata 中的宏、标量、矩阵和数据集自动传输到 R,并将 R 中不同类别的对象 (数据框 data.frame ,列表 list ,矩阵 matrix ,向量 vector ,逻辑 logical 等) 自动导入到 Stata。Stata 与 R 的结合可使 Stata 用户在 R 环境中交互执行的其他编程语言:例如使用 Rcpp 的 C ++,或使用V8软件包的JavaScript。此外,Rcall 允许在 Stata 的 ado 程序中嵌入 R 语言,从而不仅可以调用 R 函数和程序包,还可以通过Stata和R之间的转换来对 Stata 程序包进行编程。

2. R 的安装下载及 Rcall 命令的安装

2.1 R 的安装下载

Rcall命令安装之前首先要安装 R 。由于 R 是免费的开源软件,下载十分方便,可直接百度搜索:R software,登录官网下载 。具体安装操作如下图所示:



stata显示回归标准误_stata显示回归标准误

进入官网下载

stata显示回归标准误_stata 求输出相关系数矩阵命令_02

选择对应镜像

stata显示回归标准误_r语言中正定矩阵由于误差不正定_03

选择对应系统

stata显示回归标准误_r语言中正定矩阵由于误差不正定_04

选择初次安装

stata显示回归标准误_Stata_05

下载最新版本

2.2 Rcall 命令的安装

github package 是安装 rcall 的唯一推荐方式。由于 github 网络不稳定,该安装过程需要花费大量时间。

. net install github, from("https://haghish.github.io/github/")

checking github consistency and verifying not already installed...

installing into C:\Users\Aadministrator\ado\plus\...

installation complete.

. net install github, from("https://haghish.github.io/github/")

checking github consistency and verifying not already installed...

installing into C:\Users\Aadministrator\ado\plus\...

installation complete.

一旦 github 被安装,输入:

github install haghish/rcall, stable

checking rcall consistency and verifying not already installed...

installing into C:\Users\Aadministrator\ado\plus\...

installation complete.

github install haghish/rcall, stable

checking rcall consistency and verifying not already installed...

installing into C:\Users\Aadministrator\ado\plus\...

installation complete.

3. Rcall 命令及 R 语言初识

3.1 Rcall 命令基本语法

要从 Stata 调用 R ,请使用以下语法 (具体参见help rcall):

rcall [mode] [:] [R-command]

mode 模式具体有:vanilla,  sync,  interactive,  console 四种模式,一般默认交互模式不用指定。该软件包还包括一些子命令subcommand,以方便将R集成到Stata中:

rcall [subcommand]

以下函数可用于将数据从 Stata 传输到 R :

函数

英文描述

中文描述

st.scalar(name)

passes a scalar to R

传输标量

st.matrix(name)

passes a matrix to R

传输矩阵

st.var(varname)

passes a numeric or string variable to R

传输数值或字符变量

st.data(filename)

passes Stata data to R. without filename, the currently loaded data is used

将Stata数据传输到R。不带文件名,使用当前加载数据

st.load(dataframe)

loads data from R dataframe to Stata

将数据从 R 的数据框加载到 Stata

3.2 R 语言基本的数据类型

  • 1.数值型 (numeric) ,如:x=2
  • 2.字符型 (character) ,如:x=“2”
  • 3.复数型 (complex) ,如:x= 1 + 3i
  • 4.逻辑型 (logical) ,如:x=1; y=x>2
  • 5.因子型 (factor) ,如:genders
    (该部分内容源自:游万海—— R 语言导学)

3.3 R 语言基本的数据结构

  • 1.向量vector (一维) :数据类型都可取,不允许出现不同数据类型

b = c(1,2,3,4)

  • 2.矩阵matrix (二维) 数据类型都可取,不允许出现不同数据类型

matrix(c(1,2,3,4,5,6),nrow=3,byrow=T/F)

  • 3.数组array (三维) 数据类型都可取,不允许出现不同数据类型

aa=array(1:24,dim=c(3,4,2))

  • 4.数据框data.frame (二维) 数据类型都可取,不同列的数据类型可不同

M = data.frame(a1=c(1,2,3),b=c(“a”,”b”,”c”))

  • 5.列表list (“万能胶”) 数据类型都可取,任何元素数据类型均可不同

x=1:3 y=c("A","B","c") z=c(TRUE,FALSE) a=c(x,y,z) LL=list(x,y,z,a)

  • (该部分内容源自:游万海—— R 语言导学)

4. Stata实例

4.1 Stata 与 R 语言的数据转换

每当执行 rcall 时,Stata 都会自动将 R 对象作为rclass接收。如果R以交互方式运行 (即不使用vanilla子命令) ,则先前的对象仍可访问 Stata ,除非将它们从 R 中更改或删除。此外,从 Stata 中加载的R软件包将保持加载状态,直到终止。在 Stata 中访问 R 对象是同时进行的,这使得使用 rcall 变得方便。例如,可以在 Stata 中访问 R 中定义的数字或字符串向量,就像使用 rclass 调用该对象的名称一样简单,即r(objectname)

  • 数值型案例:
rcall: a display r(a)

100

rcall: a display r(a)

100

如果没有vanilla子命令,则定义的对象将保留在R的内存中,因此,只要调用R便将其返回给Stata。

rcall: a

[1] 100

rcall: a

[1] 100
  • 字符型案例:
rcall: str display r(str)

Hello World

rcall: str display r(str)

"Hello"  "World"

rcall: str display r(str)

Hello World

rcall: str display r(str)

"Hello"  "World"
  • 向量案例:
rcall: v display r(v)

1 2 3 4 5

rcall: v display r(v)

1 2 3 4 5
  • 矩阵案例:
rcall: A = matrix(1:6, nrow=2, byrow = TRUE)
mat list r(A)

r(A)[2,3]

 c1  c2  c3
r1  1  2  3
r2  4  5  6

rcall: A = matrix(1:6, nrow=2, byrow = TRUE)
mat list r(A)

r(A)[2,3]

 c1  c2  c3
r1  1  2  3
r2  4  5  6
  • 列表案例:
rcall: mylist display r(mylist_a)

1 2 3 4 5 6 7 8 9 10

rcall: mylist display r(mylist_a)

1 2 3 4 5 6 7 8 9 10
  • 逻辑性数据案例:
rcall: l display r(l)

TRUE

rcall: l display r(l)

TRUE
  • 空值型数据案例:
rcall: n display r(n)

NULL

rcall: n display r(n)

NULL

为了实现 Stata 和 R 之间的理想交互,rcall 可以将 Stata 中的变量传递给 R 。可以在 R 代码中传递局部宏和全局宏。如下例所示:

global a 99
rcall: (a 
[1] 99

global a 99
rcall: (a 
[1] 99

为了将标量从 Stata 传递到 R ,可以使用st.scalar()函数,如下所示:

scalar a = 50
rcall: (a 
[1] 50

scalar a = 50
rcall: (a 
[1] 50

同样,可以使用 st.matrix () 函数将 Stata 矩阵传输到 R ,如下所示:

matrix A = (1,2\3,4)
matrix B = (96,96\96,96)
rcall: C rcall: C

     [,1] [,2]
[1,]  97  98
[2,]  99  100

matrix A = (1,2\3,4)
matrix B = (96,96\96,96)
rcall: C rcall: C

     [,1] [,2]
[1,]  97  98
[2,]  99  100

当然,也可以调用 Stata 中的 R 矩阵:

mat list r(C)

r(C)[2,2]

    c1  c2
r1  97  98
r2  99  100

mat list r(C)

r(C)[2,2]

    c1  c2
r1  97  98
r2  99  100

使用st.var(varname)函数将变量从 Stata 传递到 R 很方便。因此,只需将分析所需的变量从 Stata 传递到 R ,即可在 R 中执行任何分析:

sysuse auto, clear
(1978 Automobile Data)

rcall: dep rcall: pre rcall: lm(dep~pre)

Call:
lm(formula = dep ~ pre)

Coefficients:
(Intercept)          pre
    11253.1       -238.9

sysuse auto, clear
(1978 Automobile Data)

rcall: dep rcall: pre rcall: lm(dep~pre)

Call:
lm(formula = dep ~ pre)

Coefficients:
(Intercept)          pre
    11253.1       -238.9

rcall包还允许在 st.data (filename) 函数中将 Stata 数据传递到 R 。此功能依赖于 R 中 readstata13 软件包来加载 Stata 数据集,而无需将其转换为 csv 或类似格式。readstata13 的 R 软件包可以按以下方式安装在 Stata 中:

rcall: install.packages("readstata13", repos="http://cran.uk.r-project.org")
package 'readstata13' successfully unpacked and MD5 sums checked

The downloaded binary packages are in C:\Users\Administrator\AppData\Local\Temp\Rtmp0qfZTJ\downloaded_packages

rcall: install.packages("readstata13", repos="http://cran.uk.r-project.org")
package 'readstata13' successfully unpacked and MD5 sums checked

The downloaded binary packages are in C:\Users\Administrator\AppData\Local\Temp\Rtmp0qfZTJ\downloaded_packages

指定数据集的相对或绝对路径,以将数据从 Stata 传输到 R 。例如:

rcall: data rcall: dim(data)

rcall: data rcall: dim(data)

若未指定文件名,则该函数将当前加载的数据传递给R。

sysuse auto, clear
(1978 Automobile Data)
rcall: data rcall: dim(data)

[1] 74 12

sysuse auto, clear
(1978 Automobile Data)
rcall: data rcall: dim(data)

[1] 74 12

最后,可以使用 st.load(dataframe)函数将数据从 R 自动导入到 Stata 中。此功能将自动从R中保存一个 Stata 数据集,并通过清除当前数据集(如果有的话)将其加载到 Stata 中。当然,如果在R中编写了适当的代码以导出 Stata 数据集,则可以更好地控制转换变量类型。该功能在大多数情况下应该可以正常工作:

clear
rcall: st.load(cars)
list in 1/2

     +--------------+
     | speed   dist |
     |--------------|
  1. |     4      2 |
  2. |     4     10 |
     +--------------+

clear
rcall: st.load(cars)
list in 1/2

     +--------------+
     | speed   dist |
     |--------------|
  1. |     4      2 |
  2. |     4     10 |
     +--------------+

4.2 在 Stata 中运行 R

本部分以 Kleiber 和 Zeileis (2008) 的Grunfeld.dta数据集为例,分别使用 Stata 和 R 软件进行固定效应分析。对于面板数据的理解,请参考 Stata - 面板数据模型-一文读懂。

clear
webuse grunfeld,clear //利用webuse从网络读取数据
list in 1/10          // 显示该数据集的前10行

     +--------------------------------------------------+
     | company   year   invest   mvalue   kstock   time |
     |--------------------------------------------------|
  1. |       1   1935    317.6   3078.5      2.8      1 |
  2. |       1   1936    391.8   4661.7     52.6      2 |
  3. |       1   1937    410.6   5387.1    156.9      3 |
  4. |       1   1938    257.7   2792.2    209.2      4 |
  5. |       1   1939    330.8   4313.2    203.4      5 |
     |--------------------------------------------------|
  6. |       1   1940    461.2   4643.9    207.2      6 |
  7. |       1   1941      512   4551.2    255.2      7 |
  8. |       1   1942      448   3244.1    303.7      8 |
  9. |       1   1943    499.6   4053.7    264.1      9 |
 10. |       1   1944    547.5   4379.3    201.6     10 |
     +--------------------------------------------------+

xtset company year,yearly //设置面板数据格式
       panel variable:  company (strongly balanced)
        time variable:  year, 1935 to 1954
                delta:  1 year

xtreg invest mvalue kstock ,fe  //fe表示固定效应

Fixed-effects (within) regression               Number of obs     =        200
Group variable: company                         Number of groups  =         10

R-sq:                                           Obs per group:
     within  = 0.7668                                         min =         20
     between = 0.8194                                         avg =       20.0
     overall = 0.8060                                         max =         20

                                                F(2,188)          =     309.01
corr(u_i, Xb)  = -0.1517                        Prob > F          =     0.0000

------------------------------------------------------------------------------
      invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      mvalue |   .1101238   .0118567     9.29   0.000     .0867345    .1335131
      kstock |   .3100653   .0173545    17.87   0.000     .2758308    .3442999
       _cons |  -58.74393   12.45369    -4.72   0.000    -83.31086     -34.177
-------------+----------------------------------------------------------------
     sigma_u |  85.732501
     sigma_e |  52.767964
         rho |  .72525012   (fraction of variance due to u_i)
------------------------------------------------------------------------------
F test that all u_i=0: F(9, 188) = 49.18                     Prob > F = 0.0000


rcall:install.packages("plm",repos ="http://cran.us.r-project.org")
package 'plm' successfully unpacked and MD5 sums checked

The downloaded binary packages are in C:\Users\Administrator\AppData\Local\Temp\RtmpwjNECq\downloaded_packages

rcall:library(plm)
rcall:data("Grunfeld", package="plm")
rcall:head(Grunfeld,10)

   firm year   inv  value capital
1     1 1935 317.6 3078.5     2.8
2     1 1936 391.8 4661.7    52.6
3     1 1937 410.6 5387.1   156.9
4     1 1938 257.7 2792.2   209.2
5     1 1939 330.8 4313.2   203.4
6     1 1940 461.2 4643.9   207.2
7     1 1941 512.0 4551.2   255.2
8     1 1942 448.0 3244.1   303.7
9     1 1943 499.6 4053.7   264.1
10    1 1944 547.5 4379.3   201.6

rcall:zz rcall:summary(zz)

Oneway (individual) effect Within Model

Call:
plm(formula = inv ~ value + capital, data = Grunfeld, index = c("firm"))

Balanced Panel: n = 10, T = 20, N = 200

Residuals:
      Min.    1st Qu.     Median    3rd Qu.       Max.
-184.00857  -17.64316    0.56337   19.19222  250.70974

Coefficients:
        Estimate Std. Error t-value  Pr(>|t|)
value   0.110124   0.011857  9.2879 < 2.2e-16 ***
capital 0.310065   0.017355 17.8666 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    2244400
Residual Sum of Squares: 523480
R-Squared:      0.76676
Adj. R-Squared: 0.75311
F-statistic: 309.014 on 2 and 188 DF, p-value: < 2.22e-16

.
end of do-file


clear
webuse grunfeld,clear //利用webuse从网络读取数据
list in 1/10          // 显示该数据集的前10行

     +--------------------------------------------------+
     | company   year   invest   mvalue   kstock   time |
     |--------------------------------------------------|
  1. |       1   1935    317.6   3078.5      2.8      1 |
  2. |       1   1936    391.8   4661.7     52.6      2 |
  3. |       1   1937    410.6   5387.1    156.9      3 |
  4. |       1   1938    257.7   2792.2    209.2      4 |
  5. |       1   1939    330.8   4313.2    203.4      5 |
     |--------------------------------------------------|
  6. |       1   1940    461.2   4643.9    207.2      6 |
  7. |       1   1941      512   4551.2    255.2      7 |
  8. |       1   1942      448   3244.1    303.7      8 |
  9. |       1   1943    499.6   4053.7    264.1      9 |
 10. |       1   1944    547.5   4379.3    201.6     10 |
     +--------------------------------------------------+

xtset company year,yearly //设置面板数据格式
       panel variable:  company (strongly balanced)
        time variable:  year, 1935 to 1954
                delta:  1 year

xtreg invest mvalue kstock ,fe  //fe表示固定效应

Fixed-effects (within) regression               Number of obs     =        200
Group variable: company                         Number of groups  =         10

R-sq:                                           Obs per group:
     within  = 0.7668                                         min =         20
     between = 0.8194                                         avg =       20.0
     overall = 0.8060                                         max =         20

                                                F(2,188)          =     309.01
corr(u_i, Xb)  = -0.1517                        Prob > F          =     0.0000

------------------------------------------------------------------------------
      invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      mvalue |   .1101238   .0118567     9.29   0.000     .0867345    .1335131
      kstock |   .3100653   .0173545    17.87   0.000     .2758308    .3442999
       _cons |  -58.74393   12.45369    -4.72   0.000    -83.31086     -34.177
-------------+----------------------------------------------------------------
     sigma_u |  85.732501
     sigma_e |  52.767964
         rho |  .72525012   (fraction of variance due to u_i)
------------------------------------------------------------------------------
F test that all u_i=0: F(9, 188) = 49.18                     Prob > F = 0.0000


rcall:install.packages("plm",repos ="http://cran.us.r-project.org")
package 'plm' successfully unpacked and MD5 sums checked

The downloaded binary packages are in C:\Users\Administrator\AppData\Local\Temp\RtmpwjNECq\downloaded_packages

rcall:library(plm)
rcall:data("Grunfeld", package="plm")
rcall:head(Grunfeld,10)

   firm year   inv  value capital
1     1 1935 317.6 3078.5     2.8
2     1 1936 391.8 4661.7    52.6
3     1 1937 410.6 5387.1   156.9
4     1 1938 257.7 2792.2   209.2
5     1 1939 330.8 4313.2   203.4
6     1 1940 461.2 4643.9   207.2
7     1 1941 512.0 4551.2   255.2
8     1 1942 448.0 3244.1   303.7
9     1 1943 499.6 4053.7   264.1
10    1 1944 547.5 4379.3   201.6

rcall:zz rcall:summary(zz)

Oneway (individual) effect Within Model

Call:
plm(formula = inv ~ value + capital, data = Grunfeld, index = c("firm"))

Balanced Panel: n = 10, T = 20, N = 200

Residuals:
      Min.    1st Qu.     Median    3rd Qu.       Max.
-184.00857  -17.64316    0.56337   19.19222  250.70974

Coefficients:
        Estimate Std. Error t-value  Pr(>|t|)
value   0.110124   0.011857  9.2879 < 2.2e-16 ***
capital 0.310065   0.017355 17.8666 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    2244400
Residual Sum of Squares: 523480
R-Squared:      0.76676
Adj. R-Squared: 0.75311
F-statistic: 309.014 on 2 and 188 DF, p-value: < 2.22e-16

.
end of do-file

也可以利用 R 通过利用write.csv()函数直接将数据转换为 .csv格式的数据储存,再导入 Stata 进行面板数据回归进一步与 R 的回归结果进行对比。使用 R 软件 (panel linear model, plm) 包自带数据集 Produc (Munnell, 1990) 。

rcall:install.packages("plm")
rcall:library(plm)
rcall:data(Produc,package="plm")
rcall:aa rcall:summary(aa)

Oneway (individual) effect Within Model

Call:
plm(formula = log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
    data = Produc, index = c("state", "year"))

Balanced Panel: n = 48, T = 17, N = 816

Residuals:
     Min.   1st Qu.    Median   3rd Qu.      Max.
-0.120456 -0.023741 -0.002041  0.018144  0.174718

Coefficients:
             Estimate  Std. Error t-value  Pr(>|t|)
log(pcap) -0.02614965  0.02900158 -0.9017    0.3675
log(pc)    0.29200693  0.02511967 11.6246 < 2.2e-16 ***
log(emp)   0.76815947  0.03009174 25.5273 < 2.2e-16 ***
unemp     -0.00529774  0.00098873 -5.3582 1.114e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    18.941
Residual Sum of Squares: 1.1112
R-Squared:      0.94134
Adj. R-Squared: 0.93742
F-statistic: 3064.81 on 4 and 764 DF, p-value: < 2.22e-16


rcall:library(plm)
rcall:data(Produc,package="plm")
rcall:write.csv(Produc,file="D:/produc.csv")
import delimited "D:\produc.csv", encoding(ISO-8859-9) clear
(12 vars, 816 obs)

encode state, gen (id)
gen lgsp = log(gsp)
gen lpcap = log(pcap)
gen lpc = log(pc)
gen lemp = log(emp)

xtset id year
       panel variable:  id (strongly balanced)
        time variable:  year, 1970 to 1986
                delta:  1 unit

xtreg lgsp lpcap lpc lemp unemp ,fe

Fixed-effects (within) regression               Number of obs     =        816
Group variable: id                              Number of groups  =         48

R-sq:                                           Obs per group:
     within  = 0.9413                                         min =         17
     between = 0.9921                                         avg =       17.0
     overall = 0.9910                                         max =         17

                                                F(4,764)          =    3064.81
corr(u_i, Xb)  = 0.0608                         Prob > F          =     0.0000

------------------------------------------------------------------------------
        lgsp |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       lpcap |  -.0261493   .0290016    -0.90   0.368    -.0830815    .0307829
         lpc |   .2920067   .0251197    11.62   0.000     .2426949    .3413185
        lemp |   .7681595   .0300917    25.53   0.000     .7090872    .8272318
       unemp |  -.0052977   .0009887    -5.36   0.000    -.0072387   -.0033568
       _cons |   2.352898   .1748131    13.46   0.000     2.009727    2.696069
-------------+----------------------------------------------------------------
     sigma_u |  .09057293
     sigma_e |  .03813705
         rho |   .8494045   (fraction of variance due to u_i)
------------------------------------------------------------------------------
F test that all u_i=0: F(47, 764) = 75.82                    Prob > F = 0.0000

.
end of do-file


rcall:install.packages("plm")
rcall:library(plm)
rcall:data(Produc,package="plm")
rcall:aa rcall:summary(aa)

Oneway (individual) effect Within Model

Call:
plm(formula = log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
    data = Produc, index = c("state", "year"))

Balanced Panel: n = 48, T = 17, N = 816

Residuals:
     Min.   1st Qu.    Median   3rd Qu.      Max.
-0.120456 -0.023741 -0.002041  0.018144  0.174718

Coefficients:
             Estimate  Std. Error t-value  Pr(>|t|)
log(pcap) -0.02614965  0.02900158 -0.9017    0.3675
log(pc)    0.29200693  0.02511967 11.6246 < 2.2e-16 ***
log(emp)   0.76815947  0.03009174 25.5273 < 2.2e-16 ***
unemp     -0.00529774  0.00098873 -5.3582 1.114e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Total Sum of Squares:    18.941
Residual Sum of Squares: 1.1112
R-Squared:      0.94134
Adj. R-Squared: 0.93742
F-statistic: 3064.81 on 4 and 764 DF, p-value: < 2.22e-16


rcall:library(plm)
rcall:data(Produc,package="plm")
rcall:write.csv(Produc,file="D:/produc.csv")
import delimited "D:\produc.csv", encoding(ISO-8859-9) clear
(12 vars, 816 obs)

encode state, gen (id)
gen lgsp = log(gsp)
gen lpcap = log(pcap)
gen lpc = log(pc)
gen lemp = log(emp)

xtset id year
       panel variable:  id (strongly balanced)
        time variable:  year, 1970 to 1986
                delta:  1 unit

xtreg lgsp lpcap lpc lemp unemp ,fe

Fixed-effects (within) regression               Number of obs     =        816
Group variable: id                              Number of groups  =         48

R-sq:                                           Obs per group:
     within  = 0.9413                                         min =         17
     between = 0.9921                                         avg =       17.0
     overall = 0.9910                                         max =         17

                                                F(4,764)          =    3064.81
corr(u_i, Xb)  = 0.0608                         Prob > F          =     0.0000

------------------------------------------------------------------------------
        lgsp |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       lpcap |  -.0261493   .0290016    -0.90   0.368    -.0830815    .0307829
         lpc |   .2920067   .0251197    11.62   0.000     .2426949    .3413185
        lemp |   .7681595   .0300917    25.53   0.000     .7090872    .8272318
       unemp |  -.0052977   .0009887    -5.36   0.000    -.0072387   -.0033568
       _cons |   2.352898   .1748131    13.46   0.000     2.009727    2.696069
-------------+----------------------------------------------------------------
     sigma_u |  .09057293
     sigma_e |  .03813705
         rho |   .8494045   (fraction of variance due to u_i)
------------------------------------------------------------------------------
F test that all u_i=0: F(47, 764) = 75.82                    Prob > F = 0.0000

.
end of do-file

4.3 拓展:在 R 中运行Stata

> install.packages("RStata")      ##在 R 中安装包“RStata”,首次运行需要选择合适镜像地址
将程序包安装入‘D:/Documents/R/win-library/4.0’
(因为‘lib’没有被指定)
--- 在此連線階段时请选用CRAN的鏡子 ---
试开URL’https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/4.0/RStata_1.1.1.zip'
Content type 'application/zip' length 19796 bytes (19 KB)
downloaded 19 KB
程序包‘RStata’打开成功,MD5和检查也通过
下载的二进制程序包在
        C:\Users\Administrator\AppData\Local\Temp\RtmpY1ixWU\downloaded_packages里
> library(RStata)           ##加载包
> chooseStataBin()          ##找出stata具体位置确定打开并复制显示路径
[1] "\"D:\\Stata16\\StataMP-64\""
> options("RStata.StataPath" =  "\"D:\\Stata16\\StataMP-64\"") 
> options("RStata.StataVersion" = 16)  ##设置版本
> stata_src + sysuse auto, clear
+ reg mpg weight 
+ "
> stata(stata_src)
.  
. sysuse auto, clear
(1978 Automobile Data)
. reg mpg weight 
      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(1, 72)        =    134.62
       Model |   1591.9902         1   1591.9902   Prob > F        =    0.0000
    Residual |  851.469256        72  11.8259619   R-squared       =    0.6515
-------------+----------------------------------   Adj R-squared   =    0.6467
       Total |  2443.45946        73  33.4720474   Root MSE        =    3.4389
------------------------------------------------------------------------------
         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      weight |  -.0060087   .0005179   -11.60   0.000    -.0070411   -.0049763
       _cons |   39.44028   1.614003    24.44   0.000     36.22283    42.65774
------------------------------------------------------------------------------
> 
> install.packages("RStata")      ##在 R 中安装包“RStata”,首次运行需要选择合适镜像地址
将程序包安装入‘D:/Documents/R/win-library/4.0’
(因为‘lib’没有被指定)
--- 在此連線階段时请选用CRAN的鏡子 ---
试开URL’https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/4.0/RStata_1.1.1.zip'
Content type 'application/zip' length 19796 bytes (19 KB)
downloaded 19 KB
程序包‘RStata’打开成功,MD5和检查也通过
下载的二进制程序包在
        C:\Users\Administrator\AppData\Local\Temp\RtmpY1ixWU\downloaded_packages里
> library(RStata)           ##加载包
> chooseStataBin()          ##找出stata具体位置确定打开并复制显示路径
[1] "\"D:\\Stata16\\StataMP-64\""
> options("RStata.StataPath" =  "\"D:\\Stata16\\StataMP-64\"") 
> options("RStata.StataVersion" = 16)  ##设置版本
> stata_src + sysuse auto, clear
+ reg mpg weight 
+ "
> stata(stata_src)
.  
. sysuse auto, clear
(1978 Automobile Data)
. reg mpg weight 
      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(1, 72)        =    134.62
       Model |   1591.9902         1   1591.9902   Prob > F        =    0.0000
    Residual |  851.469256        72  11.8259619   R-squared       =    0.6515
-------------+----------------------------------   Adj R-squared   =    0.6467
       Total |  2443.45946        73  33.4720474   Root MSE        =    3.4389
------------------------------------------------------------------------------
         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      weight |  -.0060087   .0005179   -11.60   0.000    -.0070411   -.0049763
       _cons |   39.44028   1.614003    24.44   0.000     36.22283    42.65774
------------------------------------------------------------------------------
>