forked from marton-balazs-kovacs/Weissman-replication-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.R
More file actions
203 lines (167 loc) · 6.05 KB
/
Copy pathutils.R
File metadata and controls
203 lines (167 loc) · 6.05 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#' Download all files from osf directory
#' This function is intended to run inside dplyr do() verb. Downloads data locally
#'
#' @param df row with directory on osf repository
#' @param local_data_pth Path where the data should be saved
#' @param should_overwrite whether the data should be overwritten. Default is True
#'
#' @return
#' @export
#'
#' @examples
#' local_data_pth <- file.path("data","Source")
#' create_local_structure(local_data_pth)
#' data_files <-
#' weissman_project %>%
#' osf_ls_files() %>%
#' filter(name == "Source") %>%
#' osf_ls_files()
#'
#' data_files %>%
#' group_by(name) %>% # for each experiment type
#' do(download_files(.,local_data_pth))
download_files <- function(df, local_data_pth, should_overwrite = T) {
# we need to set correct class as the current version of osfr does not works with dplyr properly
class(df) <- c("osf_tbl_file","osf_tbl", class(df))
stopifnot(nrow(df) == 1)
df %>%
osf_ls_files() %>%
rowwise() %>%
do(osf_retrieve_file(.$id) %>%
osf_download(path = file.path(local_data_pth, df$name,.$name),
overwrite = should_overwrite))
}
#' Create local file structure
#' As we have four experiments, we create individual directory for each of them
#' @param pth path, in which the file structure will be created
#'
#' @return nothing, just the side effects
#' @export
#'
#' @examples
#' local_data_pth <- file.path("data","Source")
#' create_local_structure(local_data_pth)
#'
create_local_structure <- function(pth) {
# simple function for test-and-create behavior
verbose_create <- function(f) {
if(!dir.exists(f)) {
message(sprintf("Directory %s does not exist, creating..", f))
dir.create(f, recursive = T)
}
}
flanker_dir <- file.path(pth,"flanker")
primeprobe_dir <- file.path(pth,"primeprobe")
stroop_dir <- file.path(pth,"stroop")
simon_dir <- file.path(pth,"simon")
verbose_create(flanker_dir)
verbose_create(primeprobe_dir)
verbose_create(stroop_dir)
verbose_create(simon_dir)
}
#' Removes local data
#' Just a simple wrapper around unlink function
#'
#' @param local_data_pth path to downloaded data
#'
#' @return
#' @export
#'
#' @examples
#' local_data_pth <- file.path("data","Source")
#' create_local_structure(local_data_pth)
#' remove_local_data(local_data_pth)
remove_local_data <- function(local_data_pth) {
unlink(local_data_pth, recursive = T)
}
#' Reads in and merge local csv files
#' The function reads in and merge individual locale csv files
#' into one tibble from different subdirectories. The function also
#' saves directory, subdirectory and file names as a variable.
#'
#' @param pattern The pattern to look for when listing files in the locale directory.
#' @param path The path to the locale directory that contains the subdirectories.
#' @param subfolder_name A character vector of the subfolder names to look into.
#' @param exclude A character string. Filenames containing this string will not be read in.
#' @param sep Used as delim in read_delim.
#'
#' @return All files that meets the criteria merged into a tibble from the specified subdirectory.
read_plus <- function(pattern, path, subfolder_name, include = NULL, sep) {
files <- tibble(list = list.files(path = paste0(path, subfolder_name), pattern = pattern, full.names = T, recursive = T))
if(!is.null(include)){
files <- filter(files, str_detect(list, include))
}
files %>%
pull(list) %>%
map_dfr(.,
~ read_delim(.x, delim = sep) %>%
mutate(data_type = str_extract(.x, "(?<=/).*?(?=/)"),
filename = str_remove_all(.x, ".*/|.csv"),
task = str_extract(.x, subfolder_name)))
}
#' Function to join nested dataframes with an unnested dataframe
#' Retrieved from https://stackoverflow.com/questions/50125026/joining-dataframe-to-nested-dataframes-within-purrrmap?noredirect=1&lq=1
#'
#' A wrapper function around left_join
#'
#' @param df_nest The name of the vector that contains the nested dataframes.
#' @param df_unnest The name of the unnested dataframe that will be joined to the nested ones.
#' @param var_by The id variables to join by.
#'
#' @return A vector of nested dataframes.
join_df <- function(df_nest, df_unnest, var_by = c("id", "task", "consentTime")) {
left_join(df_nest, df_unnest, by = var_by)
}
#' Writes experimental data into tdf format
#'
#' This function is meant to be run for each row containing experimental data
#'
#' @param data_column column that contains the data
#' @param id_column column that contains the ids of dataframes
#' @param folder_path path to folder where to save data
#'
#' @return
#' this function is useful for its side effect
#' @export
#'
#' @examples
#'
#' source df was created using steps in the markdown document
#'
write_tdf2 <- function(data_column, id_column, folder_path, extra_name = NULL) {
write_tsv(data_column, path = paste0(folder_path, id_column, "/", id_column, extra_name, "_data.tsv"))
}
#' Function to not include a vector in another vector
#'
#' Retrieved from https://stackoverflow.com/questions/5831794/opposite-of-in
#'
`%ni%` <- Negate(`%in%`)
#' Function to caluclate Bayes factors
#'
## The function is retrieved from
## https://link.springer.com/article/10.3758/s13423-017-1266-z
Bf <- function(sd, obtained, dfdata, meanoftheory, sdtheory, dftheory, tail = 2)
{
area <- 0
normarea <- 0
theta <- meanoftheory - 10 * sdtheory
incr <- sdtheory/200
for (A in -2000:2000){
theta <- theta + incr
dist_theta <- dt((theta-meanoftheory)/sdtheory, df=dftheory)
if(identical(tail, 1)){
if (theta <= 0){
dist_theta <- 0
} else {
dist_theta <- dist_theta * 2
}
}
height <- dist_theta * dt((obtained-theta)/sd, df = dfdata)
area <- area + height * incr
normarea <- normarea + dist_theta*incr
}
LikelihoodTheory <- area/normarea
Likelihoodnull <- dt(obtained/sd, df = dfdata)
BayesFactor <- LikelihoodTheory/Likelihoodnull
BayesFactor
}