450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531 | def create_analysis_bundle(
fit: Any,
newdata: pd.DataFrame | None = None,
ndraws: int = 1000,
include_group_effects: bool = False,
include_loo: bool = False,
) -> AnalysisBundle:
family = getattr(fit, "family", None)
if family not in {"binary", "duration"}:
raise GP3BayesError("`fit` must be an approved gp3bayes fit.")
from .postfit_exploration import (
group_effect_table,
posterior_interval_table,
summarise_mcmc_quality,
variance_component_table,
)
from .predictive import (
audit_prediction_support,
binary_calibration_table,
binary_prediction_scores,
duration_prediction_scores,
duration_quantile_calibration,
predict_model,
predictive_coverage_table,
)
training = getattr(getattr(getattr(fit, "specification", None), "prepared", None), "data", None)
target = training if newdata is None else newdata
components = {
"posterior": _capture(posterior_interval_table, fit, regex=r"^(b_|sd_|cor_|sigma$)"),
"mcmc": _capture(summarise_mcmc_quality, fit),
"prediction_support": _capture(audit_prediction_support, fit, target),
"expected_prediction": _capture(
predict_model,
fit,
newdata=newdata,
type="median" if family == "duration" else "expected",
include_group_effects=include_group_effects,
ndraws=ndraws,
),
"predictive": _capture(
predict_model,
fit,
newdata=newdata,
type="predictive",
include_group_effects=include_group_effects,
ndraws=ndraws,
seed=1,
),
"group_effects": _capture(group_effect_table, fit),
"variance_components": _capture(variance_component_table, fit),
}
expected = components["expected_prediction"]
if expected.ok and getattr(expected.value, "observed", None) is not None:
components["scores"] = _capture(
binary_prediction_scores if family == "binary" else duration_prediction_scores,
expected.value,
)
if family == "binary":
components["calibration"] = _capture(binary_calibration_table, expected.value)
predictive = components["predictive"]
if predictive.ok and getattr(predictive.value, "observed", None) is not None:
components["coverage"] = _capture(predictive_coverage_table, predictive.value)
if family == "duration":
components["quantile_calibration"] = _capture(
duration_quantile_calibration, predictive.value
)
if include_loo:
from .advanced_optional_workflows import compute_psis_loo
components["loo"] = _capture(compute_psis_loo, fit)
status = pd.DataFrame(
[
{
"component": name,
"available": item.ok,
"error": "" if item.error is None else item.error,
}
for name, item in components.items()
]
)
return AnalysisBundle("0.3", family, fit, components, status, bool(include_loo))
|