#Loading the dataset that has been reset into a long version
data.test4 <- read.csv("/Volumes/TOSHIBA EXT/Dropbox/ADULT STUDY/adult_study011615.csv")
# Load the psych package
library(psych)
items <- c("MLQ1" ,"MLQ4", "MLQ5", "MLQ6", "MLQ9")
scaleKey <- c(1, 1, 1,1,-1)
data.test4$meanmlq <- scoreItems(scaleKey, items=data.test4[,items], delete=FALSE)$score
library(reshape2); library(car)
## Warning: package 'car' was built under R version 3.1.2
##
## Attaching package: 'car'
##
## The following object is masked from 'package:psych':
##
## logit
data <- data.test4[,c("ID", "GROUP", "wave", "meanmlq")]
data <- dcast(data, ID + GROUP ~ wave, mean, value.var = "meanmlq")
data[,3:5] <- apply(data[,3:5],2,function(x) recode(x, "NaN = NA") )
Create new data set with ID Group baseline meanmlq and wave so that we have Baseline, time 1 and 2 to compare to
data2 <- as.data.frame(mapply(c,data[,1:4], data[,c(1:3,5)]))
data2$wave <- rep(1:2, each=89)
names(data2) <- c("ID", "GROUP", "BASELINE", "meanmlq", "WAVE")
Drop the cases where participants did not complete the intervention completely
#data2 <- data2[-c(which(data2$GROUP ==2)),]
Intention to treat model (ITT) where we keep the cases who dropped out and did not complete the study (http://en.wikipedia.org/wiki/Intention-to-treat_analysis).
data2[which(data2$GROUP ==2), "GROUP"] <- 1
For lme to work GROUP and ID need to be seen as factors
data2$GROUP <-as.factor(data2$GROUP)
data2$ID <-as.factor(data2$ID)
Describe the meanmlq variable by the GROUP variable
describeBy(data2[,3:4], group = data2$GROUP)
## group: 0
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 86 4.75 1.27 4.8 4.82 1.19 1.4 7 5.6 -0.43
## meanmlq 2 59 5.17 1.15 5.4 5.22 1.19 2.4 7 4.6 -0.45
## kurtosis se
## BASELINE -0.20 0.14
## meanmlq -0.38 0.15
## --------------------------------------------------------
## group: 1
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 88 4.50 1.47 4.4 4.50 1.63 1.8 7 5.2 0.05
## meanmlq 2 54 5.76 1.03 6.0 5.84 0.89 3.8 7 3.2 -0.61
## kurtosis se
## BASELINE -1.05 0.16
## meanmlq -0.82 0.14
Create a plot that visualizes meanmlq variable by the GROUP variable
library(ggplot2)
##
## Attaching package: 'ggplot2'
##
## The following object is masked from 'package:psych':
##
## %+%
Take a look at the residuals
residual <- lm(meanmlq ~ BASELINE, data=data2)$residual
Plot the residuals to see that they are random
plot(density(residual))# A density plot
qqnorm(residual) # A quantile normal plot to checking normality
qqline(residual)
Checking the different between intervention and control groups residuals. This allows us to control for individual unsystematic differences.
data2$residual <- NA
sel1 <- which(!is.na(data2$meanmlq))
sel2 <- which(!is.na(data2$BASELINE))
data2$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, meanmlq, data=data2, geom="boxplot")
## Warning: Removed 65 rows containing non-finite values (stat_boxplot).
Plot of the difference between intervention and control groups.
qplot(GROUP, residual, data=data2, geom="boxplot")
## Warning: Removed 69 rows containing non-finite values (stat_boxplot).
Two way repeated measures ======================================================== Graphing the Two-Way Interaction. Both meanmlq and the Residuals
# Load the nlme package
library(nlme)
with(data2, boxplot(meanmlq ~ WAVE + GROUP))
with(data2, boxplot(residual ~ WAVE + GROUP))
Comparing Basline to Wave 2 and 3 by Group.
fullModel <- lme(meanmlq ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data2, method = "ML", na.action = "na.omit")
Explanation of significance:
We asses the significance of our models by comparing them from the baseline model using the anova() function.
(Intercept): Where everything is 0
GROUP1: Is there a difference between group. If it is significant than there is a difference and the treatment had an effect.
WAVE: Asseses whether the effects gets bigger beteen time 2 and 3 (does not have to be significant)
BASELINE: Should not be significant. If it is then it shows that there is a difference between groups before the treatment.
GROUP1:WAVE: If this is significant then it means that the effect was either fleeting or it happened after the treatment i.e. between time 2 and 3.
summary(fullModel)
## Linear mixed-effects model fit by maximum likelihood
## Data: data2
## AIC BIC logLik
## 240.2 259.1 -113.1
##
## Random effects:
## Formula: ~1 | ID
## (Intercept) Residual
## StdDev: 0.5322 0.4951
##
## Fixed effects: meanmlq ~ GROUP * WAVE + BASELINE
## Value Std.Error DF t-value p-value
## (Intercept) 1.6902 0.3918 66 4.314 0.0001
## GROUP1 0.8796 0.3380 66 2.602 0.0114
## WAVE 0.0773 0.1455 38 0.532 0.5980
## BASELINE 0.6589 0.0654 66 10.071 0.0000
## GROUP1:WAVE -0.0842 0.2126 38 -0.396 0.6941
## Correlation:
## (Intr) GROUP1 WAVE BASELI
## GROUP1 -0.426
## WAVE -0.484 0.590
## BASELINE -0.811 0.037 -0.032
## GROUP1:WAVE 0.333 -0.869 -0.684 0.021
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -2.4086 -0.4441 0.1101 0.3570 2.0486
##
## Number of Observations: 109
## Number of Groups: 69
Table with P-values
| Value | Std.Error | DF | t-value | p-value | |
|---|---|---|---|---|---|
| (Intercept) | 1.6902 | 0.3918 | 66.0000 | 4.3136 | 0.0001 |
| GROUP1 | 0.8796 | 0.3380 | 66.0000 | 2.6023 | 0.0114 |
| WAVE | 0.0773 | 0.1455 | 38.0000 | 0.5317 | 0.5980 |
| BASELINE | 0.6589 | 0.0654 | 66.0000 | 10.0708 | 0.0000 |
| GROUP1:WAVE | -0.0842 | 0.2126 | 38.0000 | -0.3963 | 0.6941 |
``` Table with confidence intervals
| est. | lower | upper | |
|---|---|---|---|
| (Intercept) | 1.6902 | 0.9260 | 2.4543 |
| GROUP1 | 0.8796 | 0.2204 | 1.5388 |
| WAVE | 0.0773 | -0.2103 | 0.3650 |
| BASELINE | 0.6589 | 0.5313 | 0.7865 |
| GROUP1:WAVE | -0.0842 | -0.5046 | 0.3361 |