#Loading the dataset that has been reset into a long version
data.test4 <- read.csv("/Volumes/TOSHIBA EXT/Dropbox/ADULT STUDY/adult_study011615.csv")
#Creating a new variable that is the mean of all positive purpose MLQ questions
data.test4$MLQP <- apply(data.test4[, c("MLQ1" ,"MLQ4", "MLQ5", "MLQ6")], 1, mean, na.rm = TRUE)
library(reshape2); library(car)
## Warning: package 'car' was built under R version 3.1.2
data <- data.test4[,c("ID", "GROUP", "wave", "MLQP")]
data <- dcast(data, ID + GROUP ~ wave, mean, value.var = "MLQP")
data[,3:5] <- apply(data[,3:5],2,function(x) recode(x, "NaN = NA") )
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", "MLQP", "WAVE")
Drop the cases where participants did not complete the intervention complety
#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)
Load the psych package
library(psych)
##
## Attaching package: 'psych'
##
## The following object is masked from 'package:car':
##
## logit
Describe the MLQ 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.67 1.21 4.5 4.72 1.11 1.5 7 5.5 -0.35
## MLQP 2 59 5.05 1.14 5.0 5.07 1.11 2.5 7 4.5 -0.21
## kurtosis se
## BASELINE -0.10 0.13
## MLQP -0.59 0.15
## --------------------------------------------------------
## group: 1
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 88 4.47 1.45 4.5 4.47 1.48 2.00 7 5.00 0.04
## MLQP 2 54 5.67 1.05 6.0 5.74 1.11 3.25 7 3.75 -0.49
## kurtosis se
## BASELINE -0.99 0.16
## MLQP -0.78 0.14
Create a plot that visualizes MLQ 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(MLQP ~ 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$MLQP))
sel2 <- which(!is.na(data2$BASELINE))
data2$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, MLQP, 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).
# Load the nlme package
library(nlme)
Two way repeated measures Graphing the Two-Way Interaction.
with(data2, boxplot(MLQP ~ WAVE + GROUP))
with(data2, boxplot(residual ~ WAVE + GROUP))
Comparing Basline to Wave 2 and 3 by Group.
fullModel <- lme(MLQP ~ 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
## 250.6 269.4 -118.3
##
## Random effects:
## Formula: ~1 | ID
## (Intercept) Residual
## StdDev: 0.5354 0.5331
##
## Fixed effects: MLQP ~ GROUP * WAVE + BASELINE
## Value Std.Error DF t-value p-value
## (Intercept) 1.6704 0.4045 66 4.129 0.0001
## GROUP1 1.0237 0.3592 66 2.850 0.0058
## WAVE 0.1302 0.1558 38 0.835 0.4089
## BASELINE 0.6389 0.0684 66 9.340 0.0000
## GROUP1:WAVE -0.1935 0.2279 38 -0.849 0.4013
## Correlation:
## (Intr) GROUP1 WAVE BASELI
## GROUP1 -0.418
## WAVE -0.500 0.596
## BASELINE -0.798 0.012 -0.038
## GROUP1:WAVE 0.334 -0.878 -0.684 0.035
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -2.81113 -0.40934 0.03359 0.38633 1.99723
##
## Number of Observations: 109
## Number of Groups: 69
Table with P-values
| Value | Std.Error | DF | t-value | p-value | |
|---|---|---|---|---|---|
| (Intercept) | 1.6704 | 0.4045 | 66.0000 | 4.1292 | 0.0001 |
| GROUP1 | 1.0237 | 0.3592 | 66.0000 | 2.8502 | 0.0058 |
| WAVE | 0.1302 | 0.1558 | 38.0000 | 0.8352 | 0.4089 |
| BASELINE | 0.6389 | 0.0684 | 66.0000 | 9.3404 | 0.0000 |
| GROUP1:WAVE | -0.1935 | 0.2279 | 38.0000 | -0.8488 | 0.4013 |
``` Table with confidence intervals
| est. | lower | upper | |
|---|---|---|---|
| (Intercept) | 1.6704 | 0.8815 | 2.4594 |
| GROUP1 | 1.0237 | 0.3232 | 1.7241 |
| WAVE | 0.1302 | -0.1780 | 0.4383 |
| BASELINE | 0.6389 | 0.5055 | 0.7723 |
| GROUP1:WAVE | -0.1935 | -0.6442 | 0.2572 |