easystats / performance

:muscle: Models' quality and performance metrics (R2, ICC, LOO, AIC, BF, ...)
https://easystats.github.io/performance/
GNU General Public License v3.0
1.01k stars 87 forks source link

Warning message In stats::qlogis(mu) : NaNs produced for ordbeta GLMM when calculating R2 #648

Closed Sebastian-Montoya-B closed 10 months ago

Sebastian-Montoya-B commented 11 months ago

Dear performance developers.

I've been trying to calculate R2 for a GLMM with ordbeta distribution, and I keep getting the following warning:

R2 for Mixed Models

Conditional R2: 1.000 Marginal R2: 0.126 Warning messages: 1: In stats::qlogis(mu) : NaNs produced 2: Can't calculate model's distribution-specific variance. Results are not reliable.

I tried to find out where the warning came from, and I got to the .variance_distributional() function of insight. In this function, mu is being calculated mainly in four steps:

.null_model <- null_model(x, verbose = verbose)
null_fixef <- unname(.collapse_cond(lme4::fixef(.null_model)))
mu <- null_fixef
mu <- switch(faminfo$family, beta = mu, ordbeta = stats::qlogis(mu), 
                 exp(mu))

where null_fixef ends up being the intercept of the model. Since the intercept of my model is negative, stats::qlogis(mu) produces NaNs, and the distribution-specific variance cannot be calculated. I was wondering whether this is a bug, or R2 simply can't be calculated for ordbeta when the model's intercept is not between [0,1]? Is there something I am missing?

Here's an example using the data from the attached csv file: dfexample.csv


library(glmmTMB)
library(performance)

dfex<-read.csv("dfexample.csv")

model<-glmmTMB(response~predictor +(1|random), data=dfex, family=ordbeta)
summary(model)

r2(model)

x<-model

faminfo<-insight::model_info(x)

.null_model <- insight::null_model(x, verbose = TRUE)
null_fixef <- unname(insight:::.collapse_cond(lme4::fixef(.null_model)))
mu <- null_fixef
mu <- switch(faminfo$family, beta = mu, ordbeta = stats::qlogis(mu), 
             exp(mu))

And here is the session info:

R version 4.2.3 (2023-03-15 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045)

Matrix products: default

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

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

other attached packages: [1] performance_0.10.8 glmmTMB_1.1.8

loaded via a namespace (and not attached): [1] Rcpp_1.0.9 rstudioapi_0.14 TMB_1.9.6 splines_4.2.3
[5] MASS_7.3-58.2 insight_0.19.6 xtable_1.8-4 lattice_0.20-45
[9] multcomp_1.4-25 minqa_1.2.5 tools_4.2.3 grid_4.2.3
[13] nlme_3.1-162 mgcv_1.8-42 TH.data_1.1-2 coda_0.19-4
[17] emmeans_1.8.8 survival_3.5-3 lme4_1.1-34 numDeriv_2016.8-1.1 [21] Matrix_1.6-1.1 nloptr_2.0.3 codetools_0.2-19 sandwich_3.0-2
[25] estimability_1.4.1 compiler_4.2.3 boot_1.3-28.1 mvtnorm_1.1-3
[29] zoo_1.8-11

Thank you very much for your time.

strengejacke commented 10 months ago

I think it must be plogis() here.

strengejacke commented 10 months ago

Should be fixed in https://github.com/easystats/insight/commit/62b86f7aaa7c67e7173d6360e9b471e833a52296

library(glmmTMB)
#> Warning in checkDepPackageVersion(dep_pkg = "TMB"): Package version inconsistency detected.
#> glmmTMB was built with TMB version 1.9.6
#> Current TMB version is 1.9.7
#> Please re-install glmmTMB from source or restore original 'TMB' package (see '?reinstalling' for more information)
library(performance)

dfex <- read.csv("d:/Downloads/dfexample.csv")

model <- glmmTMB(response ~ predictor + (1 | random), data = dfex, family = ordbeta)
summary(model)
#>  Family: ordbeta  ( logit )
#> Formula:          response ~ predictor + (1 | random)
#> Data: dfex
#> 
#>      AIC      BIC   logLik deviance df.resid 
#>   -451.1   -433.4    231.5   -463.1      136 
#> 
#> Random effects:
#> 
#> Conditional model:
#>  Groups Name        Variance Std.Dev.
#>  random (Intercept) 0.4193   0.6475  
#> Number of obs: 142, groups:  random, 3
#> 
#> Dispersion parameter for ordbeta family (): 28.9 
#> 
#> Conditional model:
#>             Estimate Std. Error z value Pr(>|z|)    
#> (Intercept) -1.78927    0.42482  -4.212 2.53e-05 ***
#> predictor   -0.16126    0.05979  -2.697  0.00699 ** 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

r2(model)
#> # R2 for Mixed Models
#> 
#>   Conditional R2: 0.166
#>      Marginal R2: 0.021

Created on 2023-12-03 with reprex v2.0.2