Skip to contents

Calculates Double Probability Integral Transform (DPIT) residuals for regression model whose response follows a negative binomial distribution. Unlike resid_disc, which infers the fitted CDF from a model object, dpit_nb requires only the vector of fitted means and a dispersion parameter, so it can be applied to GLMs, GAMs, or other custom estimators—as long as fitted values are available.

Usage

dpit_nb(fitted, y=NULL, size=NULL, plot = TRUE, scale="uniform")

Arguments

fitted

Numeric vector of fitted means.

y

Outcome variable.

size

Dispersion (size) parameter for the negative binomial distribution; e.g., a glm.nb fitted with a negative‐binomial family, you can extract it with summary(model)$theta.

plot

Logical; if TRUE (default) a QQ-plot of the residuals is displayed.

scale

You can choose the scale of the residuals among normal and uniform scales. The sample quantiles of the residuals are plotted against the theoretical quantiles of a standard normal distribution under the normal scale, and against the theoretical quantiles of a uniform (0,1) distribution under the uniform scale. The default scale is normal

Value

DPIT residuals. If plot=TRUE, also produces a QQ plot.

References

Yang, Lu. "Double Probability Integral Transform Residuals for Regression Models with Discrete Outcomes." arXiv preprint arXiv:2308.15596 (2023).

Examples

library(assessor)
library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.9-3. For overview type 'help("mgcv-package")'.
set.seed(1234)
n  <- 500
x1 <- runif(n, 0, 1)
x2 <- runif(n, 0, 1)
f1 <- function(x)  2 * sin(2*pi*x)
f2 <- function(x) -1.5 * (x - .5)^2
eta <- -0.5 + f1(x1) + f2(x2)
mu  <- exp(eta)
theta_true <- 1.3
y <- rnbinom(n, size = theta_true, mu = mu)
dat <- data.frame(y, x1, x2)
gam_nb <- gam(y ~ s(x1) + s(x2),
              family = nb(),
              data   = dat,
              method = "REML")
gam_pois <- gam(y ~ s(x1) + s(x2),
                family = poisson(),
                data = dat,
                method = "REML")
dpit_nb(gam_nb$fitted.values, y=y, size=gam_nb$family$getTheta(TRUE))

#>   [1] 0.2510670907 0.5147551617 0.4817275002 0.4718099734 0.7493575894
#>   [6] 0.5652663440 0.8752638409 0.3033040031 0.5845521273 0.6499887177
#>  [11] 0.9594211448 0.3246652177 0.0234866399 0.6095898541 0.0098676421
#>  [16] 0.8167892215 0.4188931065 0.0721888003 0.3282979544 0.3651766460
#>  [21] 0.1362188058 0.1944871513 0.0354196349 0.2637649002 0.0877377830
#>  [26] 0.9843474389 0.6401470919 0.5962908121 0.7973146462 0.5534892767
#>  [31] 0.1301379217 0.1762931374 0.1142994184 0.6188069517 0.1024584256
#>  [36] 0.8441937271 0.4137474630 0.5211426631 0.4449454742 0.8102381760
#>  [41] 0.3177727417 0.5582686766 0.1379681560 0.4745001659 0.1438811400
#>  [46] 0.2011754474 0.6494749065 0.1846578702 0.7267274198 0.8417451666
#>  [51] 0.3852182478 0.2039728312 0.7336802514 0.1975892625 0.0446403421
#>  [56] 0.2310699080 0.5123940758 0.7220066997 0.1241601346 0.7955154554
#>  [61] 0.6889739734 0.2598390952 0.0317707682 0.3400432872 0.2613007037
#>  [66] 0.7179135629 0.8913555429 0.9551206463 0.1884323845 0.3319397855
#>  [71] 0.2205863131 0.6850087575 0.3095445190 0.8260580783 0.1204152859
#>  [76] 0.6375093215 0.1753348991 0.1532339892 0.0308709104 0.6344229523
#>  [81] 0.6180152158 0.8982272827 0.9292983000 0.8242513791 0.3782682492
#>  [86] 0.6396305392 0.0578333544 0.7268434697 0.4277293699 0.6625522987
#>  [91] 0.0415990724 0.6718612817 0.6688964635 0.0548312538 0.7273054288
#>  [96] 0.2326040065 0.3348788281 0.8021564632 0.2253014040 0.7125025135
#> [101] 0.2424034551 0.3367875112 0.9003209866 0.0982409292 0.3322824406
#> [106] 0.3681270023 0.3020523108 0.5044876514 0.3116914210 0.2341422868
#> [111] 0.9640209378 0.3073907601 0.5181182530 0.5452018652 0.7337768568
#> [116] 0.6149319230 0.5274860381 0.3097950780 0.0666622524 0.8084368183
#> [121] 0.8235282533 0.5910857823 0.4318540312 0.5735047680 0.1662848553
#> [126] 0.1183498221 0.8754415281 0.2276786439 0.5099828817 0.1279791574
#> [131] 0.5255138420 0.9355045562 0.9219495531 0.2875954774 0.6451643024
#> [136] 0.8444511477 0.6205250155 0.4475736226 0.5013376801 0.6934695584
#> [141] 0.2079262040 0.4296226541 0.3724826235 0.8735624210 0.6273901766
#> [146] 0.4961330718 0.7066647944 0.3741988461 0.4539681532 0.8937598846
#> [151] 0.9088036235 0.2886893616 0.7102298209 0.7410048063 0.1550630229
#> [156] 0.4357945099 0.9182258600 0.3990645470 0.0484614607 0.3490544282
#> [161] 0.3057894868 0.4007672193 0.1212446423 0.1539947998 0.1233212534
#> [166] 0.5331613848 0.1159206072 0.3919159878 0.8040945650 0.0386948806
#> [171] 0.7374388410 0.4154732536 0.6802862182 0.2844720142 0.1605362637
#> [176] 0.8335998025 0.4863748790 0.8815543083 0.2168978799 0.0313196014
#> [181] 0.1600627943 0.9028725155 0.4393012520 0.9720625286 0.5410227284
#> [186] 0.2900012601 0.8189037140 0.4896186023 0.2209148077 0.7613770294
#> [191] 0.5633125262 0.9079504664 0.0524735327 0.6748898500 0.7593808488
#> [196] 0.7536445888 0.7650934292 0.7432815235 0.4375475931 0.5487128846
#> [201] 0.5558338210 0.2760701016 0.5188353923 0.7892591851 0.2523011181
#> [206] 0.6980785624 0.2090942070 0.4302911556 0.7869840709 0.4508777869
#> [211] 0.3676875755 0.3353772431 0.0281937746 0.7356558884 0.8726635315
#> [216] 0.5505241877 0.5315044870 0.6537230144 0.4939914019 0.7024320473
#> [221] 0.9055137375 0.2505352865 0.1095176394 0.1258602859 0.9128554181
#> [226] 0.2226097287 0.9799146781 0.5418165146 0.2128777694 0.2117257885
#> [231] 0.8523573331 0.8698175755 0.6722818136 0.8148056392 0.3632826940
#> [236] 0.1016827928 0.7728163593 0.6690184965 0.1995407235 0.9195414615
#> [241] 0.0888470673 0.0003293794 0.1553677795 0.6363660665 0.9187905733
#> [246] 0.4047987807 0.7936234933 0.3801870455 0.4762852809 0.8317921153
#> [251] 0.2826398985 0.4336561593 0.0811562980 0.9928219843 0.1568293154
#> [256] 0.3699299016 0.5336172125 0.3819160248 0.4024711028 0.6633186308
#> [261] 0.7086299082 0.2839299128 0.4090844099 0.1748436793 0.0615416561
#> [266] 0.9673045447 0.8297505548 0.3191476141 0.2200706338 0.7472034186
#> [271] 0.3326275291 0.8354858757 0.3748840997 0.7690425386 0.2913160768
#> [276] 0.2157216750 0.7511421494 0.2992397203 0.8583309935 0.0406179019
#> [281] 0.3712097751 0.4839910109 0.9452579215 0.7828029705 0.7161378493
#> [286] 0.0174660704 0.1494370620 0.1679527532 0.6785273868 0.9756498144
#> [291] 0.6052251141 0.0694007751 0.5382900393 0.8753203237 0.9209104348
#> [296] 0.7633060779 0.9618551256 0.9771867877 0.5072523492 0.1323075552
#> [301] 0.8832516110 0.4836657281 0.9318348089 0.1191777079 0.5204826652
#> [306] 0.1349201649 0.0109818451 0.4236364904 0.9893068083 0.9783437827
#> [311] 0.6272315398 0.6245597478 0.2777388420 0.4517234580 0.0420918526
#> [316] 0.3003826876 0.9297350244 0.7652804275 0.4181486238 0.7746070310
#> [321] 0.1638252928 0.9681186833 0.6151412079 0.0396469226 0.3833194449
#> [326] 0.3447618454 0.4722851209 0.7316581322 0.5795872700 0.6069316738
#> [331] 0.7143621486 0.7003806621 0.5468725310 0.3386272098 0.9046219019
#> [336] 0.0627996763 0.8195199896 0.2707265688 0.5892245001 0.4542991946
#> [341] 0.4934418430 0.0978594361 0.2460353957 0.4433706123 0.0478970131
#> [346] 0.1353526800 0.6809895997 0.6418820341 0.6227105022 0.2264875214
#> [351] 0.3035535778 0.8278651434 0.0634365561 0.6658539652 0.4170150241
#> [356] 0.9400146670 0.3973621004 0.6952348506 0.1827525546 0.8480823920
#> [361] 0.9539759689 0.3280477682 0.3526817443 0.1340300264 0.7453045365
#> [366] 0.4915301104 0.5718160900 0.5634480324 0.7763984299 0.7202293024
#> [371] 0.9905875567 0.9551360069 0.7315613992 0.4675735962 0.2298755165
#> [376] 0.3764047517 0.7392218207 0.0844206323 0.0952119171 0.9490522646
#> [381] 0.2094243444 0.9811092498 0.3601860012 0.4596746544 0.1704961686
#> [386] 0.0826026100 0.6323752320 0.0851609094 0.9841419588 0.5088898651
#> [391] 0.6917051132 0.7848755120 0.7575952145 0.9414371811 0.2280136278
#> [396] 0.9470115239 0.3541235761 0.2950479294 0.3296950165 0.3503776416
#> [401] 0.6554669157 0.0807895315 0.8377314277 0.9791170814 0.8933250658
#> [406] 0.0892211174 0.7708314013 0.3065896408 0.5118484291 0.4459968493
#> [411] 0.7782710674 0.9801132599 0.4652445301 0.7792140174 0.6830465564
#> [416] 0.9494095699 0.2249670053 0.8541750775 0.0621679407 0.2798229775
#> [421] 0.6028597153 0.9554704091 0.1401999957 0.0764453307 0.5826436589
#> [426] 0.1690657151 0.5449826199 0.2969136617 0.0340433993 0.3753224163
#> [431] 0.5759585370 0.8562988624 0.5258314089 0.5927827665 0.1616311608
#> [436] 0.5945922614 0.1730795061 0.5669491812 0.7262571155 0.9789292217
#> [441] 0.8462667141 0.6869911079 0.8066354620 0.2180592497 0.9132065590
#> [446] 0.3263064394 0.6687449389 0.4879965053 0.9886730893 0.6638288299
#> [451] 0.0106099661 0.8502130405 0.4110448096 0.9345146481 0.8217226272
#> [456] 0.9521661622 0.0143601437 0.4491507595 0.8790216550 0.2105749082
#> [461] 0.0473411391 0.6517311635 0.9437355138 0.6663637423 0.5739899355
#> [466] 0.0873658128 0.3149592716 0.0596745327 0.5037122798 0.8804257745
#> [471] 0.0554272496 0.0214340536 0.8747411842 0.5356940928 0.8616195459
#> [476] 0.4780370366 0.9610918601 0.1434333122 0.9776005721 0.0467951755
#> [481] 0.2744529844 0.3649829464 0.5616304656 0.8852907416 0.1984183003
#> [486] 0.1411216445 0.7297794722 0.7244782216 0.7556219219 0.2463735648
#> [491] 0.4251918731 0.8120397352 0.6578273443 0.1075458138 0.6011549923
#> [496] 0.0304225600 0.4565813992 0.9260552388 0.4797909540 0.4073142204
dpit_pois(gam_pois$fitted.values, y=y)

#>   [1] 0.184679706 0.470134332 0.444081101 0.462326688 0.690666358 0.528006265
#>   [7] 0.866491859 0.240976135 0.568286339 0.668410378 0.958928471 0.289395225
#>  [13] 0.005781191 0.557130349 0.004351383 0.782050229 0.501245190 0.027626984
#>  [19] 0.271999212 0.340423007 0.071906387 0.139941773 0.007016660 0.224813085
#>  [25] 0.016396243 0.985245928 0.779210734 0.588632930 0.746743536 0.600672507
#>  [31] 0.079941553 0.097081678 0.045780174 0.625639945 0.038595235 0.826039768
#>  [37] 0.421281499 0.703832090 0.395000577 0.766429254 0.278747593 0.520743400
#>  [43] 0.077039509 0.448308104 0.077495099 0.146449814 0.616876219 0.137588560
#>  [49] 0.904071969 0.817736414 0.366696512 0.152459983 0.684283829 0.159407602
#>  [55] 0.012441912 0.187278724 0.498944583 0.737077150 0.043872801 0.772031885
#>  [61] 0.674690851 0.226667966 0.007785881 0.295405772 0.186194190 0.676455489
#>  [67] 0.985894855 0.986130607 0.151351784 0.307585355 0.147671838 0.648021065
#>  [73] 0.281215141 0.786581593 0.071485308 0.653760321 0.143178162 0.107640188
#>  [79] 0.007475588 0.593901029 0.574478341 0.945791000 0.993452044 0.842853905
#>  [85] 0.444779680 0.599990000 0.026495235 0.905068126 0.477847602 0.622524558
#>  [91] 0.010007818 0.641449486 0.878159615 0.017122973 0.862085559 0.176485348
#>  [97] 0.313900458 0.838551334 0.161391389 0.709707846 0.202936415 0.300651267
#> [103] 0.994761757 0.021365292 0.289932072 0.376312202 0.243456154 0.648367421
#> [109] 0.265014221 0.189313653 0.965205023 0.269112605 0.473869624 0.826341165
#> [115] 0.897108158 0.570607753 0.490179670 0.269559082 0.033823630 0.764638458
#> [121] 0.801729054 0.582053090 0.382084710 0.531820645 0.121215085 0.054155723
#> [127] 0.963080534 0.180763403 0.511069826 0.061991458 0.494572324 0.932200459
#> [133] 0.989770558 0.228486055 0.624707431 0.857734227 0.586460761 0.418492539
#> [139] 0.468520243 0.658584164 0.149882693 0.406491894 0.377272283 0.885727993
#> [145] 0.858988177 0.457174951 0.717660379 0.333808123 0.411102407 0.890158714
#> [151] 0.961175597 0.205155635 0.847076834 0.688895056 0.078899269 0.388259655
#> [157] 0.918500357 0.370439529 0.018219565 0.308968691 0.254808480 0.357664408
#> [163] 0.068031895 0.087359754 0.075299618 0.486206124 0.063731755 0.379516058
#> [169] 0.762848216 0.009638256 0.735290433 0.386376761 0.639706856 0.240085465
#> [175] 0.113915270 0.799928455 0.522539337 0.877983766 0.166184009 0.009103084
#> [181] 0.111966540 0.993250033 0.401537154 0.997130953 0.502879288 0.256487480
#> [187] 0.795662753 0.500574958 0.152949592 0.696964436 0.533484785 0.897715848
#> [193] 0.020047994 0.634913791 0.797721393 0.778576246 0.719441321 0.682238349
#> [199] 0.403066963 0.514753346 0.565203391 0.218776645 0.729934552 0.731719765
#> [205] 0.208825738 0.694861422 0.142572708 0.441794550 0.936437599 0.396528274
#> [211] 0.315834950 0.337527941 0.006730182 0.742439295 0.870646732 0.526342640
#> [217] 0.496200982 0.630243643 0.504643229 0.711486355 0.955810138 0.230840694
#> [223] 0.040376286 0.082010069 0.986280254 0.177545451 0.979592580 0.831245442
#> [229] 0.163904737 0.168239882 0.828371406 0.877202107 0.865482855 0.773823499
#> [235] 0.311739532 0.026152247 0.705608633 0.883730187 0.156666358 0.918038217
#> [241] 0.052231192 0.000642493 0.088143453 0.611138512 0.912756250 0.433218022
#> [247] 0.738864446 0.335233723 0.441280613 0.789638392 0.229663141 0.398056367
#> [253] 0.020584314 0.999999656 0.119355233 0.326944011 0.768449074 0.339506221
#> [259] 0.350229708 0.672926446 0.733504990 0.237502299 0.408027013 0.097494487
#> [265] 0.028670191 0.997619881 0.806987411 0.276014922 0.172350577 0.703595565
#> [271] 0.327484894 0.804469955 0.363299376 0.793234007 0.250043078 0.160406828
#> [277] 0.783844745 0.266717289 0.935514721 0.010982880 0.372843380 0.594365538
#> [283] 0.953721444 0.759268442 0.693089579 0.003517207 0.094933326 0.127873379
#> [289] 0.636654230 0.980488482 0.561197494 0.044775522 0.487833522 0.961342153
#> [295] 0.917650773 0.722101370 0.959949041 0.999303941 0.497829652 0.068448909
#> [301] 0.977995519 0.437831068 0.946921091 0.069872002 0.477106236 0.074651767
#> [307] 0.002332405 0.372312214 0.995488851 0.975401789 0.577873078 0.596441891
#> [313] 0.236309336 0.463177123 0.012656292 0.221270167 0.997386741 0.862620629
#> [319] 0.498228259 0.715879548 0.103539839 0.967709971 0.830998232 0.008932912
#> [325] 0.449989311 0.335773420 0.591151392 0.889695705 0.537467688 0.555449055
#> [331] 0.678220571 0.664592756 0.512714730 0.299277505 0.939801777 0.029020103
#> [337] 0.891807669 0.239390780 0.535799914 0.466099944 0.472254201 0.027099102
#> [343] 0.190920587 0.420561963 0.014979929 0.072337412 0.727416564 0.615150604
#> [349] 0.576175433 0.179692575 0.216016179 0.812156616 0.032810605 0.650558478
#> [355] 0.359148955 0.937977983 0.374346692 0.700046444 0.132386446 0.823609142
#> [361] 0.977559271 0.286898256 0.343542811 0.068865881 0.725666939 0.455232698
#> [367] 0.548848706 0.842265215 0.744493506 0.667506884 0.994225540 0.999747798
#> [373] 0.707385673 0.453637936 0.168739833 0.393475513 0.748532154 0.059069696
#> [379] 0.049013574 0.946563074 0.138991927 0.999987225 0.310353574 0.483679297
#> [385] 0.081542778 0.050313459 0.604110952 0.034474887 0.991933335 0.507172553
#> [391] 0.661001772 0.723883549 0.687124951 0.941668792 0.143658638 0.947270027
#> [397] 0.302025526 0.246538715 0.293605916 0.342809075 0.609414704 0.043543124
#> [403] 0.814711822 0.976924566 0.891655131 0.036687354 0.714098725 0.279913057
#> [409] 0.465594889 0.478350678 0.750320891 0.979188090 0.446718875 0.920255074
#> [415] 0.672269745 0.996632966 0.170804570 0.833063263 0.030631541 0.223646692
#> [421] 0.551386834 0.954547949 0.083886721 0.048539719 0.529669892 0.089816356
#> [427] 0.565897980 0.238196250 0.011599501 0.449169684 0.562883458 0.835094121
#> [433] 0.665029167 0.539136385 0.105993370 0.542217398 0.130427694 0.518209679
#> [439] 0.679987163 0.989552857 0.821545744 0.645607275 0.770240398 0.162893251
#> [445] 0.904957293 0.284409897 0.643863089 0.475487285 0.992908380 0.720319916
#> [451] 0.001290565 0.830187048 0.354738903 0.931572236 0.791436109 0.957402987
#> [457] 0.002680493 0.412640881 0.880668588 0.164919967 0.017634874 0.607690949
#> [463] 0.995304806 0.691317792 0.859162106 0.033453606 0.270836782 0.030991129
#> [469] 0.458774960 0.876700460 0.023039882 0.004463083 0.881740314 0.492943825
#> [475] 0.916726807 0.429001692 0.988882006 0.093051827 0.978985758 0.013531613
#> [481] 0.222961694 0.325533227 0.559511946 0.951478097 0.142095177 0.101173459
#> [487] 0.775615134 0.701820975 0.752109977 0.189827402 0.384862817 0.753899359
#> [493] 0.632625870 0.056383057 0.553066863 0.012228239 0.424689665 0.981778836
#> [499] 0.452401729 0.378779603