rlinear-regressionlme4mixed-models

Error: $ operator not defined for this S4 class when trying to run vuong() function


I am trying to run a Vuong test using the vuong() function from pscl to compare two lmer() models that use the same data. However, each time I run it, I get the following error message:

> vuong(mod1, mod2)
Error: $ operator not defined for this S4 class

I have tried restarting R and updating my packages, but I still get the same error.

These are the packages I have loaded:

# ---- Load libraries ----
library(ggplot2)
library(pscl)
library(lmtest)
library(lme4)
library(car)

And this is the R version I am running:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252
[4] LC_NUMERIC=C                    LC_TIME=English_Canada.1252    

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

other attached packages:
[1] car_3.0-11    carData_3.0-4 lme4_1.1-27   Matrix_1.2-18 lmtest_0.9-38 zoo_1.8-9     pscl_1.5.5    ggplot2_3.3.5

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.1  purrr_0.3.4       splines_4.0.2     haven_2.4.3       lattice_0.20-41   colorspace_2.0-2 
 [7] vctrs_0.3.8       generics_0.1.0    mgcv_1.8-31       utf8_1.2.2        rlang_0.4.11      nloptr_1.2.2.2   
[13] pillar_1.6.3      foreign_0.8-80    glue_1.4.2        withr_2.4.2       DBI_1.1.1         readxl_1.3.1     
[19] lifecycle_1.0.1   munsell_0.5.0     gtable_0.3.0      cellranger_1.1.0  zip_2.2.0         labeling_0.4.2   
[25] rio_0.5.27        forcats_0.5.1     curl_4.3.2        fansi_0.5.0       Rcpp_1.0.6        scales_1.1.1     
[31] abind_1.4-5       farver_2.1.0      digest_0.6.28     hms_1.1.1         stringi_1.7.4     openxlsx_4.2.4   
[37] dplyr_1.0.7       grid_4.0.2        tools_4.0.2       magrittr_2.0.1    tibble_3.1.4      crayon_1.4.1     
[43] pkgconfig_2.0.3   MASS_7.3-51.6     ellipsis_0.3.2    data.table_1.14.2 assertthat_0.2.1  minqa_1.2.4      
[49] R6_2.5.1          boot_1.3-25       nlme_3.1-148      compiler_4.0.2 

Is there an issue with the packages I have loaded that would cause this function to not work? Or is there some other solution I could try to fix this?

I've also tried running other model types (e.g. lm instear of lmer) but all combinations return the same error, so I don't suspect it's something with the data and/or model.

Any help would be appreciated!


Solution

  • As you discovered, pscl::vuong doesn't work with lmer models.

    There is a nonnest package with a vuongtest function. From the example in that help page:

    ## Supplying custom vcov function
    require(lme4)
    require(merDeriv)
    
    fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy, REML=FALSE)
    fm2 <- lmer(Reaction ~ Days + (Days || Subject), sleepstudy, REML=FALSE)
    
    vcl <- function(obj) vcov(obj, full=TRUE)
    vuongtest(fm1, fm2, vc1=vcl, vc2=vcl, nested=TRUE)