Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 21, 2024
1 parent e497370 commit ae3849c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions R/get_loglikelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ get_loglikelihood.phyloglm <- get_loglikelihood.phylolm
# first derivative of 1/x is -1/x^2 - we cannot take the log from negative
# values, so this won't work here, and we return NULL
NULL
} else if (trans == "scale") {
scale_denominator <- .extract_scale_denominator(x)
.weighted_sum(log(1 / scale_denominator), w = model_weights)
} else if (trans == "power") {
trans_power <- .extract_power_transformation(x)
.weighted_sum(log(trans_power * (get_response(x, as_proportion = TRUE)^(trans_power - 1))), w = model_weights) # nolint
Expand Down
2 changes: 1 addition & 1 deletion R/get_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ get_transformation <- function(x, verbose = TRUE) {


.extract_scale_denominator <- function(model) {
resp_term <- find_terms(x)[["response"]]
resp_term <- find_terms(model)[["response"]]
# more complicated case: scale is inside `I()`
if (startsWith(resp_term[1], "I(")) {
as.numeric(gsub("(.*)/(.*)\\)", "\\2", resp_term[1]))
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-get_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ test_that("get_transformation - detect powers", {
expect_equal(fun$transformation(2), 25.99208, tolerance = 1e-3)
expect_equal(fun$inverse(25.99208), 2, tolerance = 1e-3)
})


test_that("get_transformation - detect scale", {
data(mtcars)
m <- lm(mpg / 0.7 ~ hp, data = mtcars)
fun <- get_transformation(m)
expect_equal(fun$transformation(2), 2 / 7, tolerance = 1e-3)
expect_equal(fun$inverse(2.857143), 2, tolerance = 1e-3)
m <- lm(I(mpg / 0.7) ~ hp, data = mtcars)
fun <- get_transformation(m)
expect_equal(fun$transformation(2), 2 / 7, tolerance = 1e-3)
expect_equal(fun$inverse(2.857143), 2, tolerance = 1e-3)
})

0 comments on commit ae3849c

Please sign in to comment.