R/ResidPlotWide.R

Defines functions ResidPlotWide

Documented in ResidPlotWide

#' @rdname ResidPlotLong
#'
#' @export

ResidPlotWide <- function(Data, ModNames, GroupPat = "^[[:alnum:]]+",
                         Nsims = 1000, TestVals = T){

  ResidPlotterSub <- function(GroupMod){
    TmpMod <- get(Filter(function(x) inherits(get(x), "glmmTMB"), ls(pattern = paste0("^", GroupMod, "$"),
                                                                     pos = .GlobalEnv)), pos = .GlobalEnv)
    Group <- regmatches(GroupMod, regexpr(GroupPat, GroupMod))
    ModTab <- stats::simulate(TmpMod, nsim = Nsims)
    SimMod <- do.call(cbind, ModTab)
    SimModDharma <- DHARMa::createDHARMa(simulatedResponse = SimMod, observedResponse = Data[[Group]],
                                         fittedPredictedResponse = predict(TmpMod), integerResponse = TRUE)

    GroupResidTests <- DHARMa::testResiduals(SimModDharma, plot = T)

    ResidPlot <- grDevices::recordPlot()
    assign(paste0(GroupMod, "SimResidPlot"), ResidPlot, pos = .GlobalEnv)
    if(TestVals == T){assign(paste0(GroupMod, "ResidTests"), GroupResidTests, pos = .GlobalEnv)}
  }
  lapply(ModNames, ResidPlotterSub)
}
huntercole25/EcoCountHelper documentation built on Jan. 14, 2023, 4:13 a.m.