Skip to content

prediction

Classes

Functions

posterior_epred(model, newdata, **kwargs)

Compute expected value of posterior predictive distribution.

Calls brms::posterior_epred() to get E[Y|data] without observation noise.

BRMS documentation and parameters

Parameters:

Name Type Description Default
model FitResult

Fitted model from fit()

required
newdata DataFrame

Data for predictions

required
**kwargs

Additional arguments to brms::posterior_epred()

{}

Returns:

Type Description
PosteriorEpredResult

Object with .idata and .r attributes

posterior_predict(model, newdata=None, **kwargs)

Generate posterior predictive samples with observation noise.

Calls brms::posterior_predict() to get samples of Y_new|data.

BRMS documentation and parameters

Parameters:

Name Type Description Default
model FitResult

Fitted model from fit()

required
newdata DataFrame

Data for predictions. If None, uses original data

None
**kwargs

Additional arguments to brms::posterior_predict()

{}

Returns:

Type Description
PosteriorPredictResult

Object with .idata and .r attributes

posterior_linpred(model, newdata=None, **kwargs)

Compute linear predictor of the model.

Returns samples of the linear predictor (before applying the link function). Useful for understanding the model's predictions on the linear scale.

Parameters:

Name Type Description Default
model FitResult

Fitted model from fit()

required
newdata DataFrame

Data for predictions. If None, uses original data

None
**kwargs dict

Additional arguments to brms::posterior_linpred():

  • transform : bool - Apply inverse link function (default False)
  • ndraws : int - Number of posterior draws
  • summary : bool - Return summary statistics
{}

Returns:

Type Description
PosteriorLinpredResult

Object with .idata (IDLinpred) and .r (R matrix) attributes

See Also

brms::posterior_linpred : R documentation https://paulbuerkner.com/brms/reference/posterior_linpred.brmsfit.html posterior_epred : Expected values on response scale

Examples:

    from brmspy import brms

    epilepsy = brms.get_brms_data("epilepsy")
    model = brms.fit(
        "count ~ zAge + zBase * Trt + (1|patient)",
        data=epilepsy,
        family="poisson",
        chains=4
    )

    # Linear predictor (log scale for Poisson)
    linpred = brms.posterior_linpred(model)
    print(linpred.idata.predictions)

log_lik(model, newdata=None, **kwargs)

Compute log-likelihood values.

Returns log p(y|theta) for each observation. Essential for model comparison via LOO-CV and WAIC.

Parameters:

Name Type Description Default
model FitResult

Fitted model from fit()

required
newdata DataFrame

Data for predictions. If None, uses original data

None
**kwargs dict

Additional arguments to brms::log_lik():

  • ndraws : int - Number of posterior draws
  • combine_chains : bool - Combine chains (default True)
{}

Returns:

Type Description
LogLikResult

Object with .idata (IDLogLik) and .r (R matrix) attributes

See Also

brms::log_lik : R documentation https://paulbuerkner.com/brms/reference/log_lik.brmsfit.html arviz.loo : Leave-One-Out Cross-Validation arviz.waic : Widely Applicable Information Criterion

Examples:

Compute log-likelihood for model comparison:

from brmspy import brms
import arviz as az

epilepsy = brms.get_brms_data("epilepsy")
model = brms.fit(
    "count ~ zAge + zBase * Trt + (1|patient)",
    data=epilepsy,
    family="poisson",
    chains=4
)

# LOO-CV for model comparison
loo = az.loo(model.idata)
print(loo)

Compare multiple models:

model1 = brms.fit("count ~ zAge + (1|patient)", data=epilepsy, family="poisson", chains=4)
model2 = brms.fit("count ~ zAge + zBase + (1|patient)", data=epilepsy, family="poisson", chains=4)

comp = az.compare({'model1': model1.idata, 'model2': model2.idata})
print(comp)