Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporates option to import Stata estimation results into R. #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

cmcclellan
Copy link

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.

@tsimonato
Copy link

tsimonato commented Mar 20, 2021

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

@cmcclellan
Copy link
Author

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

@tsimonato
Copy link

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’

@cmcclellan
Copy link
Author

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?

@tsimonato
Copy link

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

@cmcclellan
Copy link
Author

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.

@tsimonato
Copy link

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

@cmcclellan
Copy link
Author

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

@tsimonato
Copy link

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.

@cmcclellan
Copy link
Author

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.

@tsimonato
Copy link

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!

@cmcclellan
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants