Running R through Stata on Mac OS

January 10, 2012
By

(This article was first published on Daniel MarcelinoDaniel Marcelino » R, and kindly contributed to R-bloggers)

imgres imgres-1
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.
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:
RStata2

(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:
RStata1
(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.
RStata3
RStata4

To leave a comment for the author, please follow the link and comment on their blog: Daniel MarcelinoDaniel Marcelino » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)