## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width  = 6,
  fig.height = 4
)
library(confoundvis)

## ----create-object------------------------------------------------------------
x <- new_confoundsens(
  lambda = seq(0, 0.25, length.out = 20),
  theta  = 0.42 - 1.8 * seq(0, 0.25, length.out = 20),
  se     = rep(0.07, 20)
)
x

## ----from-dataframe-----------------------------------------------------------
df <- data.frame(
  lambda = seq(0, 0.25, length.out = 20),
  theta  = 0.42 - 1.8 * seq(0, 0.25, length.out = 20),
  se     = rep(0.07, 20),
  level  = rep(c("within", "between"), each = 10)
)
x_ml <- as_confoundsens(df, level = "level")
summary(x_ml)

## ----robustness-curve---------------------------------------------------------
plot_robustness_curve(x, bands = TRUE, points = FALSE) +
  ggplot2::geom_hline(yintercept = 0, linetype = "dotted") +
  ggplot2::labs(title = "Robustness curve: effect path under growing confounding")

## ----robustness-curve-ml------------------------------------------------------
x_ml2 <- new_confoundsens(
  lambda = rep(seq(0, 0.25, length.out = 15), 2),
  theta  = c(0.50 - 2.0 * seq(0, 0.25, length.out = 15),
             0.35 - 1.2 * seq(0, 0.25, length.out = 15)),
  se     = rep(0.06, 30),
  level  = rep(c("within", "between"), each = 15)
)
plot_robustness_curve(x_ml2, facet_level = TRUE, bands = TRUE, points = FALSE) +
  ggplot2::labs(title = "Multilevel robustness curves")

## ----contour-plot-------------------------------------------------------------
benchmarks <- data.frame(
  r_yu  = c(0.12, 0.09, 0.18, 0.05),
  r_du  = c(0.21, 0.14, 0.08, 0.31),
  label = c("SES", "Gender", "Race", "Prior achievement")
)
plot_sensitivity_contour(threshold = 0.025, benchmarks = benchmarks) +
  ggplot2::labs(
    title   = "Sensitivity contour plot",
    subtitle = "Benchmarks: observed covariate correlations"
  )

## ----love-plot----------------------------------------------------------------
covariate_impacts <- data.frame(
  covariate = c("SES", "Gender", "Race", "Prior achievement",
                "School type", "Region", "Age"),
  impact    = c(0.025, 0.008, 0.019, 0.041, 0.011, 0.006, 0.014)
)

plot_sensitivity_love(covariate_impacts, threshold = 0.022) +
  ggplot2::labs(
    title    = "Sensitivity Love plot",
    subtitle = "Impact of observed covariates vs. sensitivity threshold"
  )

## ----taylor-plot--------------------------------------------------------------
d <- seq(0, 0.20, length.out = 40)
taylor_df <- data.frame(
  delta  = rep(d, 3),
  series = rep(c("Observed path", "Tangent (1st order)",
                  "Quadratic (2nd order)"), each = 40),
  value  = c(
    0.4 - 0.7*d - 0.6*d^2,   # concave-down path
    0.4 - 0.7*d,              # tangent
    0.4 - 0.7*d - 0.3*d^2    # local quadratic fit
  )
)

plot_local_taylor(taylor_df) +
  ggplot2::geom_hline(yintercept = 0, linetype = "dotted") +
  ggplot2::labs(
    title    = "Local Taylor diagnostic",
    subtitle = "Concave-down path: tangent overestimates robustness"
  )

## ----fit-quadratic------------------------------------------------------------
path_df <- data.frame(
  delta = seq(0, 0.5, length.out = 100),
  theta = 0.4 - 0.7 * seq(0, 0.5, length.out = 100) -
          0.5 * seq(0, 0.5, length.out = 100)^2
)

fit <- fit_local_quadratic(path_df, local_max_delta = 0.2)
cat("Intercept:", round(fit$intercept, 4), "\n")
cat("Slope    :", round(fit$slope,     4), "\n")
cat("Curvature:", round(fit$quad,      4), "\n")

## ----simulate-----------------------------------------------------------------
sims <- simulate_taylor_demo(delta_max = 1, step = 0.05,
                             theta0 = 0.4, slope = -0.5, kappa = 0.4)

# Plot all three on one figure
library(ggplot2)
paths <- rbind(
  data.frame(delta = sims$linear$lambda,
             theta = sims$linear$theta,  regime = "Linear"),
  data.frame(delta = sims$concave$lambda,
             theta = sims$concave$theta, regime = "Concave-down"),
  data.frame(delta = sims$convex$lambda,
             theta = sims$convex$theta,  regime = "Convex-up")
)

ggplot(paths, aes(x = delta, y = theta, colour = regime, linetype = regime)) +
  geom_line(linewidth = 0.8) +
  geom_hline(yintercept = 0, linetype = "dotted") +
  labs(x = expression(delta), y = expression(theta(delta)),
       title   = "Three curvature regimes",
       colour  = "Regime", linetype = "Regime")

