
Extracting tables for reporting and reuse
Source:vignettes/extracting-results.Rmd
extracting-results.RmdChoose an extractor
The public table extractors let you report results, inspect unit-level predictions, and join data without accessing internal S7 slots. All return ordinary data frames with documented column names, types, keys, and ordering. They introduce no new fitting choices and do not require a tidyverse or broom dependency.
| Question | Interface |
|---|---|
| What are the structural estimates and intervals? | repair_coefficients(fit) |
| What predictions and sampling-error SDs were used? | repair_predictions(fit) |
| How do errors in different predictions covary? | repair_prediction_covariance(fit) |
| Which people were excluded? | repair_diagnostics(fit, table = "excluded") |
| What are the analysis weights? | repair_diagnostics(fit, table = "weights") |
| What is posterior reliability? | repair_reliability(fit) |
| What is the covariance of structural coefficient estimates? | vcov(fit) |
coef(), vcov(), confint(),
nobs(), summary(), and the existing full
repair_diagnostics(fit) list remain available. Prediction
uncertainty, posterior uncertainty, and structural coefficient
uncertainty have different meanings.
Fit an example
The unit-level category below is illustrative, not an actual sleepstudy variable.
growth <- lme4::lmer(Reaction ~ Days + (Days | Subject), lme4::sleepstudy)
people <- data.frame(person = levels(lme4::sleepstudy$Subject),
category = factor(rep(c("A", "B", "C"), 6)))
models <- list(growth = growth)
structural <- re("Days", "growth") ~ re("(Intercept)", "growth") + category
fit <- repair(structural, models, people, id = "person")Report structural coefficients
coefficients <- repair_coefficients(fit)
knitr::kable(coefficients, digits = 3)| term | estimate | std.error | statistic | p.value | conf.low | conf.high | conf.level |
|---|---|---|---|---|---|---|---|
| (Intercept) | 2.645 | 2.907 | 0.910 | 0.363 | -3.053 | 8.343 | 0.95 |
| re(“(Intercept)”, model = “growth”) | -0.020 | 0.077 | -0.258 | 0.796 | -0.172 | 0.132 | 0.95 |
| categoryB | -3.882 | 4.136 | -0.939 | 0.348 | -11.988 | 4.225 | 0.95 |
| categoryC | -4.053 | 4.126 | -0.982 | 0.326 | -12.140 | 4.035 | 0.95 |
repair_coefficients(fit, conf.level = .90)
#> term estimate std.error statistic
#> 1 (Intercept) 2.64485017 2.90734375 0.9097136
#> 2 re("(Intercept)", model = "growth") -0.01998463 0.07734666 -0.2583774
#> 3 categoryB -3.88158764 4.13588008 -0.9385155
#> 4 categoryC -4.05296288 4.12634706 -0.9822157
#> p.value conf.low conf.high conf.level
#> 1 0.3629736 -2.1373047 7.4270051 0.9
#> 2 0.7961157 -0.1472086 0.1072393 0.9
#> 3 0.3479795 -10.6845050 2.9213297 0.9
#> 4 0.3259936 -10.8401998 2.7342740 0.9The columns are term, estimate,
std.error, statistic, p.value,
conf.low, conf.high, and
conf.level. term is character; the remaining
columns are numeric. Rows follow coef(fit), beginning with
the structural intercept. Factor contrast columns retain their
model-matrix names. Random-effect predictors retain model-qualified
formula labels. Use these labels as identifiers; do not parse model or
coefficient identity back out of printed text.
Changing conf.level changes only the normal interval
endpoints and reported level. Estimates remain on the input scale, and
inference remains conditional on fitted MLM quantities and supplied
error covariance. This table does not standardize coefficients or
incorporate additional parameter uncertainty.
Inspect selected predictions
predictions <- repair_predictions(fit)
head(predictions)
#> id model group term label role prediction
#> 1 308 growth Subject Days re("Days", model = "growth") outcome 11.2974165
#> 2 309 growth Subject Days re("Days", model = "growth") outcome -8.2055005
#> 3 310 growth Subject Days re("Days", model = "growth") outcome -4.3523872
#> 4 330 growth Subject Days re("Days", model = "growth") outcome -7.4592132
#> 5 331 growth Subject Days re("Days", model = "growth") outcome -5.2012672
#> 6 332 growth Subject Days re("Days", model = "growth") outcome -0.9005181
#> std.error prediction.error.variance scope analyzed status reason
#> 1 2.817564 7.938667 structural TRUE ok
#> 2 2.817564 7.938667 structural TRUE ok
#> 3 2.817564 7.938667 structural TRUE ok
#> 4 2.817564 7.938667 structural TRUE ok
#> 5 2.817564 7.938667 structural TRUE ok
#> 6 2.817564 7.938667 structural TRUE okThe schema is id, model,
group, term, label,
role, prediction, std.error,
prediction.error.variance, scope,
analyzed, status, and reason.
Identities, roles, scope, and status/reason are character; prediction
and uncertainty columns are numeric; analyzed is logical.
The unique key is (model, group, term, id). The qualified
label connects random-effect predictors to
coefficient-table term labels. IDs are always character, including
originally numeric grouping levels.
Rows follow selected-effect order, then ID order within each effect.
Default scope = "structural" uses retained analysis IDs in
their person-table order. scope = "all" uses each model’s
saved effective grouping-level order:
subset_fit <- repair(structural, models, people[3:18, ], "person")
mlm_predictions <- repair_predictions(subset_fit, scope = "all")
mlm_predictions[!mlm_predictions$analyzed, c("id", "term", "prediction", "status")]
#> id term prediction status
#> 1 308 Days 11.297416 ok
#> 2 309 Days -8.205501 ok
#> 19 308 (Intercept) -7.212436 ok
#> 20 309 (Intercept) -46.350159 okOnly formula-selected effects are returned, even for the MLM scope.
An excluded unit can have an "ok" prediction, for example
if an observed structural predictor was missing. A failed prediction
remains a row with status "failed" and a reason; its saved
missing values stay missing. An ID absent from a measurement model has
no prediction row. Inspect
repair_diagnostics(fit, "excluded") to find those IDs and
all exclusion reasons.
Predictions are likelihood-only random deviations.
Their std.error is a conditional sampling-error SD, not a
BLUP posterior SD, a structural coefficient SE, or the person’s total
fixed-plus-random effect.
These same prediction interfaces work before structural fitting:
assembled <- repair_data(structural, models, people, "person")
stopifnot(identical(repair_predictions(assembled), predictions))
stopifnot(identical(repair_prediction_covariance(assembled), repair_prediction_covariance(fit)))Preserve covariance when reusing predictions
Prediction variances do not capture correlated errors. The covariance extractor reads the assembled error inputs, including the selected cross-model policy:
prediction_error_covariance <- repair_prediction_covariance(fit)
head(prediction_error_covariance)
#> id model1 group1 term1 label1 role1 model2 group2
#> 1 308 growth Subject Days re("Days", model = "growth") outcome growth Subject
#> 2 309 growth Subject Days re("Days", model = "growth") outcome growth Subject
#> 3 310 growth Subject Days re("Days", model = "growth") outcome growth Subject
#> 4 330 growth Subject Days re("Days", model = "growth") outcome growth Subject
#> 5 331 growth Subject Days re("Days", model = "growth") outcome growth Subject
#> 6 332 growth Subject Days re("Days", model = "growth") outcome growth Subject
#> term2 label2 role2 covariance source
#> 1 Days re("Days", model = "growth") outcome 7.938667 within_model
#> 2 Days re("Days", model = "growth") outcome 7.938667 within_model
#> 3 Days re("Days", model = "growth") outcome 7.938667 within_model
#> 4 Days re("Days", model = "growth") outcome 7.938667 within_model
#> 5 Days re("Days", model = "growth") outcome 7.938667 within_model
#> 6 Days re("Days", model = "growth") outcome 7.938667 within_modelColumns are id, the two sets of identities
model1, group1, term1,
label1, role1 and model2,
group2, term2, label2,
role2, followed by numeric covariance and
character source. All other columns are character. Each
pair appears once per unit, including the diagonal. Pairs follow the
lower triangle in selected-effect order: (1,1), (2,1), (2,2), and so on;
IDs within a pair follow analysis order. The key is ID plus the two
model/group/term identities.
For multiple models, source distinguishes
"within_model", "assumed_independent", and
"supplied". An explicitly supplied zero covariance retains
source "supplied". The table includes analyzed IDs only:
externally supplied covariance need not cover excluded people, so there
is no measurement- scope option. Error-free observed columns are
omitted, and their zero error assumption remains unchanged.
Mirror off-diagonal rows to reconstruct a symmetric selected-effect covariance:
labels <- unique(predictions$label)
one <- prediction_error_covariance[prediction_error_covariance$id == people$person[1], ]
S <- matrix(0, length(labels), length(labels), dimnames = list(labels, labels))
for (i in seq_len(nrow(one))) {
S[one$label1[i], one$label2[i]] <- S[one$label2[i], one$label1[i]] <- one$covariance[i]
}
S
#> re("Days", model = "growth")
#> re("Days", model = "growth") 7.938667
#> re("(Intercept)", model = "growth") -35.724000
#> re("(Intercept)", model = "growth")
#> re("Days", model = "growth") -35.724
#> re("(Intercept)", model = "growth") 226.252This is prediction-error covariance for one person.
vcov(fit) instead returns the covariance of estimated
structural regression coefficients. Exporting only predictions and
treating them as error-free in a later regression would discard REPAIR’s
correction. For deliberate custom estimation, see the
fit_mmom() input contract.
Select diagnostic tables and join by identity
repair_diagnostics(fit, table = "scope")
#> n_requested n_analyzed n_excluded
#> 1 18 18 0
repair_diagnostics(fit, table = "excluded")
#> [1] id reason
#> <0 rows> (or 0-length row.names)
head(repair_diagnostics(fit, table = "weights"))
#> id weight fraction
#> 1 308 0.02133903 0.05555556
#> 2 309 0.02133903 0.05555556
#> 3 310 0.02133903 0.05555556
#> 4 330 0.02133903 0.05555556
#> 5 331 0.02133903 0.05555556
#> 6 332 0.02133903 0.05555556
repair_diagnostics(fit, table = "regularization")
#> step policy alpha subtraction_multiplier regularized
#> 1 preliminary minimum 5 0.7222222 TRUE
#> 2 final minimum 5 0.7222222 TRUEThe table name must match exactly; no partial matching occurs.
Omitting table returns the original diagnostics list.
Selecting a table other than reliability avoids calculating reliability.
cross_model_error is policy metadata in that list, not a
selectable data frame.
| Table | Columns |
|---|---|
status |
converged, step, message |
scope |
n_requested, n_analyzed, n_excluded |
excluded |
id, reason |
mlm |
model, group, backend, singular, n_levels, n_outside_requested, messages |
prediction_failures |
model, group, id, analyzed, reason |
prediction_error_variances |
id, term, role, variance |
regularization |
step, policy, alpha, subtraction_multiplier, regularized |
conditioning |
step, dimension, min_eigenvalue, max_eigenvalue, condition_number |
weights |
id, weight, fraction |
weight_summary |
n_units, max_fraction, weight_equivalent_n |
reliability |
model, group, term, role, measure, scope, n_units, n_defined, n_undefined, reliability, mean_defined, min, median, max, status, reason |
All selected tables have automatic row names; IDs and effect names are explicit columns. Numerical structural failure leaves typed zero-row structural diagnostic tables, while status and MLM diagnostics remain available. Coefficient extraction stops on failure; predictions and their covariance remain inspectable.
Join predictions with marginal posterior reliability using explicit
identities and a matching scope. Do not join on row number or on
term alone across models:
reliability <- repair_reliability(fit, detail = "unit", type = "marginal")
keys <- c("model", "group", "term", "id", "scope")
joined <- merge(predictions, reliability[c(keys, "reliability")], by = keys,
all.x = TRUE, sort = FALSE)
head(joined[, c("id", "model", "term", "prediction", "std.error", "reliability")])
#> id model term prediction std.error reliability
#> 1 308 growth Days 11.2974165 2.817564 0.8485308
#> 2 309 growth Days -8.2055005 2.817564 0.8485308
#> 3 310 growth Days -4.3523872 2.817564 0.8485308
#> 4 330 growth Days -7.4592132 2.817564 0.8485308
#> 5 331 growth Days -5.2012672 2.817564 0.8485308
#> 6 332 growth Days -0.9005181 2.817564 0.8485308
stopifnot(nrow(joined) == nrow(predictions), !anyNA(joined$reliability))A join may change row order; retain and use the identity columns.
Export ordinary data frames
No S7-aware serialization or special table package is needed for extracted tables:
write.csv(repair_coefficients(fit), "repair-coefficients.csv", row.names = FALSE)
write.csv(repair_predictions(fit), "repair-predictions.csv", row.names = FALSE)
saveRDS(list(predictions = repair_predictions(fit),
score_covariance = repair_prediction_covariance(fit),
coefficients = repair_coefficients(fit)), "repair-tables.rds")When importing CSV, read IDs as character to preserve identifiers
such as "001". RDS preserves column types. Keep model
specification, scope choices, and the cross-model covariance policy with
your analysis record. These extractors stabilize routine reporting and
inspection; they do not promise that internal S7 slots will remain a
permanent reporting API.