Multiple groups with MGPPM#
Estimate how trait scores differ between two groups who answered the same forced-choice blocks. Both groups use the same statement parameters, so their population means are comparable on a common trait scale.
Load rankings and group labels#
The bundled simulation contains complete rankings from 60 respondents in each
group, across 12 triplets. Group B’s generating mean is 0.60 higher on
theta1. The groups have the same generating means on the other three traits.
import seaborn as sns
import pyfcirt as fc
sns.set_theme(style="ticks")
example = fc.load_example("mgppm_groups", model="MGPPM", include_truth=True)
design = example.data.design
responses = example.data.responses.frame
people = example.person_data
design.head(3)
| block_id | position | statement_id | trait | keying | |
|---|---|---|---|---|---|
| 0 | B1 | 1 | S1 | theta1 | 1 |
| 1 | B1 | 2 | S2 | theta2 | 1 |
| 2 | B1 | 3 | S3 | theta3 | 1 |
The group table contains one row per respondent. PyFCIRT matches the
group column to responses by person_id.
people.groupby("group").head(2)
| person_id | group | |
|---|---|---|
| 0 | 1 | A |
| 1 | 2 | A |
| 60 | 61 | B |
| 61 | 62 | B |
Set the reference group#
Use PopulationSpec to read group membership from the group column
and fix group A’s trait means at zero.
population = fc.PopulationSpec(group="group", theta_residual_scale="unit", reference_group="A")
model = fc.Model()
model.add_fc(
design,
responses,
format="block_long",
model="MGPPM",
response_format="rank",
)
model.prepare(
population=population,
person_data=people,
theta_parameterization="noncentered",
)
print(model.summary())
Model: MGPPM
120 people, 4 traits, 12 blocks, 36 statements
Observations: 1,440 block events
Measurements:
rank: 12 blocks, size=3, categories=6
Population:
theta ~ MvNormal(mu=population_mean[group], cov=R)
Groups: 2; reference='A'
R [2 x 4 x 4] ~ LKJCorr(n=4, eta=1)
population_mean [4] ~ Normal(mu=0, sigma=1)
Priors:
loading [36] ~ LogNormal(mu=0, sigma=0.5)
statement_intercept [24] ~ Normal(mu=0, sigma=2)
Constraints:
statement_intercept: fixed=0 [12 fixed]
population_mean: fixed=0 [4 fixed]
The summary reports two groups and identifies A as the reference for the population means.
Fit the grouped model#
model.fit(draws=2000, tune=3500, chains=4, random_seed=45);
Diagnose the chains#
The maximum R-hat is 1.007. The minimum bulk and tail effective sample sizes are 1,046 and 954, with zero divergences.
import arviz as az
import numpy as np
posterior = model.idata.posterior.to_dataset().reset_coords(drop=True)
with np.errstate(invalid="ignore", divide="ignore"):
r_hat = az.rhat(posterior).max().to_array().max().item()
bulk = az.ess(posterior, method="bulk").min().to_array().min().item()
tail = az.ess(posterior, method="tail").min().to_array().min().item()
print(f"Max R-hat: {r_hat:.3f}")
print(f"Min bulk / tail ESS: {bulk:.0f} / {tail:.0f}")
print("Divergences:", int(model.idata.sample_stats.diverging.sum()))
Max R-hat: 1.007
Min bulk / tail ESS: 1046 / 954
Divergences: 0
The two panels check the chains for group B’s mean on theta1. This is the same group difference interpreted below. Its rank-test p-value is about 0.02.
Compare correlations and individual scores#
The heatmaps show the estimated correlations between traits in each group. The density panels show how individual scores vary. Each respondent contributes one posterior mean score per trait.
correlations = model.idata.posterior.trait_correlation.mean(("chain", "draw"))
Compare group means#
The density shows the posterior for group B’s mean on theta1.
The forest plot shows the posterior mean and 95% highest density interval for
each trait. Gray diamonds mark the generating values. Group A has mean zero
on the reference scale, so each plotted mean is also the difference B − A.
For theta1, the estimated difference is 0.75 (95% HDI [0.33, 1.19]). The intervals for the other three traits include zero.