Overview

This tutorial extends the calculation of intraindividual variability metrics into examination of intraindividual covariation. Specifically, this tutorial demonstrates calculation of intraindividual correlation (and covariance) – as derived metrics that may be useful for articulating a variety of dynamic characteristics from experience sampling data. We begin the move towards the standard multilevel modeling analysis approach by first considering individual-level regressions. At the end of the session we begin setting up the mutlilevel model.

Outline

In this session we cover …

A. Preliminary description of two variables
B. Calculating intraindividual (within-person) correlations (iCorr, iCov)
C. Calculating intraindividual (within-person) regressions (iReg)
D. Setting up the Multilevel Model (MLM)

Prelim - Loading libraries used in this script.

library(psych)
library(ggplot2)
library(data.table)
library(entropy)
library(plyr)
library(nlme)

Prelim - Reading in Repeated Measures Data

Note that we are working from a long file. For your own data, there may be some steps to get to this point.

#Setting the working directory
setwd("~/Desktop/Fall_2017")  #Person 1 Computer
#setwd("~/Desktop/Fall_2017")  #Person 2 Computer

#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))
colnames(daily)<-var.names.daily

Everything we do today uses a long file.

A: Looking at distributions and within-person time-series

Lets look at some descriptions of today’s variables.

#getting a list of the variable names
names(daily)
##  [1] "id"      "day"     "date"    "slphrs"  "weath"   "lteq"    "pss"    
##  [8] "se"      "swls"    "evalday" "posaff"  "negaff"  "temp"    "hum"    
## [15] "wind"    "bar"     "prec"
#sample descriptives
describe(daily$posaff)
##    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
describe(daily$negaff)
##    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
#histograms
ggplot(data=daily, aes(x=posaff)) +
  geom_histogram(fill="white", color="black") + 
  labs(x = "Positive Affect")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 17 rows containing non-finite values (stat_bin).

ggplot(data=daily, aes(x=negaff)) +
  geom_histogram(fill="white", color="black") + 
  labs(x = "Negative Affect")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 17 rows containing non-finite values (stat_bin).

And lets look at the bivariate longitudinal time-series plots …
1. For Person A

#ggplot version .. see also http://ggplot.yhathq.com/docs/index.html
ggplot(data = subset(daily, id ==101), aes(x=day), legend=FALSE) +
  #geom_rect(mapping=aes(xmin=day, xmax=day+1, ymin=0, ymax=100, fill=posaff), alpha=0.8) +
  geom_point(aes(x=day,y = posaff), shape=16, size=3,colour="blue") +
  geom_line(aes(x=day,y = posaff), lty=1, size=1,colour="blue") +
  geom_point(aes(x=day,y = negaff), shape=17, size=3,colour="red") +
  geom_line(aes(x=day,y = negaff), lty=1, size=1,colour="red") +
  xlab("Day") + 
  ylab("Positive (blue) and Negative (red) Affect") + ylim(1,7) +
  scale_x_continuous(breaks=seq(0,7,by=1)) 

  1. For Person B
ggplot(data = subset(daily, id ==102), aes(x=day), legend=FALSE) +
  #geom_rect(mapping=aes(xmin=day, xmax=day+1, ymin=0, ymax=100, fill=posaff), alpha=0.8) +
  geom_point(aes(x=day,y = posaff), shape=16, size=3,colour="blue") +
  geom_line(aes(x=day,y = posaff), lty=1, size=1,colour="blue") +
  geom_point(aes(x=day,y = negaff), shape=17, size=3,colour="red") +
  geom_line(aes(x=day,y = negaff), lty=1, size=1,colour="red") +
  xlab("Day") + 
  ylab("Positive (blue) and Negative (red) Affect") + ylim(1,7) +
  scale_x_continuous(breaks=seq(0,7,by=1))