diagnostics
Diagnostic functions for brms models with ArviZ integration.
This module provides diagnostic functions for analyzing fitted brms models.
All fitted models return arviz.InferenceData objects by default through the
.idata attribute, enabling seamless integration with ArviZ's diagnostic toolkit.
ArviZ Integration
brmspy models work directly with ArviZ functions without conversion:
- Summary & Convergence:
az.summary(),az.rhat(),az.ess() - Visualization:
az.plot_trace(),az.plot_posterior(),az.plot_pair() - Model Comparison:
az.loo(),az.waic(),az.compare() - Predictive Checks:
az.plot_ppc()
For multivariate models, use the var_name parameter in ArviZ functions
to specify which response variable to analyze (e.g., az.loo(model.idata, var_name="y1")).
Quick Example
import brmspy
import arviz as az
# Fit model
model = brmspy.fit("count ~ zAge + (1|patient)", data=data, family="poisson")
# Diagnostics
print(az.summary(model.idata)) # Parameter estimates with Rhat, ESS
az.plot_trace(model.idata) # MCMC trace plots
az.plot_ppc(model.idata) # Posterior predictive check
# Model comparison
loo = az.loo(model.idata)
print(loo)
See Also
Diagnostics with ArviZ : Complete guide with examples https://kaitumisuuringute-keskus.github.io/brmspy/api/diagnostics-arviz/
Notes
The InferenceData structure contains:
- posterior: All parameter samples with brms naming (e.g.,
b_Intercept,sd_patient__Intercept) - posterior_predictive: Posterior predictive samples for each response
- log_likelihood: Pointwise log-likelihood for LOO/WAIC
- observed_data: Original response values
Attributes¶
FitResult = IDResult[IDBrm]
module-attribute
¶
Classes¶
SummaryResult
dataclass
¶
Bases: RListVectorExtension
Parsed summary output for a fitted model.
This is a convenience container that holds both:
- structured Python data (mostly pandas DataFrames)
- the underlying R object reference in
.r(as a worker-side handle)
Attributes:
| Name | Type | Description |
|---|---|---|
formula |
str
|
Model formula string as reported by brms. |
data_name |
str
|
Data name as reported by brms (may be an internal label). |
nobs |
int
|
Number of observations. |
prior |
DataFrame
|
Prior summary table. |
fixed |
DataFrame
|
Fixed effects summary table. |
random |
dict[str, DataFrame] or None
|
Random effects summary tables (if present). |
Source code in brmspy/types/brms_results.py
Attributes¶
formula
instance-attribute
¶
data_name
instance-attribute
¶
group
instance-attribute
¶
nobs
instance-attribute
¶
prior
instance-attribute
¶
algorithm
instance-attribute
¶
sampler
instance-attribute
¶
total_ndraws
instance-attribute
¶
chains
instance-attribute
¶
iter
instance-attribute
¶
warmup
instance-attribute
¶
thin
instance-attribute
¶
has_rhat
instance-attribute
¶
fixed
instance-attribute
¶
spec_pars = None
class-attribute
instance-attribute
¶
cor_pars = None
class-attribute
instance-attribute
¶
rescor_pars = None
class-attribute
instance-attribute
¶
ngrps = None
class-attribute
instance-attribute
¶
autocor = None
class-attribute
instance-attribute
¶
random = None
class-attribute
instance-attribute
¶
_str = None
class-attribute
instance-attribute
¶
Functions¶
__str__()
¶
__repr__()
¶
__init__(r, formula, data_name, group, nobs, prior, algorithm, sampler, total_ndraws, chains, iter, warmup, thin, has_rhat, fixed, spec_pars=None, cor_pars=None, rescor_pars=None, ngrps=None, autocor=None, random=None, _str=None)
¶
Functions¶
iterate_robject_to_dataclass(names, get, target_dataclass, r, iteration_params=None)
¶
Generic helper to iterate over R summary-like objects and convert them into a Python dataclass instance.
namesis e.g. summary_r.namesget(param)should return the R slot already converted via rpy2 (or raw)target_dataclassis a @dataclass whose field names mirror the params
Source code in brmspy/helpers/_rpy2/_robject_iter.py
kwargs_r(kwargs)
¶
Convert Python keyword arguments to R-compatible format.
Convenience function that applies py_to_r() to all values in a keyword arguments dictionary, preparing them for R function calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
dict or None
|
Dictionary of keyword arguments where values may be Python objects (dicts, lists, DataFrames, arrays, etc.) |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with same keys but R-compatible values, or empty dict if None |
Notes
This is a thin wrapper around py_to_r() that operates on dictionaries.
It's commonly used to prepare keyword arguments for R function calls via rpy2.
Examples:
from brmspy.helpers.conversion import kwargs_r
import pandas as pd
import numpy as np
# Prepare kwargs for R function
py_kwargs = {
'data': pd.DataFrame({'y': [1, 2], 'x': [1, 2]}),
'prior': {'b': [0, 1]},
'chains': 4,
'iter': 2000
}
r_kwargs = kwargs_r(py_kwargs)
# All values converted to R objects
# Can now call: r_function(**r_kwargs)
See Also
py_to_r : Underlying conversion function for individual values brmspy.brms.fit : Uses this to prepare user kwargs for R
Source code in brmspy/helpers/_rpy2/_conversion.py
py_to_r(obj)
¶
Convert arbitrary Python objects to R objects via rpy2.
Comprehensive converter that handles nested structures (dicts, lists), DataFrames, arrays, and scalars. Uses rpy2's converters with special handling for dictionaries (→ R named lists) and lists of dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
any
|
Python object to convert. Supported types: - None → R NULL - dict → R named list (ListVector), recursively - list/tuple of dicts → R list of named lists - list/tuple (other) → R vector or list - pd.DataFrame → R data.frame - np.ndarray → R vector/matrix - scalars (int, float, str, bool) → R atomic types |
required |
Returns:
| Type | Description |
|---|---|
rpy2 R object
|
R representation of the Python object |
Notes
Conversion Rules:
- None: → R NULL
- DataFrames: → R data.frame (via pandas2ri)
- Dictionaries: → R named list (ListVector), recursively converting values
- Lists of dicts: → R list with 1-based indexed names containing named lists
- Other lists/tuples: → R vectors or lists (via rpy2 default)
- NumPy arrays: → R vectors/matrices (via numpy2ri)
- Scalars: → R atomic values
Recursive Conversion:
Dictionary values are recursively converted, allowing nested structures:
List of Dicts:
Lists containing only dicts are converted to R lists with 1-based indexing:
Examples:
from brmspy.helpers.conversion import py_to_r
import numpy as np
import pandas as pd
# Scalars
py_to_r(5) # R: 5
py_to_r("hello") # R: "hello"
py_to_r(None) # R: NULL
# Arrays
py_to_r(np.array([1, 2, 3])) # R: c(1, 2, 3)
# DataFrames
df = pd.DataFrame({'x': [1, 2], 'y': [3, 4]})
py_to_r(df) # R: data.frame(x = c(1, 2), y = c(3, 4))
See Also
r_to_py : Convert R objects back to Python kwargs_r : Convert keyword arguments dict for R function calls brmspy.brms.fit : Uses this for converting data to R
Source code in brmspy/helpers/_rpy2/_converters/_dispatch.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
r_to_py(obj, shm=None)
¶
Convert R objects to Python objects via rpy2.
Comprehensive converter that handles R lists (named/unnamed), vectors, formulas, and language objects. Provides sensible Python equivalents for all R types with special handling for edge cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
rpy2 R object
|
R object to convert to Python |
required |
Returns:
| Type | Description |
|---|---|
any
|
Python representation of the R object: - R NULL → None - Named list → dict (recursively) - Unnamed list → list (recursively) - Length-1 vector → scalar (int, float, str, bool) - Length-N vector → list of scalars - Formula/Language object → str (descriptive representation) - Other objects → default rpy2 conversion or str fallback |
Notes
Conversion Rules:
- R NULL: → Python None
- Atomic vectors (numeric, character, logical):
- Length 1: → Python scalar (int, float, str, bool)
- Length >1: → Python list of scalars
- Named lists (ListVector with names): → Python dict, recursively
- Unnamed lists: → Python list, recursively
- Formulas (e.g.,
y ~ x): → String representation - Language objects (calls, expressions): → String representation
- Functions: → String representation
- Everything else: Try default rpy2 conversion, fallback to string
Recursive Conversion:
List elements and dictionary values are recursively converted:
Safe Fallback:
R language objects, formulas, and functions are converted to descriptive strings rather than attempting complex conversions that might fail.
Examples:
from brmspy.helpers.conversion import r_to_py
import rpy2.robjects as ro
# R NULL
r_to_py(ro.NULL) # None
# Scalars
r_to_py(ro.IntVector([5])) # 5
r_to_py(ro.FloatVector([3.14])) # 3.14
r_to_py(ro.StrVector(["hello"])) # "hello"
# Vectors
r_to_py(ro.IntVector([1, 2, 3])) # [1, 2, 3]
See Also
py_to_r : Convert Python objects to R brmspy.brms.summary : Returns Python-friendly summary dict
Source code in brmspy/helpers/_rpy2/_converters/_dispatch.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
summary(model, **kwargs)
¶
Generate comprehensive summary statistics for a fitted brms model.
Returns a SummaryResult dataclass containing model information,
parameter estimates, and diagnostic information. The SummaryResult object provides
pretty printing via str() or print() and structured access to all components.
BRMS documentation and parameters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
FitResult
|
Fitted model returned by |
required |
**kwargs
|
Additional arguments passed to brms::summary(), such as:
- probs: Quantiles for credible intervals, e.g., |
{}
|
Returns:
| Type | Description |
|---|---|
SummaryResult
|
A dataclass containing:
|
See Also
brms::summary.brmsfit : R documentation
Examples:
Basic usage with pretty printing:
import brmspy
model = brmspy.fit("y ~ x", data=data, chains=4)
summary = brmspy.summary(model)
# Pretty print full summary
print(summary)
Access specific components:
# Get population-level effects as DataFrame
fixed_effects = summary.fixed
print(fixed_effects)
# Get family-specific parameters (e.g., sigma)
spec_params = summary.spec_pars
print(spec_params)
# Access random effects (if present)
random_effects = summary.random
for group_name, group_df in random_effects.items():
print(f"Random effects for {group_name}:")
print(group_df)
# Check model metadata
print(f"Formula: {summary.formula}")
print(f"Total draws: {summary.total_ndraws}")
print(f"Rhat reported: {summary.has_rhat}")
Custom credible intervals:
# Use 90% credible intervals instead of default 95%
summary_90 = brmspy.summary(model, probs=(0.05, 0.95))
print(summary_90.fixed)
Source code in brmspy/_brms_functions/diagnostics.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
fixef(object, summary=True, robust=False, probs=(0.025, 0.975), pars=None, **kwargs)
¶
Extract population-level (fixed) effects estimates from a fitted brms model.
Returns a pandas DataFrame containing estimates and uncertainty intervals for
all population-level parameters (fixed effects). By default, returns summary
statistics (mean, standard error, credible intervals). Can also return raw
posterior samples when summary=False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
FitResult or ListSexpVector
|
Fitted model returned by |
required |
summary
|
bool
|
If True, return summary statistics (mean/median, SE/MAD, credible intervals). If False, return matrix of posterior samples (iterations × parameters). |
True
|
robust
|
bool
|
If True, use median and MAD instead of mean and SD for summary statistics.
Only used when |
False
|
probs
|
tuple of float
|
Quantiles for credible intervals, e.g., (0.025, 0.975) for 95% intervals.
Only used when |
(0.025, 0.975)
|
pars
|
list of str
|
Specific parameter names to extract. If None, returns all fixed effects. Useful for subsetting when you only need specific coefficients. |
None
|
**kwargs
|
Additional arguments passed to brms::fixef() |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
When When |
See Also
brms::fixef.brmsfit : R documentation summary() : Full model summary with all parameter types
Examples:
Basic usage with summary statistics:
import brmspy
model = brmspy.fit("y ~ x1 + x2", data=data, chains=4)
# Get fixed effects summary
fixed_effects = brmspy.fixef(model)
print(fixed_effects)
# Estimate Est.Error Q2.5 Q97.5
# Intercept 10.234567 0.123456 9.992345 10.47689
# x1 0.456789 0.098765 0.263456 0.65012
# x2 -0.234567 0.087654 -0.406789 -0.06234
Get specific parameters only:
# Extract only specific coefficients
x1_x2_effects = brmspy.fixef(model, pars=["x1", "x2"])
print(x1_x2_effects)
Use robust estimates (median and MAD):
# Use median and MAD instead of mean and SD
robust_effects = brmspy.fixef(model, robust=True)
print(robust_effects)
Custom credible intervals:
Get raw posterior samples:
# Get full posterior samples matrix
samples = brmspy.fixef(model, summary=False)
print(samples.shape) # (n_iterations, n_parameters)
# Can then compute custom statistics
import numpy as np
custom_quantile = np.percentile(samples["x1"], 90)
Source code in brmspy/_brms_functions/diagnostics.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
ranef(object, summary=True, robust=False, probs=(0.025, 0.975), pars=None, groups=None, **kwargs)
¶
Extract group-level (random) effects as xarray DataArrays.
This is a wrapper around brms::ranef(). For summary=True (default),
each grouping factor is returned as a 3D array with dimensions
("group", "stat", "coef"). For summary=False, each factor is
returned as ("draw", "group", "coef") with one slice per posterior draw.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
FitResult or ListVector
|
Fitted model returned by :func: |
required |
summary
|
bool
|
If True, return posterior summaries for the group-level effects (means, errors, intervals). If False, return per-draw random effects. |
True
|
robust
|
bool
|
If True, use robust summaries (median and MAD) instead of mean and SD.
Passed through to |
False
|
probs
|
tuple of float
|
Central posterior interval probabilities, as in |
(0.025, 0.975)
|
pars
|
str or sequence of str
|
Subset of group-level parameters to include. Passed to |
None
|
groups
|
str or sequence of str
|
Subset of grouping factors to include. Passed to |
None
|
**kwargs
|
Additional keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, DataArray]
|
Mapping from grouping-factor name (e.g.
|
Examples:
Compute summary random effects and inspect all coefficients for a single group level:
from brmspy import brms
from brmspy.brms import ranef
fit = brms.fit("count ~ zAge + zBase * Trt + (1 + zBase + Trt | patient)",
data=data, family="poisson")
re = ranef(fit) # summary=True by default
patient_re = re["patient"].sel(group="1", stat="Estimate")
Extract per-draw random effects for downstream MCMC analysis:
re_draws = ranef(fit, summary=False)
patient_draws = re_draws["patient"] # dims: ("draw", "group", "coef")
first_draw = patient_draws.sel(draw=0)
Source code in brmspy/_brms_functions/diagnostics.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
posterior_summary(object, variable=None, probs=(0.025, 0.975), robust=False, **kwargs)
¶
Extract posterior summary statistics for all or selected model parameters.
Provides a DataFrame with estimates, standard errors, and credible intervals
for all parameters in a brms model, including fixed effects, random effects,
and auxiliary parameters. More comprehensive than fixef() or ranef() as it covers all
parameter types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
FitResult or ListSexpVector
|
Fitted model returned by |
required |
variable
|
str or list of str
|
Specific variable name(s) to extract. If None, returns all parameters. Supports regex patterns for flexible selection. |
None
|
probs
|
tuple of float
|
Quantiles for credible intervals, e.g., (0.025, 0.975) for 95% intervals. |
(0.025, 0.975)
|
robust
|
bool
|
If True, use median and MAD instead of mean and SD for summary statistics. |
False
|
**kwargs
|
Additional arguments passed to brms::posterior_summary() |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with parameters as rows and columns for Estimate, Est.Error, and quantiles (e.g., Q2.5, Q97.5). Includes all model parameters: population-level effects, group-level effects, and auxiliary parameters. |
See Also
brms::posterior_summary : R documentation https://paulbuerkner.com/brms/reference/posterior_summary.brmsfit.html fixef() : Extract only population-level effects ranef() : Extract only group-level effects
Examples:
Get summary for all parameters:
import brmspy
model = brmspy.fit("y ~ x1 + (1|group)", data=data, chains=4)
# Get all parameter summaries
all_params = brmspy.posterior_summary(model)
print(all_params)
Extract specific parameters:
# Get summary for specific parameters
intercept = brmspy.posterior_summary(model, variable="b_Intercept")
print(intercept)
# Multiple parameters
fixed_only = brmspy.posterior_summary(model, variable=["b_Intercept", "b_x1"])
print(fixed_only)
Custom credible intervals with robust estimates:
# 90% intervals with median/MAD
robust_summary = brmspy.posterior_summary(
model,
probs=(0.05, 0.95),
robust=True
)
print(robust_summary)
Source code in brmspy/_brms_functions/diagnostics.py
445 446 447 448 449 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 532 533 534 535 536 537 | |
prior_summary(object, all=True, **kwargs)
¶
Extract prior specifications used in a fitted brms model.
Returns a DataFrame containing all prior distributions that were used (either explicitly set or defaults) when fitting the model. Useful for documenting model specifications and understanding which priors were applied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
FitResult or ListVector
|
Fitted model returned by |
required |
all
|
bool
|
If True, return all priors including default priors. If False, return only explicitly set priors. |
True
|
**kwargs
|
Additional arguments passed to brms::prior_summary() |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns describing prior specifications: - prior: Prior distribution formula - class: Parameter class (b, sd, Intercept, etc.) - coef: Specific coefficient (if applicable) - group: Grouping factor (if applicable) - resp: Response variable (for multivariate models) - dpar: Distributional parameter (if applicable) - nlpar: Non-linear parameter (if applicable) - lb/ub: Bounds for truncated priors - source: Origin of prior (default, user, etc.) |
See Also
brms::prior_summary : R documentation https://paulbuerkner.com/brms/reference/prior_summary.brmsfit.html get_prior() : Get prior structure before fitting default_prior() : Get default priors for a model
Examples:
Get all priors used in a model:
import brmspy
model = brmspy.fit(
"y ~ x1 + (1|group)",
data=data,
priors=[brmspy.prior("normal(0, 1)", "b")],
chains=4
)
# Get all priors (including defaults)
priors = brmspy.prior_summary(model)
print(priors)
Get only explicitly set priors:
# Get only user-specified priors
user_priors = brmspy.prior_summary(model, all=False)
print(user_priors)
Compare with what would be used before fitting:
# Before fitting - check default priors
default_priors = brmspy.get_prior("y ~ x1", data=data)
# After fitting - see what was actually used
used_priors = brmspy.prior_summary(model)
Source code in brmspy/_brms_functions/diagnostics.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 | |
validate_newdata(newdata, object, re_formula=None, allow_new_levels=False, newdata2=None, resp=None, check_response=True, incl_autocor=True, group_vars=None, req_vars=None, **kwargs)
¶
Validate new data for predictions from a fitted brms model.
Ensures that new data contains all required variables and has the correct structure for making predictions. Checks variable types, factor levels, grouping variables, and autocorrelation structures. This function is primarily used internally by prediction methods but can be called directly for debugging or validation purposes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
newdata
|
DataFrame
|
DataFrame containing new data to be validated against the model. Must include all predictor variables used in the model formula. |
required |
object
|
FitResult or ListSexpVector
|
Fitted model returned by |
required |
re_formula
|
str
|
Formula string specifying group-level effects to include in validation. If None (default), include all group-level effects. If NA, include no group-level effects. |
None
|
allow_new_levels
|
bool
|
Whether to allow new levels of grouping variables not present in the original training data. If False, raises an error for new levels. |
False
|
newdata2
|
DataFrame
|
Additional data that cannot be passed via |
None
|
resp
|
str or list of str
|
Names of response variables to validate. If specified, validation is performed only for the specified responses (relevant for multivariate models). |
None
|
check_response
|
bool
|
Whether to check if response variables are present in newdata. Set to False when making predictions where response is not needed. |
True
|
incl_autocor
|
bool
|
Whether to include autocorrelation structures originally specified in the model. If True, validates autocorrelation-related variables. |
True
|
group_vars
|
list of str
|
Names of specific grouping variables to validate. If None (default), validates all grouping variables present in the model. |
None
|
req_vars
|
list of str
|
Names of specific variables required in newdata. If None (default), all variables from the original training data are required (unless excluded by other parameters). |
None
|
**kwargs
|
Additional arguments passed to brms::validate_newdata() |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Validated DataFrame based on newdata, potentially with added or modified columns to ensure compatibility with the model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If newdata is missing required variables |
ValueError
|
If factor levels in newdata don't match those in training data (when allow_new_levels=False) |
ValueError
|
If grouping variables have invalid structure |
See Also
brms::validate_newdata : R documentation https://paulbuerkner.com/brms/reference/validate_newdata.html posterior_predict() : Uses validate_newdata internally posterior_epred() : Uses validate_newdata internally
Examples:
Basic validation for prediction data:
import brmspy
import pandas as pd
# Fit model
model = brmspy.fit("y ~ x1 + x2", data=train_data, chains=4)
# Prepare new data
new_data = pd.DataFrame({
'x1': [1.0, 2.0, 3.0],
'x2': [0.5, 1.0, 1.5]
})
# Validate before prediction
validated_data = brmspy.validate_newdata(new_data, model)
print(validated_data)
Validate with group-level effects:
# Model with random effects
model = brmspy.fit("y ~ x + (1|group)", data=train_data, chains=4)
# New data with grouping variable
new_data = pd.DataFrame({
'x': [1.0, 2.0],
'group': ['A', 'B'] # Must match training data groups
})
# Validate - will error if groups A or B weren't in training
validated_data = brmspy.validate_newdata(
new_data,
model,
allow_new_levels=False
)
Allow new levels for population-level predictions:
# Allow new group levels (makes population-level predictions only)
new_data_with_new_groups = pd.DataFrame({
'x': [3.0, 4.0],
'group': ['C', 'D'] # New groups not in training
})
validated_data = brmspy.validate_newdata(
new_data_with_new_groups,
model,
allow_new_levels=True
)
Skip response variable checking:
# When making predictions, response not needed
new_data = pd.DataFrame({'x1': [1.0, 2.0]})
validated_data = brmspy.validate_newdata(
new_data,
model,
check_response=False
)
Source code in brmspy/_brms_functions/diagnostics.py
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 | |