You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/practical_context_effect2.Rmd
+16-12Lines changed: 16 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -63,7 +63,8 @@ data("qol_cancer")
63
63
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.
64
64
65
65
```{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")
67
68
```
68
69
69
70
Now we have:
@@ -85,7 +86,7 @@ mixed <- lmer(
85
86
)
86
87
# effects = "fixed" will not display random effects, but split the
87
88
# fixed effects into its between- and within-effects components.
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(
128
129
QoL ~ time * (phq4_within + phq4_between) + education + (1 + time | ID),
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
159
160
160
161
```{r}
161
162
# 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")
164
164
# 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")
166
166
```
167
167
168
168
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(
175
175
c("phq4_within", "phq4_between"),
176
176
by = "time"
177
177
) |>
178
-
display()
178
+
display(format = "tt")
179
179
```
180
180
181
181
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
193
193
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.
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
205
209
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.
0 commit comments