## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE, comment = "#>",
  fig.width = 10, fig.height = 5, out.width = "75%", fig.align = "center", dpi = 150
)

## -----------------------------------------------------------------------------
library(netmatchRI)

data("netmatch_example", package = "netmatchRI")
sim <- netmatch_example
dat <- sim$data
net_dist <- sim$net_dist

dim(dat)
table(dat$Z)

## -----------------------------------------------------------------------------
m <- netmatch(
  data = dat,
  treat = "Z",
  covariates = c("X1", "X2", "X3"),
  network = net_dist,
  method = "dual",
  kappa = 2,
  estimand = "ATT"
)

m

## -----------------------------------------------------------------------------
summary(m)

diag <- diagnose_match(m)
diag$covariate_balance
diag$network_summary

## ----fig.width=7, fig.height=6------------------------------------------------
m_covariate <- netmatch(
  dat, "Z", c("X1", "X2", "X3"), net_dist,
  method = "covariate"
)
m_single <- netmatch(
  dat, "Z", c("X1", "X2", "X3"), net_dist,
  method = "single", kappa = 2
)
designs <- list(
  "Covariate-only" = m_covariate,
  "Single-penalty" = m_single,
  "Dual-penalty" = m
)
plot_covariate_balance(designs, include_before = TRUE)
plot_covariate_similarity(designs, include_before = TRUE)

## -----------------------------------------------------------------------------
ri_unadjusted <- RI_unadjusted(m, "Y")
ri_design <- RI_design(m, "Y")
ri_adjusted <- RI_adjusted(m, "Y", eta = 0.05, rho = 0.20)

p_values <- data.frame(
  inference = c("Unadjusted", "Design-based", "Adjusted (eta = 0.05, rho = 0.20)"),
  p_value = c(ri_unadjusted$result$p_value,
              ri_design$result$p_value,
              ri_adjusted$result$p_value)
)
p_values

## -----------------------------------------------------------------------------
sens <- sensitivity_grid(
  m,
  "Y",
  eta = c(0, 0.01, 0.05, 0.10, 0.15, 0.20),
  rho = seq(0, 0.5, by = 0.05)
)
plot_sensitivity(sens)

crit <- critical_sensitivity(m, "Y", rho = seq(0, 0.5, by = 0.01))
crit
crit$summary
plot_sensitivity(crit)

