Skip to content

Commit 3babe17

Browse files
authored
include_reference not working in model_parameters with glmmTMB object (#1093)
* include_reference not working in model_parameters with glmmTMB object Fixes #1092 * revert * fix * fix * news * update * lintr
1 parent b6d4a76 commit 3babe17

File tree

6 files changed

+123
-9
lines changed

6 files changed

+123
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: parameters
33
Title: Processing of Model Parameters
4-
Version: 0.24.2.20
4+
Version: 0.24.2.21
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* Fixed printing issue with `model_parameters()` for `htest` objects when
1919
printing into markdown or HTML format.
2020

21+
* Fixed printing issue with `model_parameters()` for mixed models when
22+
`include_reference = TRUE`.
23+
2124
# parameters 0.24.2
2225

2326
## Changes

R/utils.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@
116116
# match column order
117117
row <- row[match(colnames(data), colnames(row))]
118118

119+
# modify effects and component column - required for printing
120+
if ("Effects" %in% colnames(row)) {
121+
row$Effects <- data$Effects[1]
122+
}
123+
if ("Component" %in% colnames(row)) {
124+
row$Component <- data$Component[1]
125+
}
126+
119127
# insert row
120128
if (index == 1) {
121129
rbind(row, data)

R/utils_format.R

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,24 @@
480480
out <- .insert_row_at(out, row_data, min(found))
481481
}
482482
}
483-
# update pretty_names attribute
484-
attr(out, "pretty_names") <- pretty_names
485-
# update pretty_labels attribute
486-
pretty_names[match(names(attr(out, "pretty_labels")), names(pretty_names))] <- attr(out, "pretty_labels")
487-
attr(out, "pretty_labels") <- pretty_names
483+
484+
if (length(pretty_names)) {
485+
# update pretty_names attribute
486+
attr(out, "pretty_names") <- pretty_names
487+
# update pretty_labels attribute - for mixed models, we need to add the random
488+
# effects stuff from pretty_labels to pretty_names first, else, matching will
489+
# fail
490+
pretty_labels <- attributes(out)$pretty_labels
491+
if (!is.null(pretty_labels)) {
492+
re_labels <- startsWith(names(pretty_labels), "SD (") | startsWith(names(pretty_labels), "Cor (")
493+
if (any(re_labels)) {
494+
pretty_names <- c(pretty_names, pretty_labels[re_labels])
495+
}
496+
pretty_names[stats::na.omit(match(names(pretty_labels), names(pretty_names)))] <- pretty_labels
497+
pretty_names <- pretty_names[!re_labels]
498+
}
499+
attr(out, "pretty_labels") <- pretty_names
500+
}
488501

489502
out
490503
}
@@ -703,9 +716,9 @@
703716
}
704717

705718
# clean some special parameter names
706-
component_name <- gsub("zi", "Zero-Inflation", component_name)
707-
component_name <- gsub("zoi", "Zero-One-Inflation", component_name)
708-
component_name <- gsub("coi", "Conditional-One-Inflation", component_name)
719+
component_name <- gsub("zi", "Zero-Inflation", component_name, fixed = TRUE)
720+
component_name <- gsub("zoi", "Zero-One-Inflation", component_name, fixed = TRUE)
721+
component_name <- gsub("coi", "Conditional-One-Inflation", component_name, fixed = TRUE)
709722

710723
# if we show ZI component only, make sure this appears in header
711724
if (!grepl("(Zero-Inflation Component)", component_name, fixed = TRUE) &&

tests/testthat/_snaps/include_reference.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,72 @@
155155
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
156156
using a Wald t-distribution approximation.
157157

158+
# include_reference, random effects models
159+
160+
Code
161+
print(out)
162+
Output
163+
# Fixed Effects
164+
165+
Parameter | Odds Ratio | SE | 95% CI | z | p
166+
--------------------------------------------------------------------------
167+
(Intercept) | 1.81e-06 | 4.52e-06 | [0.00, 0.00] | -5.29 | < .001
168+
flipper len | 1.07 | 0.01 | [1.04, 1.09] | 5.33 | < .001
169+
island [Biscoe] | 1.00 | | | |
170+
island [Dream] | 2.84 | 0.93 | [1.49, 5.41] | 3.18 | 0.001
171+
island [Torgersen] | 2.97 | 1.22 | [1.32, 6.65] | 2.64 | 0.008
172+
173+
# Random Effects
174+
175+
Parameter | Coefficient | 95% CI
176+
------------------------------------------------
177+
SD (Intercept: year) | 3.05e-05 | [0.00, Inf]
178+
Message
179+
180+
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
181+
using a Wald z-distribution approximation.
182+
183+
---
184+
185+
Code
186+
print(out, include_reference = TRUE)
187+
Output
188+
# Fixed Effects
189+
190+
Parameter | Odds Ratio | SE | 95% CI | z | p
191+
--------------------------------------------------------------------------
192+
(Intercept) | 1.81e-06 | 4.52e-06 | [0.00, 0.00] | -5.29 | < .001
193+
flipper len | 1.07 | 0.01 | [1.04, 1.09] | 5.33 | < .001
194+
island [Biscoe] | 1.00 | | | |
195+
island [Dream] | 2.84 | 0.93 | [1.49, 5.41] | 3.18 | 0.001
196+
island [Torgersen] | 2.97 | 1.22 | [1.32, 6.65] | 2.64 | 0.008
197+
198+
# Random Effects
199+
200+
Parameter | Coefficient | 95% CI
201+
------------------------------------------------
202+
SD (Intercept: year) | 3.05e-05 | [0.00, Inf]
203+
Message
204+
205+
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
206+
using a Wald z-distribution approximation.
207+
208+
---
209+
210+
Code
211+
print(out)
212+
Output
213+
# Fixed Effects
214+
215+
Parameter | Odds Ratio | SE | 95% CI | z | p
216+
--------------------------------------------------------------------------
217+
(Intercept) | 1.81e-06 | 4.52e-06 | [0.00, 0.00] | -5.29 | < .001
218+
flipper len | 1.07 | 0.01 | [1.04, 1.09] | 5.33 | < .001
219+
island [Biscoe] | 1.00 | | | |
220+
island [Dream] | 2.84 | 0.93 | [1.49, 5.41] | 3.18 | 0.001
221+
island [Torgersen] | 2.97 | 1.22 | [1.32, 6.65] | 2.64 | 0.008
222+
Message
223+
224+
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
225+
using a Wald z-distribution approximation.
226+

tests/testthat/test-include_reference.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,24 @@ test_that("include_reference, different contrasts", {
103103
out <- model_parameters(m, include_reference = TRUE)
104104
expect_snapshot(print(out))
105105
})
106+
107+
108+
test_that("include_reference, random effects models", {
109+
skip_if_not_installed("glmmTMB")
110+
skip_if(getRversion() < "4.5")
111+
data(penguins, package = "datasets")
112+
fit_penguins <- glmmTMB::glmmTMB(
113+
sex ~ flipper_len + island + (1 | year),
114+
family = binomial(),
115+
data = penguins
116+
)
117+
118+
out <- model_parameters(fit_penguins, exponentiate = TRUE, include_reference = TRUE)
119+
expect_snapshot(print(out))
120+
121+
out <- model_parameters(fit_penguins, exponentiate = TRUE)
122+
expect_snapshot(print(out, include_reference = TRUE))
123+
124+
out <- model_parameters(fit_penguins, effects = "fixed", exponentiate = TRUE, include_reference = TRUE)
125+
expect_snapshot(print(out))
126+
})

0 commit comments

Comments
 (0)