``` 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 /Volumes/TOSHIBA EXT/Dropbox/Schools Study Data/Emily Griffith Data and R Scripts
##
## 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$meanLS <- apply(data[, c("LS_1", "LS_2" , "LS_3" ,
"LS_4" , "LS_5" )], 1, mean, na.rm = TRUE)
#Means or plotting
data$baseline <- ifelse(data$Time<4,0,1)
pdata <- tapply(data[,"meanLS"], data[,3], mean, na.rm=TRUE)
plot(pdata, type="l")
M0 <- lmer(meanLS ~ 1 + (1|ID), data=data)
fixef(M0)
## (Intercept)
## 3.641
confint(M0)
## Computing profile confidence intervals ...
## Warning: Last two rows have identical or NA .zeta values: using minstep
## Warning: non-monotonic profile
## 2.5 % 97.5 %
## .sig01 0.000 Inf
## .sigma 1.648 2.132
## (Intercept) 3.299 3.983
M1 <- update(M0, .~. + Time, REML=FALSE)
fixef(M1)
## (Intercept) Time
## 0.907 1.213
confint(M1)
## Computing profile confidence intervals ...
## Warning: convergence code 3 from bobyqa: bobyqa -- a trust region step
## failed to reduce q
## 2.5 % 97.5 %
## .sig01 0.0000 0.7447
## .sigma 1.2490 1.6799
## (Intercept) 0.2134 1.6030
## Time 0.9268 1.4938
M2 <- update(M1, .~. + baseline)
fixef(M2)
## (Intercept) Time baseline
## -0.05849 1.76551 -2.36838
confint(M2)
## Computing profile confidence intervals ...
## 2.5 % 97.5 %
## .sig01 0.0000 0.6814
## .sigma 1.1546 1.5504
## (Intercept) -0.8287 0.7111
## Time 1.4077 2.1229
## baseline -3.4140 -1.3327
M3 <- update(M2, .~. + I(Time^2))
fixef(M3)
## (Intercept) Time baseline I(Time^2)
## -5.540 8.076 3.092 -1.571
confint(M3)
## Computing profile confidence intervals ...
## 2.5 % 97.5 %
## .sig01 0.2424 0.8972
## .sigma 0.8445 1.1874
## (Intercept) -7.0938 -3.9617
## Time 6.3893 9.7307
## baseline 1.4451 4.7021
## I(Time^2) -1.9774 -1.1573
You can also embed plots, for example:
Intervention was given between 3 and 4 (Time 1 is not relevant because the data is not clean)
boxplot(data$meanLS~Time, data=data, notch=F, col=(c("red","blue", "green", "gold")), main="Life Satisfaction", xlab="Life Satisfaction", xlim = c(), ylim = c(2, 9 ), yaxs = "i")
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.