---
title: "Getting Started with Report Creation"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Getting Started with Report Creation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# Introduction

This vignette explains how to make publication-grade DAGassist reports in LaTeX,
Word, Excel, and plaintext. Since this package's primary utility is as a robustness
check for observational studies, most of its functionality revolves around
creating reports.

## Install
```{r install, eval=FALSE}
install.packages("pak")
pak::pak("grahamgoff/DAGassist")
```
## Setup

In addition to loading `DAGassist`, we will load the following baseline packages:

- `modelsummary` to build the **model comparison** table for **LaTeX**, **Word**, **Excel**, and **plaintext**.
  - LaTeX uses `broom` as a fallback for report generation
- `knitr` to build intermediate .md for **Word** and **plaintext** report generation.
- `rmarkdown` to convert .md files to .docx files for **Word** report generation.
- `writexl` to export **Excel** files.
 
Essentially, to export:

- **LaTeX** only needs `modelsummary`
- **Excel** needs `modelsummary` and `writexl`
- **plaintext** needs `modelsummary` and `knitr`
- **Word** needs `modelsummary`, `knitr`, and `rmarkdown`
 
```{r helpers}
#load DAGassist
library(DAGassist) 
#load libraries to help export
library(modelsummary)
library(writexl)
library(knitr)
library(rmarkdown)
```

## Example DAG

```{r dag, echo=FALSE, warning=FALSE, message=FALSE, fig.width=8, fig.height=4, dpi=150, out.width="100%", fig.alt="Example DAG"}
library(ggdag)
library(dagitty)
library(tidyverse)

# X axis positions for DAG
x_pos <- c(
  "B" = 0,
  "A" = 0,
  "C" = 0,
  "F" = 1,
  "D" = 1,
  "H" = 1,
  "Y" = 2,
  "G" = 2
)

# Y axis positions for DAG
y_pos <- c(
  "A" = 1,
  "F" = 1,
  "G" = 1,
  "B" = 0,
  "D" = 0,
  "Y" = 0,
  "C" = -1,
  "H" = -1
)

# Define the DAG with custom coordinates
dag_model <- dagify(
  Y ~ D + F + H + G,
  D~ A + B + C,
  H ~ C,
  A ~ F + B,
  exposure = "D",
  outcome  = "Y",
    
  coords = list(x = x_pos, y = y_pos)
              
)

ggdag_status(dag_model,
             text = TRUE) +
  theme_dag() +
  scale_fill_manual(             
    values = c(
      exposure = "black",
      outcome  = "grey30",
      none     = "grey90"
    )
  ) +
  scale_color_manual(
    values = c(
      exposure = "black",
      outcome  = "grey30",
      none     = "grey60"
    )
  ) +
  theme(legend.position = "none") 
############################# make test data ##############################
set.seed(123)
n <- 1000

# exogenous variables 
B <- rnorm(n, 0, 1)
C <- rnorm(n, 0, 1)
F <- rnorm(n, 0, 1)
G <- rnorm(n, 0, 1)

# endogenous variables
A <- 0.6*F + 0.5*B + rnorm(n, 0, 1) # A ~ F + B
H <- 0.8*C + rnorm(n, 0, 1) # H ~ C
D <- 0.9*A + 0.5*B + 0.4*C + rnorm(n, 0, 1) # D ~ A + B + C
Y <- 1.0*D + 0.6*F + 0.5*H + 0.4*G + rnorm(n, 0, 1) # Y ~ D + F + H + G

df <- data.frame(Y, D, H, A, G, F, C, B)

```

The data, which simulates the DAG relationships:

```{r head}
head(df)
```

## Creating a report

Suppose you wrote an article uses model `original` as its main finding, and you want to ensure that your specification captures the total effect of `D` on `Y`, without mediator or collider bias. 

```{r main-model}
original <- lm(Y ~ D + G + H + F + A + B + C, data = df)
```

DAGassist can generate a report assessing the causal roles of the variables in `original`.

```{r report-path, include=FALSE}
out_tex <- file.path(tempdir(), "dagassist_report.tex")
out_docx <- file.path(tempdir(), "dagassist_report.docx")
out_xlsx <- file.path(tempdir(), "dagassist_report.xlsx")
out_txt  <- file.path(tempdir(), "dagassist_report.txt")
```


```{r report, results='asis'}
DAGassist(dag = dag_model, #specify a dagitty or ggdag object
          formula = lm(Y ~ D + G + H + F + A + B + C, data = df), #provide your formula
          type = "text", #output type
          out = out_txt) #a temporary directory, for the purpose of this vignette

cat(readLines(out_txt), sep = "\n") # show the output
```



