nemos.observation_models.GammaObservations.pseudo_r2#
- GammaObservations.pseudo_r2(y, predicted_rate, score_type='pseudo-r2-McFadden', scale=1.0, aggregate_sample_scores=<function mean>)#
Pseudo-\(R^2\) calculation for a GLM.
Compute the pseudo-\(R^2\) metric for the GLM, as defined by McFadden et al. [1] or by Cohen et al. [2].
This metric evaluates the goodness-of-fit of the model relative to a null (baseline) model that assumes a constant mean for the observations. While the pseudo-\(R^2\) is bounded between 0 and 1 for the training set, it can yield negative values on out-of-sample data, indicating potential over-fitting.
- Parameters:
y (
Array
) – The neural activity. Expected shape:(n_time_bins, )
predicted_rate (
Array
) – The mean neural activity. Expected shape:(n_time_bins, )
score_type (
Literal
['pseudo-r2-McFadden'
,'pseudo-r2-Cohen'
]) – The pseudo-\(R^2\) type.scale (
Union
[float
,Array
,ndarray
[tuple
[int
,...
],dtype
[TypeVar
(_ScalarType_co
, bound=generic
, covariant=True)]]]) – The scale parameter of the model.aggregate_sample_scores (Callable)
- Return type:
Array
- Returns:
The pseudo-\(R^2\) of the model. A value closer to 1 indicates a better model fit, whereas a value closer to 0 suggests that the model doesn’t improve much over the null model.
Notes
The McFadden pseudo-\(R^2\) is given by:
\[R^2_{\text{mcf}} = 1 - \frac{\log(L_{M})}{\log(L_0)}.\]Equivalent to statsmodels GLMResults.pseudo_rsquared(kind=’mcf’) .
The Cohen pseudo-\(R^2\) is given by:
\[\begin{split}\begin{aligned} R^2_{\text{Cohen}} &= \frac{D_0 - D_M}{D_0} \\\ &= 1 - \frac{\log(L_s) - \log(L_M)}{\log(L_s)-\log(L_0)}, \end{aligned}\end{split}\]where \(L_M\), \(L_0\) and \(L_s\) are the likelihood of the fitted model, the null model (a model with only the intercept term), and the saturated model (a model with one parameter per sample, i.e. the maximum value that the likelihood could possibly achieve). \(D_M\) and \(D_0\) are the model and the null deviance, \(D_i = -2 \left[ \log(L_s) - \log(L_i) \right]\) for \(i=M,0\).
References