Skip to contents

Convert explicitly assumed observation-level cross-covariances to the likelihood-only prediction-error blocks accepted by repair(). No covariance parameters are estimated and no measurement model is jointly refitted.

Usage

repair_cross_covariance(
  formula,
  models,
  observation_keys,
  residual_cross_covariance = NULL,
  rho = NULL,
  ids = NULL
)

Arguments

formula

Structural formula selecting random coefficients from at least two models, as in repair().

models

Named list of fitted models or repair_mlm() specifications. Specifications are fitted once in this call; reuse fitted models when constructing several scenarios.

observation_keys

Named list, one entry per referenced model. Each entry is a character vector of occasion keys, named by the original data row names. Effective fitted rows are matched by name, including after subset/NA handling; extra rows are allowed. Keys must be unique within each target person. Equal keys across models identify the same occasion for the same person. Supply meaningful keys explicitly: row position and nearest times are never used to pair observations.

residual_cross_covariance

List with one block for every unordered referenced model pair. Each block has models = c("a", "b") and covariance, a named list of observation covariance matrices, one per ID. Matrix row/column names must exactly cover that person's effective occasion keys in models a/b. Values are in observation units. Order does not matter. Include covariance of any effective nuisance contributions, but exclude the target random effects. Supply either this argument or rho.

rho

Optional scalar correlation in [-1, 1], restricted to exactly two models with iid homoscedastic errors and no nuisance random groups. Assumes covariance rho * sigma_a * sigma_b at equal occasion keys and zero at all other occasion pairs, including unmatched occasions. Marginal scales come from the fitted models. This is an assumption, not an estimate.

ids

Character vector of unique target IDs to propagate. Default is the intersection of effective target levels across referenced models. Supply the analysis ID column to require coverage of your intended scope.

Value

A plain list of list(models, covariance) blocks for cross_model_error in repair() or repair_data(). Arrays have dimensions selected coefficient in model a, selected coefficient in model b, and ID.

Details

For each person, uses the same target design Z and effective marginal error covariance R as likelihood-only scoring, with extraction operator L = (Z' R^-1 Z)^-1 Z' R^-1. Returns selected entries of L_a R_ab L_b'. Fitted fixed effects and covariance parameters are treated as fixed. Covariance between true random coefficients is excluded from R_ab.

The complete joint observation covariance, using the fitted within-model blocks, must be positive semidefinite (relative tolerance 1e-8), including when there are more than two models. The assembled selected prediction covariance is also checked. Invalid inputs and singular extraction designs stop; there is no silent covariance repair or omission. For correlated or heterogeneous errors, supply compatible explicit observation blocks instead of rho. Separate marginal fits alone do not identify these blocks.

Examples

if (requireNamespace("nlme", quietly = TRUE)) {
  case <- readRDS(system.file("extdata", "multimodel-workflow-reference.rds",
                             package = "repair"))$cases$supplied
  models <- list(growth = nlme::lme(y ~ z, random = ~ z | id, data = case$a),
    response = nlme::lme(q ~ w, random = ~ w | subject, data = case$b))
  equation <- re(w, "response") ~ re(z, "growth")
  # These two time scales encode the same synthetic measurement occasions.
  keys <- list(growth = setNames(as.character(round(4 * case$a$z)), rownames(case$a)),
    response = setNames(as.character(round(5 * (case$b$w - .5))), rownames(case$b)))
  blocks <- repair_cross_covariance(equation, models, keys, rho = .25,
                                    ids = case$people$person)
  repair(equation, models, case$people, "person", cross_model_error = blocks)
}
#> REPAIR structural regression
#> re(w, "response") ~ re(z, "growth") 
#> Units: 70 analyzed / 70 requested; 0 excluded
#> MLM: response (nlme; group subject; 0 IDs outside scope)
#> nlme singularity is not assessed; inspect the MLM fit and its fitting warnings.
#> MLM: growth (nlme; group id; 0 IDs outside scope)
#> nlme singularity is not assessed; inspect the MLM fit and its fitting warnings.
#> Cross-model prediction errors: user-supplied covariance blocks (conditional inference)
#> Predictions: likelihood-only random deviations
#>     model   group term      role
#>  response subject    w   outcome
#>    growth      id    z predictor
#> 
#> Posterior reliability (structural scope; equal unit weights):
#>     model term      role      measure reliability n_units n_defined
#>  response    w   outcome     marginal       0.575      70        70
#>  response    w   outcome residualized       0.606      70        70
#>    growth    z predictor     marginal       0.877      70        70
#>    growth    z predictor residualized       0.793      70        70
#> 
#>                            estimate      se   zstat   pval   ci_low ci_high
#> (Intercept)               -0.005505 0.08996 -0.0612 0.9512 -0.18182  0.1708
#> re("z", model = "growth")  0.163992 0.10013  1.6378 0.1015 -0.03226  0.3602
#> 
#> Regularization (preliminary / final): minimum / minimum 
#> Error-moment subtraction multipliers: 0.9571 / 0.9571 
#> Largest analysis-weight fraction: 0.0196 ; weight-equivalent count: 64.9 
#> Moment conditioning and unit weights: repair_diagnostics(fit).
#> Conditional on fitted MLM parameters; model-based normal SEs and intervals.