#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 HAPPI questions
data.test4$HAPPIPERMA17 <- apply(data.test4[, c ("HAPPI1" ,"HAPPI2", "HAPPI3", "PERMA17")], 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", "HAPPIPERMA17")]
data <- dcast(data, ID + GROUP ~ wave, mean, value.var = "HAPPIPERMA17")
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", "HAPPIPERMA17", "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)
Load the psych package
library(psych)
##
## Attaching package: 'psych'
##
## The following object is masked from 'package:car':
##
## logit
Describe the HAPPI 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 5.48 1.38 5.75 5.62 1.11 2.25 7.75 5.5 -0.89
## HAPPIPERMA17 2 59 5.69 1.40 6.00 5.87 1.11 0.75 7.75 7.0 -1.48
## kurtosis se
## BASELINE 0.00 0.15
## HAPPIPERMA17 2.39 0.18
## --------------------------------------------------------
## group: 1
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 88 5.30 1.39 5.38 5.35 1.48 1.75 7.75 6.0 -0.32
## HAPPIPERMA17 2 54 6.22 0.80 6.25 6.24 0.74 4.25 7.75 3.5 -0.26
## kurtosis se
## BASELINE -0.23 0.15
## HAPPIPERMA17 0.10 0.11
Create a plot that visualizes HAPPI 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(HAPPIPERMA17 ~ 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$HAPPIPERMA17))
sel2 <- which(!is.na(data2$BASELINE))
data2$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, HAPPIPERMA17, 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(HAPPIPERMA17 ~ WAVE + GROUP))
with(data2, boxplot(residual ~ WAVE + GROUP))
Comparing Basline to Wave 2 and 3 by Group.
fullModel <- lme(HAPPIPERMA17 ~ 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
## 267.6 286.4 -126.8
##
## Random effects:
## Formula: ~1 | ID
## (Intercept) Residual
## StdDev: 0.7308 0.4814
##
## Fixed effects: HAPPIPERMA17 ~ GROUP * WAVE + BASELINE
## Value Std.Error DF t-value p-value
## (Intercept) 2.5294 0.4799 66 5.271 0.0000
## GROUP1 0.4676 0.3566 66 1.311 0.1944
## WAVE -0.0954 0.1450 38 -0.658 0.5143
## BASELINE 0.5674 0.0752 66 7.548 0.0000
## GROUP1:WAVE 0.1537 0.2114 38 0.727 0.4716
## Correlation:
## (Intr) GROUP1 WAVE BASELI
## GROUP1 -0.344
## WAVE -0.390 0.556
## BASELINE -0.864 0.002 -0.027
## GROUP1:WAVE 0.270 -0.816 -0.686 0.015
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -2.48680 -0.30248 0.07239 0.38320 1.98865
##
## Number of Observations: 109
## Number of Groups: 69
Table with P-values
| Value | Std.Error | DF | t-value | p-value | |
|---|---|---|---|---|---|
| (Intercept) | 2.5294 | 0.4799 | 66.0000 | 5.2707 | 0.0000 |
| GROUP1 | 0.4676 | 0.3566 | 66.0000 | 1.3111 | 0.1944 |
| WAVE | -0.0954 | 0.1450 | 38.0000 | -0.6583 | 0.5143 |
| BASELINE | 0.5674 | 0.0752 | 66.0000 | 7.5479 | 0.0000 |
| GROUP1:WAVE | 0.1537 | 0.2114 | 38.0000 | 0.7272 | 0.4716 |
``` Table with confidence intervals
| est. | lower | upper | |
|---|---|---|---|
| (Intercept) | 2.5294 | 1.5935 | 3.4653 |
| GROUP1 | 0.4676 | -0.2279 | 1.1631 |
| WAVE | -0.0954 | -0.3821 | 0.1912 |
| BASELINE | 0.5674 | 0.4208 | 0.7140 |
| GROUP1:WAVE | 0.1537 | -0.2642 | 0.5716 |