## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval = FALSE-------------------------------------------------------------
# # Install from CRAN (once published)
# install.packages("MAIVE")
# 
# # Or install development version from GitHub
# install.packages("devtools")
# devtools::install_github("PetrCala/MAIVE")

## ----setup--------------------------------------------------------------------
library(MAIVE)

## ----custom-mapping-----------------------------------------------------------
custom_dat <- data.frame(
  my_est = c(0.50, 0.60, 0.40, 0.55, 0.45, 0.52, 0.48, 0.58, 0.42, 0.50, 0.47, 0.56),
  my_se = c(0.20, 0.18, 0.25, 0.22, 0.24, 0.19, 0.23, 0.17, 0.26, 0.21, 0.24, 0.18),
  my_n = c(80, 120, 95, 110, 90, 130, 100, 140, 85, 105, 88, 125),
  my_study = c("A", "A", "B", "B", "C", "C", "D", "D", "A", "B", "C", "D")
)

result <- maive(
  dat = custom_dat,
  estimate = "my_est",
  se = "my_se",
  n = "my_n",
  study_id = "my_study",
  method = 3,
  weight = 0,
  instrument = 1,
  studylevel = 2,
  SE = 3,
  AR = 1
)

result$beta

## ----metafor-bridge, eval = requireNamespace("metafor", quietly = TRUE)-------
smd <- metafor::escalc(
  measure = "SMD",
  m1i = c(2.1, 2.5, 1.9, 2.4, 2.2, 2.6, 2.3, 2.0),
  sd1i = c(1.0, 1.2, 0.9, 1.1, 1.0, 1.3, 1.1, 1.0),
  n1i = c(40, 50, 30, 45, 60, 35, 55, 48),
  m2i = c(1.8, 2.0, 1.7, 2.1, 1.9, 2.2, 2.0, 1.8),
  sd2i = c(1.0, 1.1, 1.0, 1.0, 1.1, 1.2, 1.0, 1.1),
  n2i = c(42, 55, 33, 48, 58, 37, 52, 50)
)

maive_from_metafor(smd)

## ----example-data-------------------------------------------------------------
# Simulated meta-analysis data: 50 estimates from 10 studies, with reported
# precision driven by sample size (as the first stage assumes)
set.seed(123)
n_estimates <- 50
Ns <- sample(100:1000, n_estimates, replace = TRUE)

data <- data.frame(
  bs = rnorm(n_estimates, mean = 0.3, sd = 0.2),
  sebs = 2 / sqrt(Ns) * runif(n_estimates, min = 0.8, max = 1.2),
  Ns = Ns,
  study_id = rep(1:10, each = 5)
)

head(data)

## ----default-maive------------------------------------------------------------
# Run MAIVE with defaults
result <- maive(
  dat = data,
  method = 3,      # PET-PEESE (default)
  weight = 0,      # No weights (default)
  instrument = 1,  # Instrument SEs (default)
  studylevel = 2,  # Cluster-robust (default)
  SE = 3,          # Wild bootstrap (default)
  AR = 1           # Anderson-Rubin CI (default)
)

# View key results
cat("MAIVE Estimate:", round(result$beta, 3), "\n")
cat("MAIVE SE:", round(result$SE, 3), "\n")
cat("Standard Estimate:", round(result$beta_standard, 3), "\n")
cat("Hausman Test:", round(result$Hausman, 3), "\n")
cat("First-stage F-test:", round(result$`F-test`, 3), "\n")

## ----pet, eval = FALSE--------------------------------------------------------
# result_pet <- maive(
#   dat = data,
#   method = 1,  # FAT-PET
#   weight = 0,
#   instrument = 1,
#   studylevel = 2,
#   SE = 3,
#   AR = 1
# )
# 
# cat("PET Estimate:", round(result_pet$beta, 3), "\n")

## ----peese, eval = FALSE------------------------------------------------------
# result_peese <- maive(
#   dat = data,
#   method = 2,  # PEESE
#   weight = 0,
#   instrument = 1,
#   studylevel = 2,
#   SE = 3,
#   AR = 1
# )
# 
# cat("PEESE Estimate:", round(result_peese$beta, 3), "\n")

## ----petpeese, eval = FALSE---------------------------------------------------
# result_petpeese <- maive(
#   dat = data,
#   method = 3,  # PET-PEESE (default)
#   weight = 0,
#   instrument = 1,
#   studylevel = 2,
#   SE = 3,
#   AR = 1
# )
# 
# cat("PET-PEESE Estimate:", round(result_petpeese$beta, 3), "\n")

## ----ek, eval = FALSE---------------------------------------------------------
# result_ek <- maive(
#   dat = data,
#   method = 4,  # EK
#   weight = 0,
#   instrument = 1,
#   studylevel = 2,
#   SE = 3,
#   AR = 0  # AR not available for EK
# )
# 
# cat("EK Estimate:", round(result_ek$beta, 3), "\n")

## ----no-weights, eval = FALSE-------------------------------------------------
# result_noweight <- maive(
#   dat = data,
#   method = 3,
#   weight = 0,  # No weights
#   instrument = 1,
#   studylevel = 2,
#   SE = 3,
#   AR = 1
# )

## ----iv-weights, eval = FALSE-------------------------------------------------
# result_ivweight <- maive(
#   dat = data,
#   method = 3,
#   weight = 1,  # Inverse-variance weights
#   instrument = 1,
#   studylevel = 2,
#   SE = 3,
#   AR = 0  # AR not available with weights
# )

## ----maive-weights, eval = FALSE----------------------------------------------
# result_maiveweight <- maive(
#   dat = data,
#   method = 3,
#   weight = 2,  # MAIVE-adjusted weights
#   instrument = 1,
#   studylevel = 2,
#   SE = 3,
#   AR = 1
# )

## ----studylevel, eval = FALSE-------------------------------------------------
# # No study-level adjustment
# result_none <- maive(data, method = 3, weight = 0, instrument = 1,
#                      studylevel = 0, SE = 0, AR = 1)
# 
# # Study fixed effects (demeaned)
# result_fe <- maive(data, method = 3, weight = 0, instrument = 1,
#                    studylevel = 1, SE = 1, AR = 0)  # AR not available with FE
# 
# # Cluster-robust standard errors
# result_cluster <- maive(data, method = 3, weight = 0, instrument = 1,
#                         studylevel = 2, SE = 3, AR = 1)
# 
# # Both fixed effects and clustering
# result_both <- maive(data, method = 3, weight = 0, instrument = 1,
#                      studylevel = 3, SE = 3, AR = 0)

## ----se-options, eval = FALSE-------------------------------------------------
# # CR0 (Huber-White)
# result_cr0 <- maive(data, method = 3, weight = 0, instrument = 1,
#                     studylevel = 2, SE = 0, AR = 1)
# 
# # CR1 (Standard empirical correction)
# result_cr1 <- maive(data, method = 3, weight = 0, instrument = 1,
#                     studylevel = 2, SE = 1, AR = 1)
# 
# # CR2 (Bias-reduced estimator)
# result_cr2 <- maive(data, method = 3, weight = 0, instrument = 1,
#                     studylevel = 2, SE = 2, AR = 1)
# 
# # Wild bootstrap (recommended, default)
# result_boot <- maive(data, method = 3, weight = 0, instrument = 1,
#                      studylevel = 2, SE = 3, AR = 1)

## ----first-stage-levels, eval = FALSE-----------------------------------------
# result_levels <- maive(data, method = 3, weight = 0, instrument = 1,
#                        studylevel = 2, SE = 3, AR = 1, first_stage = 0)
# 
# cat("First-stage (levels) F-test:", round(result_levels$`F-test`, 3), "\n")

## ----first-stage-log, eval = FALSE--------------------------------------------
# result_log <- maive(data, method = 3, weight = 0, instrument = 1,
#                     studylevel = 2, SE = 3, AR = 1, first_stage = 1)
# 
# cat("First-stage (log) F-test:", round(result_log$`F-test`, 3), "\n")

## ----waive, eval = FALSE------------------------------------------------------
# result_waive <- waive(
#   dat = data,
#   method = 3,
#   weight = 0,
#   instrument = 1,
#   studylevel = 2,
#   SE = 3,
#   AR = 1
# )
# 
# cat("WAIVE Estimate:", round(result_waive$beta, 3), "\n")
# cat("WAIVE SE:", round(result_waive$SE, 3), "\n")

