florianhartig / DHARMa

Diagnostics for HierArchical Regession Models
http://florianhartig.github.io/DHARMa/
201 stars 21 forks source link

Error in testCategorical() - replacement has length zero (resolved -> factor with zero observations) #348

Closed florianhartig closed 5 months ago

florianhartig commented 1 year ago

Question via email:

When I was inspecting my model with DHARMa package, I had the attached graphic. Seeing this graphic, my model seems fine. However, when I used the following code, I had the error message.

testCategorical(mysim, catPred = mydf$test) Error in out$uniformity$p.value[i] <- out$uniformity$details[[i]]$p.value : replacement has length zero

plot() shows results without any problem. But when I specifically checked uniformity in a categorical variable, I had this error message. What does this mean? Is this something I can ignore? If you can tell me what is happening, that would be greatly appreciated.

image
beneducizachary commented 9 months ago

Curious if this got resolved? I'm having the same issue for one of my categorical variables. Let me know if I can provide anything that would help diagnose the problem.

Thanks!

florianhartig commented 9 months ago

No, this was not fully resolved. It would indeed be helpful to have a reproducible example of this.

averill-murray commented 5 months ago

I just encountered the same problem - after the code had successfully run before. My dataframe is attached and code is pasted below. I did not get an error for the factor "type". Use_Avail.csv

M2 <- glmer(cbind(Used, NotUsed) ~ Sex + type + (1 | ID), family = binomial, data=Use_Avail) simOut <- simulateResiduals(M2, plot = T) plotResiduals(simOut, Use_Avail$type) plotResiduals(simOut, Use_Avail$Sex)

averill-murray commented 5 months ago

Note that the following boxplot command produces the desired plot, but with a blank factor space that is not represented in the dataframe.

boxplot(simOut$scaledResiduals ~ Use_Avail$Sex)

florianhartig commented 5 months ago

OK, I'm not getting an error. Can you open a new R session and run this again, and confirm that you get a reproducible error?

dat = read.csv("~/Downloads/Use_Avail.csv",  stringsAsFactors = T)

library(lme4)
library(DHARMa)

M2 <- glmer(cbind(Used, NotUsed) ~ Sex + type + (1 | ID), family = binomial,
            data=dat)
simOut <- simulateResiduals(M2, plot = T)
plotResiduals(simOut, dat$type)
plotResiduals(simOut, dat$Sex)
averill-murray commented 5 months ago

I opened a new R session and got the same error with this data set. However, plotResiduals works fine with (both) factors in a separate dataset/.Rmd file.

florianhartig commented 5 months ago

Can you give me a session.info() ?

averill-murray commented 5 months ago

sessionInfo() R version 4.3.2 (2023-10-31 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 11 x64 (build 22621)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

time zone: America/Bogota tzcode source: internal

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] phuassess_1.1 ggpubr_0.6.0 emmeans_1.9.0 effects_4.2-2 carData_3.0-5
[6] MuMIn_1.47.5 lattice_0.22-5 DHARMa_0.4.6 glmmTMB_1.1.8 lme4_1.1-35.1
[11] Matrix_1.6-4 nlme_3.1-163 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1
[16] dplyr_1.1.4 purrr_1.0.2 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1
[21] ggplot2_3.4.4 tidyverse_2.0.0 rstatix_0.7.2

loaded via a namespace (and not attached): [1] Rdpack_2.6 DBI_1.1.3 sandwich_3.1-0 rlang_1.1.2
[5] magrittr_2.0.3 multcomp_1.4-25 compiler_4.3.2 mgcv_1.9-0
[9] vctrs_0.6.4 pkgconfig_2.0.3 fastmap_1.1.1 backports_1.4.1
[13] ellipsis_0.3.2 labeling_0.4.3 utf8_1.2.4 promises_1.2.1
[17] rmarkdown_2.25 tzdb_0.4.0 nloptr_2.0.3 xfun_0.41
[21] highr_0.10 later_1.3.2 broom_1.0.5 parallel_4.3.2
[25] R6_2.5.1 gap.datasets_0.0.6 stringi_1.8.1 qgam_1.3.4
[29] car_3.1-2 boot_1.3-28.1 numDeriv_2016.8-1.1 estimability_1.4.1 [33] Rcpp_1.0.11 iterators_1.0.14 knitr_1.45 zoo_1.8-12
[37] httpuv_1.6.13 splines_4.3.2 nnet_7.3-19 timechange_0.2.0
[41] tidyselect_1.2.0 rstudioapi_0.15.0 abind_1.4-5 yaml_2.3.8
[45] doParallel_1.0.17 TMB_1.9.10 codetools_0.2-19 plyr_1.8.9
[49] shiny_1.8.0 withr_2.5.2 evaluate_0.23 survival_3.5-7
[53] survey_4.2-1 pillar_1.9.0 gap_1.5-3 foreach_1.5.2
[57] stats4_4.3.2 insight_0.19.7 generics_0.1.3 hms_1.1.3
[61] munsell_0.5.0 scales_1.2.1 minqa_1.2.6 xtable_1.8-4
[65] glue_1.6.2 tools_4.3.2 ggsignif_0.6.4 mvtnorm_1.2-4
[69] grid_4.3.2 mitools_2.4 rbibutils_2.2.16 colorspace_2.1-0
[73] cli_3.6.1 fansi_1.0.5 gtable_0.3.4 digest_0.6.33
[77] TH.data_1.1-2 farver_2.1.1 htmltools_0.5.7 lifecycle_1.0.4
[81] mime_0.12 MASS_7.3-60

florianhartig commented 5 months ago

OK, but then you haven't started your script in a new R Session, there are lots of packages loaded that seem to come from other stuff you may have been doing.

Can you start either a new R session or a new Rmd file with only my code above, and confirm that you still get the error?

averill-murray commented 5 months ago

OK, when I successfully start a clean, new R session with only lme4 and DHARMa loaded, I do not get the error message. If I re-run the .Rmd file with all the other packages and analyses, the error reappears. Now, I'm going to trouble-shoot conflicts with some of those other packages. There are a couple that I've only recently added.

averill-murray commented 5 months ago

It looks like something is causing the Sex factor in the Use_Avail dataframe to have an extra "" level (i.e., when I look at it with str() or levels()). However, examination of every row in the dataframe doesn't show anything extra. Do you know what might be causing that?

averill-murray commented 5 months ago

I found it! In a separate call to read a .csv file, the file came in with 3 extra blank rows. Those didn't show up in the derived files that the model was actually run on, but the blank factor in Sex apparently was still causing problems. Once I deleted those rows at the beginning, everything flowed perfectly from that point.

florianhartig commented 5 months ago

OK, great that this is solved.

I think I'll close this here also because I'm not 100% sure if we are talking about the same or different issues. If someone else is experiencing this problem please re-open!