Calling Stata Functions from R
Asked Answered
J

3

15

Is it possible to call Stata functions from R?

Jacquelinejacquelyn answered 12/11, 2010 at 16:45 Comment(0)
J
12

There's now an RStata package on CRAN that bridges R and Stata.

Jacquelinejacquelyn answered 5/3, 2016 at 14:44 Comment(1)
This blog post provides a good introduction to RStata fsolt.org/blog/2018/08/15/switch-to-r.htmlMetrology
B
13

Not directly, i.e. there is no package I am aware of that implements a bridge.

You can always call external programs using system() but that is neither elegant nor efficient. That said, you could prepare data in R, write it out, call Stata and then read the results in; see help(system).

Bluebeard answered 12/11, 2010 at 17:32 Comment(3)
This is probably the best solution. Stata has a set of command line tools that allow you to run a .do/.ado file from the shell (windows: stata.com/support/faqs/win/batch.html or unix/linux: fmwww.bc.edu/Gstat/docs/stataunixlinux.html#batch)Sleeper
There is now an RStata package on GitHub that attempts to create a bridge between R and Stata.Maternal
@Maternal RStata deserves to be it's own answer.Jacquelinejacquelyn
J
12

There's now an RStata package on CRAN that bridges R and Stata.

Jacquelinejacquelyn answered 5/3, 2016 at 14:44 Comment(1)
This blog post provides a good introduction to RStata fsolt.org/blog/2018/08/15/switch-to-r.htmlMetrology
L
3

The real problem is that Stata doesn't have an interactive interpreter you can pass arguments to.

Dirk is right; you can just go ahead and write the data to a common format (if size is large and speed is an issue, fixed width is safe), but you can also just use .dta throughout the process, using read.dta in R and natively reading in Stata.

Also, in R you can call to the system() you can pass a do file or a string containing a bunch of Stata commands.

So, generally, trying to use Stata for this or that task may or may not be worth it, especially if an R equivalent is close by.

Lanctot answered 21/11, 2010 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.