# R for Impact Evaluation: R and Stata Side-by-side

This tutorial follows the Handbook on Impact Evaluation: Quantitative Methods and Practices, chapter 11. The data files we will use can be downloaded from here. The first part of Chapter 11 is covered in Impact Evaluation on a Budget: World Bank Data and R.

# Notes on Commands

• Stata commands are typed in lowercase, R commands are functions (e.g., `ls()`)
• In Stata, you can type abbreviated forms of functions and variables provided there is no ambiguity. In R, you must use the full function or variable name.
• In Stata, use the Page-Up and Page-Down keys to cycle through previously entered commands. In R, use the Up and Down Arrow keys to do this.

# Working with Data Files: Looking at the Content

## Open the Dataset

Here I assume you saved the file (from the previous tutorial) to the `~/eval/data` folder. Stata:

`use ~/eval/data/hh_98.dta`

R:

`library(foreign) hh_98 = read.dta('~/eval/data/hh_98.dta')`

(If you don’t already have the `foreign` library installed, you can use the command `install.packages("foreign")`.)

## Listing the Variables

Stata:

`describe`

R:

`ls(hh_98) dim(hh_98) sapply(hh_98,class)`

The function `ls(x)` displays the names of the objects within `x`. If you just enter `ls()`, R will show you the names of the objects open in your current environment (remember you can use `?ls` to see the R documentation for the `ls()` function). The function `dim(x)` returns the dimensions of object `x`. When measuring a data.frame, like `hh_98`, `dim()` returns the number of rows first followed by the number of columns. The function `sapply(x,FUN)` returns a simplified result from applying the function `FUN` to each object in `x`. The function `class(x)` returns the class of object `x`.

## Wildcards and Abbreviations

Stata:

`describe exp∗`

R:

`summary(hh_98[grep("exp", colnames(hh_98))])`

In R, it is possible to do things even if we don’t know the exact name of the object we want to analyze. Starting from the innermost function and working our way out, `colnames(hh_98)` returns a vector where each element is the name of a column of `hh_98`. `grep("exp", x)` returns the indices of the elements that contain “exp” (you can also use regexp here) within `x`. Placing the resulting vector of indices into `hh_98[]` returns the matching columns. Finally, `summary()` returns the following summary of the returned columns:

` expfd expnfd exptot Min. : 945.3 Min. : 89.55 Min. : 1193 1st Qu.: 2602.1 1st Qu.: 514.37 1st Qu.: 3254 Median : 3373.7 Median : 865.31 Median : 4432 Mean : 3660.2 Mean : 1813.08 Mean : 5473 3rd Qu.: 4232.5 3rd Qu.: 1710.24 3rd Qu.: 6039 Max. :15270.7 Max. :43411.15 Max. :47981`

## Listing Data

List the first three entries in hh_98: Stata:

`list in 1/3`

R:

`hh_98[1:3,]`

In R, you can access records in a data.frame using matrix notation. The colon (`:`) separates the beginning and ending of a sequence. By leaving the portion following the comma blank, we tell R to show all columns. List household size and head’s education for households headed by a female who is younger than 45: Stata:

`list famsize educhead if (sexhead==0 &amp; agehead&lt;45)`

R:

`subset(hh_98,sexhead==0 &amp; agehead&lt;45,c(famsize,educhead))`

The `subset()` function is another method of selecting elements. Here’s the matrix form of the same subset: R:

`hh_98[hh_98\$sexhead==0 &amp; hh_98\$agehead&lt;45,c("famsize","educhead")]`

Browse or Edit the data: Stata:

`browse edit`

R:

`View(hh_98) edit(hh_98)`

## Summarizing Data

Display summary statistics for a few variables: Stata:

`sum famsize educhead sum famsize educhead, d`

R:

`summary(hh_98[,c("famsize","educhead")]) library(psych) describe(hh_98[,c("famsize","educhead")])`

(If you don’t already have the `foreign` library installed, you can use the command `install.packages("foreign")`.) Using survey weights: Stata:

`sum famsize educhead [aw=weight]`

R:

`library(survey) design &lt;- svydesign(id=~nh,weights=~weight,data=hh_98) svymean(~famsize + educhead,design)`

(If you don’t already have the `survey` library installed, you can use the command `install.packages("survey")`.) Summarize by groups: Stata:

`sort dfmfd by dfmfd: sum famsize educhead [aw=weight] tabstat famsize educhead, statistics(mean sd) by(dfmfd)`

R:

`library(survey) svyby(~famsize + educhead, ~dfmfd, design, svymean)`

(you only need to call `library(survey)` once per session).

## Frequency Distributions (Tabulations)

Stata:

`tab dfmfd `

R:

`table(hh_98\$dfmfd)`

In R, the `table()` function presents a table similar to the tabulate function in Stata, but only shows the counts grouped by factor. To see both the counts and percentages, as in the Stata program, we can divide by the total count (i.e., the `length()`). I group the counts and percentages using a `list()` so they are displayed together. R:

`list(count=table(hh_98\$dfmfd),percent=table(hh_98\$dfmfd)/length(hh_98\$dfmfd))`

Frequency tables over subsets and for multiple variables: Stata:

`tab sexhead if dfmfd==1 tab educhead sexhead`

R:

`table(hh_98[hh_98\$dfmfd==1,]\$sexhead) table(hh_98\$educhead, hh_98\$sexhead)`

Column and row percentages: Stata:

`tab dfmfd sexhead, col row`

R:

`mytable &lt;- table(hh_98\$dfmfd, hh_98\$sexhead) list(counts = mytable, percent.row = prop.table(mytable,1), percent.col = prop.table(mytable,2), count.row = margin.table(mytable,1), count.col = margin.table(mytable,2))`

## Distributions of Table Statistics

Stata:

`table dfmfd, c(mean famsize mean educhead)`

R:

`by(hh_98[c("famsize","educhead")], hh_98\$dfmfd, colMeans)`

Breakdown by two factors: Stata:

`table dfmfd sexhead, c(mean famsize mean educhead)`

R:

`by(hh_98[c("famsize","educhead")], hh_98[c("dfmfd","sexhead")], colMeans)`

## Missing Values

In Stata, missing values are represented by “`.`” In R, missing values are represented by “`NA`

## Counting Observations

Stata:

`count count if agehead&gt;50`

R:

`dim(hh_98)[1] dim(hh_98[hh_98\$agehead&gt;50,])[1]`

-or-

`length(hh_98[,1]) length(hh_98[hh_98\$agehead&gt;50,1])`

## Using Weights

For information on using weights in R, take a look at the homepage for the `survey` package: http://r-survey.r-forge.r-project.org/survey/

# Other Resources

The following websites are useful for searching for R:

Remember to use `?` to look up functions and `?? to search for help within R (e.g., "?by").`

``` Share this:FacebookTwitterRedditMorePrintLinkedInTumblrPinterestPocket Related ```
``` November 26, 2013 Jonathan Page Tutorials R, Stata ```
``` Economistry Proudly powered by WordPress ```
``` .skip-link.screen-reader-text { border: 0; clip: rect(1px,1px,1px,1px); clip-path: inset(50%); height: 1px; margin: -1px; overflow: hidden; padding: 0; position: absolute !important; width: 1px; word-wrap: normal !important; } .skip-link.screen-reader-text:focus { background-color: #eee; clip: auto !important; clip-path: none; color: #444; display: block; font-size: 1em; height: auto; left: 5px; line-height: normal; padding: 15px 23px 14px; text-decoration: none; top: 5px; width: auto; z-index: 100000; } ( function() { var skipLinkTarget = document.querySelector( 'main' ), sibling, skipLinkTargetID, skipLink; // Early exit if a skip-link target can't be located. if ( ! skipLinkTarget ) { return; } // Get the site wrapper. // The skip-link will be injected in the beginning of it. sibling = document.querySelector( '.wp-site-blocks' ); // Early exit if the root element was not found. if ( ! sibling ) { return; } // Get the skip-link target's ID, and generate one if it doesn't exist. skipLinkTargetID = skipLinkTarget.id; if ( ! skipLinkTargetID ) { skipLinkTargetID = 'wp--skip-link--target'; skipLinkTarget.id = skipLinkTargetID; } // Create the skip link. skipLink = document.createElement( 'a' ); skipLink.classList.add( 'skip-link', 'screen-reader-text' ); skipLink.href = '#' + skipLinkTargetID; skipLink.innerHTML = 'Skip to content'; // Inject the skip link. sibling.parentElement.insertBefore( skipLink, sibling ); }() ); window.WPCOM_sharing_counts = {"https:\/\/economistry.com\/2013\/11\/r-impact-evaluation-r-stata-side-side\/":753}; (function(){ var corecss = document.createElement('link'); var themecss = document.createElement('link'); var corecssurl = "https://economistry.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/styles/shCore.css?ver=3.0.9b"; if ( corecss.setAttribute ) { corecss.setAttribute( "rel", "stylesheet" ); corecss.setAttribute( "type", "text/css" ); corecss.setAttribute( "href", corecssurl ); } else { corecss.rel = "stylesheet"; corecss.href = corecssurl; } document.head.appendChild( corecss ); var themecssurl = "https://economistry.com/wp-content/plugins/syntaxhighlighter/syntaxhighlighter3/styles/shThemeDefault.css?ver=3.0.9b"; if ( themecss.setAttribute ) { themecss.setAttribute( "rel", "stylesheet" ); themecss.setAttribute( "type", "text/css" ); themecss.setAttribute( "href", themecssurl ); } else { themecss.rel = "stylesheet"; themecss.href = themecssurl; } document.head.appendChild( themecss ); })(); SyntaxHighlighter.config.strings.expandSource = '+ expand source'; SyntaxHighlighter.config.strings.help = '?'; SyntaxHighlighter.config.strings.alert = 'SyntaxHighlighter\n\n'; SyntaxHighlighter.config.strings.noBrush = 'Can\'t find brush for: '; SyntaxHighlighter.config.strings.brushNotHtmlScript = 'Brush wasn\'t configured for html-script option: '; SyntaxHighlighter.defaults['pad-line-numbers'] = false; SyntaxHighlighter.defaults['toolbar'] = false; SyntaxHighlighter.all(); // Infinite scroll support if ( typeof( jQuery ) !== 'undefined' ) { jQuery( function( \$ ) { \$( document.body ).on( 'post-load', function() { SyntaxHighlighter.highlight(); } ); } ); } var countVars = {"disqusShortname":"economistry"}; var embedVars = {"disqusConfig":{"integration":"wordpress 3.0.22"},"disqusIdentifier":"753 http:\/\/economistry.com\/?p=753","disqusShortname":"economistry","disqusTitle":"R for Impact Evaluation: R and Stata Side-by-side","disqusUrl":"https:\/\/economistry.com\/2013\/11\/r-impact-evaluation-r-stata-side-side\/","postId":"753"}; var WPGroHo = {"my_hash":""}; var sharing_js_options = {"lang":"en","counts":"1","is_stats_active":"1"}; var windowOpen; ( function () { function matches( el, sel ) { return !! ( el.matches && el.matches( sel ) || el.msMatchesSelector && el.msMatchesSelector( sel ) ); } document.body.addEventListener( 'click', function ( event ) { if ( ! event.target ) { return; } var el; if ( matches( event.target, 'a.share-facebook' ) ) { el = event.target; } else if ( event.target.parentNode && matches( event.target.parentNode, 'a.share-facebook' ) ) { el = event.target.parentNode; } if ( el ) { event.preventDefault(); // If there's another sharing window open, close it. if ( typeof windowOpen !== 'undefined' ) { windowOpen.close(); } windowOpen = window.open( el.getAttribute( 'href' ), 'wpcomfacebook', 'menubar=1,resizable=1,width=600,height=400' ); return false; } } ); } )(); var windowOpen; ( function () { function matches( el, sel ) { return !! ( el.matches && el.matches( sel ) || el.msMatchesSelector && el.msMatchesSelector( sel ) ); } document.body.addEventListener( 'click', function ( event ) { if ( ! event.target ) { return; } var el; if ( matches( event.target, 'a.share-twitter' ) ) { el = event.target; } else if ( event.target.parentNode && matches( event.target.parentNode, 'a.share-twitter' ) ) { el = event.target.parentNode; } if ( el ) { event.preventDefault(); // If there's another sharing window open, close it. if ( typeof windowOpen !== 'undefined' ) { windowOpen.close(); } windowOpen = window.open( el.getAttribute( 'href' ), 'wpcomtwitter', 'menubar=1,resizable=1,width=600,height=350' ); return false; } } ); } )(); var windowOpen; ( function () { function matches( el, sel ) { return !! ( el.matches && el.matches( sel ) || el.msMatchesSelector && el.msMatchesSelector( sel ) ); } document.body.addEventListener( 'click', function ( event ) { if ( ! event.target ) { return; } var el; if ( matches( event.target, 'a.share-linkedin' ) ) { el = event.target; } else if ( event.target.parentNode && matches( event.target.parentNode, 'a.share-linkedin' ) ) { el = event.target.parentNode; } if ( el ) { event.preventDefault(); // If there's another sharing window open, close it. if ( typeof windowOpen !== 'undefined' ) { windowOpen.close(); } windowOpen = window.open( el.getAttribute( 'href' ), 'wpcomlinkedin', 'menubar=1,resizable=1,width=580,height=450' ); return false; } } ); } )(); var windowOpen; ( function () { function matches( el, sel ) { return !! ( el.matches && el.matches( sel ) || el.msMatchesSelector && el.msMatchesSelector( sel ) ); } document.body.addEventListener( 'click', function ( event ) { if ( ! event.target ) { return; } var el; if ( matches( event.target, 'a.share-tumblr' ) ) { el = event.target; } else if ( event.target.parentNode && matches( event.target.parentNode, 'a.share-tumblr' ) ) { el = event.target.parentNode; } if ( el ) { event.preventDefault(); // If there's another sharing window open, close it. if ( typeof windowOpen !== 'undefined' ) { windowOpen.close(); } windowOpen = window.open( el.getAttribute( 'href' ), 'wpcomtumblr', 'menubar=1,resizable=1,width=450,height=450' ); return false; } } ); } )(); var windowOpen; ( function () { function matches( el, sel ) { return !! ( el.matches && el.matches( sel ) || el.msMatchesSelector && el.msMatchesSelector( sel ) ); } document.body.addEventListener( 'click', function ( event ) { if ( ! event.target ) { return; } var el; if ( matches( event.target, 'a.share-pocket' ) ) { el = event.target; } else if ( event.target.parentNode && matches( event.target.parentNode, 'a.share-pocket' ) ) { el = event.target.parentNode; } if ( el ) { event.preventDefault(); // If there's another sharing window open, close it. if ( typeof windowOpen !== 'undefined' ) { windowOpen.close(); } windowOpen = window.open( el.getAttribute( 'href' ), 'wpcompocket', 'menubar=1,resizable=1,width=450,height=450' ); return false; } } ); } )(); _stq = window._stq || []; _stq.push([ 'view', {v:'ext',j:'1:10.6',blog:'41518866',post:'753',tz:'-10',srv:'economistry.com'} ]); _stq.push([ 'clickTrackerInit', '41518866', '753' ]); ```