#Problem An experimenter wishes to compare 4 particular type of stools. Each of nine individuals randomly selected from a subject pool is asked to test the stools by recording the effort required to arise from each of 4 types of stools.

#install packages

#install.packages("nlme")
library(nlme)

#load data ergoStool{nlme}

data(ergoStool, package="nlme")
dta<-ergoStool

#exam data #show structure of data

str(dta)
## Classes 'nffGroupedData', 'nfGroupedData', 'groupedData' and 'data.frame':   36 obs. of  3 variables:
##  $ effort : num  12 15 12 10 10 14 13 12 7 14 ...
##  $ Type   : Factor w/ 4 levels "T1","T2","T3",..: 1 2 3 4 1 2 3 4 1 2 ...
##  $ Subject: Ord.factor w/ 9 levels "8"<"5"<"4"<"9"<..: 8 8 8 8 9 9 9 9 6 6 ...
##  - attr(*, "formula")=Class 'formula'  language effort ~ Type | Subject
##   .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
##  - attr(*, "labels")=List of 2
##   ..$ x: chr "Type of stool"
##   ..$ y: chr "Effort required to arise"
##  - attr(*, "units")=List of 1
##   ..$ y: chr "(Borg scale)"
##  - attr(*, "FUN")=function (x)  
##   ..- attr(*, "source")= chr "function (x) mean(x, na.rm = TRUE)"
##  - attr(*, "order.groups")= logi TRUE

1 show first 6 lines

head(dta)
## Grouped Data: effort ~ Type | Subject
##   effort Type Subject
## 1     12   T1       1
## 2     15   T2       1
## 3     12   T3       1
## 4     10   T4       1
## 5     10   T1       2
## 6     14   T2       2

#各種類型的平均數

aggregate(dta[,1], list(dta$Type), mean)
##   Group.1         x
## 1      T1  8.555556
## 2      T2 12.444444
## 3      T3 10.777778
## 4      T4  9.222222

#每位受試者的平均數

aggregate(dta[,1], list(dta$Subject), mean)
##   Group.1     x
## 1       8  8.25
## 2       5  8.50
## 3       4  9.25
## 4       9 10.00
## 5       6 10.25
## 6       3 10.75
## 7       7 10.75
## 8       1 12.25
## 9       2 12.25
pacman::p_load(tidyverse, lme4)

#Linear mixed-effects model fit by REML (PPT 第21頁)

lmer(effort ~ Type + (1 | Subject) - 1, data=dta)
## Linear mixed model fit by REML ['lmerMod']
## Formula: effort ~ Type + (1 | Subject) - 1
##    Data: dta
## REML criterion at convergence: 121.1308
## Random effects:
##  Groups   Name        Std.Dev.
##  Subject  (Intercept) 1.332   
##  Residual             1.100   
## Number of obs: 36, groups:  Subject, 9
## Fixed Effects:
## TypeT1  TypeT2  TypeT3  TypeT4  
##  8.556  12.444  10.778   9.222
m1<-lmer(effort ~ Type + (1 | Subject) - 1, data=dta)
summary(lmer(effort ~ Type + (1 | Subject) - 1, data=dta))
## Linear mixed model fit by REML ['lmerMod']
## Formula: effort ~ Type + (1 | Subject) - 1
##    Data: dta
## 
## REML criterion at convergence: 121.1
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -1.80200 -0.64317  0.05783  0.70100  1.63142 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  Subject  (Intercept) 1.775    1.332   
##  Residual             1.211    1.100   
## Number of obs: 36, groups:  Subject, 9
## 
## Fixed effects:
##        Estimate Std. Error t value
## TypeT1    8.556      0.576   14.85
## TypeT2   12.444      0.576   21.60
## TypeT3   10.778      0.576   18.71
## TypeT4    9.222      0.576   16.01
## 
## Correlation of Fixed Effects:
##        TypeT1 TypeT2 TypeT3
## TypeT2 0.595               
## TypeT3 0.595  0.595        
## TypeT4 0.595  0.595  0.595

#The End