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

## ----load---------------------------------------------------------------------
library(irtsim)

## ----path-a1-mirt-------------------------------------------------------------
prior_data <- mirt::expand.table(mirt::LSAT7)
prior_fit  <- mirt::mirt(prior_data, 1, "2PL", verbose = FALSE)

co <- mirt::coef(prior_fit, IRTpars = TRUE, simplify = TRUE)$items
co

design_from_fit <- irt_design(
  model       = "2PL",
  n_items     = nrow(co),
  item_params = list(a = co[, "a"], b = co[, "b"])
)
design_from_fit

## ----path-a1-convert----------------------------------------------------------
co_si <- mirt::coef(prior_fit, IRTpars = FALSE, simplify = TRUE)$items
a_vec <- co_si[, "a1"]
b_vec <- -co_si[, "d"] / a_vec
all.equal(b_vec, co[, "b"])  # same as IRTpars=TRUE path

## ----path-a2-csv--------------------------------------------------------------
# Imagine this CSV came from a prior calibration report.
csv_text <- "
item,a,b
i01,1.05,-1.80
i02,0.92,-0.95
i03,1.18,-0.20
i04,1.40, 0.45
i05,0.88, 1.15
i06,1.22, 1.95
"
params_df <- read.csv(text = csv_text, strip.white = TRUE)

design_from_csv <- irt_design(
  model       = "2PL",
  n_items     = nrow(params_df),
  item_params = list(a = params_df$a, b = params_df$b)
)
design_from_csv

## ----path-b-cognitive---------------------------------------------------------
# Cognitive ability — high discriminations, broad difficulty range
ip_cog <- irt_params_2pl(
  n_items = 20,
  a_mean  = 0.20, a_sd  = 0.30,   # log-normal: median a ~ 1.22
  b_mean  = 0,    b_sd  = 1.20,
  seed    = 1
)
summary(ip_cog$a); summary(ip_cog$b)

## ----path-b-personality-------------------------------------------------------
# Personality — moderate discriminations, broader trait coverage
ip_pers <- irt_params_2pl(
  n_items = 20,
  a_mean  = -0.20, a_sd = 0.30,   # log-normal: median a ~ 0.82
  b_mean  = 0,     b_sd = 1.50,
  seed    = 1
)
summary(ip_pers$a); summary(ip_pers$b)

## ----path-b-clinical----------------------------------------------------------
# Clinical (PROMIS-style GRM, 5 categories) — high discriminations,
# trait-spanning thresholds
ip_clin <- irt_params_grm(
  n_items      = 20,
  n_categories = 5,
  a_mean       = 0.10, a_sd = 0.30,   # median a ~ 1.10
  b_mean       = 0,    b_sd = 1.20,
  seed         = 1
)
summary(ip_clin$a)

## ----path-b-achievement-------------------------------------------------------
# Achievement / large-scale educational — moderate discriminations,
# difficulty distribution centered on the cut score (here 0)
ip_ach <- irt_params_2pl(
  n_items = 20,
  a_mean  = 0,    a_sd  = 0.25,   # median a = 1.00
  b_mean  = 0,    b_sd  = 1.00,
  seed    = 1
)
summary(ip_ach$a); summary(ip_ach$b)

## ----path-b-design------------------------------------------------------------
design_cog <- irt_design(model = "2PL", n_items = 20, item_params = ip_cog)
design_cog

## ----path-c-------------------------------------------------------------------
ip_screener <- irt_params_2pl(
  n_items = 12,
  a_mean  = 0,    a_sd  = 0.20,
  b_dist  = "even", b_range = c(-0.5, 2),
  seed    = 1
)
ip_screener

design_screener <- irt_design(
  model       = "2PL",
  n_items     = 12,
  item_params = ip_screener
)
design_screener

