elisa.plot.residuals#

Residual calculation.

combine_residuals(r1: ndarray[tuple[Any, ...], dtype[_ScalarT]], r2: ndarray[tuple[Any, ...], dtype[_ScalarT]], dof: int | float | ndarray[tuple[Any, ...], dtype[_ScalarT]], sign: ndarray[tuple[Any, ...], dtype[_ScalarT]] | None = None) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]#
pearson_residuals(observed: ndarray[tuple[Any, ...], dtype[_ScalarT]], expected: ndarray[tuple[Any, ...], dtype[_ScalarT]], std: ndarray[tuple[Any, ...], dtype[_ScalarT]] | None = None) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]#

Calculate Pearson residuals.

Parameters:
observedndarray

Observed counts.

expectedndarray

Expected counts.

stdndarray, optional

Standard deviation of the observed counts. If None, assumed to be the square root of the expected counts.

Returns:
ndarray

Pearson residuals.

pit_poisson(k: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam: ndarray[tuple[Any, ...], dtype[_ScalarT]], minus: bool = False) ndarray[tuple[Any, ...], dtype[_ScalarT]] | tuple[ndarray[tuple[Any, ...], dtype[_ScalarT]], ndarray[tuple[Any, ...], dtype[_ScalarT]]][source]#

Probability integral transform of poisson data fit.

Parameters:
kndarray

The data values.

lamndarray

The model values.

minusbool, optional

Whether to calculate the PIT of k - 1.

Returns:
ndarray, or tuple of ndarray

The probability integral transform values.

quantile_residuals_poisson(k: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam: ndarray[tuple[Any, ...], dtype[_ScalarT]], keep_sign: bool = False, random: bool = True, seed: int = 42) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]#

Normalized quantile residuals for fit of Poisson data.

Parameters:
kndarray

Data value.

lamndarray

Model value.

keep_signbool, optional

Whether to keep sign of the residuals as sign(k - lam). The default is False.

randombool, optional

Whether to add random noise so that residuals are normally distributed.

seedint, optional

Random seed to use in adding noise. The default is 42.

Returns:
ndarray

The quantile residuals.

Notes

When random is True, the residuals are theoretically guaranteed to be normally distributed. If keep_sign is also True, then the normality of the residuals may not be preserved. The recommended use is only random or keep_sign being True.

References

[1]

doi:10.2307/1390802

[2]

doi:10.1371/journal.pone.0181790

[3]

doi:10.1111/j.1541-0420.2009.01191.x

[4]

doi:10.1186/s12874-020-01055-2

pit_poisson_poisson(k1: ndarray[tuple[Any, ...], dtype[_ScalarT]], k2: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam1: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam2: ndarray[tuple[Any, ...], dtype[_ScalarT]], ratio: float | ndarray[tuple[Any, ...], dtype[_ScalarT]], seed: int = 42, minus: bool = False, nsim: int = 10000) ndarray[tuple[Any, ...], dtype[_ScalarT]] | tuple[ndarray[tuple[Any, ...], dtype[_ScalarT]], ndarray[tuple[Any, ...], dtype[_ScalarT]]][source]#

Probability integral transform of two poisson data fit.

Parameters:
k1, k2ndarray

The first and second data values.

lam1, lam2ndarray

The first and second model values.

ratiofloat or ndarray

Background ratio.

seedint, optional

Random seed to use in simulation. The default is 42.

minusbool, optional

Whether to calculate the PIT of the next small quantity given m1 and m2. The default is False.

nsimint, optional

The number of simulations to generate. The default is 10000.

Returns:
ndarray, or tuple of ndarray

The probability integral transform values.

quantile_residuals_poisson_poisson(k1: ndarray[tuple[Any, ...], dtype[_ScalarT]], k2: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam1: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam2: ndarray[tuple[Any, ...], dtype[_ScalarT]], ratio: float | ndarray[tuple[Any, ...], dtype[_ScalarT]], random: bool = True, seed: int = 42, nsim: int = 10000) tuple[ndarray[tuple[Any, ...], dtype[_ScalarT]], ndarray[tuple[Any, ...], dtype[_ScalarT]], ndarray[tuple[Any, ...], dtype[_ScalarT]]][source]#

Normalized quantile residuals for joint fit of two Poisson data.

Note

The calculation is based on Monte Carlo simulation. It is also possible to calculate the quantile residuals by inverting the Cornish-Fisher expansion. See the following link for more details:

Parameters:
k1, k2ndarray

The first and second data values.

lam1, lam2ndarray

The first and second model values.

ratiofloat or ndarray

Background ratio.

randombool, optional

Whether to add random noise so that residuals are normally distributed.

seedint, optional

Random seed to use in adding noise and simulation. The default is 42.

nsimint, optional

The number of simulations to generate. The default is 10000.

Returns:
tuple of ndarray

The quantile residuals, and flags to mark if the residuals are lower or upper limit.

pit_poisson_normal(k: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam: ndarray[tuple[Any, ...], dtype[_ScalarT]], v: ndarray[tuple[Any, ...], dtype[_ScalarT]], mu: ndarray[tuple[Any, ...], dtype[_ScalarT]], sigma: ndarray[tuple[Any, ...], dtype[_ScalarT]], ratio: float | ndarray[tuple[Any, ...], dtype[_ScalarT]], seed: int = 42, nsim: int = 10000) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]#

Probability integral transform of poisson and normal data fit.

Parameters:
kndarray

The Poisson data value.

lamndarray

The Poisson model value.

vndarray

The normal data value.

mundarray

The normal model value.

sigmandarray

The normal model sigma.

ratiofloat or ndarray

Background ratio.

seedint, optional

Random seed to use in simulation. The default is 42.

nsimint, optional

The number of simulations to generate. The default is 10000.

Returns:
ndarray, or tuple of ndarray

The probability integral transform values.

quantile_residuals_poisson_normal(k: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam: ndarray[tuple[Any, ...], dtype[_ScalarT]], v: ndarray[tuple[Any, ...], dtype[_ScalarT]], mu: ndarray[tuple[Any, ...], dtype[_ScalarT]], sigma: ndarray[tuple[Any, ...], dtype[_ScalarT]], ratio: float | ndarray[tuple[Any, ...], dtype[_ScalarT]], seed: int = 42, nsim: int = 10000) tuple[ndarray[tuple[Any, ...], dtype[_ScalarT]], ndarray[tuple[Any, ...], dtype[_ScalarT]], ndarray[tuple[Any, ...], dtype[_ScalarT]]][source]#

Normalized quantile residuals for joint fit of Poisson and normal data.

Note

The calculation is based on Monte Carlo simulation. It is also possible to calculate the quantile residuals by inverting the Cornish-Fisher expansion. See the following link for more details:

Parameters:
kndarray

The Poisson data value.

lamndarray

The Poisson model value.

vndarray

The normal data value.

mundarray

The normal model value.

sigmandarray

The normal model sigma.

ratiofloat or ndarray

Background ratio.

seedint, optional

Random seed to use in simulation. The default is 42.

nsimint, optional

The number of simulations to generate. The default is 10000.

Returns:
tuple of ndarray

The quantile residuals, and flags to mark if the residuals are lower or upper limit.

deviance_residuals_poisson(k: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam: ndarray[tuple[Any, ...], dtype[_ScalarT]]) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]#

Deviance residuals for fit of Poisson data.

Parameters:
kndarray

Data value.

lamndarray

Model value.

Returns:
ndarray

The deviance residuals.

References

[1]

McCullagh P, Nelder JA. Generalized Linear Models, 2nd ed., pp. 39

[2]

Spiegelhalter et al., https://doi.org/10.1111/1467-9868.00353

deviance_residuals_poisson_poisson(k1: ndarray[tuple[Any, ...], dtype[_ScalarT]], k2: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam1: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam2: ndarray[tuple[Any, ...], dtype[_ScalarT]], sign: ndarray[tuple[Any, ...], dtype[_ScalarT]] | None = None, dof: int | float | ndarray[tuple[Any, ...], dtype[_ScalarT]] = 2.0) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]#

Deviance residuals [1] [2] for joint fit of two Poisson data.

Parameters:
k1, k2ndarray

The first and second data value.

lam1, lam2ndarray

The first and second model value.

signndarray, optional

The sign of output residuals. The default is None.

dofint, float, or NDArray, optional

The degree of freedom of each data pair. The default is 2.

Returns:
ndarray

The deviance residuals.

References

[1]

McCullagh P, Nelder JA. Generalized Linear Models, 2nd ed., pp. 39

[2]

Spiegelhalter et al., https://doi.org/10.1111/1467-9868.00353

deviance_residuals_poisson_normal(k: ndarray[tuple[Any, ...], dtype[_ScalarT]], lam: ndarray[tuple[Any, ...], dtype[_ScalarT]], v: ndarray[tuple[Any, ...], dtype[_ScalarT]], mu: ndarray[tuple[Any, ...], dtype[_ScalarT]], sigma: ndarray[tuple[Any, ...], dtype[_ScalarT]], sign: ndarray[tuple[Any, ...], dtype[_ScalarT]] | None = None, dof: int | float | ndarray[tuple[Any, ...], dtype[_ScalarT]] = 2.0) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]#

Deviance residuals [1] [2] for joint fit of Poisson and normal data.

Parameters:
kndarray

The first data value.

lamndarray

The first model value.

vndarray

Data value.

mundarray

Model value.

sigmandarray

Model sigma.

signndarray, optional

The sign of output residuals. The default is None.

dofint, float, or NDArray, optional

The degree of freedom of each data pair. The default is 2.

Returns:
ndarray

The deviance residuals.

References

[1]

McCullagh P, Nelder JA. Generalized Linear Models, 2nd ed., pp. 39

[2]

Spiegelhalter et al., https://doi.org/10.1111/1467-9868.00353