rvlenth / emmeans

Estimated marginal means
https://rvlenth.github.io/emmeans/
358 stars 31 forks source link

Error in summary.emmGrid when calling inside function #367

Closed DavidLukeThiessen closed 2 years ago

DavidLukeThiessen commented 2 years ago

I see you're dealing with several recent bug reports, but I haven't read through all of them. I apologize if this is a duplicate of one of those. In the latest github version of emmeans, emmeans_1.8.0-2, I get an error message when I try to run the summary of an emmeans object from inside a function call if there's both a transformation in the response and a non-identity link function, and a type = "response" is requested. Reprex is below and a zipped copy is attached.

library(emmeans) # emmeans_1.8.0-2
mod <- glm(I(dist) ~ speed,
           data = cars,
           family = Gamma(link = "inverse"))
summary(emmeans(mod,
                specs = c("speed"),
                type = "response")) # This works as expected
foo <- function(bar) {
  summary(emmeans(bar,
                  specs = c("speed"),
                  type = "response"))
}
foo(mod) # Error, object 'bar' not found

I tracked it down to an eval() call in summary.emmGrid when object@misc$tran2 is TRUE, but haven't found anything else useful.

EmmeansTransformationBug.zip

rvlenth commented 2 years ago

Thanks. Evidently, that little piece of code is trying to retrieve the wrong object and I needed to be more explicit about it. I think it will work with 1.8.0-3

> foo(mod)
 speed response   SE df lower.CL upper.CL
  15.4       35 2.31 48     30.9     40.3

Confidence level used: 0.95 
Intervals are back-transformed from the identity[inverse] scale
DavidLukeThiessen commented 2 years ago

Looks like that fixed it. Thank you very much!