#install.packages(c("MASS"))
#install.packages(c("multcomp"))
library(multcomp)
library(MASS)
library(nlme)
library(foreign)
library(car)
library(stats)
#sem <- read.spss("e:\\POST-HOC FOR ZIPPING\\DEMO POST-HOC\\WITHIN2.sav")
sem <- read.spss("U:\\MY DOCUMENTS\\WITHIN2.sav")
attach(sem)
type2 <- recode(type, "1='A'; 2='B'; 3='C';4='D'")
sem <- data.frame(y,id,type2)
# glht not working with lme in R 4.0 in 2023 although worked OK in earlier years
Lme.mod <- lme(y ~ type2, random = ~1 | id/type2, data = sem) 
anova(Lme.mod)
summary(glht(Lme.mod, linfct=mcp(type2="Tukey")))
# glht works OK with lmer November 2023 giving same output as using lme
library(lme4)
Lme.mod <- lmer(y ~ type2 + (1|id), data = sem) 
anova(Lme.mod)
summary(glht(Lme.mod, linfct=mcp(type2="Tukey")))

# OUTPUTTED TUKEY STANDARD ERROR = SQRT[2X(MSE/{N PER GROUP)}] = sqrt[2x (1.05/10)] =0.46

> summary(glht(Lme.mod, linfct=mcp(type2="Tukey")))

         Simultaneous Tests for General Linear Hypotheses

Multiple Comparisons of Means: Tukey Contrasts


Fit: lme.formula(fixed = y ~ type2, data = sem, random = ~1 | id/type2)

Linear Hypotheses:
           Estimate Std. Error z value Pr(>|z|)    
B - A == 0  -0.7000     0.4603  -1.521  0.42490    
C - A == 0  -1.7000     0.4603  -3.693  0.00126 ** 
D - A == 0  -2.6000     0.4603  -5.649  < 0.001 ***
C - B == 0  -1.0000     0.4603  -2.173  0.13090    
D - B == 0  -1.9000     0.4603  -4.128  < 0.001 ***
D - C == 0  -0.9000     0.4603  -1.955  0.20496    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Adjusted p values reported -- single-step method)
