#' ---
#' title: "Regression and Other Stories: CausalDiagram"
#' author: "Andrew Gelman, Jennifer Hill, Aki Vehtari"
#' date: "`r format(Sys.Date())`"
#' output:
#'   html_document:
#'     theme: readable
#'     toc: true
#'     toc_depth: 2
#'     toc_float: true
#'     code_download: true
#' ---

#' Plot a causal diagram. See Chapter 19 in Regression and Other
#' Stories.
#' 
#' -------------
#' 

#+ setup, include=FALSE
knitr::opts_chunk$set(message=FALSE, error=FALSE, warning=FALSE, comment=NA)
# switch this to TRUE to save figures in separate files
savefigs <- FALSE

#' #### Load packages
library("rprojroot")
root<-has_file(".ROS-Examples-root")$make_fix_file()

#' #### Plot causal diagram
#+ eval=FALSE, include=FALSE
if (savefigs) pdf(root("CausalDiagram/figs","diagram1.pdf"), height=4.5, width=10)
#+
par(mar=c(0,0,0,0))
plot(c(-.5,4), c(0,1.8), xlab="", ylab="", xaxt="n", yaxt="n", bty="n", type="n")
text(0, 1, "x", cex=2.4)
text(1, 1, "z", cex=2.4)
text(3.5, .5, expression (y^0), cex=2)
text(3.5, 1.5, expression (y^1), cex=2)
text(c(0,1,3.5), c(1.8,1.8,1.8), c("Before","Treatment","Outcome"), cex=2)
text(1.9, .72, "Treatment 0 (z=0)", cex=2, srt=-16)
text(1.9, 1.27, "Treatment 1 (z=1)", cex=2, srt=14.3)
arrows(c(1.1,1.1), c(1,1), c(3.3,3.3), c(.5, 1.5))
arrows(-.3,.2,4,.2, length=.2)
text(1.8, .1, "Time", cex=2)
#+ eval=FALSE, include=FALSE
if (savefigs) dev.off()
