-
Notifications
You must be signed in to change notification settings - Fork 0
/
0_modelOldData.R
297 lines (252 loc) · 8.34 KB
/
0_modelOldData.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# This script reads in previous data from the accuracy
# measuring session. It then models the data to get some
# estimate for where the switch point is likely to be.
# Using this, we can estimate where people should have
# switched if we don't have their data.
#### Library ####
library(tidyverse)
library(tidybayes)
library(brms)
#### Functions ####
squash <- function(y, max, min, squash){
y <- y * ((max-squash) - (min + squash)) + (min + squash)
}
#### read data ####
# we have several experiments to read the data from
# so we'll do them one by one
#### > old Achievement Motivation ####
# df_AM <- read_csv("OldVersion/data/Accuracy/Session 2- Ability.csv", col_types = cols())
#
# # tidy up
# df_AM <- df_AM %>%
# select(Participant, Distance, Total) %>%
# mutate(inhoop = 12 - Total,
# Acc = inhoop/12,
# Participant = paste(Participant, "AM", sep ="")) %>%
# select(-Total)
#### > Asymmetry ####
#### > > Awareness ####
df_aware <- read_csv("data/previous/Asym/Aware/Part1_session_data.csv", col_types = cols())
# tidy
df_aware <- df_aware %>%
select(-Direction) %>%
rename(Distance = Slab,
inhoop = Acc) %>%
mutate(Acc = inhoop/12,
Participant = paste(Participant, "aware", sep = ""))
#### > > Hoop size ####
# Might skip this one... but I could just ignore hoop size and then we'll get something
# that kind of works?
df_HS <- readxl::read_xlsx("data/previous/Asym/HoopSize/Part_1_measures.xlsx")
# tidy
df_HS <- df_HS %>%
select(-c(experimenter, direction, hoop_size)) %>%
rename(Participant = participant,
Distance = slab) %>%
mutate(Acc = inhoop/12,
Participant = paste(Participant, "HS", sep = ""))
#### > > Two Throw ####
df_TT <- read_csv("data/previous/Asym/TwoThrow/Part1_session_data.csv", col_types = cols())
# tidy
df_TT <- df_TT %>%
select(-Direcion) %>%
rename(Distance = Slab,
inhoop = Acc) %>%
mutate(Acc = inhoop/12,
Participant = paste(Participant, "TT", sep = ""))
#### > > Unequal ####
path <- c("data/previous/Asym/Unequal/")
results_files <- dir(path)
df_unequal <- tibble()
for(f in results_files){
d <- read_csv(paste(path, f, sep = ""), col_types = cols())
d$Participant <- strsplit(f, '[_]')[[1]][2]
d <- d %>%
rename(Distance = Slab,
inhoop = InHoop) %>%
mutate(Acc = inhoop/12,
Participant = paste(Participant, "TT", sep = "")) %>%
select(Participant, Distance, inhoop, Acc)
df_unequal <- rbind(df_unequal, d)
}
# tidy
rm(d, f, path, results_files)
#### > Athletes ####
path <- c("data/previous/Athletes/")
results_files <- dir(path)
df_athletes <- tibble()
for(f in results_files){
d <- read_csv(paste(path, f, sep = ""), col_types = cols())
d <- d %>%
select(-c(Direction, Athlete)) %>%
rename(Distance = Slab,
inhoop = Accuracy) %>%
mutate(Acc = inhoop/12,
Participant = paste(Participant, "ath", sep = ""))
df_athletes <- rbind(df_athletes, d)
}
# tidy
rm(d, f, path, results_files)
#### > Clarke and Hunt ####
df_CH <- read_csv("data/previous/ClarkeHunt/Part1_session_data_CH.csv", col_types = cols())
# tidy
df_CH <- df_CH %>%
select(-Direction) %>%
rename(Participant = `Subject Code`,
Distance = Slab,
inhoop = `Number in hoop`) %>%
mutate(Acc = inhoop/12,
Participant = paste(Participant, "CH", sep = ""))
#### > Non-Naive ####
df_NN <- read_csv("data/previous/NonNaive/Part_1.csv", col_types = cols())
# tidy
df_NN <- df_NN %>%
select(-Direction) %>%
rename(Distance = Slab,
inhoop = InHoop) %>%
mutate(Acc = inhoop/12,
Participant = paste(Participant, "NN", sep = ""))
#### > Misc ####
df_misc <- read_tsv("data/previous/Misc/Part1_session_data_Anca.txt", col_types = cols())
# tidy
df_misc <- df_misc %>%
select(-c(Direction, Session)) %>%
rename(inhoop = Accuracy) %>%
mutate(Acc = inhoop/12,
Participant = paste(Participant, "CH", sep = ""))
#### > Current AM data ####
df_AM <- read_csv("data/current/Session 2- Ability.csv", col_types = cols())
df_AM <- df_AM %>%
rename(inhoop = Total) %>%
mutate(inhoop = 12 - inhoop,
Acc = inhoop/12)
#### combine all data ####
df <- rbind(df_AM, df_athletes) %>%
rbind(df_aware) %>%
rbind(df_CH) %>%
rbind(df_HS) %>%
rbind(df_misc) %>%
rbind(df_NN) %>%
rbind(df_TT) %>%
rbind(df_unequal)
# tidy
rm(df_athletes, df_aware, df_CH, df_HS, df_misc, df_NN, df_TT, df_unequal)
# sort out the Acc column to not include 1 or 0
df <- df %>%
mutate(beta_Acc = squash(Acc, 1, 0, 1e-5))
# maybe we average some values since some people did throws in each direction?
# leave it for now... but it's something to think about
#### Initial plots ####
# make a plot to look at the overall trends in performance
df %>%
ggplot(aes(Distance, Acc)) +
# inidividual participants
geom_line(aes(fill = Participant),
stat = "smooth",
method = glm,
method.args = list(family = "binomial"),
alpha = .3,
fullrange = T) +
# overall
geom_smooth(method = glm,
method.args = list(family = "binomial"),
se = F,
linetype = "dashed",
size = 1.5) +
theme_bw() +
theme(legend.position = "none")
#### Model ####
#### > Priors ####
m_priors <- c(set_prior("student_t(3, 4, 3)",
class = "Intercept"),
set_prior("student_t(3, -10, 3)",
class = "b",
coef = "scaled_D"))
#### > Model ####
max_d <- max(df$Distance)
model_data <- df %>%
drop_na() %>%
mutate(scaled_D = Distance/max_d)
# m <- brm(beta_Acc ~ scaled_D + (scaled_D|Participant),
# data = model_data,
# family = "beta",
# prior = m_priors,
# chains = 1,
# cores = 1,
# iter = 2000,
# warmup = 1000)
#
# save(m, file = "scratch/models/m")
load("scratch/models/m")
#### Plot ####
#### > brms version ####
# plot(marginal_effects(m))
# Ok plot... but we can do better...
# we should look at all the different participants and get estimates for where
# they were roughly 50% accurate
#### > using tidybayes to get predictions ####
#### > > For everyone ####
# this is just who we have data for just now
# make a dataset with all distances
# df_preds <- tibble(Participant = rep(unique(model_data$Participant), each = max_d),
# scaled_D = rep(seq(1,max_d,1)/max_d, length(unique(model_data$Participant))))
# df_preds <- df_preds %>%
# add_predicted_draws(m) %>%
# mutate(Distance = scaled_D * max_d)
#
# # make a plot of this
# test <- df_preds %>%
# # create avg column
# group_by(Distance) %>%
# mutate(
# mu = mean(.prediction),
# # upper = HDInterval::hdi(.prediction)[2],
# # lower = HDInterval::hdi(.prediction)[1]
# ) %>%
# group_by(Participant, Distance) %>%
# summarise(
# mu_p = mean(.prediction),
# mu = mean(mu),
# # upper = mean(upper),
# # lower = mean(lower)
# )
#
# test %>%
# ggplot(aes(Distance, mu_p, group = Participant)) +
# geom_path(alpha = .3,
# aes(group = Participant)) +
# geom_path(colour = "blue",
# aes(Distance, mu),
# size = 1.5,
# linetype = "dashed")
#### > > Just the current study ####
## read in session 2 data so we have a list of all participants
df_part2 <- read_csv("data/current/Session 1 - Decisions.csv", col_types = cols())
df_preds <- tibble(Participant = rep(unique(df_part2$Participant), each = max_d + 1),
scaled_D = rep(seq(0, max_d, 1)/max_d, length(unique(df_part2$Participant))))
df_preds <- df_preds %>%
add_predicted_draws(m, allow_new_levels = T) %>%
mutate(Distance = scaled_D * max_d)
# create a new file
df_exp_acc <- df_preds %>%
group_by(Distance) %>%
mutate(mu = mean(.prediction)) %>%
group_by(Participant, Distance) %>%
summarise(mu_p = mean(.prediction),
mu = mean(mu))
# Make a plot of this
df_exp_acc %>%
ggplot(aes(Distance, mu_p, group = Participant)) +
geom_path(alpha = .3) +
geom_path(colour = "blue",
aes(Distance, mu),
size = 1.5,
linetype = "dashed") +
theme_bw() +
scale_x_continuous("Estimated Accuracy")
# save this
df_exp_acc <- df_exp_acc %>%
rename(p = mu_p) %>%
select(-mu)
save(df_exp_acc,
file = "scratch/newData/df_bayes_expAcc")