rtmb_irt: RTMB-based IRT (Item Response Theory) Wrapper

View source: R/wrapper_irt.R

rtmb_irtR Documentation

RTMB-based IRT (Item Response Theory) Wrapper

Description

Performs Item Response Theory modeling (1PL, 2PL, 3PL, and Graded Response Model) using RTMB. Missing values (NA) in the data are automatically removed internally for efficient computation.

Usage

rtmb_irt(
  data,
  model = c("2PL", "1PL", "3PL"),
  type = c("binary", "ordered"),
  prior = prior_flat(),
  init = NULL,
  fixed = NULL,
  view = NULL,
  missing = c("listwise", "fiml"),
  WAIC = FALSE
)

Arguments

data

A data frame or matrix of item responses (N persons x J items).

model

Character string for the model type: "1PL", "2PL", or "3PL".

type

Character string for the data type: "binary" or "ordered".

prior

Prior configuration: 'prior_flat()', 'prior_normal()', or 'prior_weak()'. Hyperparameters can be specified within these functions (e.g., 'prior_normal(b_sd = 5)'). Available parameters for IRT: 'a_rate' (discrimination; defaults to 1/2 with 'prior_normal()'), 'b_sd' (difficulty), and 'c_alpha'/'c_beta' (guessing).

init

List of initial values.

fixed

A named list of parameter values to fix (optional).

view

Character vector of parameter names to prioritize in summary.

missing

Missing value handling strategy: "listwise" (default) or "fiml" (Full Information Maximum Likelihood).

WAIC

Logical; if TRUE, add pointwise 'log_lik' to the generate block for WAIC.

Examples


  # --- 1. Binary Data (e.g., correct/incorrect answers) ---
  # Simulate binary response data for 100 persons and 5 items
  set.seed(123)
  bin_data <- matrix(rbinom(500, size = 1, prob = 0.6), nrow = 100, ncol = 5)
  colnames(bin_data) <- paste0("Item", 1:5)

  # Introduce some missing values (NA) to demonstrate automatic handling
  #bin_data[sample(1:500, 10)] <- NA

  # Fit a 2-Parameter Logistic (2PL) model
  fit_2pl <- rtmb_irt(data = bin_data, model = "2PL", type = "binary")

  # Maximum A Posteriori (MAP) estimation
  map_2pl <- fit_2pl$optimize()
  map_2pl$summary()

  # --- 2. Ordered Data (e.g., Likert scale) ---
  # Simulate ordered response data (categories 1 to 5) with a true latent structure
  set.seed(123)
  N <- 100
  J <- 5
  K <- 4

  # True parameters
  theta <- rnorm(N, 0, 1)          # Person abilities (latent traits)
  a <- runif(J, 0.8, 2.0)          # Item discriminations

  # Item-specific thresholds (must be strictly increasing)
  b <- matrix(NA, nrow = J, ncol = K - 1)
  for (j in 1:J) {
    b[j, ] <- sort(c(-1.5, 0, 1.5) + rnorm(3, 0, 0.2))
  }

  ord_data <- matrix(NA, nrow = N, ncol = J)
  colnames(ord_data) <- paste0("Item", 1:J)

  # Generate responses based on the Graded Response Model
  for (i in 1:N) {
    for (j in 1:J) {
      # Latent continuous response (eta + standard logistic noise)
      eta <- a[j] * theta[i]
      y_star <- eta + rlogis(1)

      # Apply thresholds to determine the observed category (1 to 4)
      category <- 1
      for (k in 1:(K - 1)) {
        if (y_star > b[j, k]) category <- k + 1
      }
      ord_data[i, j] <- category
    }
  }

  # Fit a Graded Response Model (2PL for ordered data)
  fit_ord <- rtmb_irt(data = ord_data, model = "2PL", type = "ordered")
  fit_ord$print_code()

  map_ord <- fit_ord$optimize()
  map_ord$summary()

BayesRTMB documentation built on Nov. 24, 2026, 9:06 a.m.