## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  dpi = 150,
  fig.width = 7
)
options(DAGassist.color = FALSE, crayon.enabled = FALSE)

# chunks that need optional packages run only when those packages are installed
has_plot <- requireNamespace("ggdag", quietly = TRUE) &&
  requireNamespace("ggplot2", quietly = TRUE)
has_tables <- requireNamespace("modelsummary", quietly = TRUE)
has_estimands <- has_tables &&
  requireNamespace("WeightIt", quietly = TRUE) &&
  requireNamespace("marginaleffects", quietly = TRUE) &&
  requireNamespace("DirectEffects", quietly = TRUE)

## ----setup--------------------------------------------------------------------
library(DAGassist)

## ----dag----------------------------------------------------------------------
dag <- ggdag::dagify(
  turnout  ~ income + age + parental_ses + polint + elect_comp,
  income   ~ age + parental_ses + industry,
  polint   ~ income,
  industry ~ age,
  exposure = "income",
  outcome  = "turnout"
)

## ----dag-plot, echo = FALSE, message = FALSE, warning = FALSE, eval = has_plot, fig.height = 3.5, fig.alt = "The turnout DAG. Age and parental_ses affect income and turnout; age affects industry, which affects income; income affects political interest, which affects turnout; election competitiveness affects turnout."----
library(ggdag)
library(ggplot2)

#coordinates for dag labels
label_df <- data.frame(
  name = c("turnout", "income", "parental_ses", "age", "polint", "industry", "elect_comp"),
  label_x = c(10, 0, 5, 5, 5, 0, 9),
  label_y = c(0, 0, 1, -1, 0.55, -0.75, -0.75)
)
label_df$label <- unname(attr(turnout_dag, "labels")[label_df$name])

ggdag_status(turnout_dag, text = FALSE) +
  theme_dag() +
  scale_color_manual(values = c(exposure = "black", outcome = "grey30"),
                     na.value = "grey60") +
  theme(legend.position = "none") +
  geom_label(
    data = label_df,
    aes(x = label_x, y = label_y, label = label),
    inherit.aes = FALSE,
    size = 3.4,
    label.size = 0.25
  )

## ----roles--------------------------------------------------------------------
DAGassist(dag,
          lm(turnout ~ income + parental_ses + age + polint + industry + elect_comp,
             data = turnout_data),
          show = "roles")

## ----models, results = "asis", eval = has_tables------------------------------
DAGassist(dag,
          lm(turnout ~ income + parental_ses + age + polint + industry + elect_comp,
             data = turnout_data),
          show = "models", type = "text", verbose = FALSE)

## ----dotwhisker, warning = FALSE, eval = has_plot, fig.height = 4.5, fig.alt = "Dot-and-whisker plot of the income coefficient: about 0.28 in the original model and about 0.49 in the minimal and canonical models."----
DAGassist(dag,
          lm(turnout ~ income + parental_ses + age + polint + industry + elect_comp,
             data = turnout_data),
          type = "dotwhisker")

## ----estimands, results = "asis", eval = has_estimands------------------------
DAGassist(dag,
          lm(turnout ~ income + parental_ses + age + polint + industry + elect_comp,
             data = turnout_data),
          estimand = c("total", "direct"),
          show = "models", type = "text", verbose = FALSE)

## ----robustness---------------------------------------------------------------
pdag_robustness(dag,
                formula = turnout ~ income + parental_ses + age + polint + industry + elect_comp,
                uncertain_edges = "income -- polint")

