---
title: "Causal Diagrams in R"
format: html
---

```{r}
#| label: setup
library(tidyverse)
library(ggdag)
library(dagitty)
set.seed(1234)
```

## Your Turn 1

Descriptively, drinking coffee is associated with lung cancer. Does drinking coffee cause lung cancer? 

1. Specify a DAG with `dagify()`. Write your assumption that `smoking` causes `cancer` as a formula. 
2. We're going to assume that coffee does *not* cause cancer, so there's no formula for that. But we still need to declare our causal question. Specify "coffee" as the exposure and "cancer" as the outcome (both in quotations marks).
3. Plot the DAG using `ggdag()`

Stretch goal: Underneath the hood, `ggdag()` calls `tidy_dagitty()` on `dagitty` objects. Check the help page for `?tidy_dagitty()` and try using one of the layouts listed in `?layout_tbl_graph_igraph()`. Experiment with different layouts. Plot with `ggdag()`


```{r}
coffee_cancer_dag <- ______(
  ______ ~ ______,
  smoking ~ addictive,
  coffee ~ addictive,
  exposure = "______",
  outcome = "______",
  labels = c(
    "coffee" = "Coffee", 
    "cancer" = "Lung Cancer", 
    "smoking" = "Smoking", 
    "addictive" = "Addictive \nBehavior"
  )
)

______(coffee_cancer_dag)
```

## Your Turn 2

Most {ggdag} quick plotting functions are actually wrappers for functions that let you work with the DAG data directly. 

1. Call `tidy_dagitty()` on `coffee_cancer_dag` to create a tidy DAG, then pass the results to `dag_paths()`. What's different about these data?
2. Plot the open paths with `ggdag_paths()`. (Just give it `coffee_cancer_dag` rather than using `dag_paths()`; the quick plot function will do that for you.) Remember, since we assume there is *no* causal path from coffee to lung cancer, any open paths must be confounding pathways. 

Stretch goal: ggdags are just ggplots! You can add themes, geoms, and other {ggplot2} elements with `+` like a normal ggplot. Try adding a theme ({ggdag} has several, or you could try `theme_void()`).

Stretch goal: The variable names are a little hard to read. We specified some labels earlier, so let's use them. In the tidy DAG, these are called `label`. In `ggdag_paths()`, add the argument `use_labels = "label"` and remove the node text with `text = FALSE`.

```{r}
coffee_cancer_dag |>
  ______() |>
  ______()

______
```


## Your Turn 3

Now that we know the open, confounding pathways (sometimes called "backdoor paths"), we need to know how to close them! First, we'll ask {ggdag} for adjustment sets, then we would need to do something in our analysis to account for at least one adjustment set (e.g. multivariable regression, weighting, or matching for the adjustment sets).

1. Use `ggdag_adjustment_set()` to visualize the adjustment sets. Add the arguments `use_labels = "label"` and `text = FALSE`.
2. Write an R formula for each adjustment set, as you might if you were fitting a model in `lm()` or `glm()`

Stretch goal: Use `dagitty::adjustmentSets()` to print the adjustment sets to the console.

Stretch goal: Sometimes, we know a variable plays a vital role in a causal diagram but we can't measure it, or we simply don't have it in our data set. You can tell {ggdag} that a variable is unmeasured with the `latent` argument in `dagify()`. Re-run the `dagify()` call above, but set `latent = "addictive"` (meaning we can't or haven't measured this variable). Plot it with `ggdag_adjustment_set()`. What's different? Now, try setting `latent = c("addictive", "smoking")` and plotting the adjustment set. What do the results mean?

```{r}
______(______)

cancer ~ ______
cancer ~ ______
```

## Your Turn 4

Time-ordering your DAGs is incredibly useful, because it makes it cleaner, easier to read, and easier to understand what to control for --- and *not* control for.

ggdag includes several ways to manually specify coordinates, including `time_ordered_coords()`. This functional automatically determines the time ordering of the DAG based on the causal relationships. After all, something that causes something else *must* come before, otherwise we'd be violating the time-space continuum. 

Recreate the DAG we've been working with using `time_ordered_coords()`, then visualize the DAG. You don't need to use any arguments for this function, so `coords = time_ordered_coords()` will do.

```{r}
coffee_cancer_dag_to <- dagify(
  cancer ~ smoking,
  smoking ~ addictive,
  coffee ~ addictive,
  exposure = "coffee",
  outcome = "cancer",
  coords = ________,
  labels = c(
    "coffee" = "Coffee", 
    "cancer" = "Lung Cancer", 
    "smoking" = "Smoking", 
    "addictive" = "Addictive \nBehavior"
  )
)

_____(_____, use_labels = "label", text = FALSE)
```

# Take aways

* Draw your assumptions with DAGs! Use `dagify()` to specify them and `ggdag()` and friends to draw them. 
* The main goal for many analyses is to close backdoor (non-causal) paths. {ggdag} and {dagitty} can help you identify them.
* Adjustment sets are key for closing backdoor paths. Take a reasonable set and use it in your model to get a causal effect estimate.
