无法使用emmeans获得arcsin反转换。

huangapple go评论68阅读模式
英文:

Can't get arcsin back-transformation with emmeans to work

问题

I am doing a GAM with my response variable as a percentage (0-100). I have used an arcsin transformation to improve model fit (asin(sqrt(myvariable/100))). I now want to evaluate contrasts between levels of my explanatory factor variable on the original scale. I have been trying to use emmeans and followed the steps in the Transformations and link functions vignette to set up my model with the transformation in a format emmeans can read. However, when I run the emmeans function I get the following error: Error in link$mu.eta(object@bhat[estble]) : attempt to apply non-function.

I set up the transformation object like this:

tran <- make.tran("asin.sqrt", 100)

I am confident that bit works, because when I tried it on a linear model with emmeans, it worked:

warp.t <- with(tran, lm(linkfun(breaks)~wool*tension, warpbreaks))
emmeans(warp.t, ~wool|tension, type="response")
tension = L:
 wool response   SE df lower.CL upper.CL
 A        44.2 4.00 48     36.3     52.3
 B        27.7 3.61 48     20.8     35.3

tension = M:
 wool response   SE df lower.CL upper.CL
 A        23.5 3.41 48     17.0     30.7
 B        28.4 3.63 48     21.4     35.9

tension = H:
 wool response   SE df lower.CL upper.CL
 A        23.9 3.43 48     17.4     31.1
 B        18.6 3.13 48     12.7     25.3

Confidence level used: 0.95 
Intervals are back-transformed from the asin(sqrt(mu/100)) scale

However, if I try it in a gam (both straight into emmeans() or using regrid()), it doesn't work:

dat <- data.frame("x" = rep(1:3, times=12), 
                  "y" = rep(4:6, times=12), 
                  "z" = runif(36, 0, 100), 
                  "m" = rep(1:12, times=3))

gam.t <- with(tran, gam(linkfun(z) ~ x * y + s(m), data=dat))

emmeans(gam.t, ~x|y, type="response")
Error in linkinv(result[[cnm[1]]]) : could not find function "linkinv"

#or
regrid(emmeans(gam.t, ~x|y), transform="response")
Error in flink$mu.eta(object@bhat[estble]) : 
  attempt to apply non-function

It's like it's looking for the inverse link function in the gam, but it's not where emmeans expects it to be. Can I assign it to the gam somehow? Does it not work with gams? Am I doing something wrong?

英文:

I am doing a GAM with my response variable as a percentage (0-100). I have used an arcsin transformation to improve model fit (asin(sqrt(myvariable/100))). I now want to evaluate contrasts between levels of my explanatory factor variable on the original scale. I have been trying to use emmeans and followed the steps in the Transformations and link functions vignette to set up my model with the transformation in a format emmeans can read. However, when I run the emmeans function I get the following error: Error in link$mu.eta(object@bhat[estble]) : attempt to apply non-function.

I set up the transformation object like this:

tran <- make.tran("asin.sqrt", 100)

I am confident that bit works, because when I tried it on a linear model with emmeans, it worked:

warp.t <- with(tran, lm(linkfun(breaks)~wool*tension, warpbreaks))
emmeans(warp.t, ~wool|tension, type="response")
tension = L:
 wool response   SE df lower.CL upper.CL
 A        44.2 4.00 48     36.3     52.3
 B        27.7 3.61 48     20.8     35.3

tension = M:
 wool response   SE df lower.CL upper.CL
 A        23.5 3.41 48     17.0     30.7
 B        28.4 3.63 48     21.4     35.9

tension = H:
 wool response   SE df lower.CL upper.CL
 A        23.9 3.43 48     17.4     31.1
 B        18.6 3.13 48     12.7     25.3

Confidence level used: 0.95 
Intervals are back-transformed from the asin(sqrt(mu/100)) scale 

However, if I try it in a gam (both straight into emmeans() or using regrid()), it doesn't work:

dat <- data.frame("x" = rep(1:3, times=12), 
                  "y" = rep(4:6, times=12), 
                  "z" = runif(36, 0, 100), 
                  "m" = rep(1:12, times=3))

gam.t <- with(tran, gam(linkfun(z) ~ x * y + s(m), data=dat))

emmeans(gam.t, ~x|y, type="response")
Error in linkinv(result[[cnm[1]]]) : could not find function "linkinv"

#or
regrid(emmeans(gam.t, ~x|y), transform="response")
Error in flink$mu.eta(object@bhat[estble]) : 
  attempt to apply non-function

It's like it's looking for the inverse link function in the gam, but it's not where emmeans expects it to be. Can I assign it to the gam somehow? Does it not work with gams? Am I doing something wrong?

答案1

得分: 2

按照文中在标题“事后指定变换”下的说明,我认为以下代码会给你想要的结果:

gam.t2 <- gam(asin(sqrt(z/100)) ~ x * y + s(m), data = dat)

refgrid_gam.t2 <- update(ref_grid(gam.t2), tran = tran)

emmeans(refgrid_gam.t2, ~ x | y, type = "response")


在`gam()`调用中,响应变量被转换,然后我们在参考网格上调用`update()`来指定使用了什么变换。我得到的输出再次以百分比为单位:

y = 5:
x response SE df lower.CL upper.CL
2 52 10.3 32 31.5 72.2

置信水平使用: 0.95
区间从asin(sqrt(mu/100))的比例重新转换

英文:

Following the instructions in the vignette under the heading "Specifying a transformation after the fact" I think gives you the result you are looking for:

gam.t2 &lt;- gam(asin(sqrt(z/100)) ~ x * y + s(m), data = dat)

refgrid_gam.t2 &lt;- update(ref_grid(gam.t2), tran = tran)

emmeans(refgrid_gam.t2, ~ x | y, type = &quot;response&quot;)

The response variable is transformed in the call to gam(), then we call update() on the reference grid to specify what transformation was used. The output I get is on the percent scale again:

y = 5:
 x response   SE df lower.CL upper.CL
 2       52 10.3 32     31.5     72.2

Confidence level used: 0.95 
Intervals are back-transformed from the asin(sqrt(mu/100)) scale 

huangapple
  • 本文由 发表于 2023年4月13日 23:09:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定