{ "cells": [ { "cell_type": "markdown", "id": "f2b31111", "metadata": {}, "source": [ "# Detect differential item functioning with TIRT\n", "\n", "**Experimental.** The DIF API and defaults may change.\n", "\n", "A group can differ in average ability, in how a comparison works at the same\n", "ability, or both. In this example some comparison cutpoints differ between\n", "groups (uniform DIF) and some statement loadings differ (nonuniform DIF).\n", "Lee et al. (2021) investigated both\n", "types of DIF with a TIRT model for three-statement blocks. Here we use\n", "two-statement blocks to illustrate the two effects.\n", "\n", "## Model\n", "\n", "For a given respondent and comparison pair, the probability of choosing\n", "the first statement in group $g$ is\n", "\n", "$$\n", "\\Pr(Y=1\\mid\\boldsymbol\\theta,g)\n", "=\n", "\\Phi\\!\\left(\\gamma_g+k_1a_{1g}\\theta_1-k_2a_{2g}\\theta_2\\right).\n", "$$\n", "\n", "Here $\\Phi$ is the standard normal CDF. For statement $s$, $\\theta_s$ is the\n", "respondent's score on its trait, $k_s$ is its keying, and $a_{sg}>0$ is its\n", "loading magnitude. The comparison intercept $\\gamma_g$ combines the cutpoint\n", "and the difference between the statement intercepts.\n", "\n", "Relative to group A, the group B intercept and statement loadings are\n", "\n", "$$\n", "\\begin{aligned}\n", "\\gamma_B &= \\gamma_A + \\Delta_\\gamma,\\\\\n", "\\log a_{sB} &= \\log a_{sA} + \\Delta_{a,s},\\qquad s=1,2.\n", "\\end{aligned}\n", "$$\n", "\n", "**Intercept DIF** is the difference $\\Delta_\\gamma$. The B-minus-A\n", "cutpoint difference reported by the API equals $-\\Delta_\\gamma$.\n", "**Loading DIF** is the shift $\\Delta_{a,s}$ on the log scale.\n", "For B03_1, the generating loadings are 0.90 in A and 0.45 in B, so\n", "$\\Delta_{a,1}=\\log(0.5)$. The API reports the difference on the original\n", "loading scale, which is $0.45-0.90=-0.45$ in this example." ] }, { "cell_type": "code", "execution_count": 1, "id": "12aaea21", "metadata": {}, "outputs": [], "source": [ "import arviz_stats as azs\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "\n", "import pyfcirt as fc" ] }, { "cell_type": "markdown", "id": "8d453c3b", "metadata": {}, "source": [ "## Generate one dataset\n", "\n", "Simulate 800 people, split equally between groups A and B, answering 12 pairs.\n", "Each trait pairing appears four times: Trait 1 with Trait 2, Trait 2 with\n", "Trait 3, and Trait 1 with Trait 3. Group B has trait mean\n", "shifts of 0.45, -0.30 and 0.20. Its cutpoints shift by +1.50, -1.50 and +1.50 in\n", "B02, B06 and B10, respectively.\n", "\n", "In group B, statement B03_1 changes loading magnitude from 0.90 to 0.45,\n", "and B07_1 changes from 0.90 to 1.80. All other generating loadings are\n", "shared. Generating statement effects are zero. The model allows each\n", "of the 24 loadings and 12 cutpoints to vary across groups." ] }, { "cell_type": "code", "execution_count": 2, "id": "5b03615e", "metadata": { "tags": [ "hide-input" ] }, "outputs": [], "source": [ "rng = np.random.default_rng(730026)\n", "n = 800\n", "# Repeat each cross-trait pairing four times.\n", "blocks = [f\"B{i:02d}\" for i in range(1, 13)]\n", "traits = np.tile([(0, 1), (1, 2), (0, 2)], (4, 1))\n", "# Assign equal group sizes and randomize the respondent order.\n", "group = rng.permutation(np.repeat([0, 1], n // 2))\n", "people = pd.DataFrame({\"person_id\": np.arange(n), \"site\": np.where(group, \"B\", \"A\")})\n", "people[\"site\"] = pd.Categorical(people[\"site\"], categories=[\"A\", \"B\"])\n", "# Give group B different trait means within the same covariance structure.\n", "correlation = [[1, 0.35, -0.15], [0.35, 1, 0.25], [-0.15, 0.25, 1]]\n", "theta = rng.multivariate_normal([0, 0, 0], correlation, size=n)\n", "theta += group[:, None] * np.array([0.45, -0.30, 0.20])\n", "\n", "loadings = np.tile([0.9, 1.1], (len(blocks), 1))\n", "# Use opposite keying in half the pairs and positive keying in the rest.\n", "keying = np.tile([1, -1], (len(blocks), 1))\n", "keying[[1, 3, 5, 7, 9, 11], 1] = 1\n", "design = pd.DataFrame(\n", " [\n", " (block, j + 1, f\"{block}_{j + 1}\", f\"Trait {traits[k, j] + 1}\", keying[k, j])\n", " for k, block in enumerate(blocks)\n", " for j in (0, 1)\n", " ],\n", " columns=[\"block_id\", \"position\", \"statement_id\", \"trait\", \"keying\"],\n", ")\n", "# Plant three cutpoint differences and two loading differences in group B.\n", "cutpoint_shift = np.zeros(len(blocks))\n", "cutpoint_shift[[1, 5, 9]] = [1.5, -1.5, 1.5]\n", "loading_shift = np.zeros_like(loadings)\n", "loading_shift[[2, 6], 0] = [-0.45, 0.90]\n", "# Subtract the second statement's utility from the first in each pair.\n", "person_loadings = loadings + group[:, None, None] * loading_shift\n", "utility = (theta[:, traits] * person_loadings * keying) @ np.array([1, -1])\n", "threshold = np.linspace(-0.5, 0.5, len(blocks)) + group[:, None] * cutpoint_shift\n", "# Record 1 when the noisy utility difference exceeds the group-specific cutpoint.\n", "choices = (utility + rng.normal(size=threshold.shape) >= threshold).astype(int)\n", "responses = pd.DataFrame(\n", " {\n", " \"person_id\": np.repeat(people.person_id.to_numpy(), len(blocks)),\n", " \"block_id\": np.tile(blocks, n),\n", " \"response\": choices.ravel(),\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "id": "fd63ec8e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | person_id | \n", "block_id | \n", "response | \n", "
|---|---|---|---|
| 0 | \n", "0 | \n", "B01 | \n", "0 | \n", "
| 1 | \n", "0 | \n", "B02 | \n", "0 | \n", "
| 2 | \n", "0 | \n", "B03 | \n", "1 | \n", "
| 3 | \n", "0 | \n", "B04 | \n", "1 | \n", "
| 4 | \n", "0 | \n", "B05 | \n", "0 | \n", "