Loading the dataset

data.test4 <- read.csv("~/Final_Adult_Study_R_Docs/adult_study011615.csv")
# Load the psych package
library(psych)
items <- grep("PWB[0-9]", names(data.test4), value=TRUE)
scaleKey <- c(-1,-1,-1,-1,-1,1,1,1,-1)
data.test4[,items] <- apply(data.test4[,items], 2, as.numeric)
data.test4$meanPWB <- scoreItems(scaleKey, items = data.test4[, items])$score
library(reshape2); library(car); library(Amelia);library(mitools);library(nlme);library(predictmeans)
## 
## Attaching package: 'car'
## 
## The following object is masked from 'package:psych':
## 
##     logit
## 
## Loading required package: Rcpp
## ## 
## ## Amelia II: Multiple Imputation
## ## (Version 1.7.3, built: 2014-11-14)
## ## Copyright (C) 2005-2015 James Honaker, Gary King and Matthew Blackwell
## ## Refer to http://gking.harvard.edu/amelia/ for more information
## ## 
## Loading required package: lme4
## Loading required package: Matrix
## 
## Attaching package: 'lme4'
## 
## The following object is masked from 'package:nlme':
## 
##     lmList
#Remove the meanPWB and ID Group and wave from data.test4 and create a new #dataset with only those variables.
data <- data.test4[,c("ID", "GROUP", "wave", "meanPWB")]
#Use dcast to cnage from long-format data to wide format data
data <- dcast(data, ID + GROUP ~ wave, mean, value.var = "meanPWB")
# Changing all NaNs to NA
data[,3:5] <- apply(data[,3:5],2,function(x) recode(x, "NaN = NA") )

Unsing the mapply function we create a new data set with ID Group baseline meanPWB and wave 2 and 3 of meanPWB. So we have a Baseline, which is Time 1 (placed in column 3 one on top of the other) to compare to both Time 2 and 3 (placed in column 4 one on top of the other). In the next line of code we then create a separate column called “wave” which calls the first 89 (which compares Time 2 to Baseline) “wave 1” and then the second 89 we call “wave 2” which compares Time 3 to Baseline. In the third line of code we add names to the new columns of the dataset.

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", "meanPWB", "WAVE")

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

Make GROUP and ID a factor

data2$GROUP <-as.factor(data2$GROUP)
data2$ID <-as.factor(data2$ID)

Imputing missing data

MI <- amelia(data2, 50, idvars = c("ID"), ords = "GROUP")
## -- Imputation 1 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 2 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 3 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 4 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 5 --
## 
##   1  2  3  4  5  6  7  8  9 10
## 
## -- Imputation 6 --
## 
##   1  2  3  4  5  6  7
## 
## -- Imputation 7 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 8 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 9 --
## 
##   1  2  3  4  5  6  7  8  9 10 11 12 13 14
## 
## -- Imputation 10 --
## 
##   1  2  3  4  5  6  7  8  9
## 
## -- Imputation 11 --
## 
##   1  2  3  4  5  6  7
## 
## -- Imputation 12 --
## 
##   1  2  3  4  5  6  7  8  9 10 11
## 
## -- Imputation 13 --
## 
##   1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
## 
## -- Imputation 14 --
## 
##   1  2  3  4  5  6  7  8  9
## 
## -- Imputation 15 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 16 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 17 --
## 
##   1  2  3  4  5  6  7  8  9 10
## 
## -- Imputation 18 --
## 
##   1  2  3  4  5  6  7  8  9 10
## 
## -- Imputation 19 --
## 
##   1  2  3  4  5  6  7  8  9
## 
## -- Imputation 20 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 21 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 22 --
## 
##   1  2  3  4  5  6  7  8  9
## 
## -- Imputation 23 --
## 
##   1  2  3  4  5  6  7
## 
## -- Imputation 24 --
## 
##   1  2  3  4  5  6  7  8  9 10 11 12
## 
## -- Imputation 25 --
## 
##   1  2  3  4  5  6  7  8  9 10
## 
## -- Imputation 26 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 27 --
## 
##   1  2  3  4  5  6  7
## 
## -- Imputation 28 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 29 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 30 --
## 
##   1  2  3  4  5  6  7  8  9 10 11
## 
## -- Imputation 31 --
## 
##   1  2  3  4  5  6  7  8  9
## 
## -- Imputation 32 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 33 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 34 --
## 
##   1  2  3  4  5  6  7  8  9 10
## 
## -- Imputation 35 --
## 
##   1  2  3  4  5  6  7  8  9
## 
## -- Imputation 36 --
## 
##   1  2  3  4  5  6  7
## 
## -- Imputation 37 --
## 
##   1  2  3  4  5  6  7  8  9
## 
## -- Imputation 38 --
## 
##   1  2  3  4  5  6  7  8  9 10
## 
## -- Imputation 39 --
## 
##   1  2  3  4  5  6  7
## 
## -- Imputation 40 --
## 
##   1  2  3  4  5  6  7  8  9 10
## 
## -- Imputation 41 --
## 
##   1  2  3  4  5  6  7  8  9
## 
## -- Imputation 42 --
## 
##   1  2  3  4  5  6  7  8
## 
## -- Imputation 43 --
## 
##   1  2  3  4  5  6
## 
## -- Imputation 44 --
## 
##   1  2  3  4  5  6  7  8  9 10
## 
## -- Imputation 45 --
## 
##   1  2  3  4  5  6  7  8  9 10 11
## 
## -- Imputation 46 --
## 
##   1  2  3  4  5  6  7  8  9 10
## 
## -- Imputation 47 --
## 
##   1  2  3  4  5  6  7  8  9 10 11 12 13
## 
## -- Imputation 48 --
## 
##   1  2  3  4  5  6  7  8  9 10 11 12
## 
## -- Imputation 49 --
## 
##   1  2  3  4  5  6  7
## 
## -- Imputation 50 --
## 
##   1  2  3  4  5  6  7

Creating new dataset with missing data imputed

data(MI$imputations)
## Warning in data(MI$imputations): data set 'MI$imputations' not found
allimplogreg<-lapply(MI$imputations,function(X) {lme(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = X, method = "ML", na.action = "na.omit")})
betas<-MIextract(allimplogreg, fun=fixef)
vars<-MIextract(allimplogreg, fun=vcov)
summary(MIcombine(betas,vars))
## Multiple imputation results:
##       MIcombine.default(betas, vars)
##               results         se     (lower    upper) missInfo
## (Intercept) 1.7938791 0.35404056  1.0967536 2.4910046     44 %
## GROUP1      0.2152451 0.27216593 -0.3190618 0.7495520     26 %
## WAVE        0.1375976 0.12567007 -0.1093064 0.3845015     32 %
## BASELINE    0.5855195 0.06687102  0.4536008 0.7174383     52 %
## GROUP1:WAVE 0.1096885 0.17834004 -0.2408486 0.4602256     34 %

Check results with Imputations using Zelig

library("Zelig")
## Loading required package: boot
## 
## Attaching package: 'boot'
## 
## The following object is masked from 'package:car':
## 
##     logit
## 
## The following object is masked from 'package:psych':
## 
##     logit
## 
## Loading required package: MASS
## Loading required package: sandwich
## ZELIG (Versions 4.2-1, built: 2013-09-12)
## 
## +----------------------------------------------------------------+
## |  Please refer to http://gking.harvard.edu/zelig for full       |
## |  documentation or help.zelig() for help with commands and      |
## |  models support by Zelig.                                      |
## |                                                                |
## |  Zelig project citations:                                      |
## |    Kosuke Imai, Gary King, and Olivia Lau.  (2009).            |
## |    ``Zelig: Everyone's Statistical Software,''                 |
## |    http://gking.harvard.edu/zelig                              |
## |   and                                                          |
## |    Kosuke Imai, Gary King, and Olivia Lau. (2008).             |
## |    ``Toward A Common Framework for Statistical Analysis        |
## |    and Development,'' Journal of Computational and             |
## |    Graphical Statistics, Vol. 17, No. 4 (December)             |
## |    pp. 892-913.                                                |
## |                                                                |
## |   To cite individual Zelig models, please use the citation     |
## |   format printed with each model run and in the documentation. |
## +----------------------------------------------------------------+
## 
## 
## 
## Attaching package: 'Zelig'
## 
## The following objects are masked from 'package:psych':
## 
##     alpha, describe, sim
## 
## The following object is masked from 'package:utils':
## 
##     cite
zelig.fit <- zelig(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = MI$imputations,  model = "ls", cite = FALSE)
summary(zelig.fit)
## 
##   Model: ls
##   Number of multiply imputed data sets: 50 
## 
## Combined results:
## 
## Call:
## lm(formula = formula, weights = weights, model = F, data = data)
## 
## Coefficients:
##                 Value Std. Error    t-stat      p-value
## (Intercept) 1.7881850 0.35511025 5.0355772 8.887242e-07
## GROUP1      0.2155407 0.29248363 0.7369326 4.613380e-01
## WAVE        0.1375976 0.13661689 1.0071782 3.141965e-01
## BASELINE    0.5868059 0.06467635 9.0729596 3.460875e-16
## GROUP1:WAVE 0.1096862 0.19328807 0.5674753 5.706082e-01
## 
## For combined results from datasets i to j, use summary(x, subset = i:j).
## For separate results, use print(summary(x), subset = i:j).

Check assumptions with Random Computations

data1=MI$imputations[[1]]
library("Zelig")
zelig.fitdata1 <- zelig(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data1,  model = "ls", cite = FALSE)
summary(zelig.fitdata1)
## 
## Call:
## lm(formula = formula, weights = weights, model = F, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.8441 -0.3056  0.0058  0.3757  1.4487 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   2.0968     0.2774   7.557 2.29e-12 ***
## GROUP1        0.3476     0.2694   1.290    0.199    
## WAVE          0.1351     0.1224   1.103    0.271    
## BASELINE      0.5404     0.0449  12.036  < 2e-16 ***
## GROUP1:WAVE  -0.1098     0.1703  -0.645    0.520    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5677 on 173 degrees of freedom
## Multiple R-squared:  0.4591, Adjusted R-squared:  0.4466 
## F-statistic: 36.71 on 4 and 173 DF,  p-value: < 2.2e-16

Describe the meanPWB variable by the GROUP variable

describeBy(data1[,3:4], group = data1$GROUP)
## group: 0
##          vars  n mean   sd median trimmed  mad  min max range  skew
## BASELINE    1 86 4.43 0.97   4.67    4.47 0.82 2.33 6.0  3.67 -0.43
## meanPWB     2 86 4.69 0.85   4.80    4.73 0.80 2.44 6.4  3.95 -0.46
##          kurtosis   se
## BASELINE    -0.75 0.10
## meanPWB     -0.37 0.09
## -------------------------------------------------------- 
## group: 1
##          vars  n mean   sd median trimmed  mad  min  max range  skew
## BASELINE    1 92 4.22 0.94   4.06    4.25 0.91 1.00 6.49  5.49 -0.56
## meanPWB     2 92 4.76 0.68   4.87    4.81 0.53 2.89 6.10  3.21 -0.56
##          kurtosis   se
## BASELINE     1.33 0.10
## meanPWB      0.10 0.07

Create a plot that visualizes meanPWB variable by the GROUP variable

library(ggplot2)
## 
## Attaching package: 'ggplot2'
## 
## The following object is masked from 'package:psych':
## 
##     %+%
library(influence.ME)
## 
## Attaching package: 'influence.ME'
## 
## The following object is masked from 'package:stats':
## 
##     influence

Take a look at the residuals

residual <- lm(meanPWB ~ BASELINE, data=data1)$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(data1$meanPWB)) 
sel2 <- which(!is.na(data1$BASELINE))
data1$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, meanPWB, data=data1, geom="boxplot")

Plot of the difference between intervention and control groups.

qplot(GROUP, residual, data=data1, geom="boxplot")

Two way repeated measures ======================================================== Graphing the Two-Way Interaction. Both meanPWB and the Residuals

# Load the nlme package
library(nlme)
with(data1, boxplot(meanPWB ~ WAVE + GROUP))

with(data1, boxplot(residual ~ WAVE + GROUP))
Linear Mixed-Effects Model

Comparing Basline to Wave 2 and 3 by Group.

fullModeldata1 <- lme(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data1, method = "ML", na.action = "na.omit")

Cooks Distence

CookD(fullModeldata1)

Plot Cook’s distance:

plot(fullModeldata1, which="cook")
Check results on this random Imputation model
Results

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(fullModeldata1)
## Linear mixed-effects model fit by maximum likelihood
##  Data: data1 
##        AIC      BIC   logLik
##   307.1821 329.4545 -146.591
## 
## Random effects:
##  Formula: ~1 | ID
##         (Intercept)  Residual
## StdDev:   0.2747064 0.4875913
## 
## Fixed effects: meanPWB ~ GROUP * WAVE + BASELINE 
##                  Value  Std.Error DF   t-value p-value
## (Intercept)  2.0994726 0.28145720 87  7.459296  0.0000
## GROUP1       0.3474915 0.24208118 87  1.435434  0.1547
## WAVE         0.1350700 0.10666542 86  1.266296  0.2088
## BASELINE     0.5398528 0.04999342 86 10.798477  0.0000
## GROUP1:WAVE -0.1097702 0.14837071 86 -0.739838  0.4614
##  Correlation: 
##             (Intr) GROUP1 WAVE   BASELI
## GROUP1      -0.472                     
## WAVE        -0.568  0.661              
## BASELINE    -0.786  0.036  0.000       
## GROUP1:WAVE  0.404 -0.919 -0.719  0.006
## 
## Standardized Within-Group Residuals:
##         Min          Q1         Med          Q3         Max 
## -2.49627900 -0.47386962 -0.03524971  0.67464629  2.60808061 
## 
## Number of Observations: 178
## Number of Groups: 89

Another random imputation

data10=MI$imputations[[10]]
library("Zelig")
zelig.fitdata10 <- zelig(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data10,  model = "ls", cite = FALSE)
summary(zelig.fitdata10)
## 
## Call:
## lm(formula = formula, weights = weights, model = F, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.72751 -0.25471 -0.00669  0.27639  1.23293 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.67914    0.25246   6.651 3.67e-10 ***
## GROUP1       0.24324    0.24377   0.998    0.320    
## WAVE         0.18131    0.11080   1.636    0.104    
## BASELINE     0.59912    0.04107  14.589  < 2e-16 ***
## GROUP1:WAVE  0.04371    0.15413   0.284    0.777    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5138 on 173 degrees of freedom
## Multiple R-squared:  0.5646, Adjusted R-squared:  0.5545 
## F-statistic: 56.08 on 4 and 173 DF,  p-value: < 2.2e-16

Describe the meanPWB variable by the GROUP variable

describeBy(data10[,3:4], group = data10$GROUP)
## group: 0
##          vars  n mean   sd median trimmed  mad  min  max range  skew
## BASELINE    1 86 4.43 0.97   4.67    4.47 0.82 2.33 6.00  3.67 -0.43
## meanPWB     2 86 4.60 0.84   4.76    4.65 0.85 2.21 6.08  3.87 -0.59
##          kurtosis   se
## BASELINE    -0.75 0.10
## meanPWB     -0.10 0.09
## -------------------------------------------------------- 
## group: 1
##          vars  n mean   sd median trimmed  mad  min  max range  skew
## BASELINE    1 92 4.21 0.92   4.04    4.25 0.93 1.00 5.71  4.71 -0.68
## meanPWB     2 92 4.78 0.69   4.89    4.82 0.66 2.85 6.21  3.36 -0.52
##          kurtosis   se
## BASELINE     1.31 0.10
## meanPWB      0.10 0.07

Create a plot that visualizes meanPWB variable by the GROUP variable

library(ggplot2)
library(influence.ME)

Take a look at the residuals

residual <- lm(meanPWB ~ BASELINE, data=data10)$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(data10$meanPWB)) 
sel2 <- which(!is.na(data10$BASELINE))
data10$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, meanPWB, data=data10, geom="boxplot")

Plot of the difference between intervention and control groups.

qplot(GROUP, residual, data=data10, geom="boxplot")

Two way repeated measures ======================================================== Graphing the Two-Way Interaction. Both meanPWB and the Residuals

# Load the nlme package
library(nlme)
with(data10, boxplot(meanPWB ~ WAVE + GROUP))

with(data10, boxplot(residual ~ WAVE + GROUP))
Linear Mixed-Effects Model

Comparing Basline to Wave 2 and 3 by Group.

fullModeldata10 <- lme(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data10, method = "ML", na.action = "na.omit")

Cooks Distence

CookD(fullModeldata10)

Plot Cook’s distance:

plot(fullModeldata10, which="cook")
Check results on this random Imputation model
Results

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(fullModeldata10)
## Linear mixed-effects model fit by maximum likelihood
##  Data: data10 
##       AIC      BIC    logLik
##   274.319 296.5915 -130.1595
## 
## Random effects:
##  Formula: ~1 | ID
##         (Intercept)  Residual
## StdDev:   0.2098766 0.4609743
## 
## Fixed effects: meanPWB ~ GROUP * WAVE + BASELINE 
##                 Value  Std.Error DF   t-value p-value
## (Intercept) 1.6851381 0.25508932 87  6.606071  0.0000
## GROUP1      0.2430386 0.22643150 87  1.073343  0.2861
## WAVE        0.1813088 0.10084269 86  1.797937  0.0757
## BASELINE    0.5977629 0.04438237 86 13.468477  0.0000
## GROUP1:WAVE 0.0436472 0.14028306 86  0.311137  0.7564
##  Correlation: 
##             (Intr) GROUP1 WAVE   BASELI
## GROUP1      -0.481                     
## WAVE        -0.593  0.668              
## BASELINE    -0.770  0.029  0.000       
## GROUP1:WAVE  0.415 -0.929 -0.719  0.014
## 
## Standardized Within-Group Residuals:
##         Min          Q1         Med          Q3         Max 
## -2.90010617 -0.49999338  0.02029593  0.62087109  2.39451689 
## 
## Number of Observations: 178
## Number of Groups: 89

Another random imputation

data15=MI$imputations[[15]]
library("Zelig")
zelig.fitdata15 <- zelig(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data15,  model = "ls", cite = FALSE)
summary(zelig.fitdata15)
## 
## Call:
## lm(formula = formula, weights = weights, model = F, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.76742 -0.29389  0.05508  0.35141  1.46656 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.57432    0.28018   5.619 7.55e-08 ***
## GROUP1       0.47637    0.26908   1.770   0.0784 .  
## WAVE         0.22446    0.12228   1.836   0.0681 .  
## BASELINE     0.61555    0.04581  13.436  < 2e-16 ***
## GROUP1:WAVE -0.08889    0.17009  -0.523   0.6019    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.567 on 173 degrees of freedom
## Multiple R-squared:  0.5228, Adjusted R-squared:  0.5118 
## F-statistic: 47.39 on 4 and 173 DF,  p-value: < 2.2e-16

Describe the meanPWB variable by the GROUP variable

describeBy(data15[,3:4], group = data15$GROUP)
## group: 0
##          vars  n mean   sd median trimmed  mad  min  max range  skew
## BASELINE    1 86 4.43 0.97   4.67    4.47 0.82 2.33 6.00  3.67 -0.43
## meanPWB     2 86 4.64 0.85   4.78    4.67 0.82 2.44 6.44  4.00 -0.37
##          kurtosis   se
## BASELINE    -0.75 0.10
## meanPWB     -0.38 0.09
## -------------------------------------------------------- 
## group: 1
##          vars  n mean   sd median trimmed  mad  min  max range  skew
## BASELINE    1 92 4.17 0.90   4.00    4.21 0.99 1.00 5.56  4.56 -0.68
## meanPWB     2 92 4.82 0.77   4.89    4.84 0.66 2.54 6.57  4.03 -0.24
##          kurtosis   se
## BASELINE     1.46 0.09
## meanPWB      0.43 0.08

Create a plot that visualizes meanPWB variable by the GROUP variable

library(ggplot2)
library(influence.ME)

Take a look at the residuals

residual <- lm(meanPWB ~ BASELINE, data=data15)$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(data15$meanPWB)) 
sel2 <- which(!is.na(data15$BASELINE))
data15$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, meanPWB, data=data15, geom="boxplot")

Plot of the difference between intervention and control groups.

qplot(GROUP, residual, data=data15, geom="boxplot")

Two way repeated measures ======================================================== Graphing the Two-Way Interaction. Both meanPWB and the Residuals

# Load the nlme package
library(nlme)
with(data15, boxplot(meanPWB ~ WAVE + GROUP))

with(data15, boxplot(residual ~ WAVE + GROUP))
Linear Mixed-Effects Model

Comparing Basline to Wave 2 and 3 by Group.

fullModeldata15 <- lme(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data15, method = "ML", na.action = "na.omit")

Cooks Distence

CookD(fullModeldata15)

Plot Cook’s distance:

plot(fullModeldata15, which="cook")
Check results on this random Imputation model
Results

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(fullModeldata15)
## Linear mixed-effects model fit by maximum likelihood
##  Data: data15 
##       AIC      BIC   logLik
##   309.902 332.1745 -147.951
## 
## Random effects:
##  Formula: ~1 | ID
##         (Intercept)  Residual
## StdDev:   0.2198593 0.5138937
## 
## Fixed effects: meanPWB ~ GROUP * WAVE + BASELINE 
##                  Value  Std.Error DF   t-value p-value
## (Intercept)  1.5785872 0.28310353 87  5.576007  0.0000
## GROUP1       0.4761720 0.25192737 87  1.890116  0.0621
## WAVE         0.2244625 0.11241933 86  1.996654  0.0490
## BASELINE     0.6145842 0.04918392 86 12.495632  0.0000
## GROUP1:WAVE -0.0889281 0.15638116 86 -0.568662  0.5711
##  Correlation: 
##             (Intr) GROUP1 WAVE   BASELI
## GROUP1      -0.490                     
## WAVE        -0.596  0.669              
## BASELINE    -0.769  0.040  0.000       
## GROUP1:WAVE  0.420 -0.931 -0.719  0.011
## 
## Standardized Within-Group Residuals:
##        Min         Q1        Med         Q3        Max 
## -2.7142323 -0.5299018  0.0565178  0.6205942  2.7106416 
## 
## Number of Observations: 178
## Number of Groups: 89

Another random imputation

data25=MI$imputations[[25]]

library("Zelig")
zelig.fitdata25 <- zelig(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data25,  model = "ls", cite = FALSE)
summary(zelig.fitdata25)
## 
## Call:
## lm(formula = formula, weights = weights, model = F, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.7215 -0.3597 -0.0036  0.3466  1.7645 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.09192    0.28929   7.231 1.48e-11 ***
## GROUP1      -0.07913    0.27818  -0.284   0.7764    
## WAVE         0.10790    0.12640   0.854   0.3945    
## BASELINE     0.52263    0.04725  11.061  < 2e-16 ***
## GROUP1:WAVE  0.33038    0.17582   1.879   0.0619 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5861 on 173 degrees of freedom
## Multiple R-squared:  0.4597, Adjusted R-squared:  0.4472 
## F-statistic:  36.8 on 4 and 173 DF,  p-value: < 2.2e-16

Describe the meanPWB variable by the GROUP variable

describeBy(data25[,3:4], group = data25$GROUP)
## group: 0
##          vars  n mean   sd median trimmed  mad  min max range  skew
## BASELINE    1 86 4.43 0.97   4.67    4.47 0.82 2.33   6  3.67 -0.43
## meanPWB     2 86 4.57 0.81   4.72    4.62 0.91 2.44   6  3.56 -0.51
##          kurtosis   se
## BASELINE    -0.75 0.10
## meanPWB     -0.35 0.09
## -------------------------------------------------------- 
## group: 1
##          vars  n mean   sd median trimmed  mad  min  max range  skew
## BASELINE    1 92 4.20 0.90   4.06    4.25 0.97 1.00 5.56  4.56 -0.76
## meanPWB     2 92 4.87 0.74   4.89    4.89 0.74 2.89 6.69  3.80 -0.28
##          kurtosis   se
## BASELINE     1.48 0.09
## meanPWB     -0.29 0.08

Create a plot that visualizes meanPWB variable by the GROUP variable

library(ggplot2)
library(influence.ME)

Take a look at the residuals

residual <- lm(meanPWB ~ BASELINE, data=data25)$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(data25$meanPWB)) 
sel2 <- which(!is.na(data25$BASELINE))
data25$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, meanPWB, data=data25, geom="boxplot")

Plot of the difference between intervention and control groups.

qplot(GROUP, residual, data=data25, geom="boxplot")

Two way repeated measures ======================================================== Graphing the Two-Way Interaction. Both meanPWB and the Residuals

# Load the nlme package
library(nlme)
with(data25, boxplot(meanPWB ~ WAVE + GROUP))

with(data25, boxplot(residual ~ WAVE + GROUP))
Linear Mixed-Effects Model

Comparing Basline to Wave 2 and 3 by Group.

fullModeldata25 <- lme(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data25, method = "ML", na.action = "na.omit")

Cooks Distence

CookD(fullModeldata25)

Plot Cook’s distance:

plot(fullModeldata25, which="cook")
Check results on this random Imputation model
Results

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(fullModeldata25)
## Linear mixed-effects model fit by maximum likelihood
##  Data: data25 
##        AIC      BIC    logLik
##   323.0047 345.2772 -154.5023
## 
## Random effects:
##  Formula: ~1 | ID
##         (Intercept)  Residual
## StdDev:   0.1813425 0.5486156
## 
## Fixed effects: meanPWB ~ GROUP * WAVE + BASELINE 
##                  Value  Std.Error DF   t-value p-value
## (Intercept)  2.0915443 0.29127854 87  7.180565  0.0000
## GROUP1      -0.0791139 0.26702377 87 -0.296280  0.7677
## WAVE         0.1079049 0.12001509 86  0.899095  0.3711
## BASELINE     0.5227150 0.04952083 86 10.555458  0.0000
## GROUP1:WAVE  0.3303799 0.16693736 86  1.979065  0.0510
##  Correlation: 
##             (Intr) GROUP1 WAVE   BASELI
## GROUP1      -0.503                     
## WAVE        -0.618  0.674              
## BASELINE    -0.753  0.039  0.000       
## GROUP1:WAVE  0.442 -0.938 -0.719  0.003
## 
## Standardized Within-Group Residuals:
##         Min          Q1         Med          Q3         Max 
## -2.70741008 -0.59266401  0.01572838  0.60798136  2.93787964 
## 
## Number of Observations: 178
## Number of Groups: 89

Check assumptions on model without any imputations

Describe the meanPWB 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.43 0.97   4.67    4.47 0.82 2.33   6  3.67 -0.43
## meanPWB     2 59 4.69 0.82   4.78    4.75 0.82 2.44   6  3.56 -0.70
##          kurtosis   se
## BASELINE    -0.75 0.10
## meanPWB      0.03 0.11
## -------------------------------------------------------- 
## group: 1
##          vars  n mean   sd median trimmed  mad  min  max range  skew
## BASELINE    1 88 4.17 0.91   4.00    4.21 0.99 1.00 5.56  4.56 -0.69
## meanPWB     2 54 4.88 0.64   4.94    4.93 0.49 2.89 6.00  3.11 -0.83
##          kurtosis   se
## BASELINE     1.41 0.10
## meanPWB      0.69 0.09

Create a plot that visualizes meanPWB variable by the GROUP variable

library(ggplot2)
library(influence.ME)

Take a look at the residuals

residual <- lm(meanPWB ~ 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$meanPWB)) 
sel2 <- which(!is.na(data2$BASELINE))
data2$residual[intersect(sel1,sel2)] <- residual
qplot(GROUP, meanPWB, 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 meanPWB and the Residuals

# Load the nlme package
library(nlme)
with(data2, boxplot(meanPWB ~ WAVE + GROUP))

with(data2, boxplot(residual ~ WAVE + GROUP))
Linear Mixed-Effects Model

Comparing Basline to Wave 2 and 3 by Group.

fullModel <- lme(meanPWB ~ GROUP * WAVE + BASELINE, random = ~1 | ID, data = data2, method = "ML", na.action = "na.omit")

Cooks Distence

CookD(fullModel)

Plot Cook’s distance:

plot(fullModel, which="cook")
Results on Model with data that contains no imputations
Results

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
##   176.3976 195.237 -81.19878
## 
## Random effects:
##  Formula: ~1 | ID
##         (Intercept)  Residual
## StdDev:   0.4091197 0.3619596
## 
## Fixed effects: meanPWB ~ GROUP * WAVE + BASELINE 
##                 Value Std.Error DF  t-value p-value
## (Intercept) 1.8931604 0.3810992 66 4.967632  0.0000
## GROUP1      0.0601033 0.2504277 66 0.240003  0.8111
## WAVE        0.0681424 0.1066892 38 0.638700  0.5268
## BASELINE    0.5790232 0.0749272 66 7.727815  0.0000
## GROUP1:WAVE 0.2198848 0.1559119 38 1.410314  0.1666
##  Correlation: 
##             (Intr) GROUP1 WAVE   BASELI
## GROUP1      -0.380                     
## WAVE        -0.383  0.585              
## BASELINE    -0.896  0.090 -0.002       
## GROUP1:WAVE  0.258 -0.860 -0.684  0.007
## 
## Standardized Within-Group Residuals:
##         Min          Q1         Med          Q3         Max 
## -2.29290462 -0.47354223  0.08279482  0.52335417  1.88597819 
## 
## Number of Observations: 109
## Number of Groups: 69
Table with P-value

|             |      Value|  Std.Error|  DF|    t-value|    p-value|
|:------------|----------:|----------:|---:|----------:|----------:|
|(Intercept)  |  1.8931604|  0.3810992|  66|  4.9676319|  0.0000051|
|GROUP1       |  0.0601033|  0.2504277|  66|  0.2400027|  0.8110720|
|WAVE         |  0.0681424|  0.1066892|  38|  0.6387000|  0.5268493|
|BASELINE     |  0.5790232|  0.0749272|  66|  7.7278147|  0.0000000|
|GROUP1:WAVE  |  0.2198848|  0.1559119|  38|  1.4103142|  0.1665805|

Table with confidence intervals

est. lower upper
(Intercept) 1.8931604 1.1499278 2.6363930
GROUP1 0.0601033 -0.4282893 0.5484960
WAVE 0.0681424 -0.1428268 0.2791116
BASELINE 0.5790232 0.4328977 0.7251487
GROUP1:WAVE 0.2198848 -0.0884182 0.5281878