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

make parameters() show fixed effects restricted to 0 #902

Merged
merged 16 commits into from
Sep 11, 2023

Conversation

strengejacke
Copy link
Member

Fixes #715

@codecov
Copy link

codecov bot commented Sep 11, 2023

Codecov Report

Merging #902 (3e16c50) into main (dd574ba) will decrease coverage by 0.25%.
The diff coverage is 12.16%.

❗ Current head 3e16c50 differs from pull request most recent head 22798aa. Consider uploading reports for the commit 22798aa to get more accurate results

@@            Coverage Diff             @@
##             main     #902      +/-   ##
==========================================
- Coverage   57.56%   57.31%   -0.25%     
==========================================
  Files         186      186              
  Lines       13785    13857      +72     
==========================================
+ Hits         7935     7942       +7     
- Misses       5850     5915      +65     
Files Changed Coverage Δ
R/display.R 0.00% <ø> (ø)
R/print_html.R 0.00% <0.00%> (ø)
R/print_md.R 0.00% <0.00%> (ø)
R/utils.R 45.76% <0.00%> (-25.29%) ⬇️
R/utils_format.R 68.94% <4.54%> (-4.97%) ⬇️
R/1_model_parameters.R 85.83% <100.00%> (+0.11%) ⬆️
R/format.R 71.27% <100.00%> (+0.12%) ⬆️
R/print.parameters_model.R 60.36% <100.00%> (+0.48%) ⬆️

@vincentarelbundock
Copy link
Contributor

ooooh, i'm excited about this!

@strengejacke

This comment was marked as outdated.

@vincentarelbundock
Copy link
Contributor

love it!

@strengejacke
Copy link
Member Author

Not sure how to print Species [setosa (ref.)], tough. Better Species [setosa] (ref.) or something completely else? hm...

@vincentarelbundock
Copy link
Contributor

Maybe the (ref.) is superfluous? The estimate is 0 and the std errors NULL, so it's quite evident what is happening, and removing "ref." would make things uniform in the term names.

If you think they're really necessary, I would put it outside the brackets to have consistent labels (and to make it easier for downstream packages like mine to remove "ref." if we don't want it using regexes.

Just a thought.

@strengejacke
Copy link
Member Author

I have no better model at hand, here's an example for glm:

data(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$am <- as.factor(mtcars$am)
m <- glm(vs ~ cyl + gear + am, mtcars, family = binomial())
parameters::parameters(m)
#> Parameter   | Log-Odds |       SE |                 95% CI |         z |     p
#> ------------------------------------------------------------------------------
#> (Intercept) |   151.52 | 71190.45 | [-16813.63,  52679.23] |  2.13e-03 | 0.998
#> cyl [6]     |   -43.16 | 23084.31 | [-36096.45, -21647.68] | -1.87e-03 | 0.999
#> cyl [8]     |  -109.42 | 41823.10 | [         ,   2786.97] | -2.62e-03 | 0.998
#> gear        |   -21.78 | 13269.07 | [         ,   2712.57] | -1.64e-03 | 0.999
#> am [1]      |   -42.63 | 25814.34 | [         ,   3345.63] | -1.65e-03 | 0.999
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.
#> 
#> The model has a log- or logit-link. Consider using `exponentiate =
#>   TRUE` to interpret coefficients as ratios.

print(parameters::parameters(m), add_reference = TRUE)
#> Parameter      | Log-Odds |       SE |                 95% CI |         z |     p
#> ---------------------------------------------------------------------------------
#> (Intercept)    |   151.52 | 71190.45 | [-16813.63,  52679.23] |  2.13e-03 | 0.998
#> cyl [4] (ref.) |     0.00 |          |                        |           |      
#> cyl [6]        |   -43.16 | 23084.31 | [-36096.45, -21647.68] | -1.87e-03 | 0.999
#> cyl [8]        |  -109.42 | 41823.10 | [         ,   2786.97] | -2.62e-03 | 0.998
#> gear           |   -21.78 | 13269.07 | [         ,   2712.57] | -1.64e-03 | 0.999
#> am [0] (ref.)  |     0.00 |          |                        |           |      
#> am [1]         |   -42.63 | 25814.34 | [         ,   3345.63] | -1.65e-03 | 0.999
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.

print(parameters::parameters(m, exponentiate = TRUE), add_reference = TRUE)
#> Parameter      | Odds Ratio |       SE |           95% CI |         z |     p
#> -----------------------------------------------------------------------------
#> (Intercept)    |   6.37e+65 | 4.54e+70 | [0.00,      Inf] |  2.13e-03 | 0.998
#> cyl [4] (ref.) |       1.00 |          |                  |           |      
#> cyl [6]        |   1.80e-19 | 4.14e-15 | [0.00,     0.00] | -1.87e-03 | 0.999
#> cyl [8]        |   3.01e-48 | 1.26e-43 | [    ,      Inf] | -2.62e-03 | 0.998
#> gear           |   3.48e-10 | 4.62e-06 | [    ,      Inf] | -1.64e-03 | 0.999
#> am [0] (ref.)  |       1.00 |          |                  |           |      
#> am [1]         |   3.06e-19 | 7.91e-15 | [    ,      Inf] | -1.65e-03 | 0.999
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.

Created on 2023-09-11 with reprex v2.0.2

@strengejacke
Copy link
Member Author

Maybe the (ref.) is superfluous?

Yes, it could be removed, indeed.

@strengejacke strengejacke marked this pull request as ready for review September 11, 2023 16:18
@strengejacke
Copy link
Member Author

strengejacke commented Sep 11, 2023

when dropping coefficients, or models with multiple components whould work now, too:

library(parameters)
data("fish")
m1 <- glmmTMB::glmmTMB(
  count ~ child + camper + zg + (1 | ID),
  ziformula = ~ child + camper + (1 | persons),
  data = fish,
  family = glmmTMB::truncated_poisson()
)
print(model_parameters(m1), add_reference = TRUE)
#> # Fixed Effects (Count Model)
#> 
#> Parameter         | Log-Mean |   SE |         95% CI |     z |      p
#> ---------------------------------------------------------------------
#> (Intercept)       |     1.41 | 0.18 | [ 1.06,  1.75] |  8.02 | < .001
#> child             |    -0.53 | 0.12 | [-0.77, -0.29] | -4.40 | < .001
#> camper [0] (ref.) |     0.00 |      |                |       |       
#> camper [1]        |     0.58 | 0.10 | [ 0.39,  0.78] |  5.93 | < .001
#> zg                |     0.13 | 0.04 | [ 0.05,  0.21] |  3.17 | 0.002 
#> 
#> # Fixed Effects (Zero-Inflation Component)
#> 
#> Parameter         | Log-Odds |   SE |         95% CI |     z |      p
#> ---------------------------------------------------------------------
#> (Intercept)       |    -0.39 | 0.65 | [-1.67,  0.89] | -0.60 | 0.551 
#> child             |     2.05 | 0.31 | [ 1.45,  2.66] |  6.63 | < .001
#> camper [0] (ref.) |     0.00 |      |                |       |       
#> camper [1]        |    -1.01 | 0.32 | [-1.64, -0.37] | -3.12 | 0.002 
#> 
#> # Random Effects Variances
#> 
#> Parameter          | Coefficient |       95% CI
#> -----------------------------------------------
#> SD (Intercept: ID) |        0.28 | [0.13, 0.60]
#> 
#> # Random Effects (Zero-Inflation Component)
#> 
#> Parameter               | Coefficient |       95% CI
#> ----------------------------------------------------
#> SD (Intercept: persons) |        1.17 | [0.54, 2.57]
#> 
#> Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
#>   using a Wald z-distribution approximation.
#> 
#> The model has a log- or logit-link. Consider using `exponentiate =
#>   TRUE` to interpret coefficients as ratios.

data(mtcars)
mtcars$gear <- as.factor(mtcars$gear)
m <- glm(vs ~ wt + gear, data = mtcars, family = "binomial")
print(model_parameters(m, exponentiate = TRUE, drop = "(Intercept)"), add_reference = TRUE)
#> Parameter       | Odds Ratio |   SE |        95% CI |     z |     p
#> -------------------------------------------------------------------
#> wt              |       0.07 | 0.09 | [0.00,  0.52] | -2.05 | 0.040
#> gear [3] (ref.) |       1.00 |      |               |       |      
#> gear [4]        |       3.21 | 3.98 | [0.27, 41.36] |  0.94 | 0.348
#> gear [5]        |       0.03 | 0.07 | [0.00,  1.47] | -1.41 | 0.159
#> 
#> Uncertainty intervals (profile-likelihood) and p-values (two-tailed)
#>   computed using a Wald z-distribution approximation.

Created on 2023-09-11 with reprex v2.0.2

@strengejacke
Copy link
Member Author

ok, I removed (ref.) from the output now.
I bet there are edge cases, and tests only seem to work interactively, so I'll merge and wouldn't promote that feature too much, but rather consider it as "experimental".

@strengejacke

This comment was marked as off-topic.

@strengejacke
Copy link
Member Author

What's not yet properly working could be if the reference level is changed, e.g. to the last level (see screenshot in #715)

@strengejacke strengejacke merged commit 3415c02 into main Sep 11, 2023
10 of 14 checks passed
@strengejacke strengejacke deleted the strengejacke/issue715 branch September 11, 2023 18:15
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.

make parameters() show fixed effects restricted to 0
2 participants