Residuals for Probabilistic Regression Models

Description

Generic function and default method for (randomized) quantile residuals, PIT, Pearson, and raw response residuals based on distributions3 support.

Usage

proresiduals(object, ...)

## Default S3 method:
proresiduals(
  object,
  newdata = NULL,
  type = c("quantile", "pit", "pearson", "response"),
  nsim = NULL,
  prob = NULL,
  delta = NULL,
  ...
)

Arguments

object an object for which a newresponse and a prodist method is available.
further parameters passed to methods.
newdata optionally, a data frame in which to look for variables with which to predict. If omitted, the original observations are used.
type character indicating whether quantile (default), PIT, Pearson, or raw response residuals should be computed.
nsim integer. The number of randomly simulated residuals of type = “quantile” or “pit”. By default one simulation is returned.
prob numeric. Instead of simulating the probabilities (between 0 and 1) for type = “quantile” or “pit”, a vector of probabilities can be specified, e.g., prob = 0.5 corresponding to mid-quantile residuals.
delta numeric. The minimal difference to compute the range of proabilities corresponding to each observation according to get (randomized) “quantile” or “pit” residuals. For NULL, the minimal observed difference in the resonse divided by 5e-6 is used. Ignored for continuous distributions.

Details

The new generic function proresiduals comes with a powerful default method that is based on the following idea: newresponse and prodist can be used to extract the observed response and expected distribution for it, respectively. For all model classes that have methods for these two generic functions, proresiduals can compute a range of different types of residuals.

The simplest definition of residuals are the so-called “response” residuals which simply compute the difference between the observations and the expected means. The “pearson” residuals additionally standardize these residuals by the square root of the expected variance. Thus, these residuals are based only on the first and on the first two moments, respectively.

To assess the entire distribution and not just the first moments, there are also residuals based on the probability integral transform (PIT). For regression models with a continuous response distribution, “pit” residuals (see Warton 2007) are simply the expected cumulative distribution (CDF) evaluated at the observations (Dawid, 1984). For discrete distributions, a uniform random value is drawn from the range of probabilities between the CDF at the observation and the supremum of the CDF to the left of it. If the model fits well the PIT residuals should be uniformly distributed.

In order to obtain normally distributed residuals for well-fitting models (like often desired in linear regression models), “quantile” residuals, proposed by Dunn and Smyth (1996), additionally transform the PIT residuals by the standard normal quantile function.

As quantile residuals and PIT residuals are subject to randomness for discrete distributions (and also for mixed discrete-continuous distributions), it is sometimes useful to explore the extent of the random variation. This can be done either by obtaining multiple replications (via nsim) or by computing fixed quantiles of each probability interval such as prob = 0.5 (corresponding to mid-quantile residuals, see Feng et al. 2020). Another common setting is prob = c(0, 1) yielding the range of possible residuals.

Value

A vector or matrix of residuals. A matrix of residuals is returned if more than one replication of quantile or PIT residuals is computed, i.e., if either random > 1 or random = FALSE and length(prob) > 1.

References

Dawid AP (1984). “Present Position and Potential Developments: Some Personal Views: Statistical Theory: The Prequential Approach.” Journal of the Royal Statistical Society A, 147(2), 278–292. doi:10.2307/2981683.

Dunn KP, Smyth GK (1996). “Randomized Quantile Residuals.” Journal of Computational and Graphical Statistics, 5(3), 236–244. doi:10.2307/1390802

Feng C, Li L, Sadeghpour A (2020). “A Comparison of Residual Diagnosis Tools for Diagnosing Regression Models for Count Data” BMC Medical Research Methodology, 20(175), 1–21. doi:10.1186/s12874-020-01055-2

Warton DI, Thibaut L, Wang YA (2017) “The PIT-Trap – A ‘Model-Free’ Bootstrap Procedure for Inference about Regression Models with Discrete, Multivariate Responses”. PLOS ONE, 12(7), 1–18. doi:10.1371/journal.pone.0181790.

See Also

qnorm, qqrplot

Examples

library("topmodels")

## Poisson GLM for FIFA 2018 data
data("FIFA2018", package = "distributions3")
m <- glm(goals ~ difference, data = FIFA2018, family = poisson)

## random quantile residuals (on original data)
proresiduals(m)
          1           2           3           4           5           6 
 2.03998365 -0.74822960 -0.87514986 -0.56076603  1.16159944  0.30615604 
          7           8           9          10          11          12 
-0.82224422 -0.77037894  1.47495535 -0.92813631  1.01063769 -0.39657078 
         13          14          15          16          17          18 
-1.01113490 -0.37658653  1.84809612  1.00013376 -0.84937570 -1.49736705 
         19          20          21          22          23          24 
-0.70737889 -0.85456284  0.18096360 -0.03447160  1.27965029 -0.08984274 
         25          26          27          28          29          30 
 0.14144913  0.63279979 -0.65783138 -0.51068707 -0.45039875 -0.39885762 
         31          32          33          34          35          36 
-1.02724510 -2.06934027 -0.48380100 -1.49738506 -1.38544879  0.96300602 
         37          38          39          40          41          42 
-0.54911582  0.78689770  0.67822461 -0.49039186 -0.94194965  1.47061675 
         43          44          45          46          47          48 
 0.82151186 -0.75712882  0.59907261  0.12474667 -0.21854304  0.27946841 
         49          50          51          52          53          54 
-1.08733247 -0.64813147 -0.56315630  0.28411437  0.04168563 -0.66866633 
         55          56          57          58          59          60 
 0.02717105  0.93984861 -0.83086814  0.03279265  0.35122099  0.79830026 
         61          62          63          64          65          66 
-2.60870335  0.81757185 -0.47435087 -0.81970330  0.52348661  0.87122667 
         67          68          69          70          71          72 
 0.09954342  0.10365175  1.22329531 -1.58684013 -0.95099908  1.33190255 
         73          74          75          76          77          78 
 0.65743765 -1.43955481  0.19173461  0.30123558  1.85300753  1.46928061 
         79          80          81          82          83          84 
 2.20464293  0.65179480 -0.72915100 -0.32314968 -0.51114280  0.76681796 
         85          86          87          88          89          90 
-0.46875651  1.18480903 -0.50795015  0.59298035  0.55676736  0.63440627 
         91          92          93          94          95          96 
-0.83358243  1.66267721 -1.20794343 -0.33815816 -1.23720232 -0.72567599 
         97          98          99         100         101         102 
 2.00363278  1.71205536  1.12477660  0.13664949 -0.67911569  0.41474775 
        103         104         105         106         107         108 
-0.53088917  0.27779878 -0.10832557 -1.14039788  1.00087767  0.83432350 
        109         110         111         112         113         114 
-0.45078388 -1.03955854 -0.14866488 -0.31470685 -1.00781005  0.28378543 
        115         116         117         118         119         120 
-0.34587274  0.80603478 -0.32258342  0.06288464  0.32051747 -0.08609143 
        121         122         123         124         125         126 
-0.51290924 -0.65330700 -0.06334360  0.10386492  1.01664972 -1.42963389 
        127         128 
 1.51523443  1.06861282 
## various flavors of residuals on small new data
nd <- data.frame(goals = c(1, 1, 1), difference = c(-1, 0, 1))

## quantile residuals: random (1 sample), random (5 samples), mid-quantile (non-random)
proresiduals(m, newdata = nd, type = "quantile")
         1          2          3 
 0.4307241 -0.2215405 -0.6555993 
proresiduals(m, newdata = nd, type = "quantile", nsim = 5)
            r_1        r_2         r_3        r_4         r_5
[1,]  0.7905384  0.6389176  0.01642729  0.6592091  0.69749134
[2,]  0.1706228 -0.1908554 -0.41555738  0.3726144  0.01442744
[3,] -0.6098233 -0.4206220 -0.39187356 -0.3277128 -0.92230125
proresiduals(m, newdata = nd, type = "quantile", prob = 0.5)
          1           2           3 
 0.31008612 -0.07586646 -0.52976162 
## PIT residuals (without transformation to normal): random vs. minimum/maximum quantile
proresiduals(m, newdata = nd, type = "pit", nsim = 5)
           r_1       r_2       r_3       r_4       r_5
[1,] 0.7842134 0.4476646 0.7364409 0.5032401 0.5706682
[2,] 0.5933308 0.5486532 0.4781268 0.4685524 0.3489553
[3,] 0.1752934 0.4007658 0.4009715 0.1734548 0.1568566
proresiduals(m, newdata = nd, type = "pit", prob = c(0, 1))
           r_0       r_1
[1,] 0.4412492 0.8022553
[2,] 0.2902421 0.6492832
[3,] 0.1540605 0.4422167
## raw response residuals (observation - expected mean)
proresiduals(m, newdata = nd, type = "response")
         1          2          3 
 0.1818546 -0.2370397 -0.8704100 
## standardized Pearson residuals (response residuals divided by standard deviation)
proresiduals(m, newdata = nd, type = "pearson")
         1          2          3 
 0.2010523 -0.2131225 -0.6364371 
## compute residuals by manually obtaining distribution and response
## proresiduals(procast(m, newdata = nd, drop = TRUE), nd$goals)