# Between- and within-person level effects when using multilevel modelling for longitudinal data in R

I’m using nlme package in R for analysing longitudinal data. The aim is to understand if changes in need satisfaction (TNS) and need frustration (TNF) predict changes in well-being (WBtot) over time (Tim) in participants (ID). The model with predictors:

model4 <- lme (WBtot~Tim + TNS +TNF, random = ~ Tim|ID, data = longit, method = "ML", na.action = "na.omit")


As I understand, the fixed effects in the output show how Tim, TNS and TNF are related to WBtot on each measurement occasion. My issue is I need to see between-person effects of TNS and TNF on WBtot (e.g., to see if individuals with higher TNS have greater WBtot) and within-person effects (e.g., would individuals have greater WBtot at time points when they have greater TNS?).

Can anyone advise how to look at between and within-person level effects of predictors in R?

• Any particular reason why you don't use lmer from the lme4 package ? Also, why is Tim specified as a random slope, yet absent from the fixed effects ? – Robert Long Jul 24 '20 at 10:57

model4 <- lme (WBtot~RetTim + TNS +TNF, random = ~ Tim|ID, data = longit, method = "ML", na.action = "na.omit")


As I understand, the fixed effects in the output show how Tim, TNS and TNF are related to WBtot on each measurement occasion

Yes, although there is something strange in the model - I wonder if RetTim and Tim are the same variable ? Anyway, your the interpretation of the fixed effects is basically correct. Each estimate is the association between that variable and the outcome, with the other fixed effects remaining constant. When you fit random slopes (for example Tim in ~ Tim|ID) then you allow for the fixed effects of that variable to vary between subjects.

My issue is I need to see between-person effects of TNS and TNF on WBtot (e.g., to see if individuals with higher TNS have greater WBtot) and within-person effects.

One way to do this is to mean-centre the variables within groups/subjects/IDs and include the group means as level-2 predictors. So you would create new variables for the mean of each variable within each group and then subtract this from the individual level values, and then also include the mean of each group in the model along with the offseted variables.

lme(WBtot ~ RetTim.mean + RetTim.cent + TNS.mean + TNS.cent + NF.cent + TNF.mean, random = ~ Tim|ID)


The estimates for the centred variables will be within-subject and the estimates for the means will be between.

This is sometimes referred to as "contextual effects"

The other approach is to fit random effects for each of the fixed effects - so that each subject has their own slope (offset from the global fixed effect). However this assumes they are normally distributed and is probably not what you want here.

• Thank you so much, Robert! Yes, Tim and RetTim is the same, it was a spelling mistake. I did what you suggested for TNS and TNF, and it seems to work. I've chosen nlme because it was easier to see p-values than in lme4. Is lme4 better? Sorry, I'm very new to multilevel modelling... – AF1402 Jul 24 '20 at 13:41
• No problem, I thought that's what happened. It's OK to use nlme. But there is a reason lme4 doesn't poduce them: p values are the Devil's work - don't use them ! lme4 is better supported. nlme is quite old now. – Robert Long Jul 24 '20 at 14:14