Skip to contents

Match likelihood-only predictions and their full within-unit error covariance to a unit-level data frame by ID. The data frame defines the analysis scope and its order. No aggregation, row-order matching, or silent omission occurs.

Usage

repair_data(
  formula,
  models,
  data,
  id,
  na_action = c("fail", "omit"),
  cross_model_error = NULL
)

Arguments

formula

An additive structural formula with at least one re() marker. Observed predictors may be numeric, logical, or factors. The outcome must be numeric or a random effect. An intercept is always included.

models

A fitted Gaussian lme4::lmer() or linear nlme::lme() model, an experimental repair_bayes_plugin() snapshot, or named list. Bayesian support is restricted to the snapshot's documented scope; its structural inference conditions on posterior summaries, does not propagate step-1 uncertainty, and has unvalidated full-workflow coverage. Select one target grouping factor per model. Gaussian lmer models may also contain nuisance groups whose every fitted level belongs to exactly one target level; nlme models must have one grouping factor. Grouping column names may differ, but their values must identify the same units. Alternatively supply a repair_mlm() specification, or a named list mixing specifications and fitted models. Each specification is fitted once per call through its native engine before the usual validation and extraction.

data

A data frame with one row per analysis ID, containing the observed structural variables. Its ID values must match the model's grouping levels.

id

Name of the ID column in data; need not equal the grouping name.

na_action

"fail" (default) or "omit". Omission covers missing or nonfinite observed values, unmatched IDs, and failed prediction blocks; it warns and records every excluded ID and reason.

cross_model_error

Cross-model prediction-error covariance policy. Default NULL is valid only when the formula references one model. For multiple models, use "independent" to explicitly assume zero cross-model prediction extraction error covariance, or a list of blocks, one for every unordered model pair. Each block is list(models = c("a", "b"), covariance = array(...)). The numeric array must have three named dimensions: coefficient names from model a, coefficient names from model b, and unit IDs. Term names must exactly cover the effects selected from those models. Every retained ID must be present; extra IDs are allowed. Order does not matter. Values are covariances of likelihood-only prediction errors, not latent coefficients or raw observations. The assembled covariance must be positive semidefinite. Missing/invalid supplied blocks are errors even with na_action = "omit".

Value

An S7 analysis-data object. Use @ids, @outcome, @predictors, @err_x, @err_y, @err_xy, @effects, and @diagnostics to inspect it. Error arrays are ordered predictor by predictor by ID. Observed variables have zero sampling error. Predictions are deviations from fitted fixed effects. @predictions holds a prediction object for one referenced model, or a named list for multiple models. @diagnostics$mlm_models always provides diagnostics by model, including the verified nuisance_groups; @diagnostics$cross_model_error records the applied policy and blocks.

Details

Use re("term", model = "name", group = "person") to resolve coefficients shared by several groups. All references to one model must select the same target group. Nesting is checked on all effective fitted observations before structural omission; shared nuisance levels are rejected even when only one of their target people is requested. Group names alone do not establish nesting. Nested random intercepts/slopes and multiple nuisance groups are allowed, provided every nuisance group passes the check. Target-specific prediction-error covariance includes every nuisance contribution. See vignette("nested-nuisance", package = "repair") for a complete example.

Within-model covariance always comes from prediction extraction and cannot be overridden. Supplied blocks are conditional on the fitted MLMs; uncertainty in their estimation is not propagated. Independent errors do not imply independent latent processes. The package does not estimate cross-model covariance from separate fits. See vignette("multiple-models", package = "repair"). Use repair_cross_covariance() to propagate explicitly assumed observation covariances and repair_sensitivity() to compare supplied assumptions.

Examples

data(repair_example)
growth <- lme4::lmer(score ~ day + (day | person), repair_example$repeated)
analysis <- repair_data(re("day") ~ practice, growth, repair_example$people, "person")
analysis
#> REPAIR analysis data: 60 people, 1 predictor columns
#> Excluded: 0 of 60 requested IDs
#>  model  group term                    label    role
#>    mlm person  day re("day", model = "mlm") outcome
head(repair_predictions(analysis))
#>     id model  group term                    label    role prediction std.error
#> 1 P001   mlm person  day re("day", model = "mlm") outcome  0.4451909 0.1401763
#> 2 P002   mlm person  day re("day", model = "mlm") outcome  0.4105728 0.1401763
#> 3 P003   mlm person  day re("day", model = "mlm") outcome -0.4013259 0.2268540
#> 4 P004   mlm person  day re("day", model = "mlm") outcome  0.6289102 0.1618617
#> 5 P005   mlm person  day re("day", model = "mlm") outcome  0.6052267 0.1401763
#> 6 P006   mlm person  day re("day", model = "mlm") outcome -1.2120879 0.2268540
#>   prediction.error.variance      scope analyzed status reason
#> 1                0.01964941 structural     TRUE     ok       
#> 2                0.01964941 structural     TRUE     ok       
#> 3                0.05146273 structural     TRUE     ok       
#> 4                0.02619921 structural     TRUE     ok       
#> 5                0.01964941 structural     TRUE     ok       
#> 6                0.05146273 structural     TRUE     ok       
head(repair_prediction_covariance(analysis))
#>     id model1 group1 term1                   label1   role1 model2 group2 term2
#> 1 P001    mlm person   day re("day", model = "mlm") outcome    mlm person   day
#> 2 P002    mlm person   day re("day", model = "mlm") outcome    mlm person   day
#> 3 P003    mlm person   day re("day", model = "mlm") outcome    mlm person   day
#> 4 P004    mlm person   day re("day", model = "mlm") outcome    mlm person   day
#> 5 P005    mlm person   day re("day", model = "mlm") outcome    mlm person   day
#> 6 P006    mlm person   day re("day", model = "mlm") outcome    mlm person   day
#>                     label2   role2 covariance       source
#> 1 re("day", model = "mlm") outcome 0.01964941 within_model
#> 2 re("day", model = "mlm") outcome 0.01964941 within_model
#> 3 re("day", model = "mlm") outcome 0.05146273 within_model
#> 4 re("day", model = "mlm") outcome 0.02619921 within_model
#> 5 re("day", model = "mlm") outcome 0.01964941 within_model
#> 6 re("day", model = "mlm") outcome 0.05146273 within_model