ds.residuals: Dunn-Smyth Residuals for a fitted model

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/auxilaryfunctions.R

Description

\Sexpr[results=rd, stage=render]{lifecycle::badge("stable")}

Calculates the Dunn-Smyth residuals for a fitted model and, if some of the responses are ordinal, a confusion matrix between predicted and true levels.

Usage

1
ds.residuals(object, est = "median", include.ranef = TRUE)

Arguments

object

An object for class "boral".

est

A choice of either the posterior median (est = "median") or posterior mean (est = "mean"), which are then treated as parameter estimates and the residuals are calculated from. Default is posterior median.

include.ranef

If response-specific random intercepts were included as part of the fitted model, then this determines whether the predicted random effects will be used in the calculated of the fitted values and thus residuals. When set to TRUE, which is the default, then they are included (using either the posterior mean and posterior median predictor). When set to FALSE, they are not included. The former leads to what are sometimes called conditional residuals, while the latter are sometimes called marginal residuals.

Details

Details regarding Dunn-Smyth residuals, based on the randomized quantile residuals of Dunn and Smyth (1996), can be found in plot.manyglm function in the mvabund package (Wang et al., 2012) where they are implemented in all their glory. Due their inherent stochasticity, Dunn-Smyth residuals will be slightly different each time this function is run. As with other types of residuals, Dunn-Smyth residuals can be used in the context of residual analysis.

For ordinal responses, a single confusion matrix between the predicted levels (as based on the class with the highest probability) and true levels is aso returned. The table pools the results over all columns assumed to be ordinal.

The Dunn-Smyth residuals are calculated based on a point estimate of the parameters, as determined by the argument est. A fully Bayesian approach would calculate the residuals by averaging over the posterior distribution of the parameters i.e., ergodically average over the MCMC samples. In general however, the results (as in the trends seen in residual analysis) from either approach should be very similar.

Check out also the awesome DHARMa package for calculation of Dunn-Smyth and probability integral transform residuals in other regression models.

Value

A list containing agree.ordinal which is a single confusion matrix for ordinal columns, and residuals which contains Dunn-Smyth residuals.

Author(s)

Francis K.C. Hui [aut, cre], Wade Blanchard [aut]

Maintainer: Francis K.C. Hui <fhui28@gmail.com>

References

See Also

plot.boral for constructing residual analysis plots directly; fitted.boral which calculated fitted values from a model.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## Not run: 
## NOTE: The values below MUST NOT be used in a real application;
## they are only used here to make the examples run quick!!!
example_mcmc_control <- list(n.burnin = 10, n.iteration = 100, 
     n.thin = 1)
     
testpath <- file.path(tempdir(), "jagsboralmodel.txt")


library(mvabund) ## Load a dataset from the mvabund package
data(spider)
y <- spider$abun

spiderfit_nb <- boral(y, family = "negative.binomial", lv.control = list(num.lv = 2),
     row.eff = "fixed", mcmc.control = example_mcmc_control, model.name = testpath)

ds.residuals(spiderfit_nb) 

## End(Not run)

Example output

Loading required package: coda
This is boral version 1.9. If you recently updated boral, please check news(package = "boral") for the updates in the latest version.
row.ids assumed to be a matrix with one column and elements 1,2,...nrow(y) i.e., a row-specific intercept.
module glm loaded
Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 336
   Unobserved stochastic nodes: 466
   Total graph size: 2570

Initializing model

$agree.ordinal
NULL

$residuals
      Alopacce    Alopcune     Alopfabr   Arctlute    Arctperi    Auloalbi
1   2.11371766  1.01402194  0.315451916 -1.3281904  0.12527133  0.50827907
2  -0.80427831 -0.15427695 -0.846177042 -0.7919309 -0.49273896  2.13774426
3   1.14047909  1.64321448  0.805922689  1.1752429  0.48906548  0.77697210
4   0.72369831  0.13009950 -0.510835029  0.3686580 -0.32802758  0.54426993
5   0.30319622  0.92309792 -1.280397401  0.3959739  0.68187485  0.48692823
6  -0.61583411 -0.13005277 -0.016024110  1.1707197  0.75389792  0.54683389
7   0.32378559  0.52749869  0.393152030  1.7691210  0.73573482  1.40976467
8  -1.29539007  0.85274128 -0.253810450 -0.3876365  0.05700576  0.44187562
9  -0.01840567  0.38729226 -0.655059529  1.8166619  0.77516558 -0.05177644
10  0.71681919 -0.16585407  0.564522328  1.5735610 -1.03061160  0.47145617
11  1.02378885 -0.41713758  0.464287994 -0.7715030  0.39371082  0.49695839
12  1.35742264  1.50568469 -0.008770333  0.6841079 -2.12239051 -0.14488470
13  0.17133141  1.76243901  0.567903143  0.5181251 -2.33010454  1.23097617
14 -1.15985302 -0.73685687 -1.840905508  0.6882832  1.02144736  0.12023846
15 -0.84885867  0.03996916  0.260986802  0.1846852 -1.04013492 -0.92432809
16 -2.16591452  0.46864576 -2.006276710 -0.4014203  0.43062668 -0.77728634
17 -0.64793862 -0.52474560 -0.236688171  0.9789590  0.43974190 -0.82311641
18 -0.39328020  0.40341960  0.024376269  0.4677399  0.02210180 -1.50682826
19 -0.65912951  0.36386617 -0.356054831  0.6718934  0.34293448 -0.08426225
20 -1.35793918  0.97064914  0.539220219 -1.4680223 -0.17935309 -0.78761311
21 -0.73589274 -0.49488556 -0.110966318 -0.2616125 -0.74737186 -0.14061072
22  0.63859739  0.38483152  1.183927439  1.2089072  0.59234511  0.02242885
23  0.92222639 -1.45700040  0.991480063 -0.3256397  0.91853192 -0.84440742
24  1.07731737 -0.59989980  1.282299462  1.1009538  0.52140240 -0.96360611
25  0.70468335 -0.11606127  1.155763252 -0.4081418  0.49539398  0.78218683
26  0.46628796 -1.17526337  0.898248713  0.1505177  1.82047871  0.60531193
27  1.45276231 -1.07750097  0.962349234 -0.1279366  0.84164111 -0.58475439
28  1.08283816 -0.77833257  1.114395500 -1.1093768  0.07510990  0.51164362
      Pardlugu    Pardmont      Pardnigr    Pardpull  Trocterr    Zoraspin
1  -0.43314810  2.76072329  1.2031148321  2.22901187 2.4551696  0.43406914
2   0.18274940  0.27107099  1.5386090730  2.44484577 2.4968444  1.33361415
3  -0.12955772  1.67854029  1.3215725344  2.12015509 2.3049541 -0.22857463
4  -0.53511432  1.33570800  1.4333611345  2.12165656 2.7267092  1.20078223
5  -0.21022185  0.61666286  2.7117214889  2.17130757 2.8079855  0.90586133
6  -1.44261306  1.04925320  1.4389927839  1.71016042 2.2609798  1.79425164
7   0.05375637  2.12095639  3.0071727826  3.25459684 3.4282341  1.58581168
8   1.54283421  0.39995212  0.4022333258  0.77857561 1.6737467  0.00897903
9  -0.32229768  2.14193083  0.5692189775  1.13682392 1.2425757  0.21221941
10 -0.12052515  2.40711147 -1.5088938383 -0.45341982 1.0688313 -0.27957263
11 -0.93942119  2.86093264  0.2845550527  0.98171793 1.2311082 -0.31389936
12 -0.35046212  3.10962346  0.4740174769  1.46376604 1.5862997 -0.55240055
13 -0.14583420  1.48329469  1.8642384964  2.39736656 2.6928808  1.29184270
14 -0.12225230  1.02248737  0.9454664166  1.94493877 2.1281739  1.39173697
15  1.39817483 -1.79658938  0.1167282975 -0.75179403 2.4859538  1.32817055
16  0.86278881 -0.15448399  0.6480815134 -0.83816300 1.9181149  0.81514364
17  0.92521811 -0.42657635  0.5681498459  0.56822588 2.5382430  1.22909171
18  1.30375480 -1.47161463 -1.3880065145 -0.01753264 2.5182425 -1.47440060
19  1.64267962  0.25352889  0.5261982441  0.07130478 2.2820521  1.04675812
20  1.91294693 -1.11819252  0.4185937357 -1.39891510 2.2666620  0.87027678
21  1.25307378  0.28845659 -0.3453472086  0.87532308 1.7335504  0.42330708
22 -0.21205779  0.66837008 -0.4236624266  1.40510631 1.1695559  0.21093800
23  0.87727020  0.83827432 -0.0003567644 -0.36743863 0.9298944  0.10539993
24 -0.71858634  0.93296803 -0.3362749246  0.07152627 0.9176487 -1.16503694
25  0.22590023  1.16988431  1.2865193004  0.40080242 1.7251612  1.57041259
26 -0.60669728 -0.03780922  0.4114187126 -1.26538767 1.1599174 -1.26157192
27 -0.74162810  0.20830431  0.4375355841 -0.04979774 0.3194069  0.22485623
28 -0.46216050  0.98460301  0.3752735261 -0.70328585 1.0892127 -1.12319026

boral documentation built on March 12, 2021, 5:07 p.m.