rvlenth / emmeans

Estimated marginal means
https://rvlenth.github.io/emmeans/
364 stars 32 forks source link

Feature request: joint_tests to recognize asin.sqrt transformation via make.tran() #435

Closed hnguyen19 closed 1 year ago

hnguyen19 commented 1 year ago

I asked this question on Stackoverflow (https://stackoverflow.com/questions/76837389/joint-tests-doesnt-recognize-asin-sqrt-transformation-via-make-tran), but it didn't gain much attention, so I'm posting it here.

I have the following dummy model

tran = make.tran("asin.sqrt", 100)

mod <- with(tran,
lm(linkfun(cty) ~ manufacturer +  drv * linkfun(hwy), data = mpg))
but, I cannot get the ANOVA table with

joint_tests(mod)

Error in ratios && true.con && misc$tran %in% c("log", "log2", "log10", : 'length = 5' in coercion to 'logical(1)' emmeans does recognize the transformation, as detailed in https://cran.r-project.org/web/packages/emmeans/vignettes/transformations.html.

emmeans(mod, ~ manufacturer, type = "response")
manufacturer response    SE  df lower.CL upper.CL
 audi             15.0 0.258 214     14.4     15.5
 chevrolet        16.1 0.240 214     15.7     16.6
 dodge            16.2 0.216 214     15.8     16.6
 ford             16.6 0.223 214     16.2     17.1
 honda            17.4 0.390 214     16.7     18.2
 hyundai          16.1 0.289 214     15.5     16.7
 jeep             16.5 0.362 214     15.8     17.2
 land rover       15.2 0.480 214     14.2     16.1
 lincoln          15.7 0.634 214     14.4     16.9
 mercury          16.0 0.479 214     15.1     17.0
 nissan           16.8 0.289 214     16.2     17.3
 pontiac          14.8 0.423 214     14.0     15.7
 subaru           16.9 0.356 214     16.2     17.6
 toyota           16.9 0.206 214     16.5     17.3
 volkswagen       16.5 0.248 214     16.0     17.0

Results are averaged over the levels of: drv 
Confidence level used: 0.95 
Intervals are back-transformed from the asin(sqrt(mu/100)) scale 

Thanks

rvlenth commented 1 year ago

I do not get this error on my machine:

mod <- with(tran,

  • lm(linkfun(cty) ~ manufacturer + drv * linkfun(hwy), data = ggplot2::mpg))

joint_tests(mod) model term df1 df2 F.ratio p.value manufacturer 14 214 6.828 <.0001 drv 2 214 11.804 <.0001 hwy 1 214 733.144 <.0001 drv:hwy 2 214 5.378 0.0053

However, I believe this is the same as issue #428 (https://github.com/rvlenth/emmeans/issues/428) which is recently fixed and will be included in the next update to be sent to CRAN soon. Meanwhile, if you like, you can install it from GitHub: see https://github.com/rvlenth/emmeans

rvlenth commented 1 year ago

Closing this, as I think it's resolved

hnguyen19 commented 1 year ago

Thank you, could you please add asin (without square-root), too?

with

 a.tran = make.tran("asin", 100)
mod <- with(a.tran,   lm(linkfun(cty) ~ manufacturer +  drv * linkfun(hwy), data = mpg))

the back-tranformation introduced sqrt which was not included in a.tran.

 emmeans(mod, ~ manufacturer, type = "response")
 manufacturer response    SE  df lower.CL upper.CL
 audi             15.0 0.258 214     14.4     15.5
 chevrolet        16.1 0.240 214     15.7     16.6
 dodge            16.2 0.216 214     15.8     16.6
 ford             16.6 0.223 214     16.2     17.1
 honda            17.4 0.390 214     16.7     18.2
 hyundai          16.1 0.289 214     15.5     16.7
 jeep             16.5 0.362 214     15.8     17.2
 land rover       15.2 0.480 214     14.2     16.1
 lincoln          15.7 0.634 214     14.4     16.9
 mercury          16.0 0.479 214     15.1     17.0
 nissan           16.8 0.289 214     16.2     17.3
 pontiac          14.8 0.423 214     14.0     15.7
 subaru           16.9 0.356 214     16.2     17.6
 toyota           16.9 0.206 214     16.5     17.3
 volkswagen       16.5 0.248 214     16.0     17.0

Results are averaged over the levels of: drv 
Confidence level used: 0.95 
Intervals are back-transformed from the asin(sqrt(mu/100)) scale  
rvlenth commented 1 year ago

Just to be clear, the square root is included in a.tran because make.tran() uses partial matching:

> a.tran = make.tran("asin")
> a.tran$name
[1] "asin(sqrt(mu))"

that is, "asin` is matched to "asin.sqrt". And the function is documented clearly with the possible choices.

I am unaware of a case where asin(y) is a variance-stabilizing transformation or other common transformation context; and I am afraid that if I add it, it may break some existing code out there somewhere, because the partial matching will work differently. In fact, I think it is likely that that will happen.

So I would need a really good reason to add it.

hnguyen19 commented 1 year ago

Thank you so much for the explanation. Before this update, I was able to get ANOVA tables via joint_tests in which the covariate was asin(y)-transformed (now I know arsine without square-root is inappropriate), so I thought it was accepted.