# Can we really assign random slopes to between-subject effects in a mixed effects model?

This question is inspired by the comments to one of my answers on this forum: Should all adjustments be random effects in a mixed linear effect?.

In my answer, I stated something akin to the following: In a mixed effects model, only within-subject predictor variables can be allowed to have varying (or random) effects across the levels of the corresponding random grouping factor.

The comment I received from @HeteroskedasticJim was that this is not really true, implying that even between-subject predictor variables can in fact have within-subject effects that can vary across subjects. (At least, that is my interpretation of this comment.)

My own response to this comment was as follows:

How can we compute the effect of gender within one subject when there is no variability in the values of gender within that subject? The effect of gender within one person would imply that when that person switches genders from male to female, say, there is a difference in the expected value of their response. The variability of the (within-subject) gender effects across subjects would imply that the difference would vary across subjects - so all those subjects would have to have a gender switch! Just trying to understand intuitively what is going on here.

Can someone on this forum provide a concrete example that can help me understand what this comment is trying to get at? I always thought that, to estimate a within-subject effect for a predictor variable, you need to have variability in the values of that predictor variable within that subject. What glaring thing am I missing?

• @HeteroskedasticJim - this is the promised question. If it needs any edits before you answer it, just let me know. Thank you in advance for taking the time to follow up on this! – Isabella Ghement May 18 '19 at 19:12
• Can you explain why random intercepts would not fit that criteria (i.e. constant within subject but still identifiable)? Or are you saying that something that is constant within subject is not identifiable given a random intercept? Just trying to clarify the question for my own sake. – Cliff AB May 18 '19 at 19:31
• I am just trying to understand why between-subject variables would be allowed to have random slopes - that's all! – Isabella Ghement May 18 '19 at 19:36
• @CliffAB I thought about that too, and the reason random intercept would not apply is that it is typical to have one constant in the predictor matrix. – Heteroskedastic Jim May 18 '19 at 20:52
• (+1) This is a great question ! – Robert Long Jul 20 at 5:49

I will work with an example using the Verbal Aggression dataset, largely borrowing off this paper: https://www.jstatsoft.org/article/view/v039i12

library(lme4)
VA.dat <- VerbAgg[, c("Gender", "r2", "id")]
VA.dat <- VA.dat[order(VA.dat$id), ] # sort data by person id  I prepare the data to regress the binary response on gender and permit the gender effect to be different by person: VA.dat$$M <- (VA.dat$$Gender == "M") + 0 VA.dat$$F <- (VA.dat$$Gender == "F") + 0 VA.dat$$rbin <- (VA.dat$$r2 == "Y") + 0  Look at data frame to confirm gender is person level: table(aggregate(M ~ id, VA.dat, mean)$M)
#
#   0   1
# 243  73


The mean of the male indicator by person is either 1 or 0, so gender values are constant within id.

Regression:

summary(fit <- lmer(rbin ~ F + (0 + M + F || id), VA.dat))
# Random effects:
#  Groups   Name Variance Std.Dev.
#  id       M    0.04164  0.2041
#  id.1     F    0.04903  0.2214
#  Residual      0.20202  0.4495
# Number of obs: 7584, groups:  id, 316
#
# Fixed effects:
#             Estimate Std. Error t value
# (Intercept)  0.51370    0.02619  19.617
# F           -0.04885    0.03037  -1.609


I have an intercept representing Male and the Fem difference from it. Note the use of the || before id in the random effect specification. It stops the gender slopes from being correlated and forces a simpler interpretation of the random slopes. With a |, each id would have a value on both male and female slopes, which can be confusing to deal with.

On average, male responses are higher than female responses. However, the random intercept variance of female respondents is slightly larger than the random intercept variance of male respondents.

To look at the random slopes:

head(ranef(fit)\$id)
#            M            F
# 1 -0.1153768  0.000000000
# 2 -0.3926609  0.000000000
# 3  0.0000000 -0.041122748
# 4  0.0000000  0.101123911
# 5  0.0000000 -0.041122748
# 6  0.0000000 -0.005561083


The first two persons are male and the gender effects for them are more negative than the overall gender effect, $$0.514 - 0.115; 0.514 - 0.393$$. The next four persons are female, and the gender effect for the first two are: $$0.514 - 0.049 - 0.041; 0.514 - 0.049 + 0.101$$.

So clearly, one does not need variability within the grouping factor to permit the effect of a variable to vary across persons. The reason for the confusion is older model formulations which separate the data in level 1 dataset and level 2 datasets. With lme4, and more recent model formulations, there is no need for such formulation. Douglas Bates covers details here: https://cran.r-project.org/web/packages/lme4/vignettes/Theory.pdf

• Thank you for this beautiful answer, @HeteroskedasticJim! 👍 – Isabella Ghement May 18 '19 at 21:54
• @IsabellaGhement you're welcome. – Heteroskedastic Jim May 18 '19 at 21:57
• There are other places on this forum where your answer would be helpful, like here: stats.stackexchange.com/questions/245960/…. – Isabella Ghement May 18 '19 at 22:02
• @IsabellaGhement yes, it can be re-written that way. It's flexible that way. – Heteroskedastic Jim May 18 '19 at 22:14
• @IsabellaGhement I wouldn’t call it “random slopes” for F and M, you have two separate “random intercepts” for F and M. Put otherwise you just say that the random intercepts for F and M have different variances. And if you would allow the error variance to be different between F and M, this would correspond to a sub-group analysis fitting separate models for F and M. – Dimitris Rizopoulos May 19 '19 at 8:27