In the previous tutorial we covered how the multivariate multilevel modeling is used to examine dyadic repeated measures data (a version of APIM. In this tutorial, we develop a parallel example that uses teh same model set-up to examine intraindividual coupling. Specifically, we use the same dummy-variable “trick” to model relations of two outcome variables simultaneously. Here, distinguishability is given by the variables inherently.


  1. Introduction to the research questions, model, and data.
  2. Plotting and Restructuring the data.
  3. The multilevel model.
  4. Conclusion.

Prelim - Loading libraries used in this script.


1. Introduction to the Research Questions, Model, and Data.

The Research Questions.

For this example, we use the AMIB data. Here, our “dyadic data”, rather than being being about two individuals nested in dyads, are about two variables nested within an individual.

Our interest in this example is to model the intraindividual coupling of positive and negative emotion.

We are going to address: How do the emotions carry-over from day to day? This is being called emotional intertia in the literature.
How does postive emotion influence negative emotions? (protective factor) and, vice versa,
How does negative emotion influence positive emotions? (risk factor)

Basically, this is a cross-lag panel model - except that we are working with diary data, and that means we can prioritize within-person associations.

The Modeling Enterprise.

The basic multilevel model is designed as a model with a univariate outcome. Here again, we “trick” the program into thinking that two (or more) variables are one variable.

The Data.

We use the AMIB data - working from a standard long file to obtain a “double-entry” stacked file with lagged variables, and dummy variables that will be used to “turn on” and “turn off” the double records and invoke parameter estimation for each variable. In the dyadic example, the data were already prepared. Here, we walk through the data preparation process.

Load data and needed libraries.

#set filepath for data file
filepath <- "https://quantdev.ssri.psu.edu/sites/qdev/files/AMIBbrief_raw_daily1.csv"
#read in the .csv file using the url() function
daily <- read.csv(file=url(filepath),header=TRUE)

#Little bit of clean-up
var.names.daily <- tolower(colnames(daily))

#subsetting data
daily1 <- daily[,c("id","day","posaff","negaff")]

# Examine first few rows of the data set
## [1] "id"     "day"    "posaff" "negaff"

2. Plotting & Restructuring the Data.

Plotting the data

Before we begin running our models, it is always a good idea to look at our data.

We look at our two outcomes, posaff and negaff.

#sample descriptives
##    vars    n mean   sd median trimmed  mad min max range skew kurtosis
## X1    1 1441 2.45 1.04    2.2    2.34 1.04   1 6.9   5.9 0.96     0.77
##      se
## X1 0.03
##    vars    n mean  sd median trimmed  mad min max range  skew kurtosis
## X1    1 1441 4.12 1.1    4.2    4.15 1.19   1   7     6 -0.25    -0.33
##      se
## X1 0.03
ggplot(data=daily1, aes(x=negaff)) +
  geom_histogram(fill="white", color="black") + 
  labs(x = "Negative Affect")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(data=daily1, aes(x=posaff)) +
  geom_histogram(fill="white", color="black") + 
  labs(x = "Positive Affect")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Intraindividual plots

#setting up for changing ids
i <- 106
ggplot(data = subset(daily1, id <=i), aes(x=day, group=id), legend=FALSE) +
  #geom_rect(mapping=aes(xmin=day-.5, xmax=day+.5, ymin=0, ymax=10, fill=wrkstrscw), alpha=0.6) +
  geom_point(aes(x=day,y = posaff), color="blue", shape=17, size=2) +
  geom_line(aes(x=day,y = posaff), color="blue", lty=1, size=1) +
  geom_point(aes(x=day,y = negaff), color="red", shape=17, size=2) +
  geom_line(aes(x=day,y = negaff), color="red", lty=1, size=1) +
  xlab("Day") + 
  ylab("Affect (Positive = Blue, Negative = Red)") + ylim(1,7) +
  scale_x_continuous(breaks=seq(0,8,by=1)) + 
  facet_wrap( ~ id)