RStata icon indicating copy to clipboard operation
RStata copied to clipboard

Incorporates option to import Stata estimation results into R.

Open cmcclellan opened this issue 3 years ago • 12 comments

Allows users to run analyses in Stata and directly import the results for further processing, reporting, or plotting in R. The addition adds the relevant option, returns.out, to the stata() function. If selected, Stata code is appended to the SRC object which loops through the active and stored return and ereturn results in Stata and writes them to an excel file. Then the excel file is read into a list in R and returned as the output.

If both data.out and returns.out are selected, a list is returned in which the first element is the data frame from the data.out option and the second element is the returns list.

On a side note, this is my first time using Github to contribute to a project, so please forgive any breaches of etiquette.

cmcclellan avatar Mar 03 '21 18:03 cmcclellan

Hi Friend, Can you give an example using this feature? I'm trying to make it work using your branch but I can't. Thanks

tsimonato avatar Mar 20 '21 17:03 tsimonato

Could you post the code you're trying to run and whatever outcome you get? Thanks.

cmcclellan avatar Mar 20 '21 21:03 cmcclellan

Sure.

Code: data(iris) RStata::stata("sum Species", data.in = iris, returns.out=T)

Outcome: . sum Species

Variable | Obs Mean Std. Dev. Min Max -------------+-------------------------------------------------------- Species | 150 2 .8192319 1 3 Erro: path does not exist: ‘ReturnsToR.xls’

tsimonato avatar Mar 20 '21 21:03 tsimonato

So it looks like it's having trouble with the import back into R. Can you confirm that you can bring the data back to R (data.out=T by itself) and what version of Stata you have?

cmcclellan avatar Mar 20 '21 22:03 cmcclellan

data.out=T is working. My stata is version 13. Do you think I need to upgrade to work?

tsimonato avatar Mar 20 '21 23:03 tsimonato

That's likely the issue. This was written in Stata 15 and they changed the syntax for the key function (putexcel) around Stata 14.2. I'll work on a solution to accommodate earlier Stata versions, but don't really have a work around for you right now.

cmcclellan avatar Mar 21 '21 00:03 cmcclellan

I tried on stata 15, but there was another error:

Code: data (iris) names (iris) <- c ("a", "b", "c", "d", "e") RStata :: stata ("nlsur (d = {c1} + {c2} * e) variable (d e)", data.in = iris, returns.out = T)

Result: . nlsur (d = {c1} + {c2} * e) variable (d e) (obs = 150)

Calculating NLS estimates ... Iteration 0: residual SS = 7.359933 Iteration 1: residual SS = 7.359933 Calculating FGNLS estimates ... Iteration 0: scaled RSS = 150

Regression FGNLS


   Equation | Obs Parms RMSE R-sq Constant

---------------- + --------------------------------- --------------------- 1 d | 150 2 .221509 0.9150 c1



       d | Coef. Default To err. z P> | z | [95% Conf. Break]

------------- + ------------------------------------ ---------------------------- / c1 | -.5806667 .0478514 -12.13 0.000 -.6744537 -.4868796 / c2 | .89 .0221509 40.18 0.000 .846585 .933415


Error in .rowNamesDF <- (x, value = value): missing values ​​in 'row.names' are not allowed

tsimonato avatar Mar 21 '21 04:03 tsimonato

OK, I've pushed an update to accommodate the nslur output.

cmcclellan avatar Mar 21 '21 14:03 cmcclellan

There is still no update if I try to install from your git. But there is no hurry, I try again in the future. Thanks for the support.

tsimonato avatar Mar 21 '21 19:03 tsimonato

Apologies. Still getting the hang of using github, but I think it's pushed now. Give it a try and please let me know how it turns out.

cmcclellan avatar Mar 22 '21 12:03 cmcclellan

Hi Friend. No problem, I am also learning these tools.

It's working now. I just couldn't find the p-value in the output list. Is it possible to extract this information from the list?

Thanks!

tsimonato avatar Mar 28 '21 23:03 tsimonato

So the structure of the list will vary depending on what returns and ereturns the Stata call produces (so a lot of what is available depends on the whims of whomever is writing the Stata code). However, for most (all?) estimation commands, the output that is produced on the screen in Stata is stored in a return matrix.

So, for the nlsur command you mentioned above,

results <- RStata :: stata ("nlsur (d = {c1} + {c2} * e) variable (d e)", data.in = iris, returns.out = T)

the screen output table is going to be in results$Active$r$matrix$table and you can extract the pvalues from there.

cmcclellan avatar Mar 29 '21 12:03 cmcclellan