Git page for compResidual
R-package containing methods to compute residuals for observed compositions. The computed residuals will be independent, standardized, and normally distributed if the model is correctly describing the observations. The distributions currently implemented corresponds to common choices in age or length based assessment models (Multinomial, Dirichlet, Dirichlet-multinomial, multivariate-normal, and Logistic-normal).
The package is intended to be mainly useful in the situation where a purely fixed effects model is producing predictions/estimates corresponding to a set of observations, then those can be given and the proper residuals can be produced.
Can be installed by typing:
TMB:::install.contrib("https://github.com/vtrijoulet/OSA_multivariate_dists/archive/main.zip")
remotes::install_github("fishfollower/compResidual/compResidual", force=TRUE)
To ensure only installing in 64-bit some windows users may need to install with:
TMB:::install.contrib("https://github.com/vtrijoulet/OSA_multivariate_dists/archive/main.zip")
remotes::install_github("fishfollower/compResidual/compResidual", INSTALL_opts=c("--no-multiarch"), force=TRUE)
Imagine that a model assumes that the composition observations
library(compResidual)
X<-matrix(c(
9, 1, 5, 9, 9, 3, 6, 3, 3, 4,
1, 5, 7, 1, 3, 6, 8, 9, 6, 5,
3, 7, 4, 2, 1, 8, 4, 5, 1, 2,
4, 2, 3, 5, 3, 1, 0, 1, 0, 2,
1, 3, 3, 0, 5, 5, 4, 3, 1, 0,
7, 7, 3, 8, 4, 2, 3, 4, 14, 12
), nrow=6, ncol=10, byrow=TRUE)
P<-matrix(c(
0.32, 0.08, 0.16, 0.24, 0.32, 0.20, 0.20, 0.16, 0.16, 0.16,
0.16, 0.16, 0.24, 0.20, 0.12, 0.16, 0.32, 0.28, 0.20, 0.20,
0.12, 0.24, 0.20, 0.12, 0.04, 0.24, 0.16, 0.12, 0.04, 0.08,
0.04, 0.16, 0.16, 0.12, 0.04, 0.08, 0.00, 0.08, 0.04, 0.20,
0.12, 0.08, 0.12, 0.00, 0.12, 0.12, 0.12, 0.08, 0.04, 0.04,
0.24, 0.28, 0.12, 0.32, 0.36, 0.20, 0.20, 0.28, 0.52, 0.32
), nrow=6, ncol=10, byrow=TRUE)
res<-resMulti(X,P)
plot(res)
Instead of providing the probability matrix it is also allowed to provide the predicted observations (N*P).
If the observations
If the observations are discrete, but still univariate and independent, then the distribution function
The OSA residual of the i'th observation is computed using one of the methods described above depending on the observations being continuous or discrete (quantile or randomized quantile residuals), but instead of using the cdf of the observation in isolation, the cdf of the predicted distribution of the i'th prediction conditioned on all previous observations is used. This allows the resulting residuals to become independent standard normal if the model is correct.
More details are available in the following paper \url{...}
Additional data for Gulf of Maine Haddock used in the paper is supplied in the file GOMhaddock.RData.
# Example for fleet 1
## load data file:
load("GOMhaddock.RData")
## extract observations fleet 1:
obs<-GOMhaddock[GOMhaddock$Fleet==1, grep("^obsP",colnames(GOMhaddock))]
## multiply by effective sample size and round:
obs<-round(obs*GOMhaddock[GOMhaddock$Fleet==1, "ESS"])
## extract predictions fleet 1:
pred<-GOMhaddock[GOMhaddock$Fleet==1, grep("^predP",colnames(GOMhaddock))]
## load library:
library(compResidual)
set.seed(123)
## calculate residuals:
res<-resMulti(t(obs), t(pred))
## Add names to sample number for plotting:
colnames(res) <- GOMhaddock[GOMhaddock$Fleet==1,"Year"]
## plot diagnostics:
plot(res)