vasili111
vasili111

Reputation: 6930

How to capture Rstata package output for subsequent processing in R

I run the following code:

library(RStata)

stata("glm dist speed", stata.version = 15, data.in = cars)

output:

. glm dist speed

Iteration 0:   log likelihood = -206.57843  

Generalized linear models                         No. of obs      =         50
Optimization     : ML                             Residual df     =         48
                                                  Scale parameter =   236.5317
Deviance         =  11353.52105                   (1/df) Deviance =   236.5317
Pearson          =  11353.52105                   (1/df) Pearson  =   236.5317

Variance function: V(u) = 1                       [Gaussian]
Link function    : g(u) = u                       [Identity]

                                                  AIC             =   8.343137
Log likelihood   = -206.5784315                   BIC             =   11165.74

------------------------------------------------------------------------------
             |                 OIM
        dist |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       speed |   3.932409   .4155128     9.46   0.000     3.118019    4.746799
       _cons |  -17.57909    6.75844    -2.60   0.009    -30.82539   -4.332796
------------------------------------------------------------------------------ 

However, the command below does not capture the output:

a <- stata("glm dist speed", stata.version = 15, data.in = cars) 

In this case, object a is just empty.

How can I capture the above output so I can use it in subsequent R calculations?

Upvotes: 1

Views: 586

Answers (1)

user8682794
user8682794

Reputation:

The only way you can do this with RStata, is by extracting the results and saving them as variables in a modified dataset, which you then send back to R.

The following toy example returns to object a the coefficients from a linear regression:

stata_cmd <- "

sysuse auto
regress mpg weight
matrix k = e(b)
svmat k
keep k*
save myauto

"
stata(stata_cmd)

. sysuse auto
(1978 Automobile Data)
. regress 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
------------------------------------------------------------------------------
. matrix k = e(b)
. svmat k
. keep k*
. save myauto
file myauto.dta saved

a <- stata("sysuse myauto, clear", data.out = TRUE)

> head(a)
            k1       k2
1 -0.006008687 39.44028
2           NA       NA
3           NA       NA
4           NA       NA
5           NA       NA
6           NA       NA

Upvotes: 3

Related Questions