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

cannot apply check_model title with patchwork::plot_annotation #712

Closed qdread closed 5 months ago

qdread commented 5 months ago

I recently tried to run some code from 2022 again, where I applied a title to a check_model() plot using patchwork::plot_annotation(). It returns an error which I've unsuccessfully tried to debug. This reprex should be equivalent to the code I'm trying to run. Thanks very much for your help!

Reprex:

library(easystats)
library(patchwork)

m <- lm(mpg ~ factor(cyl) + disp + hp, data = mtcars)
pp <- check_model(m)

print(plot(pp) + plot_annotation(title = 'mtcars model'))

Error:

Error in p + plot_annotation(title = "mtcars model") : 
  non-numeric argument to binary operator

Other potentially useful info:

> class(pp)
[1] "check_model"     "see_check_model"
> class(plot(pp))
[1] "list"

Session info:

R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

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

time zone: America/New_York
tzcode source: internal

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

other attached packages:
 [1] patchwork_1.2.0    see_0.8.3          report_0.5.8       parameters_0.21.6  performance_0.11.0 modelbased_0.8.7   insight_0.19.10   
 [8] effectsize_0.8.7   datawizard_0.10.0  correlation_0.8.4  bayestestR_0.13.2  easystats_0.7.1   

loaded via a namespace (and not attached):
 [1] gtable_0.3.4            tensorA_0.36.2.1        xfun_0.43               ggplot2_3.5.0           twosamples_2.0.1       
 [6] caTools_1.18.2          processx_3.8.4          lattice_0.22-5          vctrs_0.6.5             tools_4.3.3            
[11] ps_1.7.6                bitops_1.0-7            generics_0.1.3          parallel_4.3.3          sandwich_3.1-0         
[16] pbmcapply_1.5.1         tibble_3.2.1            fansi_1.0.6             DEoptimR_1.1-3          cmdstanr_0.7.1         
[21] pkgconfig_2.0.3         Matrix_1.6-5            checkmate_2.3.1         distributional_0.4.0    lifecycle_1.0.4        
[26] farver_2.1.1            compiler_4.3.3          munsell_0.5.1           codetools_0.2-19        pracma_2.4.4           
[31] pillar_1.9.0            MASS_7.3-60.0.1         iterators_1.0.14        abind_1.4-5             multcomp_1.4-25        
[36] foreach_1.5.2           nlme_3.1-164            robustbase_0.99-2       posterior_1.5.0         tidyselect_1.2.1       
[41] mvtnorm_1.2-4           dplyr_1.1.4             labeling_0.4.3          splines_4.3.3           grid_4.3.3             
[46] colorspace_2.1-0        cli_3.6.2               qqconf_1.3.2            magrittr_2.0.3          survival_3.5-8         
[51] utf8_1.2.4              TH.data_1.1-2           withr_3.0.0             scales_1.3.0            backports_1.4.1        
[56] opdisDownsampling_1.0.1 estimability_1.5        emmeans_1.10.1          zoo_1.8-12              coda_0.19-4.1          
[61] qqplotr_0.0.6           knitr_1.46              doParallel_1.0.17       mgcv_1.9-1              rlang_1.1.3            
[66] Rcpp_1.0.12             glue_1.7.0              rstudioapi_0.16.0       R6_2.5.1               
strengejacke commented 5 months ago

plot() returns a list of ggplot-objects, not an object where you can add layers from patchwork. However, is you use see::plots() on a list of ggplot-objects, that will return a patchwork-object:

library(easystats)
library(patchwork)

m <- lm(mpg ~ factor(cyl) + disp + hp, data = mtcars)
pp <- check_model(m)

x <- plot(pp)
plots(x, n_columns = 2) + plot_annotation(title = 'mtcars model')

Created on 2024-04-17 with reprex v2.1.0

bwiernik commented 5 months ago

@strengejacke i had thought plot() returned the patchwork object. Did we change that at some point? I don't seem much value in retuning a list vs the final thing that is shown by default--should we change it?

strengejacke commented 5 months ago

Maybe we can just add an else here? https://github.com/easystats/see/blob/7af4340caa87077c50d4baa83ef35f6ff712f7d4/R/plot.check_model.R#L276

qdread commented 5 months ago

Yes I am fairly sure that plot() returned the patchwork object at some point, and now returns a list.