Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Programs for using Stata with R

    I've recently started playing around with R a bit. I wonder what user-written routines in Stata work with R? Just typing -findit R- yields about 10 million hits, alas.

    One routine I've discovered is Roger Newson's rsource. It is quite handy because you can run an R script and all the output appears in Stata.

    A potential use: R can read and write a bunch of different data formats. For example, it could read in an SPSS file and create a stata file, and vice versa. People are always asking about things like this, so it would be handy if, say, there was a Stata Rimport or Rexport routine. Since R is free, it would be like a poor person's stat/transfer program. I might be tempted to write such a routine, but I don't want to bother if it already exists.

    Personally, I am not that crazy about R! But it does have some features that are not in Stata. Anything that made it easy to use those features (especially for people like me who only know a little R) would be nice.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

  • #2
    Excellent suggestions, Richard.

    Nowadays, if I have to use R, I tend to perform all data management beforehand in Stata.

    Then, either I save as (.csv or .xls) before importing in R, or I go directly to RStudio and import from Stata.

    I find this last option quite handy (and free, for single-users). This would tackle just one aspect of your topic, related to directly importing .dta files into R.

    With regards to finding R routines in Stata, maybe a command such as - findit R software - could be proposed to avoid the massive an uninformative output when just typing R.

    To end, although I haven't tested it out, I gather the R package rio does some sort of stat/transfer tasks, for free.
    Best regards,

    Marcos

    Comment


    • #3
      Thanks Marcos. As luck would have it, Jacob Long from Ohio State just told me about this site:

      https://gallery.shinyapps.io/rioweb/

      The description says "rio provides a unified framework for the many data input/output routines available in the R community. rioweb exposes it on a web app, allowing you to convert between various file formats with a simple upload (original) / download (converted) interface; no need to install any software."

      I just tried converting a few spss files and it seemed to work OK. The .dta file created was in Stata 7 format. I don't know if it could handle every format thrown at it but it may be worth checking if you don't have another freebie program you could use.
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      Stata Version: 17.0 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        Richard, have a look at usespss.
        Code:
        net from http://radyakin.org/transfer/usespss/beta

        Comment


        • #5
          Yes, I have had very good luck with usespss, But rioweb can handle more formats, and the rio program in R can handle many more. At least it claims it can.

          https://cloud.r-project.org/web/pack...ettes/rio.html
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          Stata Version: 17.0 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            I just tried findit R software, as Marcos suggested. I then did a CTL-F search for R (matching on case). Among the hits, I found this one:

            Code:
            rcall from http://fmwww.bc.edu/RePEc/bocode/r
                'RCALL': module to provide seamless R in Stata / Rcall runs R commands in
                Stata and allow the two software / automatically communicate variables,
                matrices, and data. more / information read the help file or visit diagram
                homepage. Rcall / returns rclass scalars, macros, and matrices to Stata,
            Here are a few more that looked interesting, although not necessarily right on target.

            Code:
            runmixregls from http://fmwww.bc.edu/RePEc/bocode/r
                'RUNMIXREGLS': module to run the MIXREGLS software from within Stata /
                This module runs the MIXREGLS mixed-effects location scale / software
                (Hedeker and Nordgren 2013) from within Stata. The / mixed-effects
                location scale model extends the standard two-level / random-intercept
            
            runmlwin from http://fmwww.bc.edu/RePEc/bocode/r
                'RUNMLWIN': Run the MLwiN multilevel modelling software from within Stata
                / / This module fits multilevel models in MLwiN from within Stata. / There
                are three steps to using runmlwin: (1) The researcher specifies / the
                desired model using the runmlwin command syntax; (2) The model is / sent
            
            savespss from http://fmwww.bc.edu/RePEc/bocode/s
                'SAVESPSS': module to save data to SPSS (.sav) system file / -savespss-
                allows users of Stata 10-13 to save datasets in SPSS / system binary
                datafile format and supports a wide range of data / decoration elements:
                variable and value labels, data / characteristics, extended missing
            --
            Bruce Weaver
            Email: [email protected]
            Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
            Version: Stata/MP 18.0 (Windows)

            Comment


            • #7
              Thank you for the information, Bruce. Indeed, I thought to be suggesting something new....I might have been under a ‘Stataish’ frame of mind. 😊
              Best regards,

              Marcos

              Comment


              • #8
                I have installed Rcall.ado (from SSC). I have also installed R version 3.4.2 as shown in the screenshot below, and I use it frequently. Unfortunately, my Stata/IC 15.0 cannot find the R path on my Windows 10 (64-bit) system.

                Click image for larger version

Name:	rpath.png
Views:	1
Size:	31.7 KB
ID:	1413336



                I've tried the following to manually setup the R path, based on the subcommand described in the help file for Rcall , but the path to R is still not found.
                Code:
                *Without final backslash:
                R: setpath "Q:/R/R-3.4.2/bin/x64"
                R was not found on your system. Setup R path manually
                r(198);
                
                * Adding a final backslash:
                R: setpath "Q:/R/R-3.4.2/bin/x64/"
                R was not found on your system. Setup R path manually
                r(198);
                I also had no success when I tried omitting the quotation marks from the commands shown above.

                The help file for Rcall points to a blog at http://www.haghish.com/packages/Rcal...ath_subcommand, but I found nothing there that helps with the setpath subcommand.

                R is clearly installed and registered on my Windows 10 system, because I can open R data files into R by double-clicking on them and because R is recognized in my installation of R Studio.

                I would be grateful if anyone will suggest what I'm doing wrong or how I can get Rcall.ado to recognize my R path from within Stata/IC 15.0.

                Red Owl
                Stata/IC 15.0
                Windows 10 (64-bit)

                Comment


                • #9
                  The bottom of the help page for rcall (help rcall) gives this link: And that page includes this example:

                  Code:
                  Rcall setpath "/usr/bin/R"
                  So I suggest you try:

                  Code:
                  Rcall setpath "Q:/R/R-3.4.2/bin/x64"
                  --
                  Bruce Weaver
                  Email: [email protected]
                  Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
                  Version: Stata/MP 18.0 (Windows)

                  Comment


                  • #10
                    Thanks very much for the suggestion, Bruce. I'm anxious to try any other solutions.

                    I had tried that too, but this is what happened.
                    Code:
                    * With forward slashes:
                    Rcall setpath "Q:/R/R-3.4.2/bin/x64"
                    R was not found on your system. Setup R path manually
                    r(198);
                    
                    * With backslashes:
                    Rcall setpath "Q:\R\R-3.4.2\bin\x64"
                    R was not found on your system. Setup R path manually
                    r(198);
                    I just reinstalled R 3.4.2 on my system to make sure there was no error in its being registered under Windows 10, but that didn't help.

                    I'm wondering if Rcall somehow wants R to be installed only in R's default directory on the C:\ drive in Windows.

                    Red Owl
                    Stata/IC 15.0
                    Windows 10 (64-bit)


                    Comment


                    • #11
                      I've had similar issues getting -Rcall- or -Rsource- (SSC) set up after installing Stata 15 (Mac OS Sierra) but haven't had time/patience to delve into it yet, so any updates greatly appreciated.
                      Last edited by Andrew Lover; 04 Oct 2017, 20:26.
                      __________________________________________________ __
                      Assistant Professor, Department of Biostatistics and Epidemiology
                      School of Public Health and Health Sciences
                      University of Massachusetts- Amherst

                      Comment


                      • #12
                        First off, Thanks to Red Owl for helping me realize that I was using the hopelessly antiquated 3.4.1 when 3.4.2 was available.

                        This worked fine for me under windows 7:

                        Code:
                        Rcall setpath "C:\Program Files\R\R-3.4.2\bin\x64\"
                        R: a <- 100
                        display r(a)
                        Are you sure you aren't missing a subdirectory? Notice that in my installation R is a subdir of Program Files.

                        Within Stata, if you type

                        cd "C:\Program Files\R\R-3.4.2\bin\x64"

                        substituting what you think the directory for R is, do you get to the right place?

                        I'll admit, your screenshot makes it look like you are doing it right. But try to confirm you have the right path with commands like cd.

                        If all else fails, take a look at Roger Newson's rsource. Maybe it would work.
                        -------------------------------------------
                        Richard Williams, Notre Dame Dept of Sociology
                        Stata Version: 17.0 MP (2 processor)

                        EMAIL: [email protected]
                        WWW: https://www3.nd.edu/~rwilliam

                        Comment


                        • #13
                          I copied my R files to a different drive and it worked:

                          Rcall setpath "X:\R-3.4.2\bin\x64"
                          R: a <- 100
                          display r(a)

                          Maybe you could cd to the directory R is in and then give your rcall command.
                          -------------------------------------------
                          Richard Williams, Notre Dame Dept of Sociology
                          Stata Version: 17.0 MP (2 processor)

                          EMAIL: [email protected]
                          WWW: https://www3.nd.edu/~rwilliam

                          Comment


                          • #14
                            Richard,

                            Thanks so much for the time and effort you spent looking into this problem for me. Your testing was very helpful.

                            I had wondered whether the dash in my R path (Q:\R\R-3.4.2\bin\x64) was the problem, but your test shows that is not a concern. I had also wondered whether the problem was caused by R being installed on my Q:\ drive instead of the the default C:\ drive programs folder, but your test with your X:\ drive shows that is also not a concern.

                            I have uninstalled Rcall and reinstalled it, and I have also reinstalled R 3.4.2 just to eliminate the possibility of missing or corrupted files. (I even ran -adoupdate, replace- to make sure I have the most recent version of Rcall.)

                            I then tried several other approaches and tests for using Rcall in Stata/IC 15.0 in my Windows 10 (64-bit) system based on your suggestions and variations to them.
                            Code:
                            * Set current working directory in Stata to my R path.
                            . cd q:\R\R-3.4.2\bin\x64
                            q:\R\R-3.4.2\bin\x64
                            
                            * Confirm the current working directory.
                            . pwd
                            q:\R\R-3.4.2\bin\x64
                            
                            * Confirm that the R executables are in my current working directory.
                            . dir
                              <dir>  10/04/17 17:43  .                 
                              <dir>  10/04/17 17:43  ..                
                              18.5k   9/28/17 11:36  open.exe          
                              29.4M   9/28/17 11:36  R.dll             
                             102.0k   9/28/17 11:36  R.exe             
                             308.5k   9/28/17 11:36  Rblas.dll         
                             102.0k   9/28/17 11:36  Rcmd.exe          
                              86.5k   9/28/17 11:37  Rfe.exe           
                             314.0k   9/28/17 11:35  Rgraphapp.dll     
                              86.5k   9/28/17 11:36  Rgui.exe          
                              65.2k   9/28/17 11:35  Riconv.dll        
                            2673.5k   9/28/17 11:39  Rlapack.dll       
                              90.0k   9/28/17 11:36  Rscript.exe       
                              87.5k   9/28/17 11:36  RSetReg.exe       
                              86.5k   9/28/17 11:36  Rterm.exe         
                            
                            * Check whether Rcall is working and can find my R path.
                            . Rcall describe
                            R was not found on your system. Setup R path manually
                            r(198);
                            
                            * Try to setpath to my R path, which was confirmed above.
                            . Rcall setpath "Q:\R\R-3.42\bin\x64"
                            R was not found on your system. Setup R path manually
                            r(198);
                            
                            * Try to set path to Stata's current working directory, which is my R path as cd above.
                            . Rcall setpath c(pwd)
                            R was not found on your system. Setup R path manually
                            r(198);
                            I will write to the author of Rcall (E. F. Haghish at the University of Freiburg) to ask for advice, and I'll post any diagnostic information or solutions he provides.

                            I'll also take your advice and look into Roger Newson's rsource, which may satisfy my needs.

                            In the meantime, when I need to use an R routine on a project I'm managing in Stata, I'll just convert my Stata data files to R-readable data files and work directly in R or R Studio -- as I have been doing.

                            Thanks again for all your effort.

                            Red Owl
                            Stata/IC 15.0
                            Windows 10 (64-bit)

                            Comment


                            • #15
                              Andrew,

                              I tried Rcall with my Stata/IC 14.2 version as well, but I had the same problem I have been having with Rcall on Stata/IC 15.0 under Windows 10 (64-bit).

                              At least for Windows 10 or on my system, the problem does not seem to be limited to version 15 of Stata.

                              Thanks for letting me know that I'm not the only one having a problem getting Rcall to find the R path.

                              Red Owl
                              Stata/IC 15.0
                              Windows 10 (64-bit)

                              Comment

                              Working...
                              X