repair_refit() rebuilds a MLM, recomputes predictions
and their error covariance, and fits the structural regression again. It
first replays the reference analysis and verifies agreement with the
supplied fit. This supports reproducible sensitivity analyses and powers
the experimental person bootstrap in repair_bootstrap(). It
currently supports one MLM.
Verify the reference analysis
Keep the original repeated-observation and unit-level data explicitly. The reference data arguments always describe the original fit, including its requested scope and structural exclusions.
data(repair_example)
repeated <- repair_example$repeated
people <- repair_example$people
measurement <- lme4::lmer(
score ~ day + (day | person), repeated,
control = lme4::lmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 100000))
)
fit <- repair(re("day", "growth") ~ practice,
list(growth = measurement), people, "person", backend = "r")
replay <- repair_refit(
fit, mlm_data = list(growth = repeated), data = people
)
repair_coefficients(replay)
#> term estimate std.error statistic p.value conf.low conf.high
#> 1 (Intercept) -0.7895288 0.1147252 -6.881912 5.905430e-12 -1.0143861 -0.5646715
#> 2 practice 0.1754854 0.0214190 8.192979 2.548386e-16 0.1335049 0.2174659
#> conf.level
#> 1 0.95
#> 2 0.95
summary(replay)$refit$checks
#> stage n_rows n_requested n_analyzed controls verified
#> 1 reference 586 60 60 recorded_recipe TRUEWith no replacement arguments, the returned object is the actual
refitted reference analysis. Identity checks cover effective
observations, IDs/grouping, fixed/random designs, weights and offsets,
residual/nuisance and random-effect covariance, selected predictions,
assembled structural inputs, coefficients, and their covariance.
Numerical comparisons use tolerance = 1e-6; identity,
coding, and schema checks are exact. A mismatch stops before any
replacement fit.
Reference MLM data must retain the fitted observation row names. The
adapter selects the original effective rows by these identities; it does
not re-evaluate old subset, data, or
global-environment expressions. Rows excluded from the original MLM fit
are listed in
summary(replay)$refit$reference_excluded_rows. Structural
exclusion reasons remain in
repair_diagnostics(replay, "excluded").
Fit explicitly changed data or a revised scope
Use the separate new_mlm_data and new_data
arguments for replacements. Omitting either replacement argument retains
its reference inputs. For example, restrict both MLM fitting and
structural analysis to 50 people:
revised_people <- people[1:50, ]
revised_repeated <- repeated[repeated$person %in% revised_people$person, ]
revised <- repair_refit(
fit, mlm_data = repeated, data = people,
new_mlm_data = revised_repeated, new_data = revised_people
)
repair_coefficients(revised)
#> term estimate std.error statistic p.value conf.low
#> 1 (Intercept) -0.7744956 0.12942851 -5.983964 2.177711e-09 -1.0281708
#> 2 practice 0.1698606 0.02397467 7.085003 1.390417e-12 0.1228711
#> conf.high conf.level
#> 1 -0.5208203 0.95
#> 2 0.2168500 0.95
summary(revised)$refit$checks
#> stage n_rows n_requested n_analyzed controls verified
#> 1 reference 586 60 60 recorded_recipe TRUE
#> 2 replacement 489 50 50 recorded_recipe TRUEThis estimates the complete procedure on the revised scope. Keeping
all original measurement people while changing only
new_data defines a different analysis; choose the intended
MLM scope explicitly.
Every supplied replacement measurement row must be used in its
original order. Missing used values, implicit callback subsetting, or
omission cause an error. Replacement structural data use the strict
na_action = "fail" policy. Prepare a complete,
substantively chosen scope before refitting. The original structural
formula, computation backend, and regularization policy/custom alpha are
retained; sample-dependent moments and weights are recomputed.
Fixed factor coding is frozen from the reference fit. Missing or new levels and changed coefficient schemas fail explicitly rather than silently changing what a coefficient means. Newly created fits record structural factor matrices for this purpose. For older saved fits without that metadata, reference replay must first establish the original coding from the supplied reference data.
What the native adapters preserve
| Component | Behavior |
|---|---|
| MLM backend | Gaussian lmer or supported single-group linear lme |
| Formula and target | Same fixed/random effect schema and target grouping; contained lmer nuisance groups remain supported |
| ML/REML | Recorded method retained and checked |
| Weights/offsets | Data-only expressions rebuilt on the supplied rows; global vectors require an explicit recipe |
| Contrasts | Recorded fixed-factor matrices and levels retained; incompatible levels fail |
| lmer optimizer | Recorded optimizer and optimizer controls checked |
| Literal control recipes | Reconstructed with supported constructors; recorded recipes compared |
| nlme covariance | Supported AR(1)/CAR(1), varIdent/varPower, random covariance classes and fixed constraints checked |
| Shared nuisance groups | Rejected by the ordinary structural validator |
Native expressions use explicit columns and a small deterministic
grammar. Ordinary arithmetic, I, offset, basic
log/square-root transforms, and grouping operators are supported.
Sample-dependent scale/poly, arbitrary
functions, symbolic controls, and other external dependencies require an
explicit fitting function. Precompute transformed columns when possible.
Random-only categorical slopes and transformed factor coding also
require an explicit recipe.
The native adapter preserves recorded choices; fitted objects cannot
recover every historical global default or an external control object’s
earlier value. The audit’s controls column distinguishes
recorded_recipe, recorded_optimizer (lmer’s
retained optimizer settings), and callback_declared
(unrecoverable nlme controls declared by the caller’s recipe). Identity
agreement verifies the reference calculation, not unrecorded
settings.
Supply an explicit fitting recipe when needed
refitter is a deterministic function accepting
one effective MLM data frame and returning its fitted
model. It runs for both reference and replacement inputs and is checked
for method, rows, specification, and coefficient identities. The caller
must supply the original fitting choices that the backend did not
retain. In particular, do not use a callback to hide convergence
failures.
recipe <- function(data) {
lme4::lmer(score ~ day + (day | person), data,
control = lme4::lmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 100000)))
}
explicit <- repair_refit(fit, repeated, people, refitter = recipe)
summary(explicit)$refit$checks
#> stage n_rows n_requested n_analyzed controls verified
#> 1 reference 586 60 60 recorded_recipe TRUEMeasurement failures stop with the reference/replacement stage
identified. A singular random-effect covariance can be retained when
convergence, prediction extraction, and structural estimation remain
usable. Warnings and messages are recorded in
summary(result)$refit$warnings and $messages;
informational warnings are not blanket failures. Available optimizer
failure codes and convergence warnings are fatal. Inspect recorded
warnings before interpreting a refitted analysis.
Replay a deterministic list of whole people
draw accepts source IDs with repeats. It makes
no random sample and does not produce bootstrap SEs or
intervals. This example omits the first source person and includes the
second twice:
sources <- c(people$person[-1], people$person[2])
copied <- repair_refit(fit, repeated, people, draw = sources)
head(summary(copied)$refit$draw)
#> draw source_id refit_id
#> 1 1 P002 r0000001
#> 2 2 P003 r0000002
#> 3 3 P004 r0000003
#> 4 4 P005 r0000004
#> 5 5 P006 r0000005
#> 6 6 P007 r0000006
summary(copied)$refit$checks
#> stage n_rows n_requested n_analyzed controls verified
#> 1 reference 586 60 60 recorded_recipe TRUE
#> 2 replacement 586 60 60 recorded_recipe TRUEEvery occurrence gets a new target identity. All of that source
person’s effective observations and its structural row are copied
together. Character source IDs such as "001" are preserved
in a separate mapping rather than parsed from generated names.
draw requires identical MLM and structural scopes;
measurement-only people, including those omitted structurally despite
having MLM data, require an explicitly revised reference fit before
using this convenience interface. It cannot be combined with replacement
data.
For nested nuisance effects, person:day is recomputed
from each fresh person ID, while standalone nuisance ID columns are also
re-keyed. Merely copying records without new IDs could merge two
occurrences into one cluster or introduce shared nuisance effects. The
convenience interface requires a bare target ID column and nuisance
groups expressed as bare ID columns or expressions containing the
target. IDs also used as substantive covariates cannot be re-keyed
automatically; supply explicit replacement data for unsupported
mappings.
reference <- readRDS(system.file("extdata", "nested-workflow-reference.rds", package = "repair"))
example <- reference$cases$nested_intercept
model <- lme4::lmer(y ~ x + offset(off) + (x | person) + (1 | person:day),
example$data, weights = prior, na.action = na.exclude,
control = lme4::lmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 100000)))
nested <- repair(
re("x", "growth", "person") ~ re("(Intercept)", "growth", "person") + practice,
list(growth = model), example$people, "person"
)
ids <- c(example$people$person[-1], example$people$person[2])
refitted <- repair_refit(nested, example$data, example$people, draw = ids)
repair_coefficients(refitted)
#> term estimate std.error
#> 1 (Intercept) -0.03208127 0.07371712
#> 2 re("(Intercept)", model = "growth", group = "person") 0.16571391 0.06914846
#> 3 practice 0.37725573 0.09681009
#> statistic p.value conf.low conf.high conf.level
#> 1 -0.4351943 6.634214e-01 -0.17656416 0.1124016 0.95
#> 2 2.3964946 1.655274e-02 0.03018542 0.3012424 0.95
#> 3 3.8968639 9.744634e-05 0.18751145 0.5670000 0.95Current boundary and reproducibility
Results are ordinary REPAIR fits: use the existing generics and table
extractors. The original fit remains the reference. Refitted models
retain explicit fitting data in their model/formula environment; the
audit records versions, counts, warnings and any deterministic source-ID
mapping. Save analysis code, inputs, sessionInfo(), and
result tables along with serialized objects.
Multiple measurement models and supplied cross-model covariance replay remain outside this interface. Person-level bootstrap sampling now provides experimental serial attempts, failure accounting, and percentile intervals using this refitter. Parallel execution and scientific coverage validation remain future work. A refitted object’s standard errors remain the conditional model-based standard errors of that fit.
