Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix contrast-formatting for numerics #411

Merged
merged 5 commits into from
Feb 20, 2025
Merged

Fix contrast-formatting for numerics #411

merged 5 commits into from
Feb 20, 2025

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented Feb 20, 2025

We couldn't format contrasts that included numerics like we did for factors only, namely two Level columns. This PR fixes this issue and allows proper formatting for numerics as well.

library(lme4)
set.seed(123)

dat <- data.frame(
  outcome = rbinom(n = 100, size = 1, prob = 0.35),
  var_binom = as.factor(rbinom(n = 100, size = 1, prob = 0.2)),
  var_cont = rnorm(n = 100, mean = 10, sd = 7),
  grp = sample(letters[1:4], size = 100, replace = TRUE)
)

dat$var_cont <- datawizard::standardize(dat$var_cont)

m1 <- glmer(
  outcome ~ var_binom + var_cont + (1 | grp),
  data = dat,
  family = binomial(link = "logit")
)

# range of values
modelbased::estimate_contrasts(
  m1,
  c("var_binom", "var_cont"),
  predict = "link",
  transform = exp,
  length = 3
)

## OLD FORMATTING

#> Marginal Contrasts Analysis
#> 
#> Parameter           | Difference |        95% CI |     p
#> --------------------------------------------------------
#> 0 -0.187 - 0 -2.012 |       1.11 | [0.52,  2.39] | 0.791
#> 0  1.638 - 0 -2.012 |       1.23 | [0.27,  5.69] | 0.791
#> 0  3.463 - 0 -2.012 |       1.36 | [0.14, 13.58] | 0.791
#> 1 -2.012 - 0 -2.012 |       1.04 | [0.36,  3.02] | 0.940
#> 1 -0.187 - 0 -2.012 |       1.16 | [0.30,  4.50] | 0.836
#> 1  1.638 - 0 -2.012 |       1.28 | [0.19,  8.87] | 0.802
#> ...

## NEW FORMATTInG

#> Marginal Contrasts Analysis
#> 
#> Level1    | Level2    | Difference |        95% CI |     p
#> ----------------------------------------------------------
#> 0, 0.725  | 0, -2.012 |       1.17 | [0.37,  3.68] | 0.791
#> 0, 3.463  | 0, -2.012 |       1.36 | [0.14, 13.58] | 0.791
#> 1, -2.012 | 0, -2.012 |       1.04 | [0.36,  3.02] | 0.940
#> 1, 0.725  | 0, -2.012 |       1.22 | [0.24,  6.20] | 0.814
#> 1, 3.463  | 0, -2.012 |       1.42 | [0.10, 19.30] | 0.792
#> 0, 3.463  | 0, 0.725  |       1.17 | [0.37,  3.69] | 0.791
#> 1, -2.012 | 0, 0.725  |       0.89 | [0.20,  4.01] | 0.882
#> 1, 0.725  | 0, 0.725  |       1.04 | [0.36,  3.02] | 0.940
#> ...

Created on 2025-02-20 with reprex v2.1.1

@strengejacke strengejacke merged commit ce962e4 into main Feb 20, 2025
21 of 23 checks passed
@strengejacke strengejacke deleted the fix_num_levels branch February 20, 2025 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant