Research in economics and business studies relies heavily on specialised software for dinsata preparation, econometric analysis, simulation and visualisation. The statistical programming language R is used increasingly often for these tasks, and for good reason: it is free and Open Source. This makes it a natural choice from an Open Science perspective. This article is the first of a two-part series on R for researchers in economics and business studies. This first part outlines what R offers, highlights some key differences from Stata, a widely used but proprietary programme, and points to selected resources that can help with getting started. The second part looks at how to set up a reproducible workflow in R.
Three advantages of R
Open Code is a key building block of reproducible research: anyone who wants to check or build on a study needs access to the code behind it. Yet Open Code is only half the story. If the code can only be run with a proprietary programme, access still depends on a licence. This is why it also matters which programme the code depends on. R does not make research reproducible on its own, but compared with proprietary programmes such as Stata, SPSS or Excel, it has three advantages:
- Accessible to everyone: R and all its packages are free. Anyone can run code written in R, whether or not their institution holds a licence, including replicators, students and researchers in countries with limited research budgets. Access also continues after leaving university.
- Transparent: The source code of R and its packages is public. Anyone can check exactly how an estimator is computed instead of relying on the documentation, and errors can be spotted and corrected by the community.
- Interoperable: R reads and writes many formats, including Stata, SPSS and Excel files as well as open formats such as CSV, and can be combined with other languages such as Python. This makes code and data easier to reuse, one of the principles of FAIR code.
In addition, many recent econometric methods are available as R packages, often written by the developers of the methods themselves.
What changes when moving from Stata to R?
The syntax is different, but most concepts carry over. Four differences are worth knowing from the start:
- Several datasets side by side: Stata works with one active dataset; holding more requires frames (available since Stata 16). In R, any number of datasets (data frames) can be loaded in parallel and are addressed by name.
- Packages: As in Stata, additional functions come from packages. The difference: an R package is installed once with
install.packages()but must be loaded withlibrary()in every new R session, usually by listing these commands at the top of the script. Packages are distributed via CRAN (Comprehensive R Archive Network), the central online repository for R software and packages, whose Econometrics Task View gives an overview of packages relevant for empirical economic research. - Results as objects: In R, every regression result is saved under a name and can be reused, compared or exported later. In Stata, estimation results are overwritten by the next estimation unless they are saved with
estimates store. - Missing values: In Stata, a missing value counts as larger than any number, so if
income > 100000also selects observations with missing income. R marks missing values asNA(“not available”) and does not treat them as numbers. Calculations involving missing values therefore returnNA, unless you explicitly tell R to ignore them.
For the most common Stata commands, the website stata2r shows the R equivalent side by side.
Where can I learn R?
- Basics: The Introduction to R by the LMU Open Science Center and Data Carpentry's R for Social Scientists are free, self-paced courses; the latter works with survey data. A German-language R basics course is available on YouTube.
- Econometrics: “Introduction to Econometrics with R” accompanies Stock and Watson (see our interview with the authors). “Using R for Introductory Econometrics” by Florian Heiß accompanies Wooldridge. r-econometrics covers time series models such as VARs.
- Causal inference: “Causal Inference: The Mixtape” by Scott Cunningham (code in R and Stata) and “The Effect” https://theeffectbook.net/ by Nick Huntington-Klein (code in R, Stata and Python) are freely available online.
- Business research: The Open Access book “PLS-SEM Using R” by Hair, Ringle, Sarstedt and co-authors shows how to run partial least squares structural equation models in R.
- Finance: “Tidy Finance with R” shows how to work with financial data from sources such as WRDS, CRSP and Compustat in reproducible workflows.
- Learning strategy: “Ten simple rules for teaching yourself R” recommends, among other things, using CRAN's Task Views to find suitable packages and reading the code in replication materials of published papers to see how researchers in your field work.
Where can I find reusable packages and support?
Many established packages were developed by researchers in economics and business studies, for example fixest (Laurent Bergé), did (Brantly Callaway and Pedro Sant'Anna) and the forecasting packages fable and forecast from Monash University. When you use them, cite them like any other source. Questions can be asked in the Posit Community forum. rOpenSci peer-reviews packages and organises community events, and R-bloggers collects tutorials. The Big Book of R lists more than 400 books on R, most of them free.
Tip: Get into the habit of writing clear, well-commented code from the start. This helps not only others but also your future self, for example when you need to retrace your analysis months later during a revision. What to keep in mind is explained in the Open Economics Guide article on making code readable and documenting it.