easystats / parameters

:bar_chart: Computation and processing of models' parameters
https://easystats.github.io/parameters/
GNU General Public License v3.0
435 stars 36 forks source link

`pretty_labels` attribute includes `NA`s with interactions #1032

Closed vincentarelbundock closed 5 hours ago

vincentarelbundock commented 8 hours ago

@strengejacke as discussed.

Pretty labels include NA with interactions:

set.seed(1024)
N <- 5000
X <- rbinom(N, 1, .5)
M <- sample(c("a", "b", "c"), N, replace = TRUE)
b <- runif(8, -1, 1)
Y <- rbinom(N, 1, prob = plogis(
b[1] + b[2] * X +
b[3] * (M == "b") + b[4] * (M == "b") + b[5] * (M == "c") +
b[6] * X * (M == "a") + b[7] * X + (M == "b") +
b[8] * X * (M == "c")
))
dat <- data.frame(Y, X, M)
mod <- glm(Y ~ X * M, data = dat, family = binomial)

p <- parameters::parameters(mod)
attr(p, "pretty_labels")
>   (Intercept)             X            Mb            Mc          X:Mb 
> "(Intercept)"           "X"       "M [b]"       "M [c]"      "X * NA" 
>          X:Mc 
>      "X * NA"
strengejacke commented 7 hours ago

Let me see where this information gets lost... at least format_parameters() works as intended:

set.seed(1024)
N <- 5000
X <- rbinom(N, 1, .5)
M <- sample(c("a", "b", "c"), N, replace = TRUE)
b <- runif(8, -1, 1)
Y <- rbinom(N, 1, prob = plogis(
b[1] + b[2] * X +
b[3] * (M == "b") + b[4] * (M == "b") + b[5] * (M == "c") +
b[6] * X * (M == "a") + b[7] * X + (M == "b") +
b[8] * X * (M == "c")
))
dat <- data.frame(Y, X, M)
mod <- glm(Y ~ X * M, data = dat, family = binomial)

parameters::format_parameters(mod)
#>   (Intercept)             X            Mb            Mc          X:Mb 
#> "(Intercept)"           "X"       "M [b]"       "M [c]"   "X × M [b]" 
#>          X:Mc 
#>   "X × M [c]"

Created on 2024-10-20 with reprex v2.1.1

strengejacke commented 7 hours ago

dat$M <- as.factor(dat$M) solves the problem. I think I just need to convert to factor inside .format_value_labels().

vincentarelbundock commented 5 hours ago

Awesome!

strengejacke commented 5 hours ago

Who said stringsAsFactors = TRUE was a bad default? 😜