try our own data(week7)
vocdta <- read.csv("vocdta.csv", h = T)
head(vocdta)
## ID Gender T1 T2 T3
## 1 1 2 13 9 13
## 2 2 2 13 8 12
## 3 3 2 11 7 7
## 4 4 2 14 9 10
## 5 5 2 7 6 5
## 6 6 2 13 10 11
library(tidyverse)
## Warning: 套件 'tidyverse' 是用 R 版本 4.1.3 來建造的
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## Warning: 套件 'ggplot2' 是用 R 版本 4.1.3 來建造的
## Warning: 套件 'tibble' 是用 R 版本 4.1.3 來建造的
## Warning: 套件 'tidyr' 是用 R 版本 4.1.3 來建造的
## Warning: 套件 'readr' 是用 R 版本 4.1.3 來建造的
## Warning: 套件 'purrr' 是用 R 版本 4.1.3 來建造的
## Warning: 套件 'dplyr' 是用 R 版本 4.1.3 來建造的
## Warning: 套件 'stringr' 是用 R 版本 4.1.3 來建造的
## Warning: 套件 'forcats' 是用 R 版本 4.1.3 來建造的
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
資料可以變長,讓三個時間點變成同一個變項
#covert to long data
vocldta <- pivot_longer(vocdta, cols = c(, 3:5), names_to = "time", values_to = "nvoc")
str(vocldta)
## tibble [63 x 4] (S3: tbl_df/tbl/data.frame)
## $ ID : int [1:63] 1 1 1 2 2 2 3 3 3 4 ...
## $ Gender: int [1:63] 2 2 2 2 2 2 2 2 2 2 ...
## $ time : chr [1:63] "T1" "T2" "T3" "T1" ...
## $ nvoc : int [1:63] 13 9 13 13 8 12 11 7 7 14 ...
head(vocldta)
## # A tibble: 6 x 4
## ID Gender time nvoc
## <int> <int> <chr> <int>
## 1 1 2 T1 13
## 2 1 2 T2 9
## 3 1 2 T3 13
## 4 2 2 T1 13
## 5 2 2 T2 8
## 6 2 2 T3 12
#convert char to factor variable
vocldta$time <- as.factor(vocldta$time)
vocldta$ID <- as.factor(vocldta$ID)
資料也可以變寬喔
#convert to wide data
vocwdta <- pivot_wider(vocldta, names_from = "time", values_from = "nvoc")
str(vocwdta)
## tibble [21 x 5] (S3: tbl_df/tbl/data.frame)
## $ ID : Factor w/ 21 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ Gender: int [1:21] 2 2 2 2 2 2 2 2 2 2 ...
## $ T1 : int [1:21] 13 13 11 14 7 13 8 15 13 15 ...
## $ T2 : int [1:21] 9 8 7 9 6 10 7 11 9 9 ...
## $ T3 : int [1:21] 13 12 7 10 5 11 8 15 8 13 ...
#visualize your data
#三個測驗時間的結果平均都不相同
plot(nvoc ~ time, data = vocldta, frame.plot = FALSE, col="skyblue", border="purple")
library(gplots)
## Warning: 套件 'gplots' 是用 R 版本 4.1.3 來建造的
##
## 載入套件:'gplots'
## 下列物件被遮斷自 'package:stats':
##
## lowess
plotmeans(nvoc ~ time, data = vocldta, frame = F, col="skyblue")
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "frame" 不是一個繪圖參數
## Warning in axis(1, at = 1:length(means), labels = legends, ...): "frame" 不是一
## 個繪圖參數
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "frame" 不是一個繪圖參數
aggregate(nvoc ~ time, data = vocldta, mean)
## time nvoc
## 1 T1 11.857143
## 2 T2 8.285714
## 3 T3 9.333333
aggregate(nvoc ~ time, data = vocldta, sd)
## time nvoc
## 1 T1 3.054271
## 2 T2 2.552310
## 3 T3 3.022141
#ANOVA test
#三組以上的檢定是用ANOVA
summary(aov(nvoc ~ time, data = vocldta))
## Df Sum Sq Mean Sq F value Pr(>F)
## time 2 141.6 70.78 8.501 0.000561 ***
## Residuals 60 499.5 8.33
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#ANOVA for repeated measures
summary(aov(nvoc ~ time + Error(ID), data = vocldta))
##
## Error: ID
## Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 20 431.1 21.55
##
## Error: Within
## Df Sum Sq Mean Sq F value Pr(>F)
## time 2 141.56 70.78 41.36 1.83e-10 ***
## Residuals 40 68.44 1.71
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
虛無假設:T1-T3三組的平均數是相同的。 對立假設:T1-T3三組平均數是有差異的。 由ANOVA的分析結果可知,P值<0.001,表示其顯著程度甚高,則推翻虛無假設並接受對立假設,意即三組測驗時間所測出的結果是有差異的。