rerror-handlinggammgcvgratia

"Error in Ops.data.frame(guide_loc, panel_loc) : ‘==’ only defined for equally-sized data frames" when using draw() from gratia to draw GAM in r


I am trying to draw GAMs in R using the below code. This code always used to work, and I could draw the partial effect of my smoothed term, but randomly it no longer works, consistently triggering this error:

Error in Ops.data.frame(guide_loc, panel_loc) : 
  ‘==’ only defined for equally-sized data frames

My code looks like the following:

library(mgcv)
library(gratia)

gam1 <- mgcv::gam(mpg ~ cyl +
                          s(disp), data = mtcars, method = "REML")

draw(gam1)

# directly drawing function from the package also triggers the error

gratia::draw(gam1)


The error message I receive makes no sense to me, as I do not see how it implicates data frame size (or at least not multiple data frames as the wording suggests.

This error first emerged when I attempted to take the derivatives of the model and draw them instead, with the following code:


derivatives1 <- derivatives(gam1,
                             term = "s(disp)",
                            order = 1L,
                             type = "central",
                             n = 250,
                             eps = 1,
                             interval = "simultaneous",
                             n_sim = 500000,
                             unconditional = TRUE)

draw(derivatives1)


which draws the same error:

Error in Ops.data.frame(guide_loc, panel_loc) : 
  ‘==’ only defined for equally-sized data frames

But now this error happens anytime I try to draw a gam, or its derivatives. I have tried restarting r and unloading packages to ensure no package conflict is causing this, but the error still emerges.

How can I resolve this error and draw my GAMs and derivatives of the GAMs' smooths?

Update

Here is my session info:

R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

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

other attached packages:
 [1] kableExtra_1.3.4   mediation_4.5.0    mvtnorm_1.2-4      MASS_7.3-60.0.1    sandwich_3.1-0     schoenberg_2.0.3  
 [7] mgcViz_0.1.11      qgam_1.3.4         viridis_0.6.5      viridisLite_0.4.2  tidymv_3.4.2       scales_1.3.0      
[13] gamm4_0.2-6        mgcv_1.9-1         nlme_3.1-164       modelsummary_1.4.1 sjPlot_2.8.14      ggpubr_0.6.0      
[19] gridExtra_2.3      lme4_1.1-35.2      Matrix_1.6-5       countrycode_1.5.0  labelled_2.11.0    haven_2.5.4       
[25] lubridate_1.9.2    forcats_1.0.0      stringr_1.5.1      dplyr_1.1.4        purrr_1.0.2        readr_2.1.5       
[31] tidyr_1.3.1        tibble_3.2.1       ggplot2_3.5.0      tidyverse_2.0.0    gratia_0.9.0      
     

Update 2

Here is the traceback on the error from a clean session in which only mgcv and gratia are loaded:

10: stop(gettextf("%s only defined for equally-sized data frames", 
        sQuote(.Generic)), domain = NA)
9: Ops.data.frame(guide_loc, panel_loc)
8: unlist(guide_loc == panel_loc)
7: add_guides(gt, guides == "collect")
6: plot_table.ggplot(X[[i]], ...)
5: FUN(X[[i]], ...)
4: lapply(x$plots, plot_table, guides = guides)
3: build_patchwork(plot, plot$layout$guides %||% "auto")
2: print.patchwork(x)
1: (function (x, ...) 
   UseMethod("print"))(x)

and here is the session info for that clean session:

R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/New_York
tzcode source: internal

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

other attached packages:
[1] gratia_0.9.0 mgcv_1.9-1   nlme_3.1-164

loaded via a namespace (and not attached):
 [1] Matrix_1.6-5       mvnfast_0.2.8      gtable_0.3.4       dplyr_1.1.4        compiler_4.3.3     tidyselect_1.2.1  
 [7] Rcpp_1.0.12        stringr_1.5.1      tidyr_1.3.1        splines_4.3.3      scales_1.3.0       yaml_2.3.8        
[13] fastmap_1.1.1      lattice_0.22-5     ggplot2_3.5.0      R6_2.5.1           labeling_0.4.3     generics_0.1.3    
[19] patchwork_1.1.3    knitr_1.45         tibble_3.2.1       munsell_0.5.0      RColorBrewer_1.1-3 pillar_1.9.0      
[25] rlang_1.1.3        utf8_1.2.4         stringi_1.8.3      xfun_0.43          cli_3.6.2          withr_3.0.0       
[31] magrittr_2.0.3     digest_0.6.35      grid_4.3.3         rstudioapi_0.16.0  lifecycle_1.0.4    ggokabeito_0.1.0  
[37] vctrs_0.6.5        evaluate_0.23      glue_1.7.0         farver_2.1.1       fansi_1.0.6        colorspace_2.1-0  
[43] purrr_1.0.2        rmarkdown_2.26     tools_4.3.3        pkgconfig_2.0.3    htmltools_0.5.8.1 

Solution

  • You must either have an old version of {gratia} (though I doubt I've ever seen this error reported for a CRAN-released version of the package - if you installed from GitHub, update your package from CRAN to get version 0.9.0) or have some other package loaded that is masking or otherwise interacting badly with {gratia}.

    With the CRAN release of {gratia} I can't reproduce your problem.

    r$> library(mgcv) 
        library(gratia) 
         
        gam1 <- mgcv::gam(mpg ~ cyl + 
                                  s(disp), data = mtcars, method = "REML") 
         
        draw(gam1) 
         
        # directly drawing function from the package also triggers the error 
         
        gratia::draw(gam1)                                                          
    Loading required package: nlme
    This is mgcv 1.9-1. For overview type 'help("mgcv-package")'.
    
    r$> derivatives1 <- derivatives(gam1, 
                                     term = "s(disp)", 
                                    order = 1L, 
                                     type = "central", 
                                     n = 250, 
                                     eps = 1, 
                                     interval = "simultaneous", 
                                     n_sim = 500000, 
                                     unconditional = TRUE) 
         
        draw(derivatives1)                                                          
    Warning message:
    The `term` argument of `derivatives()` is deprecated as of gratia 0.8.9.9.
    ℹ Please use the `select` argument of `derivativess()` instead.
    This warning is displayed once every 8 hours.
    Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
    generated. 
    

    (You can ignore the warning; if it bothers you, change term = "s(disp)" to be select = "s(disp)".