Stata Blog: Applied Statistics Using Stata

Applied Statistics Using Stata®

Free Online Stata Tips & Tutorials. Data Management; Stata Graphs and Graphics; Data Analysis; Stata Programming; Advanced Statistics

Rcall : seemless R in Stata

Perhaps every Stata user has been in a “Stata vs R” discussion. They both are terrific statistical software and each has its own strengths and weaknesses. The attraction of R makes many users to give it a try or at least, run a specific analysis in R, while doing most of the data management in Stata. However, executing an analysis in a different programming language can harm the reproducibility of the data analysis, unless it is carried out completely in an automated process.

The Rcall package runs R within Stata and allows automated data communication between R and Stata. Stata macro, scalar, matrix, and data set can be easily transported to R and similarly, R objects are automatically imported from R to Stata and updated in real-time.

This level of integration between Stata and R, also allows Stata users to get benefit from other programming languages that can be executed interactively in R environment, such as C++ using Rcpp or JavaScript using V8 packages. However, this document is only meant to be a quick and dirty introduction to Rcall package.

Description

Rcall provides seemless interactive R in Stata. The package return rclass R objects (numeric, character, list, matrix, etc) automatically, allowing the Stata users to access whatever they are creating in R, within Stata simultaniously. Rcall also allows data communication from Stata to R by passing Stata data set, macro, scalar, and matrix to R, which provides a reciprocal interaction between Stata and R.

See also  Retina X Studios, LLC - Monitoring Software for Remote Internet Tracking of Computers and Smartphones - About the Company.

Installation

You can install the package from GitHub by executing the following command:

You also need to make sure that R statistical software is installed on your machine. Rcall package includes the default paths of R on Microsoft Windows, Mac, and Linux. But if you have installed R in a different location, then you can define the path to executable R using the setpath command as shown below. the setpath command defines the path to R permanently.

Now you should be good to go with the Rcall package. Let’s examine the package by printing something in R.

Finally, for passing Stata data sets to R automatically, you need to install the foreign R package, which you can install it within Stata:

Syntax

In general, the syntax of the Rcall package can be summarized as follows:

The Rcall command can also be abbreviated as R and the colon sign “:” is optional. For for the rest of the examples, I call R by typing R: instead of Rcall:.

The Rcall package runs R interactively. That is, when you define an object in R, it remains in the memory of R and accissable with the next command. For example:

the vanilla subcommand runs R non-interactively, which can be imagined as opening R, executing a script, and closing R without saving it. This subcommand is only useful if you want to source() a script file in R.

Data communication

The biggest advantage of Rcall package is that it allows data communication between Stata and R. Variables that are defined in R, can be accessed in Stata automatically, within the returned rclass scalars, macros, and matrices. For example, I create a numeric variable, a numeric vector, a character variable, a matrix, and a list in R, and retreive the results in Stata simultaniously as shown below

See also  Retina X Studios, LLC - Monitoring Software for Remote Internet Tracking of Computers and Smartphones - Products

note that the vector is returned as a “string” macro in Stata. But you can destring it easily. Stata does not return rclass numeric lists (to my knowledge). Nevertheless, if you want to access an R vector in Stata, now you can…

Excitingly, you can also create a Matrix in R and access it simultaniously in Stata, anytime you make a change to it. For example:

And now view the matrix in Stata! That simple!

Accessing Lists is more tricky, but yet, automatically possible. Stata returns each element of a list as a separate rclass scalar or macro. The biggest difference is that rclass cannot include $ sign in the name. Rcall automatically renames the $ sign to underscore.

rclasses

As noted earlier, without using the vanilla subcommand, R is executed interactively within Stata and “most” of the R objects are accessible in Stata. You can see the list of available objects in Stata by typing return list which shows the returned matrices, macros, and scalars:

Passing data from Stata to R

So far I documented how R variables can be accessed within Stata. This package is under constant development and I will be able to automatically import more R classes to Stata (currently it only imports numeric, character, matrix, and list).

Now I show how to pass data from Stata to R. In general, passing local and global macro is the simplest:

But when it comes to scalar, matrix, and data sets, it becomes more complicated. Similar to passing scalar, matrix, or data sets to Mata, the Rcall defines 3 functions for passing these classes to R.

See also  Retina X Studios, LLC - Computer Monitoring Software - Terms of Use

Below, I demonstrate how to use thise functions.

as shown in the example below, you can pass your Stata matrices to R, do any manipulation, and automatically get the resulting matrix back in Stata

Finally, you can also pass Stata data set to R. If the data set is on your machine, you should provide the relative or absolute path to the file name. For example, the absolute path to the auto.dta on my machine is:

if you leave the st.data() function empty, it passes the loaded data set from Stata to R. for example:

You can also load a dataframe from R to Stata. This will clear any data you have loaded in Stata automatically, so becareful with that! Nevertheless, the function can be very useful to quickly pass data frame from R to Stata. This function will export a Stata version 11 data set using the foreign R package and load it in Stata:

The mydata data frame is already loaded in Stata! You can just follow your analysis in Stata now!

It’s your turn to test the package, Fork It on GitHub and contribute to it. Connecting R to Stata in such a level of integrity, can really ease the process of running a computation in R and passing the results or variables back to Stata.

Scroll to Top