rvlenth / emmeans

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

Issue with specifying power transformation #428

Closed lainem11 closed 1 year ago

lainem11 commented 1 year ago

Describe the bug

When calculating contrasts for an emmGrid object, I get an error:

"Error in ratios && true.con && misc$tran %in% c("log", "log2", "log10", : 'length = 7' in coercion to 'logical(1)'"

This only happens when I have specified a power transformation function to the emmGrid object.

To reproduce

I'm using R 4.3.0 and emmeans 1.8.7

require(pandas) require(emmeans) df = data.frame(resp=c(1,2,3,4,'0.12','0.17','0.23','0.3'),A=c(0,0,0,0,1,1,1,1),B=c(0,1,0,1,0,1,0,1)) fit = lm(resp~B+A,data=df) emms = emmeans(fit,data=df,~A+B,tran=make.tran(type=c("power"),0.1)) pairs(emms)

Expected behavior

I wanted to make pairwise comparisons for levels of my linear model. I added the transformation function so I could back-transform the estimates and confidence intervals. Seems that just the presence of power transform function causes the error, without even doing the back-transform.

rvlenth commented 1 year ago

Thanks for reporting this. The error occurs because in your example, misc$tran is a list of length 7 when we are expecting a character value. The mystery to me is why this wasn't discovered long ago, since it will happen whenever the user uses make.tran() or make.link() and then does pairwise comparisons. I have corrected the code, and it will be in the next CRAN update.

Meanwhile, a workaround is to add ratios = FALSE) to the call:

> pairs(emms, ratios = FALSE)
 contrast      estimate    SE df t.ratio p.value
 A0 B0 - A1 B0     2.29 0.668  5   3.438  0.0644
 A0 B0 - A0 B1    -0.53 0.668  5  -0.794  0.8547
 A0 B0 - A1 B1     1.76 0.944  5   1.870  0.3451
 A1 B0 - A0 B1    -2.83 0.944  5  -2.992  0.1024
 A1 B0 - A1 B1    -0.53 0.668  5  -0.794  0.8547
 A0 B1 - A1 B1     2.29 0.668  5   3.438  0.0644

Note: contrasts are still on the mu^0.1 scale 
P value adjustment: tukey method for comparing a family of 4 estimates 
rvlenth commented 1 year ago

I believe this issue is resolved, and the fix is in CRAN version 1.8.8, so am closing