Reproducible code for our research letter, "Trends in pregnancy-associated mortality involving opioids in the United States, 2007-2016", which uses multiple cause of death data to examine trends in opioid mortality among pregnancy-related deaths. The citation is:
Gemmill A, Kiang MV, and Alexander MJ. Trends in pregnancy-associated mortality involving opioids in the United States, 2007-2016. American Journal of Obstetrics and Gynecology. Published ahead-of-print (September 2018). doi: 10.1016/j.ajog.2018.09.028
Please submit issues via Github.
This analysis uses restricted-access multiple cause of death (MCOD) data, with geographic identifiers, from the National Center for Health Statistics. Therefore, in order to reproduce the entire pipeline (i.e., go from raw data to Figure 1), you must have access to these files. Access to the raw data can be requested through NAPHSIS.
If you have the restricted-access data, you must specify the location of each file (i.e., year) in the
00b_process_raw_files.R file (starting at line 19) or create a folder called
./restricted_data with the files in their original name.
To run this code, you'll need the following
R packages from CRAN:
In addition, you'll need our package for working with multiple cause of death data,
If you would like to reproduce the full pipeline, including the raw data, you should also install the following packages:
This pipeline is divided into two parts. The first part requires restricted-access MCOD data and consists of the code files beginning with
00. The second part uses shareable (i.e., N ≥ 10 deaths) data generated from the
00 files to create the Figure shown in the letter. This file begins with
01 and can be run without access to the restricted data. All files are made to be run in alphanumeric order.
If you have restricted-access files, specify the file locations in
00b_process_raw_files.R and the entire pipeline will run. If you do not have restricted-access files, simply run the pipeline starting at
The result of this pipeline is Figure 1 of the letter (in
./figs) and the data required to generate it (
./working_data_2007_2016.csv). These data adhere to the NCHS data use agreement and omit any observation with fewer than 10 deaths (see Line 158 of the
In addition, there are two supplemental files.
99_generate_checkbox_years_table.R is a simple script that creates a table of states and the year that state implemented the checkbox. Data come from various sources — check the code for details.
mk_nytimes.R is just a plotting theme used for Figure 1.
sessionInfo() output can be found in the
./session_info.txt file. This output provides exact version information for each package we used,
R, and our operating system.
sink("./session_info.txt", append = FALSE, split = FALSE) cat(sprintf("Date/Time of Info: %s\n", Sys.time())) cat("\n\ndevtools::session_info()\n") devtools::session_info() cat("\n\n\nsessionInfo()\n") sessionInfo() sink()