If you like automating your Stata output, you have probably struggled with how to format P-values so they display in a format that is common in journals, namely: If P>0.05 and not close to 0.05, P has an equals sign and you round at the hundredth place E.g., P=0.2777 becomes P=0.28 If P is close …
Category Archives: Stata code
Adding overlaying text “boxes”/markup to Stata figures/graphs
What’s up with adding text to figures in Stata? It’s handy to add text to your plots to orient your readers to specific observations in your figures. You might opt to highlight a datapoint, add percentages to bars, or say what part of a figure range is good vs. bad. Adding text to Stata is …
Continue reading “Adding overlaying text “boxes”/markup to Stata figures/graphs”
Making a Bland-Altman plot with printed mean and SD in Stata
The below code outputs this Bland-Altman plot figure, which prints the mean and SD and puts a solid line for mean difference and red dotted lines for mean difference +/- 1.96*SD. (Note: the original BA paper used +/- 2*SD, but it’s reasonable to use +/-1.96*SD given it’s commonality in estimating the bounds of 95% confidence …
Continue reading “Making a Bland-Altman plot with printed mean and SD in Stata”
Making a scatterplot with R squared and percent coefficient of variation in Stata
I recently had to make some scatterplots for Figure 3 of this paper. I decided to clean up the code in case it might be helpful to others. The below code outputs a scatterplot with R-squared and %CV. I grabbed the %CV-from-a-regression code from Mehmet Mehmetoglu’s CV program. (Type –ssc install cv– to grab that …
Continue reading “Making a scatterplot with R squared and percent coefficient of variation in Stata”
Generate random data, make scatterplot with fitted line, and merge multiple figures in Stata
I recently made these two figures with a Stata do file that (A) generates some fake data, (B) plots the data overall with a fitted line, (C) plots each participant individually with fitted line, and (D) merges these four individual plots into one overall plot. One tricky piece of this was to get the –graph …
Using Stata’s Frames feature to build an analytical dataset
Stata 16 introduced the new Frames functionality, which allows multiple datasets to be stored in memory, with each dataset stored in its own “Frame”. This allows for dynamic manipulation of multiple datasets across multiple Frames. Stata is still simplest to use when manipulating a single dataset (or, frame). So, Stata users will probably be interested …
Continue reading “Using Stata’s Frames feature to build an analytical dataset”
Mediation analysis in Stata using IORW (inverse odds ratio-weighted mediation)
Mediation is a commonly-used tool in epidemiology. Inverse odds ratio-weighted (IORW) mediation was described in 2013 by Eric J. Tchetgen Tchetgen in this publication. It’s a robust mediation technique that can be used in many sorts of analyses, including logistic regression, modified Poisson regression, etc. It is also considered valid if there is an observed …
Continue reading “Mediation analysis in Stata using IORW (inverse odds ratio-weighted mediation)”
Rounding/formatting a value while creating or displaying a Stata local or global macro
If you are looking to automate the printing of values from macros/regressions or whatnot on your figures, make sure to check out this post. I love Stata’s macro functionality. It allows you to grab information from r- or e-level data after executing a Stata command then calling that back later. (Local macros are temporary, global …
Extracting variable labels and categorical/ordinal value labels in Stata
Stata allows the labeling of variables and also the individual values of categorical or ordinal variable values. For example, in the –sysuse auto– database, “foreign” is labeled as “Car origin”, 0 is “Domestic”, and 1 is “Foreign”. It isn’t terribly intuitive to extract the variable label of foreign (here, “Car origin”) or the labels from …
Continue reading “Extracting variable labels and categorical/ordinal value labels in Stata”
Stata-R integration with Rcall
Stata is great because of its intuitive syntax, reasonable learning curve, and dependable implementation. There’s some cutting edge functionality and graphical tools in R that are missing in Stata. I came across the Rcall package that allows Stata to interface with R and use some of these advanced features. (Note: this worked for me as …