Skip to content

Commit

Permalink
update to allow DHARMa using predictive samples
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Thorson committed Sep 16, 2024
1 parent ef4b940 commit 03f0202
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 14 additions & 2 deletions R/utility.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#' @param object Output from \code{\link{dsem}}
#' @param nsim Number of simulations to use if \code{family!="fixed"} for some variable,
#' such that simulation residuals are required.
#' @param what whether to return quantile residuals, or samples from the leave-one-out predictive
#' distribution of data, or a table of leave-one-out predictions and standard errors for the
#' latent state
#' @param ... Not used
#'
#' @details
Expand All @@ -27,9 +30,11 @@
loo_residuals <-
function( object,
nsim = 100,
what = c("quantiles","samples","loo"),
... ){

# Extract and make object
what = match.arg(what)
tsdata = object$internal$tsdata
parlist = object$obj$env$parList()
df = expand.grid( time(tsdata), colnames(tsdata) )
Expand Down Expand Up @@ -84,13 +89,18 @@ function( object,
}

# Compute quantile residuals
resid_tjr = array( NA, dim=c(dim(tsdata),nsim) )
if( all(object$internal$family == "fixed") ){
# analytical quantile residuals
resid_tj = array( NA, dim=dim(tsdata) )
resid_tj[cbind(df[,1],df[,2])] = pnorm( q=df$obs, mean=df$est, sd=df$se )
# Simulations from predictive distribution for use in DHARMa etc
for(z in 1:nrow(df) ){
resid_tjr[df[z,1],df[z,2],] = rnorm( n=nsim, mean=df[z,'est'], sd=df[z,'se'] )
}
}else{
# Sample from leave-one-out predictive distribution for states
resid_rz = apply( df, MARGIN=1, FUN=\(vec){rnorm(n=nsim, mean=as.numeric(vec['est']), sd=as.numeric(vec['se']))} )
resid_tjr = array( NA, dim=c(dim(tsdata),nsim) )
# Sample from predictive distribution of data given states
for(r in 1:nrow(resid_rz) ){
parameters = object$obj$env$parList()
Expand All @@ -107,5 +117,7 @@ function( object,
# Calculate quantile residuals
resid_tj = apply( resid_tjr > outer(tsdata,rep(1,nsim)), MARGIN=1:2, FUN=mean )
}
return( resid_tj )
if(what=="quantiles") return( resid_tj )
if(what=="samples") return( resid_tjr )
if(what=="loo") return( df )
}
6 changes: 5 additions & 1 deletion man/loo_residuals.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 03f0202

Please sign in to comment.