#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 <- grep("GRIT[0-9]*", names(data.test4), value=TRUE)
scaleKey <- c(-1,-1,-1,-1,1,1,1,1)
data.test4[,items] <- apply(data.test4[,items], 2, as.numeric)
data.test4$meanGRIT <- 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", "meanGRIT")]
data <- dcast(data, ID + GROUP ~ wave, mean, value.var = "meanGRIT")
data[,3:5] <- apply(data[,3:5],2,function(x) recode(x, "NaN = NA") )
Create new data set with ID Group baseline meanGRIT 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", "meanGRIT", "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 meanGRIT 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 3.59 0.75 3.62 3.61 0.93 2.00 5 3.00 -0.24
## meanGRIT 2 59 3.72 0.73 3.62 3.74 0.74 1.88 5 3.12 -0.25
## kurtosis se
## BASELINE -0.71 0.08
## meanGRIT -0.52 0.09
## --------------------------------------------------------
## group: 1
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 88 3.37 0.90 3.25 3.42 0.93 1.00 4.88 3.88 -0.46
## meanGRIT 2 54 3.62 0.66 3.75 3.65 0.74 1.25 4.88 3.62 -0.72
## kurtosis se
## BASELINE -0.13 0.10
## meanGRIT 1.35 0.09
Create a plot that visualizes meanGRIT 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(meanGRIT ~ 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$meanGRIT))
sel2 <- which(!is.na(data2$BASELINE))
data2$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, meanGRIT, 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 meanGRIT and the Residuals
# Load the nlme package
library(nlme)
with(data2, boxplot(meanGRIT ~ WAVE + GROUP))
with(data2, boxplot(residual ~ WAVE + GROUP))
Comparing Basline to Wave 2 and 3 by Group.
fullModel <- lme(meanGRIT ~ 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
## 124.8 143.7 -55.41
##
## Random effects:
## Formula: ~1 | ID
## (Intercept) Residual
## StdDev: 0.2563 0.3254
##
## Fixed effects: meanGRIT ~ GROUP * WAVE + BASELINE
## Value Std.Error DF t-value p-value
## (Intercept) 1.1440 0.24863 66 4.601 0.0000
## GROUP1 0.0534 0.21189 66 0.252 0.8020
## WAVE -0.0649 0.09340 38 -0.695 0.4915
## BASELINE 0.7200 0.05652 66 12.740 0.0000
## GROUP1:WAVE 0.0438 0.13679 38 0.320 0.7504
## Correlation:
## (Intr) GROUP1 WAVE BASELI
## GROUP1 -0.456
## WAVE -0.494 0.607
## BASELINE -0.818 0.082 -0.031
## GROUP1:WAVE 0.350 -0.898 -0.682 0.005
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -2.04700 -0.49141 0.02433 0.50910 2.50433
##
## Number of Observations: 109
## Number of Groups: 69
Table with P-values
| Value | Std.Error | DF | t-value | p-value | |
|---|---|---|---|---|---|
| (Intercept) | 1.1440 | 0.2486 | 66.0000 | 4.6013 | 0.0000 |
| GROUP1 | 0.0534 | 0.2119 | 66.0000 | 0.2518 | 0.8020 |
| WAVE | -0.0649 | 0.0934 | 38.0000 | -0.6947 | 0.4915 |
| BASELINE | 0.7200 | 0.0565 | 66.0000 | 12.7395 | 0.0000 |
| GROUP1:WAVE | 0.0438 | 0.1368 | 38.0000 | 0.3204 | 0.7504 |
``` Table with confidence intervals
| est. | lower | upper | |
|---|---|---|---|
| (Intercept) | 1.1440 | 0.6591 | 1.6289 |
| GROUP1 | 0.0534 | -0.3599 | 0.4666 |
| WAVE | -0.0649 | -0.2496 | 0.1198 |
| BASELINE | 0.7200 | 0.6098 | 0.8302 |
| GROUP1:WAVE | 0.0438 | -0.2267 | 0.3143 |