diff --git a/R/get_loglikelihood.R b/R/get_loglikelihood.R index 8f9221345..a58c14566 100644 --- a/R/get_loglikelihood.R +++ b/R/get_loglikelihood.R @@ -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 diff --git a/R/get_transformation.R b/R/get_transformation.R index c5fbe209f..3e9bf8f2b 100644 --- a/R/get_transformation.R +++ b/R/get_transformation.R @@ -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])) diff --git a/tests/testthat/test-get_transformation.R b/tests/testthat/test-get_transformation.R index 7472ba606..468f41f55 100644 --- a/tests/testthat/test-get_transformation.R +++ b/tests/testthat/test-get_transformation.R @@ -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) +})