A MLM can include unit-level random intercepts/slopes and additional
random effects for occasions within each person. REPAIR can target the
person coefficients while incorporating these nuisance
effects into their prediction-error covariance. This extension
currently applies to Gaussian lme4::lmer() fits.
Fit a person and day model
The bundled reference contains synthetic data for 48 people, each with multiple days and repeated observations within each day. Practice is a simulated unit-level predictor. Day numbers are local to each person. These are implementation reference datasets, not real participant data or the paper’s published simulation results.
reference <- readRDS(system.file("extdata", "nested-workflow-reference.rds", package = "repair"))
example <- reference$cases$nested_intercept
head(example$data[c("person", "day", "x", "y")])
#> person day x y
#> 941 P047 5 0.2500000 3.5782786
#> 308 P016 2 -0.5833333 1.2660762
#> 393 P020 3 0.0500000 0.3501280
#> 844 P043 1 -0.5500000 0.5768564
#> 373 P019 3 0.2500000 -0.2026211
#> 936 P047 3 1.2500000 3.3306380
head(example$people)
#> person practice
#> 1 P001 0.3505191
#> 2 P002 -0.1744997
#> 3 P003 -0.4824897
#> 4 P004 0.3510578
#> 5 P005 -0.3849021
#> 6 P006 1.2037457
growth <- lme4::lmer(
y ~ x + offset(off) + (1 + x | person) + (1 | person:day),
data = example$data, weights = prior, na.action = na.exclude,
control = lme4::lmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 100000))
)
models <- list(growth = growth)
repair_effects(models)
#> model group term n_levels
#> 1 growth person:day (Intercept) 192
#> 2 growth person (Intercept) 48
#> 3 growth person x 48person is the target grouping factor;
person:day is a nuisance grouping factor. The fitted
model’s weights, offsets, fixed effects, and effective observations are
retained. Check MLM adequacy and optimizer messages with the usual lme4
tools before structural analysis.
Select one target group per model
Both groups have an (Intercept), so a model name alone
cannot identify the person intercept. Use group = "person"
in re(). Names must match the repair_effects()
catalog; the person table’s ID column can have a different name. A
unique term can still be selected without a group qualifier.
equation <- re("x", model = "growth", group = "person") ~
re("(Intercept)", model = "growth", group = "person") + practice
analysis <- repair_data(equation, models, example$people, id = "person")
head(repair_predictions(analysis))
#> id model group term label role
#> 1 P001 growth person x re("x", model = "growth", group = "person") outcome
#> 2 P002 growth person x re("x", model = "growth", group = "person") outcome
#> 3 P003 growth person x re("x", model = "growth", group = "person") outcome
#> 4 P004 growth person x re("x", model = "growth", group = "person") outcome
#> 5 P005 growth person x re("x", model = "growth", group = "person") outcome
#> 6 P006 growth person x re("x", model = "growth", group = "person") outcome
#> prediction std.error prediction.error.variance scope analyzed status
#> 1 0.1313313 0.1721236 0.02962654 structural TRUE ok
#> 2 -0.8190702 0.1432627 0.02052421 structural TRUE ok
#> 3 -0.2214168 0.1997657 0.03990634 structural TRUE ok
#> 4 0.3966452 0.1612176 0.02599111 structural TRUE ok
#> 5 0.5551879 0.1426732 0.02035564 structural TRUE ok
#> 6 0.3675138 0.1844925 0.03403750 structural TRUE ok
#> reason
#> 1
#> 2
#> 3
#> 4
#> 5
#> 6
head(repair_prediction_covariance(analysis))
#> id model1 group1 term1 label1 role1
#> 1 P001 growth person x re("x", model = "growth", group = "person") outcome
#> 2 P002 growth person x re("x", model = "growth", group = "person") outcome
#> 3 P003 growth person x re("x", model = "growth", group = "person") outcome
#> 4 P004 growth person x re("x", model = "growth", group = "person") outcome
#> 5 P005 growth person x re("x", model = "growth", group = "person") outcome
#> 6 P006 growth person x re("x", model = "growth", group = "person") outcome
#> model2 group2 term2 label2 role2
#> 1 growth person x re("x", model = "growth", group = "person") outcome
#> 2 growth person x re("x", model = "growth", group = "person") outcome
#> 3 growth person x re("x", model = "growth", group = "person") outcome
#> 4 growth person x re("x", model = "growth", group = "person") outcome
#> 5 growth person x re("x", model = "growth", group = "person") outcome
#> 6 growth person x re("x", model = "growth", group = "person") outcome
#> covariance source
#> 1 0.02962654 within_model
#> 2 0.02052421 within_model
#> 3 0.03990634 within_model
#> 4 0.02599111 within_model
#> 5 0.02035564 within_model
#> 6 0.03403750 within_model
fit <- repair(equation, models, example$people, id = "person")
repair_coefficients(fit)
#> term estimate std.error
#> 1 (Intercept) -0.03576976 0.07328822
#> 2 re("(Intercept)", model = "growth", group = "person") 0.14820716 0.06970119
#> 3 practice 0.37219851 0.09604525
#> statistic p.value conf.low conf.high conf.level
#> 1 -0.4880698 0.6255004115 -0.17941203 0.1078725 0.95
#> 2 2.1263219 0.0334764623 0.01159535 0.2848190 0.95
#> 3 3.8752414 0.0001065191 0.18395329 0.5604437 0.95
summary(fit)$mlm_models$growth
#> $group
#> [1] "person"
#>
#> $nuisance_groups
#> [1] "person:day"
#>
#> $unused_prediction_ids
#> character(0)
#>
#> $mlm_backend
#> [1] "lme4"
#>
#> $mlm_singular
#> [1] FALSE
#>
#> $mlm_messages
#> character(0)This regresses the latent person slope deviation on the person intercept deviation and practice. Predictions remain deviations from fitted fixed effects. The selected intercept/slope prediction-error covariance is retained. Nuisance coefficients do not become structural predictors or outcomes: all references to one MLM must select the same target group.
Reported effect labels include the group for models with multiple
groups. The stable tables retain their existing schemas and explicit
model/group/term/ID keys. The structural summary identifies the verified
nuisance groups; their names are also in
summary(fit)$mlm_models$growth$nuisance_groups.
What enters prediction extraction and reliability?
For person j, the target-specific covariance is
Ordinary residual covariance includes the fitted residual scale and
prior weights. The nuisance contributions include all day effects for
that person. Person random effects are excluded from this covariance
because they are the target signal. GLS supplies a joint prediction and
error covariance (Z_T' R_T^-1 Z_T)^-1. Adding only a day
variance to the diagonal would lose the covariance between observations
sharing a day.
Posterior reliability uses the same R_T, with the fitted
target prior covariance G added for the posterior calculation. Ignoring
nuisance variance in reliability would overstate available
information.
repair_reliability(fit)[, c("term", "measure", "reliability", "n_units")]
#> term measure reliability n_units
#> 1 x marginal 0.9178252 48
#> 2 x residualized 0.9023862 48
#> 3 (Intercept) marginal 0.9066435 48
repair_diagnostics(fit, table = "scope")
#> n_requested n_analyzed n_excluded
#> 1 48 48 0
repair_diagnostics(fit, table = "mlm")
#> model group backend singular n_levels n_outside_requested messages
#> 1 growth person lme4 FALSE 48 0The fitted lmer model supplies mutually independent random-effect blocks and independent ordinary residuals, conditional on fitted parameters. The structural SEs remain conditional on those fitted quantities; they do not propagate measurement-parameter estimation uncertainty or provide bootstrap inference. The original diagnostics, omission policy, and normal-interval interpretation continue to apply.
More than one nuisance group, including slopes
Every nuisance group must satisfy the same containment rule. For example, both day and block can be contained within people even if day and block cross within a person. Nuisance coefficients within a block can be correlated.
complex <- reference$cases$nested_slope
growth2 <- lme4::lmer(
y ~ x + offset(off) + (1 + x | person) +
(1 + within | person:day) + (1 | person:block),
data = complex$data, weights = prior, na.action = na.exclude,
control = lme4::lmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 100000))
)
fit2 <- repair(equation, list(growth = growth2), complex$people, "person")
repair_coefficients(fit2)
#> term estimate std.error
#> 1 (Intercept) 0.02582658 0.07391087
#> 2 re("(Intercept)", model = "growth", group = "person") 0.26091533 0.06900310
#> 3 practice 0.31233907 0.07947360
#> statistic p.value conf.low conf.high conf.level
#> 1 0.3494287 7.267675e-01 -0.1190361 0.1706892 0.95
#> 2 3.7812120 1.560667e-04 0.1256718 0.3961589 0.95
#> 3 3.9300983 8.491114e-05 0.1565737 0.4681045 0.95Separate random-effect blocks for the target, such as
(x || person), are also supported. Several MLMs can each
have eligible nuisance groups; combining them still requires the
explicit cross-model error policy in
vignette("multiple-models").
Shared effects remain rejected
Containment is verified from all effective fitted observations: each nuisance level must occur in exactly one target person. This is a membership check, not a check of whether a name contains a colon. Globally unique visit IDs can qualify without a colon. The check precedes structural scope selection, so omission or requesting just one person per school cannot bypass it. An estimated zero nuisance variance does not waive the membership rule.
(1 | day) means a shared day effect if the same day
number occurs for several people. (1 | person:day)
represents distinct effects for each person’s days. Choose the grouping
specification from the scientific design: do not split a truly shared
school, item, or calendar-day effect merely to pass validation.
If people j and k share a nuisance effect, their prediction errors
can have nonzero cross-person covariance. The current structural
estimator accepts within-unit blocks and cannot account for that
dependence. Shared nuisance levels therefore produce an explicit error,
even with na_action = "omit". Targeting days while treating
their shared person effect as nuisance also fails this rule. Lower-level
prediction extraction of shared structures remains experimental and is
not an alternative route to validated structural inference.
Multilevel nlme fits remain unsupported. For current model and
inference limits, see
vignette("support-and-troubleshooting").
Validation and reproducibility
The tests refit both models from observations and compare
predictions, complete within-unit covariance, posterior reliability,
structural estimates, SEs, coefficient covariance, and normal intervals
with independent frozen references. Both R and C++ estimation backends
are checked. Reference GLS/posterior matrices are calculated directly
from fitted MLM quantities without loading repair; the structural
estimates are cross-checked against the companion production estimator
and algebra audit. The generator and source provenance are in
dev/generate_nested_fixtures.R and the bundled reference’s
provenance. These comparisons establish per-dataset
implementation agreement, not nominal coverage across all nested
designs.
