I’ve friends favouring Stata rather than R, while others do the inverse. However, few of them actually know that it is possible to work on both softwares, simultaneously inclusive. It is possible to integrate R and Stata, complementing so we can run almost all kind of analyses using them. Therefore, who prefers the Stata’s interface but favor R capabilities and algorithms can keep doing in the same way by invoking one package through the other. How do I do that? It is possible thanks for a user written Stata bundle “rsource” by Roger Newson.
Initially I’ve seen how this written package works on PC-Windows, since that I’ve worked a little bit to get the same behavior under Mac OS — the help file of rsource does not provide much aid for Mac users, so I also needed help of the user’s list.
To have it working in my Apple computer I need both Stata and R, of course. To download the latest version of R package visit: http://cran-r.c3sl.ufpr.br/bin/macosx/.
After have installed R, let’s do the Stata task. I open Stata and type to install rsource package:
ssc install rsource
In very simple words, the rsource run R font files in lote mode on Stata, so, R is running behind the Stata. Therefore, it is possible to have a print of R output on Stata itself. Moreover, it is possible to create enter and exit files to be used whatever you want later.
Step (1): Let’s try it using an internal databank “swiss” made available with R. As it comes with R package, I just need to call it. Just write a small script to read and export this file through R. Let’s save it as “mytest.R”
Command (2), attaching it to memory
Export it to Stata format:
Call a simple OLS using these three variables.
model < – lm(Fertility~Agriculture+Catholic)
(2) Make a Stata do-file containing the following commands and save it as “test.do”. To make sure that the home directory is the same directory where I placed “mytest.R” script. In this case I’m using my default directory: Desktop.
/*Command (1) */
The following command defines where the application is and the output option. I must define the path where to find the R application. Also, I must define the output option. It is important to me define the path according to the exact place I installed R. You can have more information typing in your Stata “help rsource”
/*Command (2) */
global Rterm_path “/usr/bin/R”
global Rterm_options `”–vanilla”‘
The next command run the script created somewhere.
/*Command (3) */
rsource using mytest.R
/*Command (4) */
regress Fertility Agriculture Catholic