简体   繁体   中英

How to reconcile afex mixed-effects model output with sjPlot visualisation

I've fit a mixed effects model with the afex::mixed in R (normally I'd used lme4::lmer but I've read that the || notation doesn't work properly for categorical variables in that package, see here ), like so:

>str(DF)
'data.frame':   1521 obs. of  3 variables:
 $ p: Factor w/ 100 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 2 2 ...
 $ a: Factor w/ 2 levels "Down","Up": 2 2 2 2 2 2 2 2 2 2 ...
 $ y: num  12 0 13 0 0 10 5 0 0 5 ...
>
>
> # fit mixed effects model with afex::mixed
> m1 <- mixed(y ~ a + (a||p), # random slopes and intercepts by participant, no correlation parameters
+             data = DF,
+             expand_re = TRUE, 
+             method = "S",
+             return = "merMod")
Contrasts set to contr.sum for the following variables: a, p

The main thing I'm interested in is the fixed effect of a on y , so I inspect like so:

> fixef(m1)
(Intercept)          a1 
   6.837455    4.608073 

I (maybe incorrectly?) interpret this to mean the model predicts that when a == "down" , y will be 4.61 greater than when a == "up" .

I then visualise the model with sjPlot::plot_model like so:

plot_model(m1, type = "pred", terms = "a")

在此处输入图像描述

Why does this plot seem to show a greater fixed effect than the statistical output of the model? Shouldn't the difference between the two points be 4.61? If not, what is the plot showing?


I can retrieve the exact data being visualised using sjPlot::get_model_data :

> get_model_data(m1, terms = "a", type = "pred")

# Predicted values of y
# x = a

x | Predicted |   SE | group_col |         95% CI
-------------------------------------------------
1 |     11.45 | 0.40 |         1 | [10.65, 12.24]
2 |      2.23 | 0.55 |         1 | [ 1.15,  3.31]

Adjusted for:
* re1.a1 = -0.05
*      p = 0 (population-level)

However I am still confused by how this relates to the model output, specifically the fixed effect of a .


I can also recreate the sjPlot visualisation using afex::afex_plot :

> afex_plot(m1, x = "a", mapping = c("color"))
Aggregating data over: p 

在此处输入图像描述

What's most interesting here is the message Aggregating data over: p . Is this to say that the random effect of p is not taken into account? If so, how is this different from a plot that doesn't reference the model at all? For instance, the following seems to plot the same values despite simply being an y ~ x line...

ggplot(DF, aes(x = a, y = y, color = a))+
  geom_smooth(aes(group = 1), method = "lm", se = T, color = "black")

在此处输入图像描述

This is caused by the variable getting sum contrasts from Afex, which is an important improvement over the standard dummy coding by lme4.

In essence what you say is right, you probably interpreted the model coefficient incorrectly, as sum contrast coding could set your variable level 'Down' to -1, while setting 'Up' to 1. Simple arithmetic shows that this is probably right: 6.83 - 4.61 ~ 2.23; 6.83 + 4.61 ~ 11.45.

You could try to see how the factor levels are coded in the model to resolve this issue. I am unable to do this myself because you haven't provided a reproduceable example.

For explanation on sum contrasts: https://rpubs.com/monajhzhu/608609

I've fit a mixed effects model with the afex::mixed in R (normally I'd used lme4::lmer but I've read that the || notation doesn't work properly for categorical variables in that package, see here ), like so:

>str(DF)
'data.frame':   1521 obs. of  3 variables:
 $ p: Factor w/ 100 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 2 2 ...
 $ a: Factor w/ 2 levels "Down","Up": 2 2 2 2 2 2 2 2 2 2 ...
 $ y: num  12 0 13 0 0 10 5 0 0 5 ...
>
>
> # fit mixed effects model with afex::mixed
> m1 <- mixed(y ~ a + (a||p), # random slopes and intercepts by participant, no correlation parameters
+             data = DF,
+             expand_re = TRUE, 
+             method = "S",
+             return = "merMod")
Contrasts set to contr.sum for the following variables: a, p

The main thing I'm interested in is the fixed effect of a on y , so I inspect like so:

> fixef(m1)
(Intercept)          a1 
   6.837455    4.608073 

I (maybe incorrectly?) interpret this to mean the model predicts that when a == "down" , y will be 4.61 greater than when a == "up" .

I then visualise the model with sjPlot::plot_model like so:

plot_model(m1, type = "pred", terms = "a")

在此处输入图像描述

Why does this plot seem to show a greater fixed effect than the statistical output of the model? Shouldn't the difference between the two points be 4.61? If not, what is the plot showing?


I can retrieve the exact data being visualised using sjPlot::get_model_data :

> get_model_data(m1, terms = "a", type = "pred")

# Predicted values of y
# x = a

x | Predicted |   SE | group_col |         95% CI
-------------------------------------------------
1 |     11.45 | 0.40 |         1 | [10.65, 12.24]
2 |      2.23 | 0.55 |         1 | [ 1.15,  3.31]

Adjusted for:
* re1.a1 = -0.05
*      p = 0 (population-level)

However I am still confused by how this relates to the model output, specifically the fixed effect of a .


I can also recreate the sjPlot visualisation using afex::afex_plot :

> afex_plot(m1, x = "a", mapping = c("color"))
Aggregating data over: p 

在此处输入图像描述

What's most interesting here is the message Aggregating data over: p . Is this to say that the random effect of p is not taken into account? If so, how is this different from a plot that doesn't reference the model at all? For instance, the following seems to plot the same values despite simply being an y ~ x line...

ggplot(DF, aes(x = a, y = y, color = a))+
  geom_smooth(aes(group = 1), method = "lm", se = T, color = "black")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM