Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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 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.
Basic exemple:
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”
data(swiss)
Command (2), attaching it to memory
attach(swiss)
Export it to Stata format:
library(foreign)
write.dta(swiss, “/Users/DM/Desktop/swiss.dta”)
Call a simple OLS using these three variables.
model < – lm(Fertility~Agriculture+Catholic)
summary(model)
You must have something like the following pic:
(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) */
cd “/Users/DM/Desktop”
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) */
use “swiss.dta”
regress Fertility Agriculture Catholic
Again, you must have something like the this:
(3) Finally, let’s try our work. Run the do-file by typing “do teste.do” on Stata.
(4) Compare the results: First, R output followed by the default Stata output. Fortunately, the results looks like all the same.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.