``` Load Packages
library(arm); library(lmerTest); library(psych)
## Loading required package: MASS
## Loading required package: Matrix
## Loading required package: lme4
## Loading required package: Rcpp
##
## arm (Version 1.7-03, built: 2014-4-27)
##
## Working directory is /private/var/folders/9c/88_yf73x0ys02zvn5hkvvq6r0000gn/T/RtmpmxWTU5
##
## KernSmooth 2.23 loaded
## Copyright M. P. Wand 1997-2009
##
## Attaching package: 'lmerTest'
##
## The following object is masked from 'package:lme4':
##
## lmer
##
## The following object is masked from 'package:stats':
##
## step
##
##
## Attaching package: 'psych'
##
## The following objects are masked from 'package:arm':
##
## logit, rescale, sim
Loading Data
setwd("/Volumes/TOSHIBA EXT/Dropbox/Schools Study Data/Emily Griffith Data and R Scripts")
data <- read.csv("EmilyGriffith_all.csv")
Creating School ID as ID
data$ID <- data$Q1
#Create scale scores
data$meanMLQ <- apply(data[, c("MLQ_1" ,"MLQ_4", "MLQ_5", "MLQ_6")], 1, mean, na.rm = TRUE)
#Means or plotting
data$baseline <- ifelse(data$Time<4,0,1)
pdata <- tapply(data[,"meanMLQ"], data[,3], mean, na.rm=TRUE)
plot(pdata, type="l")
M0 <- lmer(meanMLQ ~ 1 + (1|ID), data=data)
fixef(M0)
## (Intercept)
## 5.403
confint(M0)
## Computing profile confidence intervals ...
## 2.5 % 97.5 %
## .sig01 0.4126 0.9868
## .sigma 0.8318 1.1634
## (Intercept) 5.1368 5.6746
M1 <- update(M0, .~. + Time, REML=FALSE)
fixef(M1)
## (Intercept) Time
## 5.23198 0.07806
confint(M1)
## Computing profile confidence intervals ...
## 2.5 % 97.5 %
## .sig01 0.4154 0.9871
## .sigma 0.8287 1.1593
## (Intercept) 4.7149 5.7481
## Time -0.1246 0.2807
M2 <- update(M1, .~. + baseline)
fixef(M2)
## (Intercept) Time baseline
## 5.357977 0.006649 0.310006
confint(M2)
## Computing profile confidence intervals ...
## 2.5 % 97.5 %
## .sig01 0.4205 0.9898
## .sigma 0.8248 1.1542
## (Intercept) 4.7477 5.9663
## Time -0.2668 0.2807
## baseline -0.4944 1.1119
M3 <- update(M2, .~. + I(Time^2))
fixef(M3)
## (Intercept) Time baseline I(Time^2)
## 6.3378 -1.1199 -0.6554 0.2814
confint(M3)
## Computing profile confidence intervals ...
## 2.5 % 97.5 %
## .sig01 0.4517 1.0105
## .sigma 0.8073 1.1316
## (Intercept) 4.8332 7.8177
## Time -2.7016 0.4884
## baseline -2.2084 0.9169
## I(Time^2) -0.1146 0.6710
Pdata <- tapply(data[,"meanMLQ"], data[,3], mean, na.rm=TRUE)
# Add random noise to time to better see the points of interest
data$TimeJIT <- data$Time+runif(126, min=-.1, max=.1)
with(data, plot(TimeJIT, meanMLQ, col="grey", pch="*"))
lines(pdata, col="red", lwd=2)
boxplot(data$meanMLQ~Time, data=data, notch=F, col=(c("red","blue", "green", "gold")), main="MLQ All", xlab="Purpose in Life", xlim = c(), ylim = c(2, 8 ), yaxs = "i")