An Introduction to Item Response Theory (IRT) Models

Estimation of Bayesian 2PL models using R-INLA

Haziq Jamil

Research Specialist
BAYESCOMP @ CEMSE-KAUST

https://haziqj.ml/uinmataram-apr26 | 📥 PDF

April 23, 2026

Bayesian Computational Statistics and Modeling Research Group (BAYESCOMP)

Led by Prof. Håvard Rue

  • Rue et al. (2009). Approximate Bayesian Inference for Latent Gaussian Models by Using Integrated Nested Laplace Approximations. J. R. Stat. Soc., B: Stat. Methodol., 71(2).
  • Krainski et al. (2018). Advanced spatial modeling with stochastic partial differential equations using R and INLA. Chapman and Hall/CRC.
  • van Niekerk et al. (2023). A new avenue for Bayesian inference with INLA. Comput. Stat. Data Anal., 181, 107692.
  • Abdul Fattah et al. (2025). sTiles: An accelerated computational framework for sparse factorizations of structured matrices. ISC High Performance 2025 Research Paper Proceedings.
  • Gaedke-Merzhäuser et al. (2023). Parallelized integrated nested Laplace approximations for fast Bayesian inference. Statistics and Computing, 33(1), 25.

My research interests

Statistical methodology and computation

  • Latent variable models
  • Spatio-temporal models
  • Bayesian statistics
  • Integrated nested Laplace approximations
    (INLA)

My research interests

Statistical methodology and computation

  • \(\,\)Latent variable models\(\,\)
  • Spatio-temporal models
  • Bayesian statistics
  • Integrated nested Laplace approximations
    (INLA)

Credit: NCRM Resource Repository

(Psychology)
Personality traits

(Healthcare)
Quality of life

(Political science)
Social trust

(Education)
Competencies

Example: Inventory of Interpersonal Problems (IIP)

  • Behavioural Circumplex A way to quantify interpersonal behaviour patterns (similar spirit to Big Five, but focused on interaction style).

  • Predict interpersonal dynamics (complementarity). Expected interpersonal response patterns e.g., dominance \(\xrightarrow{}\) submission; hostility \(\xrightarrow{}\) hostility.

Current research

  • IIP circumplex modelling yields a 256-parameter factor model. Standard Bayesian MCMC is too slow for this scale!

  • We developed an INLA-based approximate Bayesian inference strategy for structural equation models (SEMs) and latent variable models more generally.

Advancing scalable Bayesian methods for latent variable modelling

  • Jamil, H., & Rue, H. (2026a). Approximate Bayesian inference for structural equation models using integrated nested Laplace approximations (2603.25690 [stat.ME]). arXiv.
  • Jamil, H., & Rue, H. (2026b). Implementation and workflows for INLA-based approximate Bayesian structural equation modelling (2604.00671 [stat.CO]). arXiv.
  • R/INLAvaan package available on CRAN and GitHub.
  • Awarded USD 900,000 KAUST Competetive Research Grant 2026-2029.

🗺️ Roadmap

Part I — Foundations 🏗️

  1. What is IRT?   Motivation, real-world use cases (PISA), and the logic of testing
  2. IRT Model Family   1PL → 4PL: difficulty, discrimination, guessing & slipping
  3. A Short History   From CTT & Rasch to modern adaptive testing

Part II — Estimation 📐

  1. Maximum Likelihood   JML, MML, and the {ltm} package in R
  2. Bayesian Estimation   Priors, posteriors, and why Bayesian helps with small samples

Part III — INLA for IRT 🚀

  1. Why INLA?   From MCMC to deterministic approximation
  2. IRT 2PL with INLA   model specification, fitting, and results
  3. Scalability & Frontiers   ENEM dataset (2.7M students), DIF, and spatial models

Item Response Theory (IRT) models

What is IRT?

Item Response Theory (IRT) is a statistical framework for modelling the relationship between:

  • A person’s latent ability or trait (unobserved)

  • Their responses to test questions (observed)

Key Idea:
People with higher ability are more likely to answer difficult questions correctly—and we can model this in a mathematical way.

Where and how is IRT used?

  • Educational Testing:
    Design, calibrate, and score standardized exams (e.g., PISA).

  • Psychometrics:
    Measure latent traits like anxiety, personality, or satisfaction.

  • Fairness & Comparability:
    Place people and items on the same scale, compare across years, languages, or groups.

  • Beyond Testing:
    Applied in marketing, health outcomes research, HR assessments, and more.

Source: AI generated image.

Questions of interest

  1. How difficult is each test item?
  2. How well does each item discriminate between students of different ability levels?
  3. Can I accurately estimate students’ abilities?

Program for International Student Assessment (PISA)

Organization for Economic Co-operation and Development (OECD)

Source: Instagram.
  • International assessment that measures 15 year-old students’ reading, mathematics, and science literacy.

  • PISA makes use of IRT models for

    • Scoring students: Estimate students’ abilities (latent traits).
    • Item calibration: Ensure items are appropriately challenging and can effectively differentiate students.
    • Reporting outcomes: Country and trends analyses (e.g. over time).
    • Diagnostic information: Identify strengths and weakness in specific areas.

Data

For student \(s=1,\dots,n\) consider binary responses for item \(y_i\), \(i=1,\dots,p\).

Subject: Mathematics

\(s\) \(y_1\) \(y_2\) \(y_3\) \(y_4\) \(y_5\)
1 1 0 0 1 1
2 1 1 1 1 1
3 1 1 1 0 1
\(\vdots\)
\(n\) 1 0 0 1 1

How to “convert” this data to meaningful scores and item information?

Toy example

Student Item1 Item2 Item3 Item4 Item5
1 1 1 1 1 1
2 0 1 1 1 1
3 1 1 0 1 1
4 1 1 1 1 0
5 1 1 1 1 0
6 0 0 1 1 0
7 1 0 0 0 0
8 0 0 0 1 0
9 1 0 0 0 0
10 0 0 0 0 0

Scores and difficulties

Student Item1 Item2 Item3 Item4 Item5 Score
1 1 1 1 1 1 5
2 0 1 1 1 1 4
3 1 1 0 1 1 4
4 1 1 1 1 0 4
5 1 1 1 1 0 4
6 0 0 1 1 0 2
7 1 0 0 0 0 1
8 0 0 0 1 0 1
9 1 0 0 0 0 1
10 0 0 0 0 0 0
Difficulty 4 5 5 3 7

Discrimination

Student Item1 Item2 Item3 Item4 Item5 Score
1 1 1 1 1 1 5
2 0 1 1 1 1 4
3 1 1 0 1 1 4
4 1 1 1 1 0 4
5 1 1 1 1 0 4
Difficulty 1 0 1 0 2
6 0 0 1 1 0 2
7 1 0 0 0 0 1
8 0 0 0 1 0 1
9 1 0 0 0 0 1
10 0 0 0 0 0 0
Difficulty 3 5 4 3 5

The logic of (educational) testing

Assumptions

  1. Students have an underlying latent ability \(\theta\) that we want to measure but cannot observe.
  2. Items have fixed properties that interact with \(\theta\) to determine \(\Pr(\text{correct})\).

A “good test item” should have the following properties:

  • Monotonicity: \(\theta \uparrow \, \Rightarrow \Pr(\text{correct}) \uparrow\)

  • Appropriate difficulty: Ideally match target population

    • Too easy \(\Rightarrow\) everyone correct \(\Rightarrow\) learn nothing
    • Too hard \(\Rightarrow\) everyone wrong \(\Rightarrow\) learn nothing
  • High discrimination: Steep “success curve” around item difficulty

    • Distinguish low-ability from high-ability students
    • Ensures the response is driven by ability, not by guessing or ambiguous wording

Mathematical representation: The IRT model

  • Let \(y_{si} \in \{0,1\}\) represent the binary response of a subject \(s\). Assume \[ y_{si} = \begin{cases} 1 \text{ (correct)} & \text{w.p. } \pi_{si} \\ 0 \text{ (wrong)} & \text{w.p. } 1-\pi_{si} \end{cases} \]
  • Model the probability of obtaining a correct answer using the two-parameter logistic (2PL) model defined by: \[ \pi_{si}(\boldsymbol{\theta}, \mathbf a, \mathbf b) := \Pr(y_{si} = 1 \mid \boldsymbol{\theta}, \mathbf a, \mathbf b) = \frac{1}{1 + e^{-\eta_{si}} } \in [0,1], \] where the linear predictor term is given by: \[ \eta_{si} = a_i(\theta_s - b_i) \in \mathbb R \] and

    • \(\boldsymbol\theta = (\theta_1,\dots,\theta_n)^\top\) are the latent traits of the subjects
    • \(\mathbf b = (b_1,\dots,b_p)^\top\) are the item difficulty parameters \(b_i\) (location)
    • \(\mathbf a = (a_1,\dots,a_p)^\top\) are the item discrimination parameters \(a_i\) (scale)

Aside: The logistic function

The logistic function \(f:\mathbb{R} \to (0,1)\) is defined by \(f(x)=\frac{1}{1+e^{-x}}\).

Effect of item parameters on response probabilities

Summary of 2PL parameterisation

  • Monotonicity: Provided by the logistic function, i.e. \(\theta \uparrow \, \Rightarrow \Pr(\text{correct}) \uparrow\).
  • Difficulty: \(b\) controls the location of the curve along the ability axis \(\theta\).
    • \(\uparrow b \, \Rightarrow\) more difficult item (curve shifts right). Larger \(\theta\) needed for correct probability.
    • \(\downarrow b \, \Rightarrow\) easier item (curve shifts left). Smaller \(\theta\) needed for correct probability.
    • Good items have a range of difficulties that match the target population’s ability distribution.
  • Discrimination: \(a\) controls the steepness or slope of the curve around the difficulty point.
    • \(\uparrow a \, \Rightarrow\) stronger discrimination around item difficulty.
    • \(\downarrow a \, \Rightarrow\) weaker discrimination around item difficulty.
    • Good items separate low- and high-ability learners more efficiently.

IRT in practice

Source: AI generated image.
  • Knowing item parameters allows better tests design and accurate scoring.

  • Item Anchoring: Use well-calibrated items to link different test forms or administrations, ensuring comparability over time and across groups.

  • Computerized Adaptive Testing (CAT): Select items in real time based on a test-taker’s estimated ability level, improving efficiency and precision.

Family of IRT models

  • The 2PL IRT model is a special case of the wider class of IRT models


\[ \pi_{si}(\theta) = c_i + \frac{d_i-c_i}{1+e^{-a_i(\theta_s - b_i)}} \]

Parameter 1PL 2PL 3PL 4PL
Difficulty (\(b_i\)) ✓ ✓ ✓ ✓
Discrimination (\(a_i\)) 1 ✓ ✓ ✓
Guessing (\(c_i\)) 0 0 ✓ ✓
Slipping (\(d_i\)) 1 1 1 ✓

Family of IRT models (cont.)

A brief history

  • Classical test theory (CTT), \(Y = T + e\), focused on total scores and reliability.
    • Consider: Ahmad and Siti scored 5/10 for their test.
    • Ahmad answered all easy items correctly; Siti all hard items correctly.
    • Do they really have the same ability?
  • Early 20th century: Spearman’s \(g\)-factor and factor analysis sparked the shift from classical test theory (CTT) to latent trait thinking. See Veritasium.

  • Mid-20th century: Georg Rasch developed a probabilistic item-response framework (related to the 1PL, but it has unique invariance properties).

  • 1970s–1990s: The 2PL, 3PL, and 4PL models generalized Rasch’s approach, enabling discrimination, guessing, and slipping parameters. Foundations for SAT, GRE, PISA.

  • Today: Computer-based adaptive testing uses IRT to route items in real time. Duolingo, Khan Academy, etc. now personalise learning at scale.

Estimation

via Maximum Likelihood

Aside: Bernoulli distribution

  • Suppose we are interested in the outcome \(X\) of a single random trial, which can either be “success” (\(X=1\)) or “failure” (\(X=0\)) only. Examples include

    • A coin flip can land either Heads or Tails.
    • A dice roll outcome can either be an Even or an Odd number.
    • Babies being born being Girl or Boy.
    • Answer to a test item can be either Correct or Wrong.
  • Let the probability mass function (pmf) for \(X\) be \[ f(x; \pi) = \pi^x(1-\pi)^{1-x} = \begin{cases} \pi & x=1\text{ (``success'') }\\ 1-\pi &x=0 \text{ (``failure'')} \end{cases} \]

  • We say that \(X\) has a Bernoulli distribution written \(X\sim\operatorname{Bern}(p)\).

    • \(\operatorname{E}(X) = p\)
    • \(\operatorname{Var}(X)= p(1-p)\)

Aside: Maximum likelihood estimation

  • Context: Assume available \(X_1,\dots,X_n\sim f(x;\vartheta)\) (iid samples) but \(\vartheta\) is unknown. How to estimate it?

  • Define the likelihood function \(L(\vartheta) = \prod_{i=1}^n f(X_i; \vartheta)\).

  • The value \(\hat\vartheta = \arg\max_\vartheta L(\vartheta)\) is called the maximum likelihood estimator (MLE) of \(\vartheta\).

IRT: What needs to be estimated?

  • Given data \(\mathbf Y = \{y_{si}\}\), for the 2PL model we need to estimate:
    1. Item difficulties \(b_1,\dots,b_p\);
    2. Item discriminations \(a_1,\dots,a_p\);
    3. Latent abilities \(\theta_1,\dots,\theta_n\).
  • Construct the joint likelihood function based on the Bernoulli model for \(y_{si}\): \[ L(\boldsymbol\theta, \mathbf a, \mathbf b) = \prod_{s=1}^n\prod_{i=1}^p \pi_{si}^{y_{si}}(1-\pi_{si})^{1-y_{si}} \] where, as before, \[ \pi_{si} = \frac{1}{1 + e^{-a_i(\theta_s - b_i)}}. \]
  • Treating everything as unknown, we can maximize \(L(\boldsymbol\theta, \mathbf a, \mathbf b)\) to get estimates of all parameters. This is known as joint maximum likelihood (JML).

Marginal Maximum Likelihood (MML)

  • JML can be computationally challenging, with bias and inconsistency for item parameters as \(n \to \infty\) with fixed \(p\).
Proposal:

\(\hspace{3cm}\)Abilities follow a bell-shaped curve, \(\theta_s \sim \operatorname{N}(0,1)\) say.

  • We can then integrate out1 the latents, like so: \[ L(\mathbf a, \mathbf b) = \prod_{s=1}^n \int \Bigg\{ \prod_{i=1}^p \pi_{si}(\theta_s; a_i, b_i)^{y_{si}}(1-\pi_{si}(\theta_s; a_i, b_i))^{1-y_{si}} \,\phi(\theta_s)\,d\theta_s \Bigg\} \]

  • Here, the bias is \(O(n^{-1})\) and thus disappears the more data we have. It is also generally less biased than JML.

  • MML is also more robust to sample size variations and provides more stable item parameter estimates.

ML in Practice: The {ltm} package

Data set

  • The Law School Admission Test (LSAT), Section VI. A classical example in education testing for measuring ability traits.
library(ltm)
dim(LSAT)
[1] 1000    5
head(LSAT)
  Item 1 Item 2 Item 3 Item 4 Item 5
1      0      0      0      0      0
2      0      0      0      0      0
3      0      0      0      0      0
4      0      0      0      0      1
5      0      0      0      0      1
6      0      0      0      0      1
apply(LSAT, 2, mean)  # proportion correct (item 'easiness')
Item 1 Item 2 Item 3 Item 4 Item 5 
 0.924  0.709  0.553  0.763  0.870 

ML in Practice: The {ltm} package

Model fit

fit <- ltm(LSAT ~ z1, IRT.param = TRUE)
print(fit)

Call:
ltm(formula = LSAT ~ z1, IRT.param = TRUE)

Coefficients:
        Dffclt  Dscrmn
Item 1  -3.360   0.825
Item 2  -1.370   0.723
Item 3  -0.280   0.890
Item 4  -1.866   0.689
Item 5  -3.124   0.657

Log.Lik: -2466.653

ML in Practice: The {ltm} package

Plot Item Characteristic Curves (ICC)

plot(fit)

ML in Practice: The {ltm} package

Student abilities

abilities <- factor.scores(fit)
print(abilities)

Call:
ltm(formula = LSAT ~ z1, IRT.param = TRUE)

Scoring Method: Empirical Bayes

Factor-Scores for observed response patterns:
   Item 1 Item 2 Item 3 Item 4 Item 5 Obs     Exp     z1 se.z1
1       1      1      1      1      1 298 296.693  0.606 0.855
2       1      1      1      1      0  28  29.127  0.139 0.833
3       1      1      1      0      1  61  62.520  0.117 0.832
4       1      0      1      1      1  80  83.541  0.093 0.831
5       0      1      1      1      1  15  13.920  0.023 0.828
6       1      1      0      1      1 173 173.310 -0.022 0.827
7       1      1      1      0      0  11   8.445 -0.329 0.816
8       1      0      1      1      0  15  11.463 -0.352 0.815
9       1      0      1      0      1  28  24.989 -0.373 0.815
10      0      1      1      1      0   2   2.000 -0.420 0.813
11      0      1      1      0      1   3   4.370 -0.441 0.813
12      1      1      0      1      0  21  25.646 -0.463 0.812
13      0      0      1      1      1   4   5.955 -0.463 0.812
14      1      1      0      0      1  56  56.105 -0.483 0.812
15      1      0      0      1      1  81  76.562 -0.506 0.811
16      0      1      0      1      1  16  13.577 -0.573 0.809
17      1      0      1      0      0   3   4.659 -0.803 0.804
18      0      0      1      1      0   3   1.179 -0.891 0.803
19      1      1      0      0      0  16  11.254 -0.911 0.802
20      0      0      1      0      1   1   2.614 -0.911 0.802
21      1      0      0      1      0  14  15.590 -0.933 0.802
22      1      0      0      0      1  29  34.616 -0.953 0.802
23      0      1      0      0      1   8   6.431 -1.019 0.801
24      0      0      0      1      1  11   8.942 -1.041 0.800
25      0      0      1      0      0   1   0.696 -1.331 0.797
26      1      0      0      0      0  10   9.480 -1.373 0.797
27      0      1      0      0      0   1   1.840 -1.438 0.796
28      0      0      0      1      0   2   2.596 -1.460 0.796
29      0      0      0      0      1   6   5.861 -1.479 0.796
30      0      0      0      0      0   3   2.277 -1.895 0.795

ML in Practice: The {ltm} package

Distribution of student abilities

plot(abilities)

Bayesian Estimation

Bayesian inference basics

Bayesian inference basics (cont.)

Bayes Theorem

\[ \text{Posterior} =: p(\vartheta \mid y) = \frac{p(y \mid \vartheta)\,p(\vartheta)}{p(y)} = \frac{\text{Likelihood} \times \text{Prior}}{\text{Evidence}} \]

Why Bayesian?

Bayesian inference blends data with expert belief.

  • A modeller might say, “I believe this item is fairly difficult” (so \(b_i\) is probably above 0), or “this item should not be extremely discriminating” (so \(a_i\) is likely positive but not huge).
  • Priors gently pull estimates away from unrealistic extremes when data are weak/noisy.
    • Example: with very few responses, ML might estimate a very large \(a_i\) (too steep ICC).
    • A sensible prior shrinks that toward plausible values, improving stability and interpretation.
  • Why this helps in practice:
    • More stable estimates with sparse or small samples
    • Natural uncertainty intervals (credible intervals)
    • Clean extension to hierarchical and spatial IRT models

ML with small samples: a warning

  • We’ll see later how Bayesian methods can help mitigate this scenario.

ML vs Bayesian (Quick Comparison)

Aspect ML (JML/MML) Bayesian
Point estimates Direct optimization Posterior means/medians
Uncertainty Asymptotic SEs Credible intervals
Priors Not used Required/optional
Small samples Can be unstable Often more stable with priors
Complex hierarchy Harder Natural framework
  • Although Bayesian methods are preferable, Bayesian methods can be computationally intensive, especially for large datasets or complex models. This is where INLA comes in.

  • Why? The “normalising constant” \(p(y) = \int p(y \mid \vartheta)p(\vartheta)d\vartheta\) is often intractable, especially with high-dimensional parameters.

IRT with INLA

Markov chain Monte Carlo (MCMC)

Why INLA?

  • MCMC is the gold standard for Bayesian inference but can be slow to converge, especially with many parameters (e.g., large \(n\) and \(p\) in IRT).

  • INLA is a deterministic approximation strategy for Bayesian inference in latent Gaussian models.

  • It is attractive when:

    • MCMC is too slow
    • data are large
    • spatial or structured random effects are needed

Latent Gaussian Models and INLA

The Ingredients 🥕🍅🧅🧄

\[ \begin{align} y \mid \mathcal X, \vartheta_1 &\sim \prod_{i=1}^n \pi(y_i \mid \mathcal X, \vartheta_1) \\ \mathcal X \mid \vartheta_2 &\sim \operatorname{N}(0, Q(\vartheta_2)^{-1}) \\ \vartheta := \{\vartheta_1, \vartheta_2\} &\sim \pi(\vartheta) \end{align} \]

Hierarchical Latent Gaussian Models (LGMs), encompassing

  • Generalized linear mixed models
  • Generalized additive models
  • Spatial/spatio-temporal models
  • Survival models
  • etc.

The Recipe 🧑‍🍳 (avail. in R/INLA)

  1. Form the latent field \(\mathcal X\)
  2. Mode finding for Laplace approx. \[\pi(\mathcal X \mid \vartheta,y)\approx \operatorname{N}(\mu(\vartheta), P(\vartheta)^{-1})\]
  3. Conditional posterior refinement
    • Classic: Laplace approx. \(x_i \in \mathcal X\)
    • Modern: VB mean correction
  4. Integrate over \(\vartheta\) to get marginals \(\pi(x_i \mid y)\) and \(\pi(\vartheta_j \mid y)\)
  5. (Modern) Post hoc formation of \(\eta=A\mathcal X\)

Simple INLA Demo

Installing INLA

To install INLA, run the following:

install.packages(
  pkgs = "INLA", 
  repos = c(getOption("repos"), 
            INLA = "https://inla.r-inla-download.org/R/stable"),     
  dep = TRUE
)
library(INLA)  # load the library

Generate some data from \(y_i = \beta_0 + \beta_1 x_i + \epsilon_i\):

n <- 200
x <- rnorm(n)
y <- 2 + 1.5 * x + rnorm(n, sd = 0.8)
dat <- data.frame(x = x, y = y)

Simple INLA Demo

Data preview

ggplot(dat, aes(x, y)) +
  geom_point() +
  geom_smooth(
    method = "lm", 
    se = FALSE
  )

Given these data points, can we
recover \(\beta_0\) (intercept) and \(\beta_1\) (slope)?

Simple INLA Demo

Linear Regression

fit <- inla(
  formula = y ~ 1 + x,
  data = dat,
  family = "gaussian"
)
summary(fit)
Time used:
    Pre = 0.451, Running = 0.138, Post = 0.00657, Total = 0.596 
Fixed effects:
             mean    sd 0.025quant 0.5quant 0.975quant  mode kld
(Intercept) 1.904 0.058      1.790    1.904      2.018 1.904   0
x           1.496 0.056      1.386    1.496      1.606 1.496   0

Model hyperparameters:
                                        mean    sd 0.025quant 0.5quant
Precision for the Gaussian observations 1.49 0.148       1.21     1.48
                                        0.975quant mode
Precision for the Gaussian observations       1.79 1.47

Marginal log-Likelihood:  -263.40 
 is computed 
Posterior summaries for the linear predictor and the fitted values are computed
(Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')

IRT as an INLA-Friendly Model

  • The good news is that IRT falls under the category of latent Gaussian models, so we can use INLA for estimation.

  • Recall the model hierarchy:

Layer Type R-INLA component
\(y_{si}\sim \text{Bernoulli}(\pi_{si})\) Likelihood family = "binomial"
\(\text{logit}(\pi_{si}) = \eta_{si}\) Link function control.family = list(link = "logit")
\(\eta_{si} = a_i(\theta_s - b_i)\) Linear predictor formula = y ~ .
\(\theta_s \sim \operatorname{N}(0,1)\) Latent field f(model = "iid")
\(a_i\), \(b_i\) “fixed effects”
  • For our purpose, specify the following priors:
    • \(b_i \sim \operatorname{N}(0, 3^2)\) (weakly informative)
    • \(a_i \sim \operatorname{N}(1, 0.5^2)\) (mode at 1, not too wide)

IRT 2PL using INLA

Long format data

dim(LSAT)
[1] 1000    5
head(LSAT)
  Item 1 Item 2 Item 3 Item 4 Item 5
1      0      0      0      0      0
2      0      0      0      0      0
3      0      0      0      0      0
4      0      0      0      0      1
5      0      0      0      0      1
6      0      0      0      0      1
dat_long <-
  as_tibble(LSAT) |>
  mutate(student = row_number()) |>
  pivot_longer(-student, names_to = "idx_a", values_to = "y") |>
  mutate(
    minus1 = -1,
    idx_a = as.integer(as.factor(idx_a)),
    idx_b = idx_a
  )
print(dat_long)
# A tibble: 5,000 × 5
   student idx_a     y minus1 idx_b
     <int> <int> <int>  <dbl> <int>
 1       1     1     0     -1     1
 2       1     2     0     -1     2
 3       1     3     0     -1     3
 4       1     4     0     -1     4
 5       1     5     0     -1     5
 6       2     1     0     -1     1
 7       2     2     0     -1     2
 8       2     3     0     -1     3
 9       2     4     0     -1     4
10       2     5     0     -1     5
# ℹ 4,990 more rows

Aside: pivot_longer()

Source: GitHub
gadenbuie/tidyexplain

IRT 2PL using INLA

The linear predictor

\[ \eta_{si} = {\color{gray}a_i}(\theta_s + -1 \times b_i) \]

form <- y ~ -1 +
  # Add the student abilities random effect
  f(
    student,
    model = "iid",
    hyper = list(prec = list(initial = log(1), fixed = TRUE)) # θ ~ N(0, 1)
  ) +
  # Add item difficulties, treated as random effect with fixed precision
  f(
    item,
    minus1, # to multiply by -1 for the difficulty parameter
    model = "iid",
    hyper = list(prec = list(initial = -log(3), fixed = TRUE)) # b_i ~ N(0, 3)
  )

IRT 2PL using INLA

Discrimination parameters

\[ {\color{gray}\eta_{si} = } \, a_i{\color{gray}(\theta_s -b_i)} \]

apriors <- vector("list", length = p)
for (k in seq_along(apriors)) {
  apriors[[k]] <- list(
    prior = "normal",
    param = c(1, 1 / 0.3^2), # a_i ~ N(1, 0.3^2)
    initial = 1
  )
}
names(apriors) <- paste0("theta", seq_len(p))

Note: Undocumented feature!!!

IRT 2PL using INLA

Model fit

fit_inla <- inla(
  formula  = form,
  data     = dat_long,
  family   = "binomial",
  lp.scale = idx_a,
  control.lp.scale = list(hyper = apriors)
)
print(fit_inla)
Time used:
  Pre = 0.577, Running = 0.259, Post = 0.0163, Total = 0.852

Note: Undocumented feature!!!

IRT 2PL using INLA

Summary and comparison to {ltm}

print(tab)  # see the R script
       INLA_Disc ltm_Disc INLA_Diff ltm_Diff
Item 1    0.9099   0.8254   -3.1842  -3.3597
Item 2    0.6981   0.7229   -1.4376  -1.3696
Item 3    0.7144   0.8905   -0.3341  -0.2799
Item 4    0.7070   0.6886   -1.8655  -1.8659
Item 5    0.7816   0.6575   -2.7621  -3.1236
print(fit_inla$summary.random$item, digits = 3)  # difficulties
      ID   mean    sd 0.025quant 0.5quant 0.975quant   mode      kld
1 Item 1 -3.184 0.486     -4.283   -3.124     -2.430 -3.087 2.48e-05
2 Item 2 -1.438 0.265     -2.084   -1.395     -1.039 -1.375 9.91e-06
3 Item 3 -0.334 0.114     -0.586   -0.326     -0.133 -0.309 1.01e-06
4 Item 4 -1.866 0.331     -2.693   -1.812     -1.369 -1.790 1.31e-05
5 Item 5 -2.762 0.460     -3.910   -2.697     -2.064 -2.669 1.62e-05
print(fit_inla$summary.hyperpar, digits = 3)     # discrimination
                      mean    sd 0.025quant 0.5quant 0.975quant  mode
beta[1] for lp_scale 0.910 0.162      0.598    0.908      1.235 0.898
beta[2] for lp_scale 0.698 0.129      0.444    0.698      0.951 0.699
beta[3] for lp_scale 0.714 0.126      0.467    0.714      0.963 0.713
beta[4] for lp_scale 0.707 0.132      0.449    0.707      0.967 0.705
beta[5] for lp_scale 0.782 0.144      0.501    0.781      1.068 0.777

IRT 2PL using INLA

Distribution of student abilities

MCMC using {brms}

tictoc::tic()
fit_brms <- brm(
  bf(y ~ 0 + item + (1 | student)),
  data = dat_long,
  family = bernoulli(),
  prior = prior(normal(0, 1), class = b) +
    prior(normal(0, 0.5), class = sd, group = student),
  chains = 4,
  iter = 2000,
  cores = 4
)
tictoc::toc()
30.235 sec elapsed

INLA able to reduce time to inference from 30 seconds to 1 second for this dataset!

Why this is promising for education data

ENEM data set (Brazil)

  • \(p=45\) items across 5 subjects (Mathematics, Languages and Codes, Human Sciences, Natural Sciences, and Writing)
  • \(n\approx 2.7M\) students across 4 years (2018-2022)
  • Additional information:
    • demographic and student-specific
    • school-level data
  • Complex dynamic spatial multidimensional IRT with latent regressions

Why this is promising for education data

Differential Item Functioning (DIF)

  • An item has DIF if learners with equal latent ability have different probabilities of success across groups. Useful for investigating inequity in test items.

Wrap up

Key messages

  • 📊 Beyond total scores: IRT reveals item difficulty & discrimination, and estimates individual abilities — richer insight than raw sums.

  • 🧩 Model flexibility: 1PL → 4PL models let you balance simplicity against realism (guessing, slipping, varying discrimination).

  • ⚖️ Estimation choices: Maximum likelihood is fast and familiar; Bayesian inference adds stability with sparse data and natural uncertainty quantification.

  • 🚀 INLA enables scalability: Deterministic Bayesian inference makes complex, structured IRT models feasible — orders of magnitude faster than MCMC.

  • 🌍 Fairness matters: Differential Item Functioning (DIF) and spatial DIF detection help ensure tests are equitable across groups and regions.

  • 🔮 Future frontiers: Spatio-temporal, multidimensional, and hierarchical IRT models open new doors for large-scale educational assessment.

شكراً جزيلاً

https://haziqj.ml

References

Rue, H., Martino, S., & Chopin, N. (2009). Approximate Bayesian inference for latent Gaussian models by using integrated nested Laplace approximations. Journal of the Royal Statistical Society Series B: Statistical Methodology, 71(2), 319–392. https://doi.org/10.1111/j.1467-9868.2008.00700.x
van Niekerk, J., Krainski, E., Rustand, D., & Rue, H. (2023). A new avenue for Bayesian inference with INLA. Computational Statistics & Data Analysis, 181, 107692. https://doi.org/10.1016/j.csda.2023.107692