

install.packages("lme4","nlme","foreign","tidyverse","readr","lsmeans","lmertest","multcompView")
library(lme4)
library(nlme)
library(foreign)
library(readr)
dat <- read_csv('U:\\My Documents\\BDI DUMMY.csv')
dat2 <- data.frame(dat)
attach(dat2)
head(dat2)
# time as a continuous predictor
fm1 <- lme(BDI ~ 1 + time, random=~1 | Participant)
# variance components for intercept and residual time as continuous
VarCorr(fm1)
summary(fm1)

time2 <- as.factor(time)
grp <- as.factor(Group)
is.factor(time2)
fm1 <- lme(BDI ~ 1, random=~1 | Participant)
fm2 <- lme(BDI ~ 1 + time2, random=~1 | Participant)
summary(fm1)
summary(fm2)
# variance components for intercept and residual time as a factor
VarCorr(fm2)
# Likelihood Ratio test
anova(fm1,fm2)
# time and group as factors in lme which can also fit covariance models unlike lmer below
fm3 <- lme(BDI ~ 1 + time2*Group, random=~1 | Participant)
summary(fm3)
anova(fm3)
require(lmerTest)
# time and group as factors in lmer with random intercept
fm33 <- lmer(BDI ~ time2*grp + (1 | Participant))
require(lsmeans)
require(multcompView)
# predicted means for group and time
fmm <- lsmeans(fm33, ~ time2:grp)
summary(fmm)
# simple effects
fmcon <- update(pairs(fmm, by = "grp"), by = NULL)
cld(fmcon)


>>>>>>>>>>>>>> OUTPUT



R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(lme4)
Loading required package: Matrix
> library(nlme)

Attaching package: ‘nlme’

The following object is masked from ‘package:lme4’:

    lmList

> library(foreign)
> library(readr)
Warning message:
package ‘readr’ was built under R version 3.3.3 
> dat <- read_csv('U:\\My Documents\\BDI DUMMY.csv')
Parsed with column specification:
cols(
  Participant = col_integer(),
  Group = col_integer(),
  Gender = col_integer(),
  Age = col_integer(),
  PerProBDI = col_integer(),
  time = col_integer(),
  BDI = col_double()
)
> dat2 <- data.frame(dat)
> attach(dat2)
> head(dat2)
  Participant Group Gender Age PerProBDI time       BDI
1           1     1      2  37         1    1  4.897813
2           1     1      2  37         1    2 24.130611
3           1     1      2  37         1    3 36.000000
4           1     1      2  37         1    4 38.000000
5           1     1      2  37         1    5 45.000000
6           2     1      2  22         2    1  5.346381
> # time as a continuous predictor
> fm1 <- lme(BDI ~ 1 + time, random=~1 | Participant)
> # variance components for intercept and residual time as continuous
> VarCorr(fm1)
Participant = pdLogChol(1) 
            Variance  StdDev  
(Intercept)  4.096127 2.023889
Residual    75.351294 8.680512
> summary(fm1)
Linear mixed-effects model fit by REML
 Data: NULL 
       AIC      BIC    logLik
  5413.467 5431.936 -2702.733

Random effects:
 Formula: ~1 | Participant
        (Intercept) Residual
StdDev:    2.023889 8.680512

Fixed effects: BDI ~ 1 + time 
                Value Std.Error  DF   t-value p-value
(Intercept) 13.485142 0.7615009 599 17.708635  0.0000
time         0.316735 0.2241299 599  1.413178  0.1581
 Correlation: 
     (Intr)
time -0.883

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.71857055 -0.77619299 -0.06761834  0.67865847  3.07459570 

Number of Observations: 750
Number of Groups: 150 
> 
> time2 <- as.factor(time)
> grp <- as.factor(Group)
> is.factor(time2)
[1] TRUE
> fm1 <- lme(BDI ~ 1, random=~1 | Participant)
> fm2 <- lme(BDI ~ 1 + time2, random=~1 | Participant)
> summary(fm1)
Linear mixed-effects model fit by REML
 Data: NULL 
      AIC      BIC    logLik
  5412.31 5426.166 -2703.155

Random effects:
 Formula: ~1 | Participant
        (Intercept) Residual
StdDev:    2.017693 8.687722

Fixed effects: BDI ~ 1 
               Value Std.Error  DF  t-value p-value
(Intercept) 14.43535 0.3574576 600 40.38339       0

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.67524882 -0.78116704 -0.08262496  0.69215700  3.14724149 

Number of Observations: 750
Number of Groups: 150 
> summary(fm2)
Linear mixed-effects model fit by REML
 Data: NULL 
       AIC      BIC    logLik
  5409.635 5441.929 -2697.818

Random effects:
 Formula: ~1 | Participant
        (Intercept) Residual
StdDev:    2.020582 8.684364

Fixed effects: BDI ~ 1 + time2 
                Value Std.Error  DF   t-value p-value
(Intercept) 14.123789 0.7280152 596 19.400404  0.0000
time22      -0.747892 1.0027840 596 -0.745816  0.4561
time23       0.456085 1.0027840 596  0.454819  0.6494
time24       1.279742 1.0027840 596  1.276189  0.2024
time25       0.569860 1.0027840 596  0.568278  0.5701
 Correlation: 
       (Intr) time22 time23 time24
time22 -0.689                     
time23 -0.689  0.500              
time24 -0.689  0.500  0.500       
time25 -0.689  0.500  0.500  0.500

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.7015934 -0.7732293 -0.0642908  0.6922590  3.1176518 

Number of Observations: 750
Number of Groups: 150 
> # variance components for intercept and residual time as a factor
> VarCorr(fm2)
Participant = pdLogChol(1) 
            Variance  StdDev  
(Intercept)  4.082751 2.020582
Residual    75.418177 8.684364
> # Likelihood Ratio test
> anova(fm1,fm2)
    Model df      AIC      BIC    logLik   Test  L.Ratio p-value
fm1     1  3 5412.310 5426.166 -2703.155                        
fm2     2  7 5409.635 5441.929 -2697.818 1 vs 2 10.67451  0.0305
Warning message:
In anova.lme(fm1, fm2) :
  fitted objects with different fixed effects. REML comparisons are not meaningful.
> # time and group as factors in lme which can also fit covariance models unlike lmer below
> fm3 <- lme(BDI ~ 1 + time2*Group, random=~1 | Participant)
> summary(fm3)
Linear mixed-effects model fit by REML
 Data: NULL 
       AIC      BIC    logLik
  5401.612 5456.892 -2688.806

Random effects:
 Formula: ~1 | Participant
        (Intercept) Residual
StdDev:    2.058192 8.676514

Fixed effects: BDI ~ 1 + time2 * Group 
                 Value Std.Error  DF   t-value p-value
(Intercept)  15.052382  2.293410 592  6.563319  0.0000
time22       -3.191883  3.155797 592 -1.011435  0.3122
time23       -1.481839  3.155797 592 -0.469561  0.6388
time24       -1.224226  3.155797 592 -0.387929  0.6982
time25        3.669880  3.155797 592  1.162901  0.2453
Group        -0.621825  1.456317 148 -0.426985  0.6700
time22:Group  1.636601  2.003933 592  0.816694  0.4144
time23:Group  1.297717  2.003933 592  0.647585  0.5175
time24:Group  1.676764  2.003933 592  0.836737  0.4031
time25:Group -2.075906  2.003933 592 -1.035916  0.3007
 Correlation: 
             (Intr) time22 time23 time24 time25 Group  tm22:G tm23:G tm24:G
time22       -0.688                                                        
time23       -0.688  0.500                                                 
time24       -0.688  0.500  0.500                                          
time25       -0.688  0.500  0.500  0.500                                   
Group        -0.948  0.652  0.652  0.652  0.652                            
time22:Group  0.652 -0.948 -0.474 -0.474 -0.474 -0.688                     
time23:Group  0.652 -0.474 -0.948 -0.474 -0.474 -0.688  0.500              
time24:Group  0.652 -0.474 -0.474 -0.948 -0.474 -0.688  0.500  0.500       
time25:Group  0.652 -0.474 -0.474 -0.474 -0.948 -0.688  0.500  0.500  0.500

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.83004816 -0.77814406 -0.07229412  0.68420530  2.95704921 

Number of Observations: 750
Number of Groups: 150 
> anova(fm3)
            numDF denDF   F-value p-value
(Intercept)     1   592 1620.1547  <.0001
time2           4   592    1.1180  0.3470
Group           1   148    0.0256  0.8731
time2:Group     4   592    1.2698  0.2806
> require(lmerTest)
Loading required package: lmerTest

Attaching package: ‘lmerTest’

The following object is masked from ‘package:lme4’:

    lmer

The following object is masked from ‘package:stats’:

    step

> # time and group as factors in lmer with random intercept
> fm33 <- lmer(BDI ~ time2*grp + (1 | Participant))
> require(lsmeans)
Loading required package: lsmeans
The 'lsmeans' package is being deprecated.
Users are encouraged to switch to 'emmeans'.
See help('transition') for more information, including how
to convert 'lsmeans' objects and scripts to work with 'emmeans'.
Warning message:
package ‘lsmeans’ was built under R version 3.3.3 
> require(multcompView)
Loading required package: multcompView
Warning message:
package ‘multcompView’ was built under R version 3.3.3 
> # predicted means for group and time
> fmm <- lsmeans(fm33, ~ time2:grp)
> summary(fmm)
 time2 grp   lsmean       SE     df lower.CL upper.CL
 1     1   14.43056 1.022883 731.69 12.42242 16.43869
 2     1   12.87527 1.022883 731.69 10.86714 14.88341
 3     1   14.24643 1.022883 731.69 12.23830 16.25457
 4     1   14.88309 1.022883 731.69 12.87496 16.89123
 5     1   16.02453 1.022883 731.69 14.01639 18.03267
 1     2   13.80873 1.036614 731.69 11.77364 15.84382
 2     2   13.89005 1.036614 731.69 11.85496 15.92514
 3     2   14.92233 1.036614 731.69 12.88723 16.95742
 4     2   15.93803 1.036614 731.69 13.90294 17.97313
 5     2   13.32680 1.036614 731.69 11.29171 15.36189

Degrees-of-freedom method: satterthwaite 
Confidence level used: 0.95 


E.G. (TIME 2 - TIME 1) GROUP 2 - (TIME 2 - TIME 1) GROUP 1 = 
(13.89 - 13.80) - (12.87 - 14.43)  = 0.09 + 1.56 = 1.65 = APPROX. TIME22:GROUP in above summary(fm3)
 (TIME 3 - TIME 1) GROUP 2 - (TIME 3 - TIME 1) GROUP 1 = 1.31 APPROX. TIME23:GROUP in above summary(fm3) 

> # simple effects
> fmcon <- update(pairs(fmm, by = "grp"), by = NULL)
> cld(fmcon)
 contrast grp    estimate       SE  df t.ratio p.value .group
 2 - 5    1   -3.14925601 1.407516 592  -2.237  0.1674  1    
 1 - 4    2   -2.12930236 1.426410 592  -1.493  0.5676  1    
 2 - 4    2   -2.04798341 1.426410 592  -1.436  0.6047  1    
 2 - 4    1   -2.00782045 1.407516 592  -1.426  0.6108  1    
 3 - 5    1   -1.77809585 1.407516 592  -1.263  0.7139  1    
 1 - 5    1   -1.59397376 1.407516 592  -1.132  0.7893  1    
 2 - 3    1   -1.37116016 1.407516 592  -0.974  0.8668  1    
 4 - 5    1   -1.14143556 1.407516 592  -0.811  0.9273  1    
 1 - 3    2   -1.11359529 1.426410 592  -0.781  0.9362  1    
 2 - 3    2   -1.03227635 1.426410 592  -0.724  0.9510  1    
 3 - 4    2   -1.01570707 1.426410 592  -0.712  0.9537  1    
 3 - 4    1   -0.63666029 1.407516 592  -0.452  0.9913  1    
 1 - 4    1   -0.45253820 1.407516 592  -0.322  0.9977  1    
 1 - 2    2   -0.08131894 1.426410 592  -0.057  1.0000  1    
 1 - 3    1    0.18412209 1.407516 592   0.131  0.9999  1    
 1 - 5    2    0.48193244 1.426410 592   0.338  0.9972  1    
 2 - 5    2    0.56325138 1.426410 592   0.395  0.9949  1    
 1 - 2    1    1.55528225 1.407516 592   1.105  0.8039  1    
 3 - 5    2    1.59552773 1.426410 592   1.119  0.7968  1    
 4 - 5    2    2.61123480 1.426410 592   1.831  0.3568  1    

P value adjustment: tukey method for comparing a family of 5 estimates 
P value adjustment: tukey method for comparing a family of 20 estimates 
significance level used: alpha = 0.05 
