Thanks to Kit Baum a new update of -markstat- is available from SSC. New this time is support for R code blocks and inline code. Here is a simple example:
As you can see, we handle R code the same way as Stata and Mata, using code fences but with an r instead of an s or m. You can copy and paste this script, or download it to your working directory using the command
To run this script in Stata you use the command:
All five output formats are supported. You can see the HTML output here. For this to work you need to have R installed, and you need to use -whereis- from SSC to register the location of R in your computer. Instructions and examples here.
Following a suggestion of Doug Hemken, -markstat- now removes intermediate files, but has an option to specify which ones to keep. It also detects code fences and switches to strict mode automatically.
As usual, much more at the website http://data.princeton.edu/stata/markdown, including a more extensive example using Bootstrap tabs to switch between Stata and R versions of a Cox regression analysis.
Code:
% Quantiles in Stata and R Stata and R compute percentiles differently. Let us load the `auto` dataset and compute the 75th percentile of `price` using Stata's `centile` ```s sysuse auto, clear centile price, centile(75) save auto, replace ``` We find that the 75-th percentile is `s r(c_1)`. Now let us do the same with R. We'll use the `haven` library to read a Stata file ```r library(haven) auto <- read_dta("auto.dta") q <- quantile(auto$price, 0.75); q ``` According to R, the 75-th percentile is `r round(q, 1)`. Turns out R has 9 types of quantiles, the default is 7. To get the same result as `centile` specify type 6, which gives `r quantile(auto$price, 0.75, type=6)`. The Stata commands `summarize, detail`, `xtile`, `pctile` and `_pctile` use yet another method, equivalent to R's type 2. These give the third quartile as `r quantile(auto$price, 0.75, type=2)`. The last three commands have an `altdef` option that gives the same answer as `centile`. For a discussion of these methods see Hyndman, R. J. and Fan, Y. (1996) Sample quantiles in statistical packages, *American Statistician* 50:361-365.
Code:
copy http://data.princeton.edu/stata/markdown/quantiles.stmd quantiles.stmd
Code:
markstat using quantiles
Following a suggestion of Doug Hemken, -markstat- now removes intermediate files, but has an option to specify which ones to keep. It also detects code fences and switches to strict mode automatically.
As usual, much more at the website http://data.princeton.edu/stata/markdown, including a more extensive example using Bootstrap tabs to switch between Stata and R versions of a Cox regression analysis.
Comment