Skip to content

Commit d1e2ab3

Browse files
authored
Add vignette on custom contrasts and joint tests (#502)
* Add vignette on custom contrasts and joint tests * draft * finalize draft * fiix * typos * styler * fix * fix * add test * fix * fix * news, desc
1 parent fa86c20 commit d1e2ab3

14 files changed

+522
-304
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: modelbased
33
Title: Estimation of Model-Based Predictions, Contrasts and Means
4-
Version: 0.11.0.4
4+
Version: 0.11.0.5
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* The `comparison` argument can now also be `"joint"`, to jointly test
99
hypotheses (i.e. conducting a joint test) in factorial designs.
1010

11+
* New vignette about user-defined contrasts and joint tests in
12+
`estimate_contrasts()`.
13+
1114
# modelbased 0.11.0
1215

1316
## New functions

R/estimate_grouplevel.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ estimate_grouplevel.stanreg <- function(model,
223223
if (!is.null(clean_parameters)) {
224224
# fix for rstanarm, which contains a sigma columns
225225
clean_parameters <- clean_parameters[
226-
clean_parameters$Component != "sigma" & !startsWith(clean_parameters$Parameter, "Sigma["), , drop = FALSE # nolint
226+
clean_parameters$Component != "sigma" & !startsWith(clean_parameters$Parameter, "Sigma["), ,
227+
drop = FALSE # nolint
227228
]
228229
clean_parameters <- clean_parameters[
229-
clean_parameters$Parameter %in% params$Parameter, , drop = FALSE
230+
clean_parameters$Parameter %in% params$Parameter, ,
231+
drop = FALSE
230232
]
231233

232234
params$Parameter <- insight::trim_ws(sub(":.*", "", clean_parameters$Group))

R/format.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ format.estimate_contrasts <- function(x,
66
select = getOption("modelbased_select", NULL),
77
include_grid = getOption("modelbased_include_grid", FALSE),
88
...) {
9+
# for joint test, no select and include_grid options
10+
if (isTRUE(attributes(x)$joint_test)) {
11+
select <- NULL
12+
include_grid <- FALSE
13+
}
914
# don't print columns of adjusted_for variables
1015
adjusted_for <- attr(x, "adjusted_for", exact = TRUE)
1116
if (!is.null(adjusted_for) && all(adjusted_for %in% colnames(x)) && !isTRUE(include_grid)) {

pkgdown/_pkgdown.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ navbar:
8484
- text: "Contrasts"
8585
- text: "Contrasts and comparisons"
8686
href: articles/introduction_comparisons_1.html
87-
- text: "Slopes, floodlight and spotlight analysis (Johnson-Neyman intervals)"
87+
- text: "User Defined Contrasts and Joint Tests"
8888
href: articles/introduction_comparisons_2.html
89-
- text: "Contrasts and comparisons for generalized linear models"
89+
- text: "Slopes, floodlight and spotlight analysis (Johnson-Neyman intervals)"
9090
href: articles/introduction_comparisons_3.html
91-
- text: "Contrasts and comparisons for zero-inflation models"
91+
- text: "Contrasts and comparisons for generalized linear models"
9292
href: articles/introduction_comparisons_4.html
93+
- text: "Contrasts and comparisons for zero-inflation models"
94+
href: articles/introduction_comparisons_5.html
9395
- icon: fa fa-newspaper
9496
text: News
9597
href: news/index.html
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# estimate_contrasts - joint test, works with select printing
2+
3+
Code
4+
estimate_contrasts(m, contrast = "time", by = "coffee", comparison = "joint")
5+
Output
6+
Marginal Joint Test
7+
8+
Contrast | coffee | df1 | df2 | F | p
9+
---------------------------------------------
10+
time | coffee | 2 | 114 | 1.76 | 0.176
11+
time | control | 2 | 114 | 5.29 | 0.006
12+
13+
p-values are uncorrected.
14+

tests/testthat/test-joint_test.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,23 @@ test_that("estimate_contrasts - joint test, 3-way", {
137137
})
138138

139139

140-
test_that("estimate_contrasts - joint test, 3-way", {
140+
test_that("estimate_contrasts - joint test, 3-way, error", {
141141
data(coffee_data, package = "modelbased")
142142
m <- lm(alertness ~ time * coffee * sex, data = coffee_data)
143143
expect_error(
144144
estimate_contrasts(m, contrast = "time", comparison = "joint"),
145145
regex = "Joint tests using"
146146
)
147147
})
148+
149+
150+
skip_if_not_installed("withr")
151+
152+
withr::with_options(
153+
list(modelbased_select = "minimal"),
154+
test_that("estimate_contrasts - joint test, works with select printing", {
155+
data(coffee_data, package = "modelbased")
156+
m <- lm(alertness ~ time * coffee, data = coffee_data)
157+
expect_snapshot(estimate_contrasts(m, contrast = "time", by = "coffee", comparison = "joint"))
158+
})
159+
)

vignettes/estimate_slopes.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ plot(slopes)
101101

102102
Interactions between two continuous variables are often not straightforward to visualize and interpret. Thanks to the model-based approach, one can represent the effect of one of the variables as a function of the other variable.
103103

104-
Such plot, also referred to as **Johnson-Neyman intervals**, shows how the effect (the "slope") of one variable varies depending on another variable. It is useful in the case of complex interactions between continuous variables. See also [this vignette](https://easystats.github.io/modelbased/articles/introduction_comparisons_2.html) for further details.
104+
Such plot, also referred to as **Johnson-Neyman intervals**, shows how the effect (the "slope") of one variable varies depending on another variable. It is useful in the case of complex interactions between continuous variables. See also [this vignette](https://easystats.github.io/modelbased/articles/introduction_comparisons_3.html) for further details.
105105

106106
For instance, the plot below shows that the effect of `hp` (the y-axis) is significantly negative only when `wt` is low (`< ~4`).
107107

vignettes/introduction_comparisons_1.Rmd

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ if (!all(insight::check_if_installed(pkgs, quietly = TRUE))) {
3333
```
3434

3535

36-
This vignette is the first in a 4-part series:
36+
This vignette is the first in a 5-part series:
3737

3838
1. **Contrasts and Pairwise Comparisons**
3939

40-
2. [**Comparisons of Slopes, Floodlight and Spotlight Analysis (Johnson-Neyman Intervals)**](https://easystats.github.io/modelbased/articles/introduction_comparisons_2.html)
40+
2. [**User Defined Contrasts and Joint Tests**](https://easystats.github.io/modelbased/articles/introduction_comparisons_2.html)
4141

42-
3. [**Contrasts and Comparisons for Generalized Linear Models**](https://easystats.github.io/modelbased/articles/introduction_comparisons_3.html)
42+
3. [**Comparisons of Slopes, Floodlight and Spotlight Analysis (Johnson-Neyman Intervals)**](https://easystats.github.io/modelbased/articles/introduction_comparisons_3.html)
4343

44-
4. [**Contrasts and Comparisons for Zero-Inflation Models**](https://easystats.github.io/modelbased/articles/introduction_comparisons_4.html)
44+
4. [**Contrasts and Comparisons for Generalized Linear Models**](https://easystats.github.io/modelbased/articles/introduction_comparisons_4.html)
45+
46+
5. [**Contrasts and Comparisons for Zero-Inflation Models**](https://easystats.github.io/modelbased/articles/introduction_comparisons_5.html)
4547

4648

4749
# Hypothesis testing for categorical predictors
@@ -316,4 +318,4 @@ Let's replicate this step-by-step:
316318

317319
While the current implementation in `estimate_contrasts()` already covers many common use cases for testing contrasts and pairwise comparison, there still might be the need for more sophisticated comparisons. In this case, we recommend using the [*marginaleffects*](https://marginaleffects.com/) package directly. Some further related recommended readings are the vignettes about [Comparisons](https://marginaleffects.com/chapters/comparisons.html) or [Hypothesis Tests](https://marginaleffects.com/chapters/hypothesis.html).
318320

319-
[Go to next vignette: **Comparisons of Slopes, Floodlight and Spotlight Analysis (Johnson-Neyman Intervals)**](https://easystats.github.io/modelbased/articles/introduction_comparisons_2.html)
321+
[Go to next vignette: **User Defined Contrasts and Joint Tests**](https://easystats.github.io/modelbased/articles/introduction_comparisons_2.html)

0 commit comments

Comments
 (0)