'coeftest() gives error with drc class object

I am trying to reproduce a supplementary example 2 Continuous response: one dose-response curve from the drc package.

When trying to run coeftest(ryegrass.LL.4, vcov = sandwich), I get the following error:

Error in UseMethod("estfun") : 
 no applicable method for 'estfun' applied to an object of class "drc".

I tried to google the error with no success and to use the vcov. instead of vcov (since vcov. is automatically suggested in R, but article uses vcov) and still got the same error.

Any ideas how to solve this?

Session info:

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

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

other attached packages:
[1] lmtest_0.9-38  zoo_1.8-8      sandwich_3.0-0 drc_3.0-1      MASS_7.3-53   

loaded via a namespace (and not attached):
 [1] zip_2.1.1         Rcpp_1.0.5        pillar_1.4.7      compiler_4.0.3    cellranger_1.1.0 
 [6] forcats_0.5.0     tools_4.0.3       lifecycle_0.2.0   tibble_3.0.4      lattice_0.20-41  
[11] pkgconfig_2.0.3   rlang_0.4.8       Matrix_1.2-18     openxlsx_4.2.3    rstudioapi_0.13  
[16] curl_4.3          mvtnorm_1.1-1     haven_2.3.1       xfun_0.18         rio_0.5.16       
[21] vctrs_0.3.5       gtools_3.8.2      hms_0.5.3         grid_4.0.3        data.table_1.13.4
[26] R6_2.5.0          plotrix_3.7-8     survival_3.2-7    readxl_1.3.1      foreign_0.8-80   
[31] multcomp_1.4-15   TH.data_1.0-10    carData_3.0-4     car_3.0-10        magrittr_1.5     
[36] scales_1.1.1      codetools_0.2-16  splines_4.0.3     ellipsis_0.3.1    abind_1.4-5      
[41] colorspace_2.0-0  tinytex_0.26      stringi_1.5.3     munsell_0.5.0     crayon_1.3.4 


Solution 1:[1]

@jay.sf is right, estfun is a function from the sandwich package. And it is defined as a generic function. Just type estfun and press enter and you'll see

function (x, ...) 
{
  UseMethod("estfun")
}

This means that if it sees an object of class "drc" (which ryegrass.LL.4 is) it searches for a function called estfun.drc to apply. If it were an object of class "foo" it would've tried to find estfun.foo. For more details, see a chapter on S3 classes in H. Wickham's book.

Actually, the drc package even provides a method estfun.drc - and the companion bread.drc method for the bread generic from sandwich. However, this is not found by the corresponding generic functions because the methods are not formally registered in the drc package. In older versions of R this used to work but has been disabled by now. The dispatch to estfun.drc based on the naming convention alone just works for functions in the global environment but not in packages. Thus, a quick and dirty solution would be to create copies of the functions in your global environment:

estfun.drc <- drc::estfun.drc
bread.drc <- drc::bread.drc

A better approach would be to register the functions as S3 methods, rather than copying them:

registerS3method("estfun", "drc", drc::estfun.drc)
registerS3method("bread", "drc", drc::bread.drc)

Of course, it would be even better if drc did this within the package so that you as the user don't have to do that. Maybe the authors would be willing to update the package correspondingly.

Solution 2:[2]

coeftest(model1, vcov.dcr=sandwich)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Achim Zeileis
Solution 2 Jeremy Caney