사용한 페키지는 총 3가지 이다.

library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
## 
##     filter
library(DescTools)
library(reshape2)

일단 데이터를 만든다. m은 id를 기준으로 t에 따라 반복된 데이터를 만든 것이다. 450행이 된다.

#data_seting
t1<-rnorm(150,0,1)
t2<-rnorm(150,2,1)
t3<-rnorm(150,3,1)
id<-seq(1,150)
a<-data.frame(id,t1, t2,t3)
m<-melt(a, id.vars = c("id"))
colnames(m)<-c("id", "t", "y")

#one-way rmanova one-way rmanova를 한다. t1~3가지 3번 측정했다고 가정해보자. 아래는 rstatix패키지를 사용 하였다. 일단 구형성(Sphericity)을 위반하지 않아 구형성을 가정한 F검정이 가능하다. 사실 위에 편차를 모두 1로 해 두어서 당연한 결과이다.

rm1<-anova_test(m,dv=y, wid = id, within = t)
rm1
## ANOVA Table (type III tests)
## 
## $ANOVA
##   Effect DFn DFd       F        p p<.05   ges
## 1      t   2 298 354.633 1.55e-79     * 0.599
## 
## $`Mauchly's Test for Sphericity`
##   Effect     W     p p<.05
## 1      t 0.986 0.349      
## 
## $`Sphericity Corrections`
##   Effect   GGe       DF[GG]   p[GG] p[GG]<.05   HFe    DF[HF]    p[HF]
## 1      t 0.986 1.97, 293.85 1.8e-78         * 0.999 2, 297.77 1.78e-79
##   p[HF]<.05
## 1         *

aov함수를 써도 된다. 다만 변인에 에러항을 넣어야 한다.

rm1<-aov(y ~ t + Error(id/t), data = m)
summary(rm1)
## 
## Error: id
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals  1 0.1761  0.1761               
## 
## Error: id:t
##   Df Sum Sq Mean Sq
## t  2  442.4   221.2
## 
## Error: Within
##            Df Sum Sq Mean Sq F value Pr(>F)    
## t           2  179.3   89.64   96.26 <2e-16 ***
## Residuals 444  413.5    0.93                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

사후검정은 본페로니를 사용하였다.

prm1<-pairwise_t_test(y ~ t,data = m ,paired = T, p.adjust.method = "bonferroni")
prm1
## Warning: `...` is not empty.
## 
## We detected these problematic arguments:
## * `needs_dots`
## 
## These dots only exist to allow future extensions and should be empty.
## Did you misspecify an argument?
## # A tibble: 3 x 10
##   .y.   group1 group2    n1    n2 statistic    df        p    p.adj p.adj.signif
## * <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl>    <dbl>    <dbl> <chr>       
## 1 y     t1     t2       150   150    -18.5    149 2.17e-40 6.51e-40 ****        
## 2 y     t1     t3       150   150    -24.8    149 1.15e-54 3.45e-54 ****        
## 3 y     t2     t3       150   150     -7.37   149 1.10e-11 3.30e-11 ****

#wto-way rmanova 집단 변인을 추가한다음 새로운 data를 만들었다.

xx1<-rep("i",50)
xx2<-rep("o",50)
xx3<-rep("u",50)
x1<-c(xx1, xx2, xx3)
a<-data.frame(id,x1,t1, t2,t3)
m<-melt(a, id.vars = c("id","x1"))
colnames(m)<-c("id","g" ,"t", "y")

구형성은 역시 가정해도 된다.

rm1<-anova_test(data = m, dv = y, wid = id, within = c(t), between = c(g), effect.size = "pes")
rm1
## ANOVA Table (type II tests)
## 
## $ANOVA
##   Effect DFn DFd       F        p p<.05   pes
## 1      g   2 147   0.270 7.63e-01       0.004
## 2      t   2 294 352.295 8.65e-79     * 0.706
## 3    g:t   4 294   0.509 7.29e-01       0.007
## 
## $`Mauchly's Test for Sphericity`
##   Effect     W     p p<.05
## 1      t 0.987 0.388      
## 2    g:t 0.987 0.388      
## 
## $`Sphericity Corrections`
##   Effect   GGe       DF[GG]    p[GG] p[GG]<.05   HFe    DF[HF]    p[HF]
## 1      t 0.987 1.97, 290.26 7.97e-78         * 1.001 2, 294.19 8.65e-79
## 2    g:t 0.987 3.95, 290.26 7.27e-01           1.001 4, 294.19 7.29e-01
##   p[HF]<.05
## 1         *
## 2
rm1<-aov(y ~ t + g + t:g +Error(id/t), data = m)
summary(rm1)
## 
## Error: id
##   Df Sum Sq Mean Sq
## g  1 0.1761  0.1761
## 
## Error: id:t
##   Df Sum Sq Mean Sq
## t  2  442.4   221.2
## 
## Error: Within
##            Df Sum Sq Mean Sq F value Pr(>F)    
## t           2  179.3   89.64  95.202 <2e-16 ***
## g           2    0.5    0.27   0.292  0.747    
## t:g         4    0.5    0.13   0.134  0.970    
## Residuals 438  412.4    0.94                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1