% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math} % this also loads fontspec
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
  % xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\usepackage[paperheight=10in,paperwidth=8in,margin=0.5in]{geometry}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same}
\hypersetup{
  pdftitle={A Causal Zoo},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{A Causal Zoo}
\author{}
\date{\vspace{-2.5em}2023-09-08}

\begin{document}
\maketitle

{
\setcounter{tocdepth}{2}
\tableofcontents
}
\newcommand{\Var}{\mathrm{Var}}
\newcommand{\E}{\mathrm{E}}
\newcommand{\N}{\mathrm{N}}

\listoffigures
\clearpage

\hypertarget{a-causal-zoo}{%
\section{A Causal Zoo}\label{a-causal-zoo}}

\begin{figure}
\centering
\includegraphics[width=0.7\textwidth,height=\textheight]{dag_full.png}
\caption{A DAG}
\end{figure}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{  nams }\OtherTok{\textless{}{-}} \FunctionTok{c}\NormalTok{(}\StringTok{\textquotesingle{}z\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}zx\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}zy\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}cov\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}y\textquotesingle{}}\NormalTok{,}
            \StringTok{\textquotesingle{}m\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}i\textquotesingle{}}\NormalTok{, }\StringTok{\textquotesingle{}xd\textquotesingle{}}\NormalTok{, }\StringTok{\textquotesingle{}yd\textquotesingle{}}\NormalTok{, }\StringTok{\textquotesingle{}zd\textquotesingle{}}\NormalTok{, }
            \StringTok{\textquotesingle{}col\textquotesingle{}}\NormalTok{)}
\NormalTok{  mat }\OtherTok{\textless{}{-}} \FunctionTok{matrix}\NormalTok{(}\DecValTok{0}\NormalTok{, }\FunctionTok{length}\NormalTok{(nams), }\FunctionTok{length}\NormalTok{(nams))}
  \FunctionTok{rownames}\NormalTok{(mat) }\OtherTok{\textless{}{-}}\NormalTok{ nams}
  \FunctionTok{colnames}\NormalTok{(mat) }\OtherTok{\textless{}{-}}\NormalTok{ nams}
  
  \CommentTok{\# confounding back{-}door path}
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}zx\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}z\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{3} 
\NormalTok{  mat[}\StringTok{\textquotesingle{}zy\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}z\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{3}
\NormalTok{  mat[}\StringTok{\textquotesingle{}x\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}zx\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{1}
\NormalTok{  mat[}\StringTok{\textquotesingle{}y\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}zy\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{2}
  
  \CommentTok{\# direct effect of x on y}
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}y\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{3}
  
  \CommentTok{\# indirect effect: Note that the causal effect is 3 + 1 x 1 = 4}
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}m\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{1}
\NormalTok{  mat[}\StringTok{\textquotesingle{}y\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}m\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{1}
  
  \CommentTok{\# Instrumental variable }
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}x\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}i\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{1}
  
  \CommentTok{\# \textquotesingle{}Covariate\textquotesingle{}}
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}y\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}cov\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{2}
  
  \CommentTok{\# descendant of X}
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}xd\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{1}
  
  \CommentTok{\# descendant of Y}
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}yd\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}y\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{1}
  
  \CommentTok{\# descendant of z {-}{-} imperfect control}
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}zd\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}z\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{2}
  
  \CommentTok{\# collider}
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}col\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}y\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{1}
\NormalTok{  mat[}\StringTok{\textquotesingle{}col\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}x\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{1}
  
  \CommentTok{\# independent SD of error for every variable}
  
  \FunctionTok{diag}\NormalTok{(mat) }\OtherTok{\textless{}{-}} \DecValTok{1}
  
  \CommentTok{\# but make SD of Y different }
  
\NormalTok{  mat[}\StringTok{\textquotesingle{}y\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}y\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \DecValTok{4}
\NormalTok{  mat[}\StringTok{\textquotesingle{}i\textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}i\textquotesingle{}}\NormalTok{] }\OtherTok{\textless{}{-}} \FloatTok{2.4}
\end{Highlighting}
\end{Shaded}

\hypertarget{dag---not-in-lower-triangular-form}{%
\section{DAG - not in lower triangular form}\label{dag---not-in-lower-triangular-form}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{  mat}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
          z zx zy cov x y m   i xd yd zd col
      z   1  0  0   0 0 0 0 0.0  0  0  0   0
      zx  3  1  0   0 0 0 0 0.0  0  0  0   0
      zy  3  0  1   0 0 0 0 0.0  0  0  0   0
      cov 0  0  0   1 0 0 0 0.0  0  0  0   0
      x   0  1  0   0 1 0 0 1.0  0  0  0   0
      y   0  0  2   2 3 4 1 0.0  0  0  0   0
      m   0  0  0   0 1 0 1 0.0  0  0  0   0
      i   0  0  0   0 0 0 0 2.4  0  0  0   0
      xd  0  0  0   0 1 0 0 0.0  1  0  0   0
      yd  0  0  0   0 0 1 0 0.0  0  1  0   0
      zd  2  0  0   0 0 0 0 0.0  0  0  1   0
      col 0  0  0   0 1 1 0 0.0  0  0  0   1
\end{verbatim}

\hypertarget{dag---in-lower-triangular-form}{%
\section{DAG - in lower triangular form}\label{dag---in-lower-triangular-form}}

Expressing the DAG in lower triangular form makes it
easy to iteratively work out the variance matrix.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{dag }\OtherTok{\textless{}{-}} \FunctionTok{permute\_to\_dag}\NormalTok{(mat) }\CommentTok{\# can be permuted to lower{-}diagonal form}
\NormalTok{dag }
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
            i z zx x m cov zy y col zd yd xd
      i   2.4 0  0 0 0   0  0 0   0  0  0  0
      z   0.0 1  0 0 0   0  0 0   0  0  0  0
      zx  0.0 3  1 0 0   0  0 0   0  0  0  0
      x   1.0 0  1 1 0   0  0 0   0  0  0  0
      m   0.0 0  0 1 1   0  0 0   0  0  0  0
      cov 0.0 0  0 0 0   1  0 0   0  0  0  0
      zy  0.0 3  0 0 0   0  1 0   0  0  0  0
      y   0.0 0  0 3 1   2  2 4   0  0  0  0
      col 0.0 0  0 1 0   0  0 1   1  0  0  0
      zd  0.0 2  0 0 0   0  0 0   0  1  0  0
      yd  0.0 0  0 0 0   0  0 1   0  0  1  0
      xd  0.0 0  0 1 0   0  0 0   0  0  0  1
      attr(,"class")
      [1] "dag"    "matrix" "array"
\end{verbatim}

\hypertarget{variance-matrix}{%
\section{Variance matrix}\label{variance-matrix}}

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{covld}\NormalTok{(dag)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
              i  z zx      x      m cov zy      y   col zd     yd     xd
      i    5.76  0  0   5.76   5.76   0  0  23.04  28.8  0  23.04   5.76
      z    0.00  1  3   3.00   3.00   0  3  18.00  21.0  2  18.00   3.00
      zx   0.00  3 10  10.00  10.00   0  9  58.00  68.0  6  58.00  10.00
      x    5.76  3 10  16.76  16.76   0  9  85.04 101.8  6  85.04  16.76
      m    5.76  3 10  16.76  17.76   0  9  86.04 102.8  6  86.04  16.76
      cov  0.00  0  0   0.00   0.00   1  0   2.00   2.0  0   2.00   0.00
      zy   0.00  3  9   9.00   9.00   0 10  56.00  65.0  6  56.00   9.00
      y   23.04 18 58  85.04  86.04   2 56 473.16 558.2 36 473.16  85.04
      col 28.80 21 68 101.80 102.80   2 65 558.20 661.0 42 558.20 101.80
      zd   0.00  2  6   6.00   6.00   0  6  36.00  42.0  5  36.00   6.00
      yd  23.04 18 58  85.04  86.04   2 56 473.16 558.2 36 474.16  85.04
      xd   5.76  3 10  16.76  16.76   0  9  85.04 101.8  6  85.04  17.76
\end{verbatim}

\clearpage

\hypertarget{some-models-to-try}{%
\section{Some models to try}\label{some-models-to-try}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fmlas }\OtherTok{\textless{}{-}} \FunctionTok{list}\NormalTok{(}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x,             }\CommentTok{\# with confounding}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ z,         }\CommentTok{\# unconfounded}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ zy,        }\CommentTok{\# unconfounded using generating model}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ zx,        }\CommentTok{\# unconfounded using assignment model}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ zx }\SpecialCharTok{+}\NormalTok{ zy,   }\CommentTok{\# \textquotesingle{}doubly robust\textquotesingle{}}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ zy }\SpecialCharTok{+}\NormalTok{ cov,  }\CommentTok{\# adding a covariate unrelated to x}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ z }\SpecialCharTok{+}\NormalTok{ m,     }\CommentTok{\# adding a mediator}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ z }\SpecialCharTok{+}\NormalTok{ xd,    }\CommentTok{\# adding a descendant of X}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ z }\SpecialCharTok{+}\NormalTok{ yd,    }\CommentTok{\# adding a descendant of Y}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ z }\SpecialCharTok{+}\NormalTok{ yd }\SpecialCharTok{+}\NormalTok{ cov,    }\CommentTok{\# adding a descendant of Y and a covariate}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ z }\SpecialCharTok{+}\NormalTok{ col,   }\CommentTok{\# adding a collider}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ z }\SpecialCharTok{+}\NormalTok{ i,     }\CommentTok{\# adding an instrumental variable}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ z }\SpecialCharTok{+}\NormalTok{ i }\SpecialCharTok{+}\NormalTok{ cov,  }\CommentTok{\# adding an instrumental variable and a covariate}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ xd,        }\CommentTok{\# adding a descendant of x}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ i,         }\CommentTok{\# using an instrumental variable as a control}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ zd,        }\CommentTok{\# imperfect control for confounding}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ zd }\SpecialCharTok{+}\NormalTok{ cov,  }\CommentTok{\# imperfect control + covariate}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ zd }\SpecialCharTok{+}\NormalTok{ xd,   }\CommentTok{\# imperfect control + descendant of x}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{+}\NormalTok{ zd }\SpecialCharTok{+}\NormalTok{ i,    }\CommentTok{\# bias amplification}
\NormalTok{  y }\SpecialCharTok{\textasciitilde{}}\NormalTok{ x }\SpecialCharTok{|}\NormalTok{ i          }\CommentTok{\# instrumental variable using two{-}stage least squares}
\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\clearpage

\hypertarget{fitting-the-models}{%
\section{`Fitting' the models}\label{fitting-the-models}}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fmlas }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{lapply}\NormalTok{(coefx, dag) }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{lapply}\NormalTok{(as.data.frame) }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{do.call}\NormalTok{(rbind.data.frame, .) }\OtherTok{{-}\textgreater{}}\NormalTok{ df}

\NormalTok{df }\OtherTok{\textless{}{-}} \FunctionTok{within}\NormalTok{(}
\NormalTok{  df,}
\NormalTok{  \{ }\CommentTok{\# label positions for plotting}
\NormalTok{    pos }\OtherTok{\textless{}{-}} \FunctionTok{ifelse}\NormalTok{(}\FunctionTok{grepl}\NormalTok{(}\StringTok{\textquotesingle{}IV|zy|m|zd.*z\textquotesingle{}}\NormalTok{, label), }\DecValTok{2}\NormalTok{, }\DecValTok{4}\NormalTok{)}
\NormalTok{    pos2 }\OtherTok{\textless{}{-}} \FunctionTok{ifelse}\NormalTok{(}\FunctionTok{grepl}\NormalTok{(}\StringTok{\textquotesingle{}yd$|z . xd|zd$\textquotesingle{}}\NormalTok{, label), }\DecValTok{2}\NormalTok{, pos)}
\NormalTok{    pos2 }\OtherTok{\textless{}{-}} \FunctionTok{ifelse}\NormalTok{(}\FunctionTok{grepl}\NormalTok{(}\StringTok{\textquotesingle{}yd\textquotesingle{}}\NormalTok{, label), }\DecValTok{3}\NormalTok{, pos2)}
\NormalTok{    pos2 }\OtherTok{\textless{}{-}} \FunctionTok{ifelse}\NormalTok{(}\FunctionTok{grepl}\NormalTok{(}\StringTok{\textquotesingle{}yd.*cov$\textquotesingle{}}\NormalTok{, label), }\DecValTok{1}\NormalTok{, pos2)}
\NormalTok{  \}}
\NormalTok{)}
\NormalTok{pdf }\OtherTok{\textless{}{-}}\NormalTok{ df}
\FunctionTok{sapply}\NormalTok{(pdf, is.numeric) }\SpecialCharTok{\%\textgreater{}\%} 
\NormalTok{  \{pdf[,.] }\OtherTok{\textless{}\textless{}{-}} \FunctionTok{round}\NormalTok{(pdf[,.], }\DecValTok{3}\NormalTok{)\}}
\NormalTok{pdf[, }\FunctionTok{c}\NormalTok{(}\DecValTok{6}\NormalTok{,}\DecValTok{1}\NormalTok{,}\DecValTok{4}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{,}\DecValTok{5}\NormalTok{)] }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{print}\NormalTok{(}\AttributeTok{row.names=}\NormalTok{F)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
       pos2 beta_x sd_factor  sd_e sd_x_avp                label
          4  5.074     1.577 6.455    4.094                y ~ x
          4  4.000     1.795 5.000    2.786            y ~ x + z
          2  4.000     1.557 4.583    2.943           y ~ x + zy
          4  4.000     2.057 5.348    2.600           y ~ x + zx
          2  4.000     1.763 4.583    2.600      y ~ x + zx + zy
          2  4.000     1.401 4.123    2.943     y ~ x + zy + cov
          2  3.000     5.205 4.899    0.941        y ~ x + z + m
          2  4.000     5.312 5.000    0.941       y ~ x + z + xd
          3  0.154     0.846 0.981    1.159       y ~ x + z + yd
          1  0.182     0.904 0.977    1.081 y ~ x + z + yd + cov
          4 -0.808     1.024 0.981    0.958      y ~ x + z + col
          4  4.000     3.536 5.000    1.414        y ~ x + z + i
          4  4.000     3.240 4.583    1.414  y ~ x + z + i + cov
          4  5.074     6.645 6.455    0.971           y ~ x + xd
          4  5.636     1.693 5.617    3.317            y ~ x + i
          2  4.377     1.796 5.554    3.092           y ~ x + zd
          4  4.377     1.676 5.181    3.092     y ~ x + zd + cov
          4  4.377     5.837 5.554    0.951      y ~ x + zd + xd
          4  4.947     2.752 5.366    1.949       y ~ x + zd + i
          2  4.000     3.254 7.810    2.400       y ~ x (IV = i)
\end{verbatim}

\clearpage

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{cap }\OtherTok{=} \StringTok{"$Var(}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{hat\{}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{beta\}\_x)$ and $E(}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{hat\{}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{beta\}\_x)$: variance versus bias."}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{xyplot}\NormalTok{(}\FunctionTok{log2}\NormalTok{(sd\_factor}\SpecialCharTok{\^{}}\DecValTok{2}\NormalTok{) }\SpecialCharTok{\textasciitilde{}}\NormalTok{ beta\_x, . , }\AttributeTok{font =} \DecValTok{2}\NormalTok{, }\AttributeTok{cex =} \DecValTok{1}\NormalTok{,}
         \AttributeTok{scales =} \FunctionTok{list}\NormalTok{(}\AttributeTok{y =} \FunctionTok{list}\NormalTok{(}\AttributeTok{at=}\FunctionTok{seq}\NormalTok{(}\SpecialCharTok{{-}}\DecValTok{3}\NormalTok{,}\DecValTok{10}\NormalTok{), }\AttributeTok{labels=}\DecValTok{2}\SpecialCharTok{\^{}}\NormalTok{(}\DecValTok{5}\SpecialCharTok{+}\FunctionTok{seq}\NormalTok{(}\SpecialCharTok{{-}}\DecValTok{3}\NormalTok{,}\DecValTok{10}\NormalTok{)))),}
         \AttributeTok{xlim =} \FunctionTok{c}\NormalTok{(}\SpecialCharTok{{-}}\FloatTok{1.6}\NormalTok{, }\FloatTok{6.5}\NormalTok{), }
         \AttributeTok{pch =} \DecValTok{20}\NormalTok{,}
         \AttributeTok{xlab =} \FunctionTok{TeX}\NormalTok{(}\StringTok{\textquotesingle{}$E(}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{hat\{}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{beta\})$\textquotesingle{}}\NormalTok{),}
         \AttributeTok{ylab =} \FunctionTok{TeX}\NormalTok{(}\StringTok{\textquotesingle{}$Var(}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{hat\{}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{beta\})$ using relative $n$ to get equivalent power\textquotesingle{}}\NormalTok{),}
         \AttributeTok{labs =} \FunctionTok{sub}\NormalTok{(}\StringTok{\textquotesingle{}y \textasciitilde{} \textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}\textquotesingle{}}\NormalTok{,.}\SpecialCharTok{$}\NormalTok{label),}
         \AttributeTok{pos =}\NormalTok{ .}\SpecialCharTok{$}\NormalTok{pos) }\SpecialCharTok{+}
  \FunctionTok{layer}\NormalTok{(}\FunctionTok{panel.grid}\NormalTok{(}\AttributeTok{h=}\SpecialCharTok{{-}}\DecValTok{1}\NormalTok{,}\AttributeTok{v=}\SpecialCharTok{{-}}\DecValTok{1}\NormalTok{)) }\SpecialCharTok{+}
  \FunctionTok{layer}\NormalTok{(}\FunctionTok{panel.abline}\NormalTok{(}\AttributeTok{v =} \DecValTok{4}\NormalTok{, }\AttributeTok{lty =} \DecValTok{3}\NormalTok{)) }\SpecialCharTok{+} 
  \FunctionTok{layer}\NormalTok{(}\FunctionTok{panel.text}\NormalTok{(..., }\AttributeTok{labels =}\NormalTok{ labs, }\AttributeTok{pos =}\NormalTok{ pos)) }
\end{Highlighting}
\end{Shaded}

\begin{figure}
\centering
\includegraphics{The_Causal_Zoo_files/figure-latex/bias-1.pdf}
\caption{\label{fig:bias}\(Var(\hat{\beta}_x)\) and \(E(\hat{\beta}_x)\): variance versus bias.}
\end{figure}

\clearpage

\begin{figure}
\centering
\includegraphics{bias_dag.png}
\caption{Variance versus bias with DAG}
\end{figure}

\clearpage

\hypertarget{which-model-is-best}{%
\section{Which model is best?}\label{which-model-is-best}}

\textbf{It depends on the purpose of the analysis!} Thanks to Hugh McCague for
the idea of including the following figure to illustrate how focusing
on predictive power does not lead to a suitable model to estimate the
causal effect of X.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{caption }\OtherTok{\textless{}{-}} \StringTok{"Adjusted measure of fit versus bias. Which model(s) would you choose?"}
\end{Highlighting}
\end{Shaded}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{df }\SpecialCharTok{\%\textgreater{}\%} 
  \FunctionTok{subset}\NormalTok{(}\SpecialCharTok{!}\FunctionTok{grepl}\NormalTok{(}\StringTok{\textquotesingle{}i2\textquotesingle{}}\NormalTok{, label)) }\SpecialCharTok{\%\textgreater{}\%}
  \FunctionTok{xyplot}\NormalTok{(var\_e\_adj }\SpecialCharTok{\textasciitilde{}}\NormalTok{ beta\_x, . , }\AttributeTok{font =} \DecValTok{2}\NormalTok{,}
         \CommentTok{\#scales = list(y = list(at=seq({-}3,10), labels=2\^{}(5+seq({-}3,10)))),}
         \CommentTok{\# xlim = c({-}.6, 6), }
         \AttributeTok{pch =} \DecValTok{16}\NormalTok{,}
         \AttributeTok{xlab =} \FunctionTok{TeX}\NormalTok{(}\StringTok{\textquotesingle{}$E(}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{hat\{}\SpecialCharTok{\textbackslash{}\textbackslash{}}\StringTok{beta\})$\textquotesingle{}}\NormalTok{),}
         \AttributeTok{ylab =} \FunctionTok{TeX}\NormalTok{(}\StringTok{\textquotesingle{}Goodness of fit using an equivalent to adjusted R{-}squared (smaller is better)\textquotesingle{}}\NormalTok{),}
         \AttributeTok{labs =} \FunctionTok{sub}\NormalTok{(}\StringTok{\textquotesingle{}y \textasciitilde{} \textquotesingle{}}\NormalTok{,}\StringTok{\textquotesingle{}\textquotesingle{}}\NormalTok{,.}\SpecialCharTok{$}\NormalTok{label),}
         \AttributeTok{pos =}\NormalTok{ .}\SpecialCharTok{$}\NormalTok{pos2) }\SpecialCharTok{+}
  \FunctionTok{layer}\NormalTok{(}\FunctionTok{panel.text}\NormalTok{(..., }\AttributeTok{labels =}\NormalTok{ labs, }\AttributeTok{pos =}\NormalTok{ pos)) }\SpecialCharTok{+}
  \FunctionTok{layer}\NormalTok{(}\FunctionTok{panel.grid}\NormalTok{(}\AttributeTok{h=}\SpecialCharTok{{-}}\DecValTok{1}\NormalTok{,}\AttributeTok{v=}\SpecialCharTok{{-}}\DecValTok{1}\NormalTok{)) }\SpecialCharTok{+}
  \FunctionTok{layer}\NormalTok{(}\FunctionTok{panel.abline}\NormalTok{(}\AttributeTok{v =} \DecValTok{4}\NormalTok{, }\AttributeTok{lty =} \DecValTok{3}\NormalTok{))}
\end{Highlighting}
\end{Shaded}

\clearpage

\hypertarget{whats-happening-with-ivs}{%
\section{What's happening with IVs?}\label{whats-happening-with-ivs}}

\begin{figure}
\centering
\includegraphics[width=0.5\textwidth,height=\textheight]{dag1.png}
\caption{A simple DAG with an IV}
\end{figure}

Let's assume multivariate normality
and build a variance matrix for Z, I, X, Y.

We can scale \(I\), \(Z\) and
\(X\) so they have unit variance and zero means.
This eliminates irrelevant nuisance
parameters.

Since \(I\) is an instrument for the confounding effect of \(Z\):

\[Var{
\begin{pmatrix}
Z \\ I \\ X
\end{pmatrix}
} =
\begin{pmatrix}
1 & 0 & a \\ 0 & 1 & b \\ a & b & 1
\end{pmatrix}\]
with \(a^2 + b^2 \le 1\).

Focus first on the \emph{assignment model}, i.e.~the model that determines
the value of \(X\) from the values of \(Z\), \(I\) and \(U_X\).

Letting \(c^2 = 1 - a^2 - b^2\), \(c^2\) represent the portion of the variance
in \(X\) that is not attributed to the instrument, \(I\), nor to the confounder,
\(Z\), define
\[\rho_I = \frac{b^2}{b^2 + c^2}\]
the proportion of the variance in \(X\) not
due to \(Z\) that is `explained' by \(I\).

For an instrument that captures
all of the variation not due to the confounder, \(c^2 = 0\) and \(\rho_I = 1\).

Focusing next on the model generating \(Y\),
let
\[Y = \alpha X + \beta Z + \gamma \varepsilon\]
with \(\varepsilon \sim \mathrm{N}(0,1)\),
independent of
other variables.

The variance matrix is:
\[Var{
\begin{pmatrix}
Z \\ I \\ X \\ Y
\end{pmatrix}
} =
\begin{pmatrix}
1 & 0 & a & a\alpha + \beta\\ 
0 & 1 & b & b\alpha\\ 
a & b & 1 & \alpha + a \beta \\
a\alpha + \beta & b\alpha & \alpha + a \beta & v_{yy}
\end{pmatrix}\]
where \(v_{yy} = \alpha^2 + \beta^2 + 2 a \alpha\beta + \sigma^2_\varepsilon\)

We can verify that the regression coefficients for the regression of \(Y\) on
\(X\) and \(Z\) are
\[ \begin{pmatrix}
1 & a \\
a & 1
\end{pmatrix} ^{-1}
\begin{pmatrix}
\alpha + a\beta \\
a \alpha + \beta
\end{pmatrix} =
\begin{pmatrix}
\alpha \\ \beta
\end{pmatrix}\]

The variance of the least-squares estimator of \(\alpha\) based on a regression
on \(X\) and the confounder \(Z\) is:
\[\begin{aligned}
\mathrm{Var}(\hat{\alpha}) & \approx \frac{1}{n}\;\frac{\sigma^2_\epsilon}{1-a^2} \\
        & = \frac{1}{n}\;\frac{\gamma^2}{b^2 + c^2}
\end{aligned}\]

The asymptotic expectation of the instrumental variable estimator \(\tilde{\alpha}\) is
\[\sigma_{IX}^{-1}\sigma_{IY} = \frac{1}{b}\times b\alpha = \alpha\]

The variance of \(\tilde{\alpha}\) is
(\textbf{fox2016?}):

\[\begin{aligned}
\mathrm{Var}(\tilde{\alpha}) & \approx \frac{1}{n}\;\sigma^2_{\epsilon IV} \sigma_{IX}^{-1}\sigma_{II}\sigma_{XI}^{-1}
        & = \frac{1}{n}\;(\beta^2 + \gamma^2)\frac{1}{b^2}
\end{aligned}\]

Thus, the variance inflation factor -- which is the same as the `sample
size inflation
factor to achieve the same power' -- using IV estimation instead of controlling for
a confounder (assuming that both approaches are available) is:

\[\begin{aligned}
IVVIF & = \frac{\mathrm{Var}(\tilde{\alpha})}{\mathrm{Var}(\hat{\alpha})} \\
& =  \frac{\beta^2 + \gamma^2}{b^2} / \frac{\gamma^2}{b^2 + c^2} \\
& =   \frac{\beta^2 + \gamma^2}{\gamma^2} / \frac{b^2}{b^2 + c^2} \\
& = 1/\left( {\frac{\gamma^2}{\gamma^2 + \beta^2} \times \frac{b^2}{b^2 + c^2}}\right) \\
& =   \left(1 + \frac{\beta^2}{\gamma^2}\right) \times \left(1 + \frac{c^2}{b^2} \right) \\
& = \frac{1}{1 - R^2_{Y,Z|X}} \times \frac{1}{R^2_{X,I|Z}}
\end{aligned}\]

The first term is structural in the sense that it is a consequence
of the problem, specifically the degree of confounding relative
to the residual error variance in the model. For a given problem,
the IV has no impact on this, so it represents a lower bound
for the IVVIF. The second term clarifies that it is not the \emph{correlation
of the IV with X} directly that affects the IVVIF, but its \textbf{partial
correlation} adjusted for the relationship of X with confounders.

In conclusion: In any situation where you have a choice,
controlling for confounders will do better than using a corresponding IV,
i.e.~an IV that annihilates the confounder. Fitting with IVs does
not take the same advantage of a model with a small error variance in
the same way that a regression model does.

The lower bound for error variance
created by the confounder could swamp the benefit of
small residual variance in the generating model. In contrast,
a regression model takes full proportional advantage of a
reduction in residual error.

\hypertarget{references}{%
\section{References}\label{references}}

\end{document}
