library(arm)
## 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 /Users/levibrackman/Google Drive/R/Emily Griffith
library(lmerTest)
## 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
library(psych)
##
## Attaching package: 'psych'
##
## The following objects are masked from 'package:arm':
##
## logit, rescale, sim
data <- read.csv("EmilyGriffith_all.csv")
data$ID <- data$Q1
# Create scale scores
data$meanSDQ <- apply(data[, c("Q46_1", "Q46_2", "Q46_4", "Q46_5", "Q46_6",
"Q46_7", "Q46_8", "Q46_9", "Q46_10", "Q46_11")], 1, mean, na.rm = TRUE)
# Means or plotting
data$baseline <- ifelse(data$Time < 4, 0, 1)
pdata <- tapply(data[, "meanSDQ"], data[, 3], mean, na.rm = TRUE)
plot(pdata, type = "l")
M0 <- lmer(meanSDQ ~ 1 + (1 | ID), data = data)
fixef(M0)
## (Intercept)
## 5.123
confint(M0)
## Computing profile confidence intervals ...
## 2.5 % 97.5 %
## .sig01 0.000 0.6368
## .sigma 1.021 1.3794
## (Intercept) 4.898 5.3537
M1 <- update(M0, . ~ . + Time, REML = FALSE)
fixef(M1)
## (Intercept) Time
## 3.4143 0.7775
confint(M1)
## Computing profile confidence intervals ...
## 2.5 % 97.5 %
## .sig01 0.0000 0.6280
## .sigma 0.7505 1.0386
## (Intercept) 2.9791 3.8479
## Time 0.6024 0.9527
M2 <- update(M1, . ~ . + baseline)
fixef(M2)
## (Intercept) Time baseline
## 3.002 1.014 -1.034
confint(M2)
## 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.6082
## .sigma 0.7215 0.9993
## (Intercept) 2.5026 3.4995
## Time 0.7861 1.2440
## baseline -1.7125 -0.3610
M3 <- update(M2, . ~ . + I(Time^2))
fixef(M3)
## (Intercept) Time baseline I(Time^2)
## -0.2992 4.8555 2.3002 -0.9619
confint(M3)
## Computing profile confidence intervals ...
## 2.5 % 97.5 %
## .sig01 0.2180 0.6064
## .sigma 0.5762 0.7983
## (Intercept) -1.3416 0.7330
## Time 3.7465 5.9740
## baseline 1.2091 3.3931
## I(Time^2) -1.2372 -0.6886
Pdata <- tapply(data[, "meanSDQ"], 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 = -0.1, max = 0.1)
with(data, plot(TimeJIT, meanSDQ, col = "grey", pch = "*"))
lines(pdata, col = "red", lwd = 2)
You can also embed plots, for example:
```r
plot(cars)