# Mean Squares :API: {py:func}`irtlib.fit.mean_squares`; {py:func}`irtlib.fit.person_mean_squares`; {py:class}`irtlib.fit.MeanSquares` :Sources: {cite:t}`Wright1982`; {cite:t}`Wright1979`; {cite:t}`Meijer2001`; {cite:t}`Karabatsos2003` ## Motivation The weighted and unweighted mean squares compare each response with its expectation at the examinee's estimated ability {cite:p}`Wright1982{pp. 97-99}`, summed item by item: the infit and the outfit of the Rasch model. Wright and Masters define them for the models of their book, all built from the Rasch model (p. 10; p. 100, Table 5.4a), whose model for dichotomous responses (p. 38, Equation 3.1.1) is the 1PL, every item with the same slope $a$, with person parameters $a\theta_j$ and item parameters $ab_i$. irtlib computes them for every model, 1PL to 4PL, at each item's probabilities. The Implementation gives the sources, and measures them against {doc}`s-x2`, which detected a misfitting item that the item mean squares missed under the 2PL to 4PL. Summed examinee by examinee instead, over the items each examinee answered, the same residuals give each examinee's mean squares: person fit, which flags the examinees whose response patterns the model does not explain {cite:p}`Wright1982{p. 108}`. Item fit serves the construction and calibration of a test, and person fit the validity of the measures a calibrated test makes (p. 109). ## Definition For examinee $j$ and item $i$, with $P_{ij}$ the probability of a correct response at the examinee's ability and $Q_{ij} = 1 - P_{ij}$, the score residual $y_{ij} = u_{ij} - P_{ij}$ {cite:p}`Wright1982{p. 98, Equation 5.4.2}` has variance $W_{ij}$ and kurtosis $C_{ij}$ (p. 98, Equations 5.4.3 and 5.4.4), which for the two categories of a dichotomous item are $$ W_{ij} = P_{ij} Q_{ij}, \quad C_{ij} = P_{ij} Q_{ij} (P_{ij}^3 + Q_{ij}^3), $$ and the standardized residual is $z_{ij} = y_{ij} / W_{ij}^{1/2}$ (p. 99, Equation 5.4.5). Over the $N$ examinees, the unweighted and the weighted mean squares, which Wright and Masters write $u_i$ and $v_i$, are $$ U_i = \frac{1}{N} \sum_j z_{ij}^2, \quad V_i = \frac{\sum_j W_{ij} z_{ij}^2}{\sum_j W_{ij}} = \frac{\sum_j y_{ij}^2}{\sum_j W_{ij}} $$ (p. 99, Equations 5.4.6 and 5.4.7). irtlib calls $U_i$ the outfit and $V_i$ the infit. $U_i$ is sensitive to unexpected responses of examinees for whom the item is far too easy or far too hard, and the weights $W_{ij}$, smallest for those examinees, lessen their influence on $V_i$ (pp. 99, 101). When the data fit the model both have expectation 1, $U_i$ with variance $\sum_j (C_{ij} / W_{ij}^2) / N^2 - 1 / N$ (p. 100, Table 5.4a) and $V_i$ with variance $$ q_i^2 = \frac{\sum_j (C_{ij} - W_{ij}^2)}{\left(\sum_j W_{ij}\right)^2} $$ (p. 101, Equation 5.4.8). The weighted mean square is standardized to $$ t_i = (V_i^{1/3} - 1)(3 / q_i) + q_i / 3, $$ which has a mean near 0 and a standard deviation near 1 when the data fit (p. 101; p. 100, Table 5.4a). Wright and Masters estimate the person and item parameters from the same data as the residuals, which lowers the expected mean square by the factor $(M - 1)(N - 1) / (MN)$ for $M$ items (p. 101). {cite:t}`Karabatsos2003{p. 296}` standardizes an examinee's mean squares by the same transformation, "where MS can either refer to U or W", the unweighted or the weighted. irtlib standardizes the unweighted mean square of an examinee so, and of an item likewise, since the examinees' mean squares parallel the items' {cite:p}`Wright1982{p. 109}`, with the standard deviation from Table 5.4a in place of $q_i$. **Person fit.** The examinees' mean squares "parallel the corresponding item fit statistics exactly", the squared residuals summed over the items for one examinee rather than over the examinees for one item {cite:p}`Wright1982{p. 109}`. Over the $M_j$ items examinee $j$ answered, the weighted mean square, its variance and its standardized value are $$ V_j = \frac{\sum_i y_{ij}^2}{\sum_i W_{ij}}, \quad q_j^2 = \frac{\sum_i (C_{ij} - W_{ij}^2)}{\left(\sum_i W_{ij}\right)^2}, \quad t_j = (V_j^{1/3} - 1)(3 / q_j) + q_j / 3 $$ (pp. 108-109, Equations 5.7.6-5.7.8), and the unweighted mean square $U_j = \sum_i z_{ij}^2 / M_j$ has the variance $\sum_i (C_{ij} / W_{ij}^2) / M_j^2 - 1 / M_j$ of Table 5.4a (p. 100), with the items in place of the examinees. {cite:t}`Wright1979{p. 77, Equations 4.4.1-4.4.2}` divide an examinee's sum of squared standardized residuals by $M - 1$ instead, one less than the number of items, and standardize it by a logarithm; irtlib follows Wright and Masters. ## Implementation **The abilities.** The item mean squares take the abilities the caller passes, estimated at the calibration's items by any of the {doc}`/methods/ability/index`, or by default EAP estimates of {func}`irtlib.ability.expected_a_posteriori` on the calibration's grid, abilities normal with mean 0 and variance 1 ({doc}`ADR-0006 `). A calibration with no grid, such as one by {doc}`/methods/calibration/cml`, needs the caller's abilities, such as maximum likelihood estimates. An examinee whose ability is infinite or NaN, as the maximum likelihood estimate of a zero or perfect score is, is left out of every item's sums, as Wright and Stone edit the zero and perfect scores out of the data before calibrating, since they "tell us in which direction to look for an estimate" of ability but not how far {cite:p}`Wright1979{p. 32}`. The examinees' mean squares take the abilities from the caller, estimated by any of the {doc}`/methods/ability/index`. Under the 1PL, the {doc}`conditional-mean-squares` need no abilities, and their expectation is not lowered by them. **The models.** The mean squares of the items and of the examinees take items of any model, 1PL to 4PL, at each item's probabilities. Wright and Masters write $W_{ij}$ and $C_{ij}$ in terms of the model's probabilities (p. 108, Equations 5.7.1-5.7.3). {cite:t}`Meijer2001{p. 115, Equations 20-21}` write an examinee's unweighted and weighted mean squares at $P_g(\theta)$, "the item response function" of any model (p. 114). They note that the unweighted one, "developed for the Rasch model, also can be used for other IRT models" (p. 108), and report the examinees' mean squares in use under the 2PL and the 3PL (pp. 124-125), where a study under the 3PL found the standardized unweighted one "poorly standardized" (p. 125). No source on the shelf applies the examinees' mean squares to the 4PL, or the items' to any model but the Rasch model. irtlib takes both at the probabilities of every model, since the examinees' mean squares "parallel the corresponding item fit statistics exactly" {cite:p}`Wright1982{p. 109}`. Measured on 2026-12-02 for each model, with slopes from 0.7 to 2 (1 under the 1PL), difficulties from −2 to 2, guessing parameters from .10 to .25 and upper asymptotes from .85 to .97 where the model has them, and abilities normal with mean 0 and variance 1: - at the generating items and abilities, over 200 data sets of 1,000 examinees and 20 or 40 items, every model's item mean squares averaged 0.99 to 1.00, and their standardized values had means within 0.05 of 0 and standard deviations of 0.94 to 0.99, the 4PL's as near as the 1PL's; - over 30 data sets of 2,000 examinees and 20 or 40 items, calibrated by {doc}`/methods/calibration/mml` and evaluated at the default EAP abilities, in which one item's probability of a correct response rose and then fell with ability, which no model fits, and 5% of the examinees answered every item at random: - no other item of any model had a standardized value above 1.96; 34% to 87% had a standardized outfit below −1.96, and 24% to 70% a standardized infit, the 4PL's no more often than the 1PL's; - the misfitting item had a standardized outfit above 1.96 in half the data sets of 20 items and in all those of 40 under the 1PL, and in at most 3% under the 2PL to 4PL, whose estimates of the item absorb its misfit, while {doc}`s-x2` rejected it at the 5% level in each of 15 data sets of 20 items without the random responders, under every model; - 32% to 94% of the examinees who answered at random had a standardized infit above 1.96, by model and test length, the 4PL's within 0.06 of the 3PL's, and at most 1% of the others; - the 4 of these 240 calibrations that stopped short of convergence are included, and the 5 of the 4PL that failed with a singular information matrix were drawn again. **Missing responses.** Wright and Masters' sums run over every examinee and every item, with no rule for a missing response. The item mean squares need complete responses ({doc}`ADR-0006 `). The examinees' mean squares sum over the items each examinee answered, $M_j$ of them, with no source on the shelf for that rule; an examinee who answered no item, or whose ability is infinite or NaN, has none of the four values. **Two choices no source on the shelf covers:** - a variance $W_{ij}$ whose square is below $10^{-5}$ is raised to $10^{-5/2}$, leaving $C_{ij}$ and $y_{ij}$ as they are, so that a mean square's variance can fall below 0, and its $t$ is then NaN; - the standard deviation of either mean square is capped at 1.4142 before standardizing. The [sources wanted](https://github.com/douglasrizzo/irtlib/blob/dev/backlog/discovery/sources-wanted.md) list these two, the item mean squares beyond the Rasch model and the examinees' of the 4PL, and the examinees' sums over the answered items. :::{dropdown} Comparison with mirt - mirt 1.48 calls $U$ and $V$ outfit and infit too (`R/itemfit.R:584, 588`), and takes EAP estimates by default (`R/itemfit.R:268, 535-538`), on the grid of its `fscores`, or the abilities of its `Theta` argument (`R/itemfit.R:84-87, 536`). - irtlib's standardized outfit and its two choices above are mirt's: the standardized outfit (`R/itemfit.R:585, 587`), the floor on $W_{ij}$ (`R/personfit.R:189-200`) and the cap on the standard deviation (`R/itemfit.R:586, 591`; `R/personfit.R:205, 209`). - mirt computes the mean squares of any model from its probabilities (`R/personfit.R:181-194`), and with missing responses sums over the examinees who answered each item, with $N$ their number (`R/itemfit.R:577-584`), and over the items each examinee answered, with $M_j$ their number (`R/personfit.R:195, 202-210`). - Three departures. mirt replaces an infinite ability by the largest finite one plus 0.1, or the smallest less 0.1, in the item mean squares (`R/itemfit.R:542-549`) and in the examinees' (`R/personfit.R:146-151`); irtlib leaves that examinee out of the items' sums, and gives the examinee NaN. In the examinees' mean squares with missing responses, mirt divides the first term of the outfit's variance by the square of the number of all items, $M^2$, where irtlib divides it by $M_j^2$ (`R/personfit.R:204`). Where the floor on $W_{ij}$ leaves that variance negative, mirt takes its absolute value (`R/personfit.R:204`), and irtlib gives NaN. - At mirt's estimates and abilities the parity tests find the item mean squares and their standardized values within a relative $10^{-10}$; on irtlib's own calibration, the mean squares within a relative $10^{-4}$ and the standardized values within $10^{-3}$ ({doc}`ADR-0003 `). At the same abilities, on 2PL, 3PL and 4PL items, they find mirt's mean squares of the items, with complete responses, and of the examinees, with responses complete and missing, within a relative $10^{-10}$, and their standardized values too, except the examinees' standardized outfit with missing responses. ::: ## Parameters {func}`~irtlib.fit.mean_squares` takes the calibration and the responses it was calibrated on, and optionally each examinee's ability, `theta`, by default the EAP estimates on the calibration's grid. {func}`~irtlib.fit.person_mean_squares` takes the responses, the items and each examinee's ability. Neither has other settings. ## Guarantees 1. Each item, or each examinee, gets its outfit and infit, and their standardized values; a standardized value is NaN where its mean square's variance is not positive. 2. For the item mean squares, a missing response, or, without abilities passed in, a calibration whose likelihood is not marginal raises an error. 3. The item mean squares leave out an examinee whose ability is infinite or NaN, without a warning; an item with no examinee left has NaN for all four values. 4. The mean squares of an examinee leave out the examinee's missing responses, and are NaN when the examinee answered no item or the ability is infinite or NaN, without a warning. ## References ```{bibliography} :filter: docname in docnames ``` ## API Reference ```{eval-rst} .. autofunction:: irtlib.fit.mean_squares .. autofunction:: irtlib.fit.person_mean_squares .. autoclass:: irtlib.fit.MeanSquares ```