arviz_stats.r2_score#
- arviz_stats.r2_score(data, var_name=None, data_pair=None, summary=True, point_estimate=None, ci_kind=None, ci_prob=None, round_to='2g')[source]#
R² for Bayesian regression models.
The R², or coefficient of determination, is defined as the proportion of variance in the data that is explained by the model. It is computed as the variance of the predicted values divided by the variance of the predicted values plus the variance of the residuals. For details of the Bayesian R² see [1].
- Parameters:
- data
xarray.DataTree
orInferenceData
Input data. It should contain the posterior and the log_likelihood groups.
- var_name
str
Name of the variable to compute the R² for.
- data_pair
dict
Dictionary with the first element containing the posterior predictive name and the second element containing the observed data variable name.
- summary: bool
Whether to return a summary (default) or an array of R² samples. The summary is a Pandas’ series with a point estimate and a credible interval
- point_estimate: str
The point estimate to compute. If None, the default value is used. Defaults values are defined in rcParams[“stats.point_estimate”]. Ignored if summary is False.
- ci_kind: str
The kind of credible interval to compute. If None, the default value is used. Defaults values are defined in rcParams[“stats.ci_kind”]. Ignored if summary is False.
- ci_prob: float
The probability for the credible interval. If None, the default value is used. Defaults values are defined in rcParams[“stats.ci_prob”]. Ignored if summary is False.
- round_to: int or str, optional
If integer, number of decimal places to round the result. If string of the form ‘2g’ number of significant digits to round the result. Defaults to ‘2g’. Use None to return raw numbers.
- data
- Returns:
Namedtuple
orarray
References
[1]Gelman et al. R-squared for Bayesian regression models. The American Statistician. 73(3) (2019). https://doi.org/10.1080/00031305.2018.1549100 preprint http://www.stat.columbia.edu/~gelman/research/published/bayes_R2_v3.pdf.
Examples
Calculate R² samples for Bayesian regression models :
In [1]: from arviz_stats import r2_score ...: from arviz_base import load_arviz_data ...: data = load_arviz_data('regression1d') ...: r2_score(data) ...: Out[1]: R2(mean=0.68, eti_lb=0.61, eti_ub=0.75)