13.2 Data Preparation and Description

13.2.1 Loading libraries used in this script.

library(psych)  #for basic functions
library(ggplot2)  #for plotting
library(data.table) #for fast data management
library(nlme) #for mixed effects models
library(plyr) #for data management
library(see)
library(ggeffects)

For our examples, we use 4-occasion WISC data. Load the repeated measures data.

filepath <- "https://quantdev.ssri.psu.edu/sites/qdev/files/wisc3raw.csv"
wisc3raw <- read.csv(file=url(filepath),header=TRUE)

Subsetting to the variables of interest. Specifically, we include the id variable; the repeated measures outcome variables verb1, verb2, verb4, verb6; and the predictors grad and momed variables.

varnames <- c("id","verb1","verb2","verb4","verb6","grad","momed")
wiscsub <- wisc3raw[ ,varnames]
describe(wiscsub)
##       vars   n   mean    sd median trimmed   mad   min    max  range  skew
## id       1 204 102.50 59.03 102.50  102.50 75.61  1.00 204.00 203.00  0.00
## verb1    2 204  19.59  5.81  19.34   19.50  5.41  3.33  35.15  31.82  0.13
## verb2    3 204  25.42  6.11  25.98   25.40  6.57  5.95  39.85  33.90 -0.06
## verb4    4 204  32.61  7.32  32.82   32.42  7.18 12.60  52.84  40.24  0.23
## verb6    5 204  43.75 10.67  42.55   43.46 11.30 17.35  72.59  55.24  0.24
## grad     6 204   0.23  0.42   0.00    0.16  0.00  0.00   1.00   1.00  1.30
## momed    7 204  10.81  2.70  11.50   11.00  2.97  5.50  18.00  12.50 -0.36
##       kurtosis   se
## id       -1.22 4.13
## verb1    -0.05 0.41
## verb2    -0.34 0.43
## verb4    -0.08 0.51
## verb6    -0.36 0.75
## grad     -0.30 0.03
## momed     0.01 0.19

Multilevel modeling analyses typically require a tall (long) data set. So, we reshape from wide to tall:

verblong <- reshape(
  data=wiscsub, 
  varying=c("verb1","verb2","verb4","verb6"), 
  timevar="grade", 
  idvar="id", 
  direction="long", 
  sep=""
)
verblong <- verblong[order(verblong$id,verblong$grade),c("id","grade","verb","grad","momed")]
head(verblong,12)
##     id grade  verb grad momed
## 1.1  1     1 24.42    0   9.5
## 1.2  1     2 26.98    0   9.5
## 1.4  1     4 39.61    0   9.5
## 1.6  1     6 55.64    0   9.5
## 2.1  2     1 12.44    0   5.5
## 2.2  2     2 14.38    0   5.5
## 2.4  2     4 21.92    0   5.5
## 2.6  2     6 37.81    0   5.5
## 3.1  3     1 32.43    1  14.0
## 3.2  3     2 33.51    1  14.0
## 3.4  3     4 34.30    1  14.0
## 3.6  3     6 50.18    1  14.0