Search code examples
rlinear-regressionlme4mixed-modelsrandom-effects

How do I get the within-group association using lme4 in r?


Setup: I'm testing if the association between pairs of individuals for a trait (BMI) changes over time. I have repeated measures, where each individual in a pair gives BMI data at 7 points in time. Below is a simplified data frame in long format with Pair ID (the identifier given to each pair of individuals), BMI measurements for both individuals at each point in time (BMI_1 and BMI_2), and a time variable with seven intervals, coded as continuous.

Pair_ID BMI_1 BMI_2 Time
1 25 22 1
1 23 24 2
1 22 31 3
1 20 27 4
1 30 26 5
1 31 21 6
1 19 18 7
2 21 17 1
2 22 27 2
2 24 22 3
2 25 20 4

First, I'm mainly interested in testing the within-pair association (the regression coefficient of BMI_2, below) and whether it changes over time (the interaction between BMI_2 and Time). I'd like to exclude any between-pair effects, so that I'm only testing associated over time within pairs.

I was planning on fitting a linear mixed model of the form:

    lmer(BMI_1 ~ BMI_2 * Time + (BMI_2 | Pair_ID), Data)

I understand the parameters of the model (e.g., random slopes/intercepts), and that the BMI_2 * Time interaction tests whether the relationship between BMI_1 and BMI_2 is moderated by time.

However, I'm unsure how to identify the (mean) within-pair regression coefficients, and whether my approach is even suitable for this.

Second, I'm interested in understanding whether there is variation between pairs in the BMI_2 * Time interaction (i.e., the variance in slopes among pairs) - for example, does the associated between BMI_1 and BMI_2 increase over time in some pairs but not others?

For this, I was considering fitting a model like this:

    lmer(BMI_1 ~ BMI_2 * Time + (BMI_2 : Time | Pair_ID), Data)

and then looking at the variance in the BMI_2 : Time random effect. As I understand it, large variance would imply that this interaction effect varied a lot between pairs.

Any help on these questions (especially the first question) would be greatly appreciated.

P.s., sorry if the question is poorly formatted. It's my first attempt.


Solution

  • Answering for completeness. @benimwolfspelz's comment is spot on. This is known as "contextual effects" in some areas of applied work. The idea is to split the variable into between and within components by mean-centering each group and fitting the mean-centred variable (which will estimate the within component) and the group means (which will estimate the between component).