1

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 
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Ana
  • 87
  • 9
  • I was able to reproduce the same error on my machine. I'd suggest writing to the package author for clarifications. Contacts are available at https://cran.r-project.org/web/packages/drc/index.html – Iaroslav Domin Jan 13 '21 at 11:10
  • Also you can try installing older `drc` or `lmtest` versions via `remotes::install_version`. The list of available `drc` versions can be found at https://cran.r-project.org/src/contrib/Archive/drc/ . Similarly, for `lmtest`. – Iaroslav Domin Jan 13 '21 at 11:12
  • `estfun()` is is in the _sandwich_ package, and the error says that there is no method implemented to calculate a sandwich estimator for a `"drc"` object. However, you could write a new version of the package's own method `drc:::vcov.drc` to account for heteroscedasticity. – jay.sf Jan 13 '21 at 11:25
  • @jay.sf Well, sounds like a way to solve it, but it's waaay out of my knowledge zone :D Thanks anyway. – Ana Jan 13 '21 at 11:28
  • 1
    @Ana X-mas is just over, but you could try to request the [authors](http://sandwich.r-forge.r-project.org/) ;) – jay.sf Jan 13 '21 at 12:10
  • 1
    The problem is not the _sandwich_ package but the _drc_ package: It provides both estfun.drc and bread.drc as methods to the generics from _sandwich_ but it does not formally register these methods. This used to work in older versions of R (up to 3.5.x or 3.6.x or so, didn't check) but was disabled. I'll let the _drc_ maintainers know about this. – Achim Zeileis Jan 13 '21 at 15:36

2 Answers2

3

@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.

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
Iaroslav Domin
  • 2,698
  • 10
  • 19
  • But anyways it's better to let the `drc` package author know about it, because it's a strange behavior. – Iaroslav Domin Jan 13 '21 at 11:49
  • Indeed, it solves the problem. However, I am not sure if the results are considered ok or not. Not sure about your results, but mine differ by a factor 5 (std. error - 0.09 instead of 0.29 for b and t value - 26.8 instead of 8.5 for b). Estimate is ok. – Ana Jan 13 '21 at 13:52
  • @Ana same for me. Don't know why results are different. Maybe this guide is just too old. – Iaroslav Domin Jan 13 '21 at 14:48
  • 2
    The problem was that you just enabled the estfun.drc method but not the bread.drc method. Both are needed to get correctly scaled results. Without bread.drc the bread.default method is used which does not match the scaling of the estfun.drc method. I've updated (and upvoted) the response by Iaroslav correspondingly and will send the drc maintainers a note. – Achim Zeileis Jan 13 '21 at 15:51
-1
coeftest(model1, vcov.dcr=sandwich)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Apr 16 '22 at 00:37