easystats / insight

:crystal_ball: Easy access to model information for various model objects
https://easystats.github.io/insight/
GNU General Public License v3.0
404 stars 39 forks source link

Partial fail in new Box-Cox-handling in insight 0.99.0.15 #970

Closed JoakimStefansson closed 1 week ago

JoakimStefansson commented 1 week ago

First of all, thank you for the great work with these packages, and for implementing changes so quickly. I may well be too early, testing functions that aren't official yet. But anyway, this is how insight is working right now.

library(easystats)
packageVersion("insight") # ‘0.99.0.15’
packageVersion("performance") # ‘0.12.4.5’
packageVersion("easystats") # ‘0.7.3.2’

a <- aov(formula = (mpg ^ 1.3 - 1) / 1.3 ~ hp, data = mtcars)
insight::find_transformation(a) # "box-cox" - correct
insight::get_transformation(a) # also has a correct output

a <- aov(formula = (mpg ^ -1.3 - 1) / -1.3 ~ hp, data = mtcars)
insight::find_transformation(a) # "power" - not correct
debugonce(insight::get_transformation)
insight::get_transformation(a)
# Error in !is.null(trans_power) && trans_power != 0 : 
#   'length = 2' in coercion to 'logical(1)'
# In addition: Warning message:
#   In doTryCatch(return(expr), name, parentenv, handler) :
#   NAs introduced by coercion

The error in get:transformation() appears to be caused by the misdirection from find_transformation(). When running .extract_power_transformation(model) instead of .extract_scale_denominator(model) on the formula, we get c(NA, 1.3) instead of the single value that the next line wants to evaluate.

But there may be more to this bug. While in debugging mode inside .get_transformation(), I evaluated .extract_scale_denominator(model), and it becomes 1.3 but I think it ought to be -1.3. Something happens in the lapply call between line 21 and 31 of insight:::.get_variables_list(), when f$response goes from "(mpg^-1.3 - 1)/-1.3" to "(mpg^-1.3 - 1)" "1.3".