source("readraw_func.R")
##install.packages("httr")
library(httr)
# 先將收集的受試者檔案上傳到Rawdata資料夾

# OSFID改成個別raw file的網址,根據實際raw file增加
rawurls <- c(
        "https://osf.io/tkp63/?action=download",
        "https://osf.io/q4bge/?action=download",
        "https://osf.io/5vqrd/?action=download",
        "https://osf.io/qnx7y/?action=download",
        "https://osf.io/5ghfn/?action=download",
        "https://osf.io/c2df3/?action=download",
        "https://osf.io/uvh45/?action=download",
        "https://osf.io/8m3e9/?action=download",
        "https://osf.io/x2gfy/?action=download",
        "https://osf.io/a782s/?action=download",
        "https://osf.io/d279h/?action=download",
        "https://osf.io/v98bq/?action=download",
        "https://osf.io/ryn2w/?action=download",
        "https://osf.io/4utzs/?action=download",
        "https://osf.io/j4z29/?action=download",
        "https://osf.io/9hb2g/?action=download",
        "https://osf.io/hdeqn/?action=download",
        "https://osf.io/e37rk/?action=download",
        "https://osf.io/fc7qh/?action=download",
        "https://osf.io/re2m9/?action=download"
)


# 下載後的檔案名稱
filenames <- paste0("gazeraw", paste0(sprintf( "%02d", 1:length(rawurls)), ".csv"))
#filenames <- paste0("subject-", paste0(1:5, ".csv"))

# 執行完畢檢查是否已下載到本機資料夾    
for(i in 1:length(rawurls)){
    GET( rawurls[i], write_disk(paste0("RAW/", filenames[i]), overwrite = TRUE) )
}
    
# 讀入原始資料到data frame
DF <- data.frame()  # 空白data frame
for(i in 1:length(filenames)){
    DF <- readsubj(paste0("RAW/",filenames[i]), DF, 16)
}  # 根據實際實驗設定,更改第三個argument
colnames(DF) <- c("RT", "ACC", "ID", "SOA", "Gaze", "Target_Congruency", "Response_Congruency")

#檢查data frame是否符合codebook
summary(DF)
##        RT             ACC                 ID        SOA      Gaze   
##  Min.   :347.5   Min.   :0.4375   gazeraw01: 16   100 :160   L:160  
##  1st Qu.:480.3   1st Qu.:0.9375   gazeraw02: 16   1000:160   R:160  
##  Median :566.0   Median :1.0000   gazeraw03: 16                     
##  Mean   :569.3   Mean   :0.9398   gazeraw04: 16                     
##  3rd Qu.:630.1   3rd Qu.:1.0000   gazeraw05: 16                     
##  Max.   :959.6   Max.   :1.0000   gazeraw06: 16                     
##                                   (Other)  :224                     
##  Target_Congruency Response_Congruency
##  TC:160            RC:160             
##  TI:160            RI:160             
##                                       
##                                       
##                                       
##                                       
## 
print(read.csv("codebook_Tidy.csv"))
##              Varibles      Values
## 1                  RT    integers
## 2                 ACC proportions
## 3                  ID        Char
## 4                 SOA        Char
## 5                Gaze        Char
## 6   Target_Congruency        Char
## 7 Response_Congruency        Char
##                                                    Descrptions
## 1                                           Average correct RT
## 2                                       Propotions of accuracy
## 3                                                   Subject ID
## 4                                     Levels of SOA: 100; 1000
## 5                 Levels of gaze directions: L(Left); R(Right)
## 6   Levels of Target Congruency: TC(Congruent);TI(Incongruent)
## 7 Levels of Response Congruency: RC(Congruent);RI(Incongruent)
#回顧文獻的統計結果
x <- factor(rep(c("cued", "neutral", "uncued"),4))
SOA <- factor(rep(c("105 ms","300 ms", "600 ms", "1,005 ms"),each=3))
resp <- c(504, 509, 508, 481, 500, 481, 478, 488, 488, 470, 482, 483)
interaction.plot(x, SOA, resp, type = "b", xlab = "Cue Validity", ylab = "RT (ms)", main = "Identification", ylim=c(460, 520), lty = 1, pch = 1:4)

#總體描述統計
Two.Mean <- with(data = DF, tapply(RT, paste(SOA, Target_Congruency), mean) )
Two.Resp.Mean <- with(data = DF, tapply(RT, paste(SOA, Response_Congruency), mean) )
Four.Mean <- with(data = DF, tapply(RT, paste(SOA, paste(Gaze, paste(Target_Congruency, Response_Congruency))), mean) )

#二因子ANOVA
summary( aov(RT ~ SOA*Target_Congruency + Error( ID/(SOA*Target_Congruency) ), data = DF)  )
## 
## Error: ID
##           Df  Sum Sq Mean Sq F value Pr(>F)
## Residuals 19 3325800  175042               
## 
## Error: ID:SOA
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## SOA        1  61700   61700   16.91 0.000593 ***
## Residuals 19  69332    3649                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Target_Congruency
##                   Df Sum Sq Mean Sq F value  Pr(>F)   
## Target_Congruency  1  15784   15784   10.09 0.00497 **
## Residuals         19  29731    1565                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:SOA:Target_Congruency
##                       Df Sum Sq Mean Sq F value  Pr(>F)   
## SOA:Target_Congruency  1   8250    8250   11.49 0.00307 **
## Residuals             19  13640     718                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: Within
##            Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 240 658586    2744
#另一種二因子ANOVA
summary( aov(RT ~ SOA*Response_Congruency + Error( ID/(SOA*Response_Congruency) ), data = DF)  ) 
## 
## Error: ID
##           Df  Sum Sq Mean Sq F value Pr(>F)
## Residuals 19 3325800  175042               
## 
## Error: ID:SOA
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## SOA        1  61700   61700   16.91 0.000593 ***
## Residuals 19  69332    3649                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Response_Congruency
##                     Df Sum Sq Mean Sq F value Pr(>F)
## Response_Congruency  1    348   347.8   0.384  0.543
## Residuals           19  17209   905.7               
## 
## Error: ID:SOA:Response_Congruency
##                         Df Sum Sq Mean Sq F value Pr(>F)
## SOA:Response_Congruency  1    565   565.1   0.578  0.457
## Residuals               19  18591   978.5               
## 
## Error: Within
##            Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 240 689278    2872
#三因子ANOVA?
#summary( aov(RT ~  + Error( ID/() ), data = DF)  )
summary( aov(RT ~SOA*Response_Congruency*Target_Congruency  + Error( ID/(SOA*Response_Congruency*Target_Congruency) ), data = DF)  )
## 
## Error: ID
##           Df  Sum Sq Mean Sq F value Pr(>F)
## Residuals 19 3325800  175042               
## 
## Error: ID:SOA
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## SOA        1  61700   61700   16.91 0.000593 ***
## Residuals 19  69332    3649                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Response_Congruency
##                     Df Sum Sq Mean Sq F value Pr(>F)
## Response_Congruency  1    348   347.8   0.384  0.543
## Residuals           19  17209   905.7               
## 
## Error: ID:Target_Congruency
##                   Df Sum Sq Mean Sq F value  Pr(>F)   
## Target_Congruency  1  15784   15784   10.09 0.00497 **
## Residuals         19  29731    1565                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:SOA:Response_Congruency
##                         Df Sum Sq Mean Sq F value Pr(>F)
## SOA:Response_Congruency  1    565   565.1   0.578  0.457
## Residuals               19  18591   978.5               
## 
## Error: ID:SOA:Target_Congruency
##                       Df Sum Sq Mean Sq F value  Pr(>F)   
## SOA:Target_Congruency  1   8250    8250   11.49 0.00307 **
## Residuals             19  13640     718                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: ID:Response_Congruency:Target_Congruency
##                                       Df Sum Sq Mean Sq F value Pr(>F)
## Response_Congruency:Target_Congruency  1   7297    7297     1.5  0.236
## Residuals                             19  92427    4865               
## 
## Error: ID:SOA:Response_Congruency:Target_Congruency
##                                           Df Sum Sq Mean Sq F value Pr(>F)
## SOA:Response_Congruency:Target_Congruency  1    646   646.4   0.294  0.594
## Residuals                                 19  41836  2201.9               
## 
## Error: Within
##            Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 160 479667    2998
#成對t檢定
ID.100.Means <- matrix( with(data = subset(DF, SOA == 100), tapply(RT, paste(ID, paste(SOA, Target_Congruency) ), mean) ), ncol = 2,  byrow =TRUE)

t.test(ID.100.Means[,1], ID.100.Means[,2], paired = TRUE, var.equal = TRUE)  # SOA = 100ms
## 
##  Paired t-test
## 
## data:  ID.100.Means[, 1] and ID.100.Means[, 2]
## t = -0.6836, df = 19, p-value = 0.5025
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -15.805069   8.022721
## sample estimates:
## mean of the differences 
##               -3.891174
ID.1000.Means <- matrix( with(data = subset(DF, SOA == 1000), tapply(RT, paste(ID, paste(SOA, Target_Congruency) ), mean) ), ncol = 2,  byrow =TRUE)

t.test(ID.1000.Means[,1], ID.1000.Means[,2], paired = TRUE, var.equal = TRUE, data = subset(DF, SOA == 1000) )  # SOA = 1,000 ms
## 
##  Paired t-test
## 
## data:  ID.1000.Means[, 1] and ID.1000.Means[, 2]
## t = -4.8729, df = 19, p-value = 0.0001057
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -34.59641 -13.80623
## sample estimates:
## mean of the differences 
##               -24.20132