## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(didintrjl)

## ----include = FALSE----------------------------------------------------------
# Only run the examples below if Julia can be set up on this machine.
# (DiDInt.jl must also be installed; see the README.)
knitr::opts_chunk$set(
  eval = requireNamespace("JuliaConnectoR", quietly = TRUE) &&
    JuliaConnectoR::juliaSetupOk() &&
    JuliaConnectoR::juliaEval('using Pkg; _didint_pkgs = filter(p -> p.second.name == "DiDInt", Pkg.dependencies()); !isempty(_didint_pkgs) && first(values(_didint_pkgs)).version >= v"0.9.6"') #nolint
)

## -----------------------------------------------------------------------------
# Load the example data
df <- read.csv(system.file("extdata", "merit.csv", package = "didintrjl"))

# Estimate the ATT
res <- didint("coll", "state", "year", df, verbose = FALSE,
              treated_states = c(71, 58, 64, 59, 85, 57, 72, 61, 34, 88),
              treatment_times = c(1991, 1993, 1996, 1997, 1997, 1998,
                                  1998, 1999, 2000, 2000))

summary(res)

# Aggregate and sub-aggregate results can also be accessed directly
res$agg
res$sub

## ----fig.width = 9, fig.height = 10, out.width = "100%", fig.align = "center"----
res_event <- didint_plot("coll", "state", "year", df, event = TRUE,
                         treated_states = c(71, 58, 64, 59, 85, 57,
                                            72, 61, 34, 88),
                         treatment_times = c(1991, 1993, 1996, 1997, 1997,
                                             1998, 1998, 1999, 2000, 2000),
                         covariates = c("asian", "black", "male"))

plot(res_event)

## ----fig.width = 8, fig.height = 6, out.width = "100%", fig.align = "center"----
# Using a subset of states to keep the plot readable
df_sub <- df[df$state %in% c(71, 58, 11, 34, 14), ]
res_parallel <- didint_plot("coll", "state", "year", df_sub,
                            treatment_times = c(1991, 1993, 2000),
                            covariates = c("asian", "black", "male"))

plot(res_parallel)

## ----include = FALSE----------------------------------------------------------
# Shut down the Julia session started during the examples.
if (requireNamespace("JuliaConnectoR", quietly = TRUE) &&
    JuliaConnectoR::juliaSetupOk()) {
  JuliaConnectoR:::stopJulia()
}

