DAGassist in 10 minutes

What you’ll learn

Load packages

library(DAGassist)
# load helper libraries
library(modelsummary)
library(dagitty)

Make your DAG and data

n <- 500
Z <- rnorm(n)
A <- rnorm(n)
X <- 0.6*Z + rnorm(n)
M <- 0.5*X + 0.3*Z + rnorm(n)
Y <- 1.0*X + 0.0*M + 0.5*Z + rnorm(n)
df <- data.frame(Y, X, M, Z, A)

dag_model <- dagitty('
  dag {
    Z -> X
    Z -> Y
    A -> Y
    X -> M
    X -> Y
    M -> Y
    X [exposure]
    Y [outcome]
  }')

Generate a console report with DAGassist

See the Getting Started vignette for a guide on using different parameters get the most out of DAGassist. For the purpose of this tutorial, we will keep it simple.

See the Supported Models vignette for documentation on what engines DAGassist supports. Since DAGassist is model-agnostic, if an engine accepts a standard formula + data interface, it will usually work.

Interpreting the output:


DAGassist(
  dag = dag_model,
  formula = lm(Y ~ X + M + Z + A, data = df)
)
#> DAGassist Report: 
#> 
#> Roles:
#> variable  role        X  Y  conf  med  col  IO  dMed  dCol
#> X         exposure    x                                   
#> Y         outcome        x                      x         
#> Z         confounder        x                             
#> M         mediator                x                       
#> A         other                                           
#> 
#>  (!) Bad controls in your formula: {M}
#> Minimal controls 1: {Z}
#> Canonical controls: {A, Z}
#> 
#> Formulas:
#>   original:  Y ~ X + M + Z + A
#> 
#> Model comparison:
#> 
#> +---+----------+-----------+-----------+
#> |   | Original | Minimal 1 | Canonical |
#> +===+==========+===========+===========+
#> | X | 1.089*** | 1.091***  | 1.092***  |
#> +---+----------+-----------+-----------+
#> |   | (0.051)  | (0.044)   | (0.044)   |
#> +---+----------+-----------+-----------+
#> | M | 0.007    |           |           |
#> +---+----------+-----------+-----------+
#> |   | (0.044)  |           |           |
#> +---+----------+-----------+-----------+
#> | Z | 0.461*** | 0.463***  | 0.463***  |
#> +---+----------+-----------+-----------+
#> |   | (0.051)  | (0.050)   | (0.050)   |
#> +---+----------+-----------+-----------+
#> | A | 0.020    |           | 0.020     |
#> +---+----------+-----------+-----------+
#> |   | (0.041)  |           | (0.041)   |
#> +===+==========+===========+===========+
#> | + p < 0.1, * p < 0.05, ** p < 0.01,  |
#> | *** p < 0.001                        |
#> +===+==========+===========+===========+

Exporting

See the Making Reports vignette for more detailed information on producing publication-quality DAGassist reports in LaTex, Word, Excel, and plaintext.

Since DAGassist is designed to make appendix robustness checks, this is an example of how to output a report in LaTeX.

#initialize a temporary path
out_tex <- file.path(tempdir(), "dagassist_report.tex")

DAGassist(
  dag = dag_model,
  formula = lm(Y ~ X + M + Z + A, data = df),
  type = "latex", 
  out = out_tex) #put your output directory and file name here

cat(readLines(out_tex, n = 15), sep = "\n") # briefly show the output
#> % ---- DAGassist LaTeX fragment (no preamble) ----
#> % Requires: \usepackage{tabularray} \UseTblrLibrary{booktabs,siunitx,talltblr}
#> \begingroup\footnotesize
#> \begingroup\setlength{\emergencystretch}{3em}
#> \begin{longtblr}[presep=0pt, postsep=0pt, caption={DAGassist Report:}, label={tab:dagassist}]%
#> {width=\textwidth,colsep=1.5pt,rowsep=0pt,abovesep=0pt,belowsep=0pt,colspec={X[35,l]X[15,l]X[8,c]X[8,c]X[8,c]X[8,c]X[8,c]X[8,c]X[8,c]X[8,c]}}
#> \toprule
#> Variable & Role & X & Y & CON & MED & COL & IO & dMed & dCol \\
#> \midrule
#> A & other &  &  &  &  &  &  &  &  \\
#> M & mediator &  &  &  & x &  &  &  &  \\
#> X & exposure & x &  &  &  &  &  &  &  \\
#> Y & outcome &  & x &  &  &  &  & x &  \\
#> Z & confounder &  &  & x &  &  &  &  &  \\
#> \bottomrule