Tuesday, April 1, 2014

Stata Fully Mapped into R

Hello all of you Stata loving statistical analysts out there!  I have great news.  I am finally nearly done with the package I have been working on which provides the mechanism for Stata users to seamlessly move from Stata to R though use of my new package "RStata"!

In this package I have taken 150 of the most commonly used commands in Stata and directly mapped their syntax into R.  Not only can they be called using identical syntax but they also return identical arguments to the active window.  In order to accomplish this task, the package has built in dependencies on many useful R packages such as plry, ggplot2, glm, etc.  So installation could take a while.

To see this new package in action, here is some sample code:

library("RStata")

sysuse auto
regress mpg weight c.weight#c.weight foreign


      Source |       SS       df       MS              Number of obs =      74
-------------+------------------------------           F(  3,    70) =   52.25
       Model |  1689.15372     3   563.05124           Prob > F      =  0.0000
    Residual |   754.30574    70  10.7757963           R-squared     =  0.6913
-------------+------------------------------           Adj R-squared =  0.6781
       Total |  2443.45946    73  33.4720474           Root MSE      =  3.2827

-----------------------------------------------------------------------------------
              mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
------------------+----------------------------------------------------------------
           weight |  -.0165729   .0039692    -4.18   0.000    -.0244892   -.0086567
c.weight#c.weight |   1.59e-06   6.25e-07     2.55   0.013     3.45e-07    2.84e-06
          foreign |    -2.2035   1.059246    -2.08   0.041      -4.3161   -.0909002
            _cons |   56.53884   6.197383     9.12   0.000     44.17855    68.89913
-----------------------------------------------------------------------------------


Please note, I only have a licensed version of Stata up to version 11 so newer commands are omitted from the package.

If you would like to beta test this package or contribute to mapping additional Stata commands, you can find it and installation instructions at:
github.com/EconometricsBySimulation/RStata

14 comments:

  1. You are the man.

    Your package has the very potential to kill the Stata Corp. They may just hire an assassin, who is currently on her way to your office.

    :-)

    Nice Work!!!!!!

    ReplyDelete
  2. I caught this off R-bloggers. I have a friend who is desperately trying to a Stata script into R. This is not funny and has wasted my time.

    ReplyDelete
    Replies
    1. I am sorry for any distress this post may have caused. Your response to it however is not wasted as there are many R/Stata users who are altogether very happy and eager to help you or your friend with any coding issues you may have. The best place to look for such help is StackOverflow. If you were to post a block of code in Stata and ask for someone to help you translate it into R, I am sure you would get the help that you are requesting.

      Cheers,
      Francis

      Delete
    2. I wouldn't make too many promises about what Stack Overflow might do. I am active there and I have to warn that expecting translation of chunks of code is unlikely to work. The best kind of post highlights that you have tried writing code but have run into a specific problem on which you want advice.

      Delete
  3. Interesting and ambitious. A later step might be to show the user the "R way" of accomplishing the same thing. Showing all of the underlying commands would probably be messy, but perhaps you don't have to show the formatting parts and can just show the main call to, e.g. glm. Also there's a small typo: 'plyr' instead of 'plry'.

    ReplyDelete
    Replies
    1. Thanks for that correction Scott :)

      Delete
    2. Oh now I see, haha you definitely got me! I was wondering how you worked around R's function call mechanism. I was thinking you used OOP and redefined a space to be an operator or something tricky. You did indeed do something tricky :). Thanks for a good laugh!

      Delete
  4. I have to admit you got me there. And for Anonymous, who probably isn't coming back to check these comments, we've all been up against deadlines. Rushing stats and programming is a bad idea. Don't do it!

    ReplyDelete
  5. Ohmigod--you totally got me. Nicely done.

    ReplyDelete
  6. Great Work! I always want to work in R but my head is still thinking in terms of Stata. Your package can really help in the transition! Thanks!

    ReplyDelete
  7. Hilarious. My favorite part is the combined logo with R subtly boxing out stata.

    ReplyDelete