/
4_data_seq_analysis.R
100 lines (81 loc) · 3.15 KB
/
4_data_seq_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#######################################################################
# R code accompanying Sau-Chin Chen et al. (2019)
# OSF projet: https://osf.io/e428p/
# ---------------------------------------------------------
# Download rawdata from lab OSF
# Written by Sau-Chin Chen
# E-mail: pmsp96@gmail.com
# Last update: October 17, 2019
#############################################################
library(tidyverse)
library(data.table)
library(magrittr)
# Import multiple-bytes string in English system
Sys.setlocale("LC_ALL","English")
##set working directory to current project folder
setwd(here::here())
## Sequential analysis, subset by the end sequence
require(BayesFactor)
BF_sequential <- function(DF, acc, scale=0.707){
data <- DF %>%
group_by(subject_nr) %>%
subset(correct == 1)
tmp_seq <- data %>% pull(subject_nr) %>% table() %>% unlist()
tmp_ind <- data.frame(id = rep(names(tmp_seq) %>% as.numeric(), tmp_seq), end_seq = rep(names(tmp_seq) %>% order(), tmp_seq))
data <- data %>% left_join(tmp_ind,by=c("subject_nr"="id")) %>%
group_by(end_seq, subject_nr, Match) %>%
summarise(V_RT = median(response_time, na.rm = TRUE), V_Accuracy = (n()/12) )
bfProg <- matrix(0, ncol = 3, nrow = length(unique(data$end_seq)) )
colnames(bfProg) <- c("ID","N", "BF")
for(i in data$end_seq){
## Initial Point
if(i < 3){
bfProg[i, "ID"] <- as.numeric(names(tmp_seq)[i])
bfProg[i, "N"] <- 1
bfProg[i, "BF"] <- 1
}
if(i > 2){
## BF analysis for large and small objects
## Save the current sample size
bfProg[i, "ID"] <- as.numeric(names(tmp_seq)[i])
bfProg[i, "N"] <- i
bfProg[i, "BF"] <- (ttestBF(
x = data %>%
filter(end_seq %in% (1:i), Match == "N") %>%
pull(V_RT),
y = data %>%
filter(end_seq %in% (1:i), Match == "Y") %>%
pull(V_RT),
nullInterval = c(0,Inf),
paired = TRUE,
rscale = scale) %>%
## Return BF10 and BF01
extractBF(onlybf = TRUE))[1] #%>%
#round(2)
}
}
data.frame(bfProg)
}
# Processing lab data
rawdata_SP_V <- dirname(getwd()) %>%
dir(pattern = "all_rawdata_SP_V",
recursive = TRUE, full.names = TRUE) %>% read.csv
# Mark Acc < .70
rawdata_SP_V = (rawdata_SP_V %>%
group_by(PSA_ID, subject_nr) %>%
summarise(Acc_bound = (mean(correct) < .7))) %>%
right_join(rawdata_SP_V, by = c("PSA_ID","subject_nr"))
## Setup of randomization seed
set.seed(100)
## Run sequential analysis and export the result to lab folder
for(LAB in unique(rawdata_SP_V$PSA_ID)){
## Get the raw data from the storage path
route <- (dirname(paste0(getwd(),"/1_raw_data/")) %>%
dir(pattern = paste0(LAB,"$"),
recursive = TRUE, full.names = TRUE, include.dirs = TRUE))[1] ## Make sure only one target directory
BF_sequential(subset(rawdata_SP_V, (PSA_ID == LAB & Acc_bound != TRUE) )) %>% ## Revise the condition in terms of the preregistered plan
write.csv(file = paste0(route,"/Seq_output.csv"),row.names = FALSE)
print(paste(LAB,"analyzed"))
}
## Export the rawdata_SP_V
write_csv(rawdata_SP_V, file = "1_raw_data/all_rawdata_SP_V.csv")