## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE, comment = "#>",
  fig.width = 7, fig.height = 4.5
)
library(PITS)

## ----individual-functions-----------------------------------------------------
data("example_cfr_data")
outcome <- example_cfr_data$outcome

# Baseline: intercept of a linear trend fit at t = 1
b <- estimate_baseline(outcome)
cat("Baseline:", round(b, 3), "\n")

# Sigma: residual SD after detrending
s <- estimate_sigma(outcome)
cat("Sigma:   ", round(s, 3), "\n")

# Rho: lag-1 autocorrelation of residuals
r <- estimate_rho(outcome)
cat("Rho:     ", round(r, 3), "\n")

# Pre-trend: slope per time unit (should be near zero)
t_pre <- estimate_trend(outcome)
cat("Trend:   ", round(t_pre, 4), "per month\n")

## ----all-in-one---------------------------------------------------------------
# From a data frame with 'time' and 'outcome' columns
params <- estimate_its_params(example_cfr_data)

## ----all-in-one-vector--------------------------------------------------------
# Or from a plain numeric vector (time index auto-generated)
params2 <- estimate_its_params(example_cfr_data$outcome, verbose = FALSE)
identical(params$sigma, params2$sigma)

## ----params-to-power, eval = FALSE--------------------------------------------
# result <- calculate_power(
#   n_pre        = params$n_pre,
#   n_post       = 30,
#   baseline     = params$baseline,
#   level_change = -3,          # <-- your clinical hypothesis
#   sigma        = params$sigma,
#   rho          = params$rho
# )

## ----custom-cols--------------------------------------------------------------
# Rename for illustration
alt_data <- example_cfr_data
names(alt_data) <- c("month", "cfr_pct")

params_alt <- estimate_its_params(
  alt_data,
  outcome_col = "cfr_pct",
  time_col    = "month",
  verbose     = FALSE
)
cat("Sigma:", round(params_alt$sigma, 3), "\n")

## ----simulate-predata---------------------------------------------------------
# Simulate pre-intervention data with assumed parameters
pre_synthetic <- simulate_predata(
  n        = 24,
  baseline = 15,
  sigma    = 2.0,
  rho      = 0.40,
  seed     = 42
)
head(pre_synthetic)

## ----plot-synthetic, fig.cap = "Synthetic pre-intervention series (baseline = 15, sigma = 2, rho = 0.40)."----
plot(pre_synthetic$time, pre_synthetic$outcome,
     type = "o", pch = 16, col = "steelblue",
     xlab = "Time", ylab = "Outcome",
     main = "Synthetic pre-intervention data",
     las = 1, bty = "l")
abline(h = 15, lty = 2, col = "grey50")
grid(NULL, NULL, lwd = 0.6, col = rgb(0, 0, 0, 0.1))

## ----verify-synthetic---------------------------------------------------------
recovered <- estimate_its_params(pre_synthetic, verbose = FALSE)
cat(sprintf("Target  → baseline: 15.00  sigma: 2.000  rho: 0.400\n"))
cat(sprintf("Recovered → baseline: %.2f  sigma: %.3f  rho: %.3f\n",
            recovered$baseline, recovered$sigma, recovered$rho))

## ----date-example, eval = FALSE-----------------------------------------------
# # If your data has a 'date' column of class Date:
# params <- estimate_its_params(
#   my_data,
#   outcome_col = "cfr",
#   time_col    = "date"     # Date objects are handled automatically
# )

## ----missing-values-----------------------------------------------------------
data_with_na <- example_cfr_data
data_with_na$outcome[c(5, 12)] <- NA

params_na <- suppressWarnings(
  estimate_its_params(data_with_na, verbose = FALSE)
)
cat("n_pre after removing NAs:", params_na$n_pre, "\n")

## ----short-preperiod, warning = TRUE------------------------------------------
params_short <- estimate_its_params(
  example_cfr_data$outcome[1:9],
  verbose = FALSE
)

