parameters icon indicating copy to clipboard operation
parameters copied to clipboard

Character variable in model makes every variable unlabeled

Open raffaem opened this issue 5 months ago • 7 comments

mydf <- mtcars
mydf %<>% mutate(across(gear, as.character))
mydf %<>% labelled::set_variable_labels(
  .strict = FALSE,
  mpg = "Miles per gallon",
  hp = "Horsepower",
  gear = "Number of forward gears"
)

# Works: Shows "Horsepower" as label
mod <- lm(
  mpg ~ hp,
  data = mydf
)
mp <- parameters::model_parameters(mod)
print(mp, pretty_names = "labels")

# Doesn't work: Shows "hp" as name
mod <- lm(
  mpg ~ hp + gear,
  data = mydf
)
mp <- parameters::model_parameters(mod)
print(mp, pretty_names = "labels")

# Doesn't work: Shows "hp" as name
mod <- lm(
  mpg ~ hp + factor(gear),
  data = mydf
)
mp <- parameters::model_parameters(mod)
print(mp, pretty_names = "labels")

# Works: Shows "Horsepower" as label
mydf %<>% mutate(across(gear, as.factor))
mod <- lm(
  mpg ~ hp + gear,
  data = mydf
)
mp <- parameters::model_parameters(mod)
print(mp, pretty_names = "labels")

raffaem avatar Jul 20 '25 11:07 raffaem

For me, the first example doesn't work?

mydf <- as.data.frame(lapply(mtcars, as.character))
mydf <- labelled::set_variable_labels(mydf,
  .strict = FALSE,
  mpg = "Miles per gallon",
  hp = "Horsepower",
  gear = "Number of forward gears"
)

# Works: Shows "Horsepower" as label
mod <- lm(
  mpg ~ hp,
  data = mydf
)
mp <- parameters::model_parameters(mod)
print(mp, pretty_names = "labels")
#> Parameter   | Coefficient |   SE |          95% CI | t(10) |      p
#> -------------------------------------------------------------------
#> (Intercept) |       18.10 | 1.32 | [ 15.16, 21.04] | 13.74 | < .001
#> hp [109]    |        3.30 | 1.86 | [ -0.85,  7.45] |  1.77 | 0.107 
#> hp [110]    |        3.03 | 1.52 | [ -0.36,  6.42] |  1.99 | 0.074 
#> hp [113]    |       12.30 | 1.86 | [  8.15, 16.45] |  6.60 | < .001
#> hp [123]    |        0.40 | 1.61 | [ -3.20,  4.00] |  0.25 | 0.809 
#> hp [150]    |       -2.75 | 1.61 | [ -6.35,  0.85] | -1.70 | 0.119 
#> hp [175]    |        1.10 | 1.52 | [ -2.29,  4.49] |  0.72 | 0.486 
#> hp [180]    |       -1.80 | 1.52 | [ -5.19,  1.59] | -1.18 | 0.264 
#> hp [205]    |       -7.70 | 1.86 | [-11.85, -3.55] | -4.13 | 0.002 
#> hp [215]    |       -7.70 | 1.86 | [-11.85, -3.55] | -4.13 | 0.002 
#> hp [230]    |       -3.40 | 1.86 | [ -7.55,  0.75] | -1.82 | 0.098 
#> hp [245]    |       -4.30 | 1.61 | [ -7.90, -0.70] | -2.66 | 0.024 
#> hp [264]    |       -2.30 | 1.86 | [ -6.45,  1.85] | -1.23 | 0.245 
#> hp [335]    |       -3.10 | 1.86 | [ -7.25,  1.05] | -1.66 | 0.127 
#> hp [52]     |       12.30 | 1.86 | [  8.15, 16.45] |  6.60 | < .001
#> hp [62]     |        6.30 | 1.86 | [  2.15, 10.45] |  3.38 | 0.007 
#> hp [65]     |       15.80 | 1.86 | [ 11.65, 19.95] |  8.48 | < .001
#> hp [66]     |       11.75 | 1.61 | [  8.15, 15.35] |  7.28 | < .001
#> hp [91]     |        7.90 | 1.86 | [  3.75, 12.05] |  4.24 | 0.002 
#> hp [93]     |        4.70 | 1.86 | [  0.55,  8.85] |  2.52 | 0.030 
#> hp [95]     |        4.70 | 1.86 | [  0.55,  8.85] |  2.52 | 0.030 
#> hp [97]     |        3.40 | 1.86 | [ -0.75,  7.55] |  1.82 | 0.098
#> 
#> Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
#>   using a Wald t-distribution approximation.

Created on 2025-08-03 with reprex v2.1.1

strengejacke avatar Aug 03 '25 15:08 strengejacke

For me, the first example doesn't work?

  1. That's not an example of mine?
  2. It confirms the bug, since you are converting every variable to a string, no?

Am I missing something?

raffaem avatar Aug 03 '25 17:08 raffaem

Yes, it confirms the bug, I was just confused that your comment was "works" for this example

strengejacke avatar Aug 03 '25 17:08 strengejacke

Yes, it confirms the bug, I was just confused that your comment was "works" for this example

But this is not an example of mine!

For me, the first example doesn't work?

In my first example I didn't have any character variable in the model, as I only converted gear to character.

mydf %<>% mutate(across(gear, as.character))

Instead you have converted everything to character!

mydf <- as.data.frame(lapply(mtcars, as.character))

raffaem avatar Aug 04 '25 04:08 raffaem

@strengejacke were you able to make any progress on this?

raffaem avatar Sep 08 '25 07:09 raffaem

No, not yet. I will see if I can find a reprex using base / core R packages, so it's easier to understand what is happening and what is failing.

strengejacke avatar Sep 08 '25 09:09 strengejacke

No, not yet. I will see if I can find a reprex using base / core R packages, so it's easier to understand what is happening and what is failing.

Thank you very much!

raffaem avatar Sep 08 '25 09:09 raffaem