Skip to content

Commit c6529a8

Browse files
committed
tt as format
1 parent 6d7712b commit c6529a8

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

vignettes/practical_context_effect2.Rmd

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ data("qol_cancer")
6363
To calculate the within- and between-effects, we perform a special way of centering variables called [*demeaning*](https://easystats.github.io/datawizard/reference/demean.html). This "separates" the within-effect from a between-effect of a predictor.
6464

6565
```{r}
66-
qol_cancer <- datawizard::demean(qol_cancer, select = c("phq4", "QoL"), by = "ID")
66+
library(datawizard)
67+
qol_cancer <- demean(qol_cancer, select = c("phq4", "QoL"), by = "ID")
6768
```
6869

6970
Now we have:
@@ -85,7 +86,7 @@ mixed <- lmer(
8586
)
8687
# effects = "fixed" will not display random effects, but split the
8788
# fixed effects into its between- and within-effects components.
88-
model_parameters(mixed, effects = "fixed") |> display()
89+
model_parameters(mixed, effects = "fixed") |> display(format = "tt")
8990
```
9091

9192
Looking at the fixed effects output for the `phq4` (Patient Health Questionnaire) variable, we can interpret the coefficients as follows:
@@ -110,7 +111,7 @@ To test whether the within- and between-effects are significantly different from
110111

111112
```{r}
112113
library(modelbased)
113-
estimate_contrasts(mixed, c("phq4_within", "phq4_between")) |> display()
114+
estimate_contrasts(mixed, c("phq4_within", "phq4_between")) |> display(format = "tt")
114115
```
115116

116117
The output shows a significant contrast of 2.41 between the within- and between-effects. Since the between-effect in our model (-6.13) is stronger (more negative) than the within-effect (-3.72), the context-effect (Between minus Within) is -2.41.
@@ -128,7 +129,7 @@ mixed <- lmer(
128129
QoL ~ time * (phq4_within + phq4_between) + education + (1 + time | ID),
129130
data = qol_cancer
130131
)
131-
model_parameters(mixed, effects = "fixed") |> display()
132+
model_parameters(mixed, effects = "fixed") |> display(format = "tt")
132133
```
133134

134135
The results table now shows us whether time has a moderating influence on our effects. To see this, we look at the two interaction terms at the bottom of the table:
@@ -159,10 +160,9 @@ Before calculating the context effect directly, it is helpful to first look at t
159160

160161
```{r}
161162
# average marginal effect of within-effect at each time point
162-
estimate_slopes(mixed, "phq4_within", by = "time") |> display()
163-
163+
estimate_slopes(mixed, "phq4_within", by = "time") |> display(format = "tt")
164164
# average marginal effect of between-effect at each time point
165-
estimate_slopes(mixed, "phq4_between", by = "time") |> display()
165+
estimate_slopes(mixed, "phq4_between", by = "time") |> display(format = "tt")
166166
```
167167

168168
Looking at these separate slopes reveals two opposing trends. The acute impact of a temporary symptom spike (`phq4_within`) slightly decreases in magnitude over time (shifting from -4.04 at Time 1 to -3.39 at Time 3). Conversely, the detrimental impact of a chronically high baseline burden (`phq4_between`) becomes progressively more severe, worsening from -5.36 to -6.68.
@@ -175,7 +175,7 @@ estimate_contrasts(
175175
c("phq4_within", "phq4_between"),
176176
by = "time"
177177
) |>
178-
display()
178+
display(format = "tt")
179179
```
180180

181181
The contrast analysis reveals a clear and interesting trajectory: the context effect grows substantially stronger as time progresses.
@@ -193,7 +193,11 @@ Over time, having a chronically high baseline of psychological symptoms (the tra
193193
While the previous table showed the context effect at each specific time point, we also need to formally test whether the change in this effect over time is statistically significant. We can do this by computing pairwise comparisons of the context effect across the different time points.
194194

195195
```{r}
196-
estimate_contrasts(mixed, c("phq4_within", "phq4_between", "time")) |> display()
196+
estimate_contrasts(
197+
mixed,
198+
c("phq4_within", "phq4_between", "time")
199+
) |>
200+
display(format = "tt")
197201
```
198202

199203
This pairwise comparison table adds a crucial statistical caveat to our visual and descriptive observations. The Difference column here represents the mathematical change in the size of the context effect between two specific time points (e.g., the context effect grew by 0.99 points from Time 1 to Time 2).
@@ -205,7 +209,7 @@ This uniformity is a direct mathematical consequence of our model assuming a *st
205209
Because a linear model yields constant step-by-step changes, testing these identical individual intervals is often less informative. To test whether the context effect meaningfully changes over time overall, it is more appropriate to evaluate the average contrast of the slopes across the entire study period. To do this, we calculate the contrast between the within- and between-effects without stratifying by time.
206210

207211
```{r}
208-
estimate_contrasts(mixed, c("phq4_within", "phq4_between")) |> display()
212+
estimate_contrasts(mixed, c("phq4_within", "phq4_between")) |> display(format = "tt")
209213
```
210214

211215
**What does this mean practically?**
@@ -230,7 +234,7 @@ estimate_contrasts(
230234
c("phq4_within", "phq4_between"),
231235
by = "education"
232236
) |>
233-
display()
237+
display(format = "tt")
234238
```
235239

236240
The marginal contrasts analysis yields nuanced results that add an important layer to our understanding of the context effect:
@@ -258,7 +262,7 @@ estimate_contrasts(
258262
mixed,
259263
c("phq4_within", "phq4_between", "education")
260264
) |>
261-
display()
265+
display(format = "tt")
262266
```
263267

264268
The output now displays the mathematical difference in the size of the context effect between two specific groups.

0 commit comments

Comments
 (0)