Between and within-subjects data

Between-subjects data arise when your dataset contains information about multiple individuals each measured at a single time point. Resulting inferences from models fit to these data can only be made at the beween-persons level. Across many discplines it’s becoming more and more common to have data from multiple individuals each measured at multiple time points. This multilevel data inherently contains information about both between and within-subject differences. That being said, care needs to be taken when analyzing these data to ensure that resulting inferences are being made are made at the correct level.

Let’s look at some data below to illustrate this point. The data are simulated data from a study conducted by van de Pol & Wright (2009), a study that addresses the title of this post in greater detail. We’ll give names to the variables in the simulated data and create a story along with it to make the illustration more interesting.

Let’s imagine we’re conducting a study looking at how graduate student productivity relates to advisor attention for 50 graduate students over 5 weeks. Each instance of measured adivsor attention is nested within a participant.

##    id    attn   prod
## 1   1  5.7795 3.1926
## 2   1  2.9782 1.5520
## 3   1  5.5259 2.5482
## 4   1  7.2101 0.1699
## 5   1  3.0303 2.6182
## 6   2  3.3205 2.5597
## 7   2 -0.7063 4.5633
## 8   2  4.2287 3.4239
## 9   2  6.6303 4.0967
## 10  2 -0.3719 3.4084

We can analyze the data using a mixed model containing a fixed effect of productivity in addition to two random terms (random subject intercept and random slope of productivity). From this analysis, there doesn’t appear to be any relationship between advisor attention and productivity. However, the resulting beta coefficient represents a combination of both between and within-person effects. Below, we’ll differentiate these effects to get a better idea of what’s going on.

m1 <- lmer(attn ~ prod + (1 + prod|id), data = wb_dat)

summary(m1)
## Linear mixed model fit by REML t-tests use Satterthwaite approximations
##   to degrees of freedom [lmerMod]
## Formula: attn ~ prod + (1 + prod | id)
##    Data: wb_dat
## 
## REML criterion at convergence: 1335.2
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.08952 -0.64253 -0.07723  0.65575  2.49898 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 11.5326  3.3960        
##           prod         0.1035  0.3217   -0.27
##  Residual              7.9202  2.8143        
## Number of obs: 250, groups:  id, 50
## 
## Fixed effects:
##             Estimate Std. Error      df t value Pr(>|t|)
## (Intercept)   0.3833     0.5226 26.3774   0.734     0.47
## prod         -0.2127     0.1634 41.0776  -1.302     0.20
## 
## Correlation of Fixed Effects:
##      (Intr)
## prod -0.150

Disaggregating between and within-person effects

We need to split our “productivity” variable into two separate variables, one representing within and one depicting between-subject effects. This disaggregation can be easily accomplished by person-mean centering the data. Specifically:

In the context of our example, each individual’s mean productivity will be subtracted from each of their 5 productivity scores to create the within-person component (in multilevel verbage this is a level 1 predictor). Each individual’s mean prodictivity score will be turned into a level 2 predictor. We can create a function that will accomplish this for us.

win_btwn <- function(dat, var) {
  
  w <- paste(var, "win", sep = "_" )
  
  b <- paste(var, "btwn", sep = "_" )
  
  dat[,w] <- scale(dat[,var], center = TRUE, scale = FALSE)
  
  dat[,b] <- rep(mean(dat[,var], na.rm = TRUE),nrow(dat))
  
  return(dat)
  
}

The data are split by “id” and the “win_btwn” function is applied over the resulting list of dataframes.

wb_splt <- split.data.frame(wb_dat,wb_dat$id)

wb_splt_l <- lapply(wb_splt,win_btwn, var = "prod")

wb_splt_comb <- do.call("rbind", wb_splt_l)
##      id    attn   prod prod_win prod_btwn
## 1.1   1  5.7795 3.1926  1.17642   2.01618
## 1.2   1  2.9782 1.5520 -0.46418   2.01618
## 1.3   1  5.5259 2.5482  0.53202   2.01618
## 1.4   1  7.2101 0.1699 -1.84628   2.01618
## 1.5   1  3.0303 2.6182  0.60202   2.01618
## 2.6   2  3.3205 2.5597 -1.05070   3.61040
## 2.7   2 -0.7063 4.5633  0.95290   3.61040
## 2.8   2  4.2287 3.4239 -0.18650   3.61040
## 2.9   2  6.6303 4.0967  0.48630   3.61040
## 2.10  2 -0.3719 3.4084 -0.20200   3.61040

Disaggregation reveals drastically different results

After fitting this new model, both of the within and between effects are significant but in opposing directions. These opposing effects canceled each other out in the earlier analysis. Further, the interpretation of these effects make perfect sense. The within-person component indicates that having a lower productivity level than your average will attract the attention of your advisor. The between-person component indicates that students who are more productive on average will recieve more advisor attention.

m2 <- lmer(attn ~ prod_win + prod_btwn + (1+prod_win|id), data = wb_splt_comb)

summary(m2)
## Linear mixed model fit by REML t-tests use Satterthwaite approximations
##   to degrees of freedom [lmerMod]
## Formula: attn ~ prod_win + prod_btwn + (1 + prod_win | id)
##    Data: wb_splt_comb
## 
## REML criterion at convergence: 1284.7
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.85584 -0.64315 -0.05622  0.63059  3.06508 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr
##  id       (Intercept) 5.5409   2.354        
##           prod_win    0.0222   0.149    0.71
##  Residual             7.2775   2.698        
## Number of obs: 250, groups:  id, 50
## 
## Fixed effects:
##             Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)  0.05748    0.37524 48.00667   0.153    0.879    
## prod_win    -1.11807    0.20157 45.85481  -5.547 1.39e-06 ***
## prod_btwn    0.93387    0.16948 48.03666   5.510 1.39e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##           (Intr) prd_wn
## prod_win   0.066       
## prod_btwn -0.079 -0.004

A non-simulated example

As mentioned earlier, the data were simulated to contain stark opposing between and within-person effects to illustrate one extreme result of this process. Next, we can turn to a “real” dataset to see this in practice. I put real in quotes as the data are from a poorly design study from my first year as a graduate student. Participants saw a series of images (either neutral or negative) and were then later presented with another series of images (containing both the images they saw earlier and new iamges) and were asked to indicate (with a keyboard press) whether or not the image had been witnessed at the intial viewing. Of interest was the relationship between reaction time, image type (positive vs negative), and whether or not they correctly recalled the image.

Again, the data will be split by id or in this case “Subject” and win_btwn will be applied over the resulting list of dataframes.

jf_split<-split.data.frame(jf_dat4split,jf_dat4split$Subject)

jf_splt_l <- lapply(jf_split, win_btwn, var="RT")

jf_split_comb <- do.call("rbind", jf_splt_l)

Of interest was how (or not) reaction time interacted with the type of image to influence a correct recollection. Before that analysis is set up, contrasts are specified reflecting the hypothesis that negative images would be better remembered. It also allows us to avoid dummy coding where the main effects of the within and between-person reaction time components reflect slopes for the reference group. Instead, we get an average effect.

cons <- solve(rbind(c(1,1),c(1,-1)))

negVSneu <- cons[,2]

contrasts(mem_dat$emotion) <- cbind(negVSneu)

contrasts(mem_dat$emotion)
##     negVSneu
## Neg      0.5
## Neu     -0.5

Now a model is specifed with the non disaggregated RT variable to examine whether image type (emotion) and reaction time interact to influence a correct recollection. From the results, it appears they do not. A faster (lower) RT is associated with a higher probability of a correct answer, and it also looks like negative images are easier to correctly identify than neutral images. How about the disaggregated model?

m1.1<-glmer(response ~ RT*emotion + (1+RT+emotion|Subject), data = mem_dat, family = binomial,control=glmerControl(optimizer="bobyqa"))

summary(m1.1)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: response ~ RT * emotion + (1 + RT + emotion | Subject)
##    Data: mem_dat
## Control: glmerControl(optimizer = "bobyqa")
## 
##      AIC      BIC   logLik deviance df.resid 
##   2887.8   2951.8  -1433.9   2867.8     4406 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.7280  0.2205  0.2916  0.3638  1.1297 
## 
## Random effects:
##  Groups  Name            Variance Std.Dev. Corr       
##  Subject (Intercept)     0.47996  0.6928              
##          RT              0.02841  0.1686   -0.05      
##          emotionnegVSneu 0.01938  0.1392    0.91  0.36
## Number of obs: 4416, groups:  Subject, 69
## 
## Fixed effects:
##                    Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         2.33709    0.10303  22.683  < 2e-16 ***
## RT                 -0.37787    0.06318  -5.981 2.22e-09 ***
## emotionnegVSneu     0.44874    0.12066   3.719   0.0002 ***
## RT:emotionnegVSneu  0.07305    0.08621   0.847   0.3968    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) RT     emtnVS
## RT          -0.099              
## emotinngVSn  0.220 -0.065       
## RT:mtnngVSn -0.038  0.016 -0.204

The disaggregated model once again revelas a different story

Here, we can see the non-disaggregated effect from early was driven by within-subjects information. If you’re responding faster than typical, you’re more apt to make a correct response. However, a between-subject RT and emotion interaction emerges such that those who tend to react slower on average are better at correctly identify a negative compared to a neutral image (though the effect is tiny). Nonetheless, it’s novel information that could not be gleaned from the non-disaggregated model

m1.2<-glmer(response ~ emotion+RT_win+RT_btwn+RT_win:emotion+RT_btwn:emotion + (1+RT_win+emotion|Subject), data = mem_dat ,family=binomial,control=glmerControl(optimizer="bobyqa"))

summary(m1.2)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: 
## response ~ emotion + RT_win + RT_btwn + RT_win:emotion + RT_btwn:emotion +  
##     (1 + RT_win + emotion | Subject)
##    Data: mem_dat
## Control: glmerControl(optimizer = "bobyqa")
## 
##      AIC      BIC   logLik deviance df.resid 
##   2887.6   2964.4  -1431.8   2863.6     4404 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.7695  0.2170  0.2890  0.3641  1.2872 
## 
## Random effects:
##  Groups  Name            Variance Std.Dev. Corr       
##  Subject (Intercept)     0.48382  0.6956              
##          RT_win          0.02459  0.1568   -0.28      
##          emotionnegVSneu 0.01746  0.1321    0.79 -0.81
## Number of obs: 4416, groups:  Subject, 69
## 
## Fixed effects:
##                         Estimate Std. Error z value Pr(>|z|)    
## (Intercept)              2.36009    0.10368  22.763  < 2e-16 ***
## emotionnegVSneu          0.44952    0.12074   3.723 0.000197 ***
## RT_win                  -0.35352    0.06231  -5.674  1.4e-08 ***
## RT_btwn                 -0.01297    0.12100  -0.107 0.914607    
## emotionnegVSneu:RT_win  -0.01570    0.08653  -0.181 0.856005    
## emotionnegVSneu:RT_btwn  0.23999    0.10964   2.189 0.028605 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##                (Intr) emtnVS RT_win RT_btw emtnngVSn:RT_w
## emotinngVSn     0.205                                    
## RT_win         -0.211 -0.122                             
## RT_btwn         0.055  0.073 -0.286                      
## emtnngVSn:RT_w -0.044 -0.197 -0.029 -0.069               
## emtnngVSn:RT_b  0.056  0.034 -0.045  0.221 -0.158

Conclusion

We’ve seen how within and between-person information is embedded in multilevel data. Person-mean centering provides a simple way to tease apart these two effects and test both components. Doing so has important theoretical implications and can be of critical importance, particularly if the relationships are different within partcipants as compared to across participants.