data.test4$meanAPSI <- apply(data.test4[, c("APSI1", "APSI7")], 1, mean, na.rm = TRUE)
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", "meanAPSI")]
data <- dcast(data, ID + GROUP ~ wave, mean, value.var = "meanAPSI")
data[,3:5] <- apply(data[,3:5],2,function(x) recode(x, "NaN = NA") )
Create new data set with ID Group basline meanAPSI 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", "meanAPSI", "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 meanAPSI 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.48 0.95 3.5 3.53 0.74 1.0 5 4.0 -0.55
## meanAPSI 2 59 3.72 0.90 4.0 3.78 0.74 1.5 5 3.5 -0.42
## kurtosis se
## BASELINE -0.25 0.10
## meanAPSI -0.50 0.12
## --------------------------------------------------------
## group: 1
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 88 3.15 1.19 3.5 3.18 1.48 1.0 5 4.0 -0.29
## meanAPSI 2 54 4.01 0.70 4.0 4.06 0.74 2.5 5 2.5 -0.48
## kurtosis se
## BASELINE -0.86 0.13
## meanAPSI -0.41 0.09
Create a plot that visualizes meanAPSI 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(meanAPSI ~ 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$meanAPSI))
sel2 <- which(!is.na(data2$BASELINE))
data2$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, meanAPSI, 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 meanAPSI and the Residuals
# Load the nlme package
library(nlme)
with(data2, boxplot(meanAPSI ~ WAVE + GROUP))
with(data2, boxplot(residual ~ WAVE + GROUP))
Comparing Basline to Wave 2 and 3 by Group.
fullModel <- lme(meanAPSI ~ 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
## 180.4 199.3 -83.21
##
## Random effects:
## Formula: ~1 | ID
## (Intercept) Residual
## StdDev: 0.4397 0.3543
##
## Fixed effects: meanAPSI ~ GROUP * WAVE + BASELINE
## Value Std.Error DF t-value p-value
## (Intercept) 1.4920 0.2808 66 5.313 0.0000
## GROUP1 0.5879 0.2506 66 2.346 0.0220
## WAVE 0.0955 0.1052 38 0.908 0.3696
## BASELINE 0.5705 0.0636 66 8.971 0.0000
## GROUP1:WAVE -0.0327 0.1536 38 -0.213 0.8328
## Correlation:
## (Intr) GROUP1 WAVE BASELI
## GROUP1 -0.493
## WAVE -0.495 0.573
## BASELINE -0.798 0.108 -0.024
## GROUP1:WAVE 0.341 -0.844 -0.685 0.013
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -1.56932 -0.41071 -0.03953 0.48908 1.75156
##
## Number of Observations: 109
## Number of Groups: 69
Table with P-values
| Value | Std.Error | DF | t-value | p-value | |
|---|---|---|---|---|---|
| (Intercept) | 1.4920 | 0.2808 | 66.0000 | 5.3128 | 0.0000 |
| GROUP1 | 0.5879 | 0.2506 | 66.0000 | 2.3456 | 0.0220 |
| WAVE | 0.0955 | 0.1052 | 38.0000 | 0.9081 | 0.3696 |
| BASELINE | 0.5705 | 0.0636 | 66.0000 | 8.9710 | 0.0000 |
| GROUP1:WAVE | -0.0327 | 0.1536 | 38.0000 | -0.2126 | 0.8328 |
``` Table with confidence intervals
| est. | lower | upper | |
|---|---|---|---|
| (Intercept) | 1.4920 | 0.9443 | 2.0397 |
| GROUP1 | 0.5879 | 0.0991 | 1.0767 |
| WAVE | 0.0955 | -0.1125 | 0.3036 |
| BASELINE | 0.5705 | 0.4465 | 0.6945 |
| GROUP1:WAVE | -0.0327 | -0.3364 | 0.2711 |