Loading the dataset
data.test4 <- read.csv("/Volumes/TOSHIBA EXT/Dropbox/ADULT STUDY/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
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
##
## -- Imputation 2 --
##
## 1 2 3 4 5 6 7 8 9 10 11
##
## -- Imputation 3 --
##
## 1 2 3 4 5 6 7 8 9
##
## -- Imputation 4 --
##
## 1 2 3 4 5 6 7 8
##
## -- Imputation 5 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14
##
## -- Imputation 6 --
##
## 1 2 3 4 5 6 7
##
## -- Imputation 7 --
##
## 1 2 3 4 5 6 7 8 9
##
## -- Imputation 8 --
##
## 1 2 3 4 5 6 7 8
##
## -- Imputation 9 --
##
## 1 2 3 4 5 6 7 8
##
## -- Imputation 10 --
##
## 1 2 3 4 5 6 7
##
## -- Imputation 11 --
##
## 1 2 3 4 5 6 7 8
##
## -- Imputation 12 --
##
## 1 2 3 4 5 6 7 8 9
##
## -- Imputation 13 --
##
## 1 2 3 4 5 6 7 8 9 10 11
##
## -- Imputation 14 --
##
## 1 2 3 4 5 6 7 8 9
##
## -- Imputation 15 --
##
## 1 2 3 4 5 6 7 8 9 10
##
## -- Imputation 16 --
##
## 1 2 3 4 5 6 7 8
##
## -- Imputation 17 --
##
## 1 2 3 4 5 6 7
##
## -- Imputation 18 --
##
## 1 2 3 4 5 6 7
##
## -- Imputation 19 --
##
## 1 2 3 4 5 6 7 8
##
## -- Imputation 20 --
##
## 1 2 3 4 5 6 7 8 9 10 11
##
## -- Imputation 21 --
##
## 1 2 3 4 5 6 7 8 9 10 11
##
## -- Imputation 22 --
##
## 1 2 3 4 5 6 7 8 9 10
##
## -- Imputation 23 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12
##
## -- Imputation 24 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12
##
## -- Imputation 25 --
##
## 1 2 3 4 5 6
##
## -- Imputation 26 --
##
## 1 2 3 4 5 6 7 8
##
## -- Imputation 27 --
##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
##
## -- 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
##
## -- Imputation 31 --
##
## 1 2 3 4 5
##
## -- Imputation 32 --
##
## 1 2 3 4 5 6 7 8 9
##
## -- Imputation 33 --
##
## 1 2 3 4 5 6 7 8 9 10
##
## -- Imputation 34 --
##
## 1 2 3 4 5 6 7 8 9
##
## -- Imputation 35 --
##
## 1 2 3 4 5 6 7
##
## -- Imputation 36 --
##
## 1 2 3 4 5 6 7 8
##
## -- Imputation 37 --
##
## 1 2 3 4 5 6 7
##
## -- Imputation 38 --
##
## 1 2 3 4 5 6 7 8 9 10
##
## -- Imputation 39 --
##
## 1 2 3 4 5 6 7 8
##
## -- Imputation 40 --
##
## 1 2 3 4 5 6 7
##
## -- Imputation 41 --
##
## 1 2 3 4 5 6
##
## -- Imputation 42 --
##
## 1 2 3 4 5 6 7 8 9 10
##
## -- Imputation 43 --
##
## 1 2 3 4 5 6 7 8 9 10
##
## -- 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 11 12 13
##
## -- Imputation 47 --
##
## 1 2 3 4 5 6 7 8 9
##
## -- Imputation 48 --
##
## 1 2 3 4 5 6 7 8 9
##
## -- Imputation 49 --
##
## 1 2 3 4 5 6 7 8 9 10
##
## -- Imputation 50 --
##
## 1 2 3 4 5 6 7 8
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.7154080 0.38739724 0.9509664 2.4798495 53 %
## GROUP1 0.1893165 0.29126243 -0.3831342 0.7617673 34 %
## WAVE 0.1362177 0.13495974 -0.1292768 0.4017121 39 %
## BASELINE 0.6035440 0.07480108 0.4555681 0.7515199 62 %
## GROUP1:WAVE 0.1252107 0.19066476 -0.2500052 0.5004267 41 %
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.7106748 0.3862160 4.4293217 1.620215e-05
## GROUP1 0.1895433 0.3092512 0.6129103 5.401869e-01
## WAVE 0.1362177 0.1446034 0.9420091 3.467133e-01
## BASELINE 0.6046133 0.0726183 8.3259086 1.517495e-13
## GROUP1:WAVE 0.1252191 0.2038988 0.6141240 5.394910e-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.84967 -0.28214 -0.00048 0.34253 1.29064
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.78739 0.26558 6.730 2.39e-10 ***
## GROUP1 0.34781 0.25641 1.356 0.177
## WAVE 0.15664 0.11649 1.345 0.181
## BASELINE 0.57885 0.04323 13.391 < 2e-16 ***
## GROUP1:WAVE 0.03926 0.16204 0.242 0.809
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5401 on 173 degrees of freedom
## Multiple R-squared: 0.532, Adjusted R-squared: 0.5212
## F-statistic: 49.16 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 3.67 -0.43
## meanPWB 2 86 4.58 0.80 4.70 4.63 0.94 2.44 6 3.56 -0.52
## kurtosis se
## BASELINE -0.75 0.10
## meanPWB -0.23 0.09
## --------------------------------------------------------
## group: 1
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 92 4.21 0.91 4.06 4.25 0.99 1.00 5.82 4.82 -0.70
## meanPWB 2 92 4.87 0.74 4.91 4.90 0.63 2.74 6.41 3.67 -0.49
## kurtosis se
## BASELINE 1.35 0.10
## meanPWB 0.03 0.08
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))
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")
CookD(fullModeldata1)
plot(fullModeldata1, which="cook")
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
## 289.4385 311.711 -137.7193
##
## Random effects:
## Formula: ~1 | ID
## (Intercept) Residual
## StdDev: 0.2619204 0.4636408
##
## Fixed effects: meanPWB ~ GROUP * WAVE + BASELINE
## Value Std.Error DF t-value p-value
## (Intercept) 1.7910791 0.26978398 87 6.638938 0.0000
## GROUP1 0.3476111 0.23036334 87 1.508969 0.1349
## WAVE 0.1566355 0.10142601 86 1.544332 0.1262
## BASELINE 0.5780153 0.04814989 86 12.004499 0.0000
## GROUP1:WAVE 0.0392716 0.14108229 86 0.278359 0.7814
## Correlation:
## (Intr) GROUP1 WAVE BASELI
## GROUP1 -0.480
## WAVE -0.564 0.660
## BASELINE -0.790 0.050 0.000
## GROUP1:WAVE 0.410 -0.919 -0.719 -0.006
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -3.09860914 -0.51390101 0.01253942 0.57410552 2.21424375
##
## 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.59946 -0.30742 0.01035 0.34814 1.54135
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.02986 0.26021 3.958 0.00011 ***
## GROUP1 -0.06887 0.25108 -0.274 0.78418
## WAVE 0.19933 0.11410 1.747 0.08243 .
## BASELINE 0.73558 0.04236 17.364 < 2e-16 ***
## GROUP1:WAVE 0.28444 0.15872 1.792 0.07486 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5291 on 173 degrees of freedom
## Multiple R-squared: 0.6554, Adjusted R-squared: 0.6474
## F-statistic: 82.25 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.58 0.91 4.78 4.63 0.83 2.16 6.56 4.40 -0.50
## kurtosis se
## BASELINE -0.75 0.1
## meanPWB -0.23 0.1
## --------------------------------------------------------
## group: 1
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 92 4.21 0.91 4.06 4.25 0.91 1.00 5.94 4.94 -0.68
## meanPWB 2 92 4.78 0.86 4.89 4.85 0.66 1.16 6.48 5.32 -1.07
## kurtosis se
## BASELINE 1.39 0.10
## meanPWB 2.20 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=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))
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")
CookD(fullModeldata10)
plot(fullModeldata10, which="cook")
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
## 287.3138 309.5863 -136.6569
##
## Random effects:
## Formula: ~1 | ID
## (Intercept) Residual
## StdDev: 0.1002689 0.511868
##
## Fixed effects: meanPWB ~ GROUP * WAVE + BASELINE
## Value Std.Error DF t-value p-value
## (Intercept) 1.0299358 0.26084385 87 3.948476 0.0002
## GROUP1 -0.0688757 0.24735461 87 -0.278449 0.7813
## WAVE 0.1993309 0.11197619 86 1.780119 0.0786
## BASELINE 0.7355584 0.04313362 86 17.053017 0.0000
## GROUP1:WAVE 0.2844421 0.15575658 86 1.826196 0.0713
## Correlation:
## (Intr) GROUP1 WAVE BASELI
## GROUP1 -0.514
## WAVE -0.644 0.679
## BASELINE -0.732 0.034 0.000
## GROUP1:WAVE 0.460 -0.944 -0.719 0.005
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -2.93140751 -0.55458270 0.02778605 0.65180129 2.92575242
##
## 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.63024 -0.28008 -0.01045 0.24735 1.30970
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.61659 0.24121 6.702 2.78e-10 ***
## GROUP1 0.05048 0.23185 0.218 0.828
## WAVE 0.09096 0.10532 0.864 0.389
## BASELINE 0.64017 0.03942 16.239 < 2e-16 ***
## GROUP1:WAVE 0.19399 0.14650 1.324 0.187
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4883 on 173 degrees of freedom
## Multiple R-squared: 0.6178, Adjusted R-squared: 0.609
## F-statistic: 69.92 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 3.67 -0.43
## meanPWB 2 86 4.59 0.82 4.75 4.63 0.86 2.44 6 3.56 -0.49
## kurtosis se
## BASELINE -0.75 0.10
## meanPWB -0.48 0.09
## --------------------------------------------------------
## group: 1
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 92 4.19 0.90 4.05 4.24 0.92 1.00 5.56 4.56 -0.73
## meanPWB 2 92 4.78 0.74 4.89 4.84 0.66 2.29 6.00 3.71 -0.92
## kurtosis se
## BASELINE 1.5 0.09
## meanPWB 0.7 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))
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")
CookD(fullModeldata15)
plot(fullModeldata15, which="cook")
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
## 254.2403 276.5128 -120.1201
##
## Random effects:
## Formula: ~1 | ID
## (Intercept) Residual
## StdDev: 0.2289384 0.4235173
##
## Fixed effects: meanPWB ~ GROUP * WAVE + BASELINE
## Value Std.Error DF t-value p-value
## (Intercept) 1.6199532 0.24501006 87 6.611783 0.0000
## GROUP1 0.0502845 0.20993818 87 0.239521 0.8113
## WAVE 0.0909639 0.09264861 86 0.981816 0.3289
## BASELINE 0.6394107 0.04364187 86 14.651313 0.0000
## GROUP1:WAVE 0.1940042 0.12887294 86 1.505391 0.1359
## Correlation:
## (Intr) GROUP1 WAVE BASELI
## GROUP1 -0.484
## WAVE -0.567 0.662
## BASELINE -0.788 0.054 0.000
## GROUP1:WAVE 0.412 -0.921 -0.719 -0.006
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -2.96771487 -0.52956887 -0.02611949 0.57234343 2.69211466
##
## 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.65965 -0.31483 0.00817 0.34480 1.25885
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.53793 0.27409 5.611 7.85e-08 ***
## GROUP1 0.22909 0.26514 0.864 0.389
## WAVE 0.12096 0.12045 1.004 0.317
## BASELINE 0.63884 0.04453 14.346 < 2e-16 ***
## GROUP1:WAVE 0.11874 0.16755 0.709 0.479
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5585 on 173 degrees of freedom
## Multiple R-squared: 0.562, Adjusted R-squared: 0.5519
## F-statistic: 55.49 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.00 3.67 -0.43
## meanPWB 2 86 4.55 0.87 4.56 4.61 0.82 1.70 6.21 4.51 -0.64
## kurtosis se
## BASELINE -0.75 0.10
## meanPWB 0.42 0.09
## --------------------------------------------------------
## group: 1
## vars n mean sd median trimmed mad min max range skew
## BASELINE 1 92 4.21 0.92 4.06 4.25 0.91 1.00 6.22 5.22 -0.64
## meanPWB 2 92 4.82 0.78 4.89 4.86 0.58 2.25 6.69 4.44 -0.66
## kurtosis se
## BASELINE 1.37 0.10
## meanPWB 1.05 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))
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")
CookD(fullModeldata25)
plot(fullModeldata25, which="cook")
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
## 305.8344 328.1069 -145.9172
##
## Random effects:
## Formula: ~1 | ID
## (Intercept) Residual
## StdDev: 0.1732411 0.5226527
##
## Fixed effects: meanPWB ~ GROUP * WAVE + BASELINE
## Value Std.Error DF t-value p-value
## (Intercept) 1.5405111 0.27580973 87 5.585412 0.0000
## GROUP1 0.2289453 0.25446177 87 0.899724 0.3708
## WAVE 0.1209606 0.11433546 86 1.057944 0.2930
## BASELINE 0.6382604 0.04666829 86 13.676531 0.0000
## GROUP1:WAVE 0.1187501 0.15903943 86 0.746671 0.4573
## Correlation:
## (Intr) GROUP1 WAVE BASELI
## GROUP1 -0.509
## WAVE -0.622 0.674
## BASELINE -0.749 0.045 0.000
## GROUP1:WAVE 0.452 -0.938 -0.719 -0.006
##
## Standardized Within-Group Residuals:
## Min Q1 Med Q3 Max
## -3.05664893 -0.52421392 0.01444234 0.61537119 2.22010279
##
## 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))
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")
CookD(fullModel)
plot(fullModel, which="cook")
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 |