/cibookex-r

Causal Inference: What If. R and Stata code for Exercises

Primary LanguageRMarkdownGNU General Public License v3.0GPL-3.0

Repository of R and Stata code for the exercises in Causal Inference: What If by Hernán and Robins

  • The R code by Joy Shi and Sean McGrath is available here
  • The Stata code by Eleanor Murray and Roger Logan is available here
  • The book by by Hernán MA and Robins JM is available here
  • These files are rendered using bookdown.

Install dependencies

  • If you have downloaded/forked this repository you can install the R dependencies with (assuming your working directory is at the top level of the repo):

    # install.packages("devtools") # uncomment if devtools not installed
    devtools::install_dev_deps()
  • You can install the Stata dependencies, in Stata, with

    do depdendency
  • For the pdf book please install the Fira Code font on your system from here

Building the book

  • If you need to install LaTeX, in R install the tinytex package (which is included in the dependencies) and run

    tinytex::install_tinytex()
  • Render all formats

    rmarkdown::render_site(encoding = 'UTF-8')
  • Render the PDF book

    rmarkdown::render_site(output_format = 'bookdown::pdf_book', encoding = 'UTF-8')
  • Render the HTML book

    rmarkdown::render_site(output_format = 'bookdown::gitbook_book', encoding = 'UTF-8')
  • Render the EPUB book

    rmarkdown::render_site(output_format = 'bookdown::epub_book', encoding = 'UTF-8')
  • Preview a specific chapter

    bookdown::preview_chapter("chapter-filename.Rmd")
  • To build the chapters containing Stata code:

    • You need Stata installed on your machine.

    • You need to install Doug Hemken's Statamarkdown package, which is now on CRAN and is installed when you run devtools::install_dev_deps(), or install with

      install.packages("Statamarkdown")
    • If your version of Stata is installed at a default installation location the Statamarkdown package will find it. If your version of Stata is installed at a bespoke file path you'll need to set the file path to with something like

      library(Statamarkdown)
      stataexe <- "C:/Program Files/Stata17/StataSE-64.exe"
      knitr::opts_chunk$set(engine.path = list(stata = stataexe))
    • Install the Stata dependencies, in Stata, with:

      do dependency