## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7,
                      fig.height = 5)
library(aiDIF)

## ----data---------------------------------------------------------------------
eg <- make_aidif_eg()
str(eg, max.level = 2)

## ----metric-common------------------------------------------------------------
fit_common <- fit_aidif(eg$human, eg$ai, metric = "common")

## ----metric-linked------------------------------------------------------------
fit_linked <- fit_aidif(eg$human, eg$ai, metric = "linked")
attr(fit_linked$scoring_bias, "offset")

## ----paired, eval = FALSE-----------------------------------------------------
# # With cross-condition covariance available:
# fit_paired <- fit_aidif(eg$human, eg$ai,
#                         design    = "paired",
#                         cross_cov = my_cross_cov)

## ----fit----------------------------------------------------------------------
mod <- fit_aidif(eg$human, eg$ai, metric = "linked", adjust = "BH")
print(mod)

## ----dif----------------------------------------------------------------------
round(mod$dif_human, 4)
round(mod$dif_ai, 4)

## ----dasb---------------------------------------------------------------------
mod$scoring_bias

## ----effect-------------------------------------------------------------------
ai_effect_summary(mod)

## ----anchors------------------------------------------------------------------
anchor_weights(mod)

## ----plot-forest, fig.alt = "Forest plot of item-level DIF estimates with confidence intervals under human and AI scoring."----
plot(mod, type = "dif_forest")

## ----plot-dasb, fig.alt = "Bar chart of DASB estimates per item with confidence intervals."----
plot(mod, type = "dasb")

## ----plot-weights, fig.alt = "Dot plot of bi-square anchor weights per item in each scoring condition."----
plot(mod, type = "weights")

## ----plot-rho, fig.alt = "Bi-square objective function against candidate location values, with the robust estimate marked."----
plot(mod, type = "rho")

## ----summary------------------------------------------------------------------
summary(mod)

## ----summary-object-----------------------------------------------------------
s <- summary(mod, adjust = "holm")
s$dasb$p_adj

## ----mirt, eval = requireNamespace("mirt", quietly = TRUE)--------------------
set.seed(2026)
n <- 600
J <- 8

a <- runif(J, 0.8, 1.6)
d <- rnorm(J, 0, 0.6)

sim_group <- function(n, a, d, mu = 0, d_shift = rep(0, length(d))) {
  th <- rnorm(n, mu, 1)
  p  <- t(vapply(th, function(t) plogis(a * t + d + d_shift), numeric(length(d))))
  matrix(rbinom(length(p), 1, p), nrow = n)
}

# Human scoring: item 1 has DIF in the focal group.
dif_h <- c(0.5, rep(0, J - 1))
# AI scoring adds a uniform +0.1 drift, plus DASB at item 3.
drift <- rep(0.1, J)
dasb  <- c(0, 0, 0.4, rep(0, J - 5), 0, 0)

human <- rbind(sim_group(n, a, d),
               sim_group(n, a, d + dif_h, mu = 0.5))
ai    <- rbind(sim_group(n, a, d + drift),
               sim_group(n, a, d + dif_h + drift + dasb, mu = 0.5))
grp   <- rep(c("reference", "focal"), each = n)

colnames(human) <- colnames(ai) <- paste0("item", seq_len(J))

# No invariance constraints: each group is calibrated on its own metric with
# the latent mean fixed at 0 and variance at 1. That is exactly the input
# robust scaling expects -- it estimates the linking constant itself, so
# constraining anchors here would pre-empt the method.
human_mod <- mirt::multipleGroup(as.data.frame(human), 1, group = grp,
                                 itemtype = "2PL", SE = TRUE, verbose = FALSE)
ai_mod    <- mirt::multipleGroup(as.data.frame(ai), 1, group = grp,
                                 itemtype = "2PL", SE = TRUE, verbose = FALSE)

human_in <- as_aidif(human_mod, groups = c("reference", "focal"))
ai_in    <- as_aidif(ai_mod,    groups = c("reference", "focal"))

fit_mirt <- fit_aidif(human_in, ai_in, metric = "linked", adjust = "BH")
print(fit_mirt)
fit_mirt$scoring_bias

## ----sim----------------------------------------------------------------------
dat <- simulate_aidif_data(n_items = 8, n_obs = 600,
                           dif_items = c(1, 2), dif_mag = 0.5,
                           dasb_items = 5, dasb_mag = 0.4, seed = 123)
sim_mod <- fit_aidif(dat$human, dat$ai, metric = "linked", adjust = "BH")
print(sim_mod)

