Skip to content

Fix atanh mu.eta so type = "response" works#581

Open
SchmidtPaul wants to merge 1 commit into
rvlenth:mainfrom
SchmidtPaul:fix/atanh-mu-eta
Open

Fix atanh mu.eta so type = "response" works#581
SchmidtPaul wants to merge 1 commit into
rvlenth:mainfrom
SchmidtPaul:fix/atanh-mu-eta

Conversation

@SchmidtPaul

Copy link
Copy Markdown

The atanh response transformation defines its mu.eta derivative as
alpha * (1 - tanh^2(eta)). That is invalid R: tanh^2 applies ^ to the
tanh closure, so any back-transformation (type = "response") of an
atanh-transformed response errors out. The intended derivative of the inverse
link alpha * tanh(eta) is alpha * (1 - tanh(eta)^2).

rng <- range(iris$Sepal.Width)
iris$r <- 1.8 * (iris$Sepal.Width - rng[1]) / (rng[2] - rng[1]) - 0.9  # in (-0.9, 0.9)
mod <- lm(atanh(r) ~ Species, data = iris)

# --- before:
emmeans(mod, "Species", type = "response")
## Error in alpha * (1 - tanh^2(eta)) : attempt to apply non-function

# --- after:
emmeans(mod, "Species", type = "response")
##  Species    response     SE  df lower.CL upper.CL
##  setosa        0.194 0.0427 147    0.108    0.276
##  versicolor   -0.352 0.0388 147   -0.426   -0.273
##  virginica    -0.180 0.0429 147   -0.263   -0.094
##
## Intervals are back-transformed from the atanh(mu) scale

Root cause: R/transformations.R, the atanh branch's mu.eta. Present since
the make.tran() revamp (commit 22f1ffc, 2022-12-02; first shipped in 1.8.3) —
the type = "response" path for this transformation appears to be untested.
Added a regression test in tests/testthat/test-transformations.R.

Found via an AI-assisted bug hunt (see #580).

The atanh response transformation defined mu.eta as
alpha * (1 - tanh^2(eta)), which is invalid R: tanh^2 applies ^ to the
tanh closure, so summary(..., type='response') errored with
'attempt to apply non-function'. Corrected to alpha * (1 - tanh(eta)^2),
the derivative of the inverse link alpha*tanh(eta). Added a regression
test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant