-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.R
More file actions
320 lines (259 loc) · 11.4 KB
/
Copy pathanalysis.R
File metadata and controls
320 lines (259 loc) · 11.4 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Daily summary from raw data -------------------------------------------------------------
# Calculates hit rate, false alarm rate, and trial count from data across conditions
# Summarized by day and individual
writeLines("Analyzing summary data (trials/hits/FAs)")
# Summarize Hits, misses, FAs, & CRs for each individual by day
Hit_summary_by_day <-
Analysis_data %>%
filter(!(ID %in% HL_not_done)) %>%
filter(Condition != "Recovery" & Condition != "Recovery") %>%
group_by(ID, Date, Sex, Condition, Stim, BG_Type, BG_Intensity, Response) %>%
summarise(count = n(), .groups = "keep") %>%
spread(Response, count, fill = 0) %>%
mutate(Trials = `C.R.` + `F.A.` + Hit + Miss,
`Hit%` = Hit / (Hit + Miss),
`FA%` = `F.A.` / (`C.R.` + `F.A.`)) %>%
# Sanity checking count
# left_join(., Analysis_data %>%
# group_by(ID, Date) %>%
# count(), by = c("Date", "ID")) %>%
# filter(Trials != n)
# Summarize for each individual by day
group_by(ID, Sex, Condition, Stim, BG_Type, BG_Intensity) %>%
summarise(count = n_distinct(Date),
Trials = mean(Trials, na.rm = T),
Hit = mean(`Hit%`, na.rm = T),
FA = mean(`FA%`, na.rm = T),
.groups = "keep") %>%
ungroup() %>%
mutate(BG_Intensity = factor(BG_Intensity, levels = c("NA", "30", "50")),
Stim = factor(Stim, levels = c("BBN", "tone")))
Hit_summary_by_day_BBN <-
Hit_summary_by_day %>%
filter(Stim == "BBN")
Hit_summary_by_day_tone <-
Hit_summary_by_day %>%
filter(Stim == "tone")
# Trial Count Analysis ----------------------------------------------------
# ANOVA
Trials.aov = aov(Trials ~ Condition * BG_Intensity, data = Hit_summary_by_day_tone)
# Parametric check
Trials.aov$residuals %>%
shapiro.test()
# Summary
summary(Trials.aov)
# Hit Rate Analysis ----------------------------------------------------
# ANOVA
Hit.aov = aov(Hit ~ Condition * BG_Intensity, data = Hit_summary_by_day_tone)
# Parametric check
Hit.aov$residuals %>%
shapiro.test()
# Summary
summary(Hit.aov)
# Non-Parametric ANOVA
kruskal.test(Hit ~ BG_Intensity, data = Hit_summary_by_day_tone)
kruskal.test(Hit ~ Condition, data = Hit_summary_by_day_tone)
kruskal.test(Hit ~ Condition,
data = Hit_summary_by_day_tone %>%
filter(Stim == "tone" & BG_Intensity == "NA"))
kruskal.test(Hit ~ Condition,
data = Hit_summary_by_day_tone %>%
filter(Stim == "tone" & BG_Intensity == "50"))
# False Alarm Rate Analysis ----------------------------------------------------
# ANOVA
FA.aov = aov(FA ~ Condition * BG_Intensity, data = Hit_summary_by_day_tone)
# Parametric check
FA.aov$residuals %>%
shapiro.test()
# Summary
summary(FA.aov)
TukeyHSD(FA.aov)$`BG_Intensity` %>%
as_tibble(.name_repair = "unique", rownames = "Comparison")
TukeyHSD(FA.aov)$`Condition` %>%
as_tibble(.name_repair = "unique", rownames = "Comparison")
# Threshold Calculation ---------------------------------------------------
# Signal detection index calculation by the psycho package. We use d' a sensitivity measure.
# https://neuropsychology.github.io/psycho.R/2018/03/29/SDT.html
# Creates a properly formatted table for psycho by adding the overall CR/FA to each row
dprime_table <- function(df) {
# print(df)
check = df %>% filter(Type == 0) %>% count() %>% as.numeric() #%>% print
CRnum = (if (check == 1) filter(df, Type == 0) %>% .$C.R. %>% as.numeric() else check) #%>% print
FAnum = (if (check == 1) filter(df, Type == 0) %>% .$F.A. %>% as.numeric() else check) #%>% print
new_df = df %>% filter(Type == 1) %>% rename(CR = C.R., FA = F.A.) %>%
mutate(CR = ifelse(is.na(CR), CRnum, CR),
FA = ifelse(is.na(FA), FAnum, CR),
Hit = as.numeric(Hit),
Miss = as.numeric(Miss)) %>% replace(is.na(.), 0) #%>% print
return(new_df)
}
# Signal detection index calculation
dprime_calc <- function(df) {
# print(df)
dprime(n_hit = df$Hit,
n_fa = df$FA,
n_miss = df$Miss,
n_cr = df$CR,
adjusted = TRUE) %>%
as_tibble() %>%
mutate(dB = df$`Inten (dB)`,
Type = case_when(df$`Freq (kHz)` == 0 ~ "BBN",
TRUE ~ paste0(df$`Freq (kHz)`, "kHz"))# %>% print
) #%>% print
}
writeLines("Calculating Thresholds")
# Calculate d' and save (along with hit/miss/CR/FA table)
TH_data <-
Analysis_data %>%
group_by(ID, Sex, Condition, Stim, BG_Type, BG_Intensity, `Dur (ms)`, Type, `Freq (kHz)`, `Inten (dB)`, Response) %>% #View
summarise(count = n(), .groups = "keep") %>%
spread(Response, count) %>% #View
group_by(ID, Sex, Condition, Stim, BG_Type, BG_Intensity, `Dur (ms)`) %>% #print
nest() %>%
mutate(dprime_data = map(data, dprime_table),
dprime = map(dprime_data, dprime_calc)) %>% #print
unnest(dprime) #%>% print
# Threshold calculation calculation based on TH_cutoff intercept of fit curve
# LOESS: Local Regression is a non-parametric approach that fits multiple regressions
# see http://r-statistics.co/Loess-Regression-With-R.html
TH_calc <- function(df) {
# Uncomment to see line fitting by a package which shows line
# library(drda)
# drda(dprime ~ dB, data = df) %>% plot
fit = loess(dprime ~ dB, data = df)
# plot(fit)
TH = approx(x = fit$fitted, y = fit$x, xout = TH_cutoff)$y #%>% print
return(TH)
}
TH <-
TH_data %>%
select(ID:`Dur (ms)`, dprime, dB, Type) %>% #print
mutate(Type = fct_relevel(Type, levels = c("BBN", "4kHz", "8kHz", "16kHz", "32kHz"))) %>% #print
group_by(ID, Sex, Condition, BG_Type, BG_Intensity, `Dur (ms)`, Type) %>%
nest() %>%
mutate(TH = map_dbl(data, TH_calc)) %>%
select(-data) %>%
spread(Type, TH)
# Threshold Tables for Viewing --------------------------------------------
# Average Thresholds
Avg_TH_Condition <-
TH %>%
filter(Condition %in% c("Baseline", "Post HHL")) %>%
filter(!(ID %in% HL_not_done)) %>%
group_by(Condition, BG_Type, BG_Intensity) %>%
summarise("BBN_avg" = mean(BBN, na.rm = TRUE) %>% round(digits = 0),
"4kHz_avg" = mean(`4kHz`, na.rm = TRUE) %>% round(digits = 0),
"8kHz_avg" = mean(`8kHz`, na.rm = TRUE) %>% round(digits = 0),
"16kHz_avg" = mean(`16kHz`, na.rm = TRUE) %>% round(digits = 0),
"32kHz_avg" = mean(`32kHz`, na.rm = TRUE) %>% round(digits = 0),
.groups = "keep")
Avg_TH <-
TH %>%
filter(Condition %in% c("Baseline", "Post HHL")) %>%
filter(!(ID %in% HL_not_done)) %>%
group_by(BG_Type, BG_Intensity) %>%
summarise("BBN" = mean(BBN, na.rm = TRUE) %>% round(digits = 0),
"4kHz" = mean(`4kHz`, na.rm = TRUE) %>% round(digits = 0),
"8kHz" = mean(`8kHz`, na.rm = TRUE) %>% round(digits = 0),
"16kHz" = mean(`16kHz`, na.rm = TRUE) %>% round(digits = 0),
"32kHz" = mean(`32kHz`, na.rm = TRUE) %>% round(digits = 0),
.groups = "keep") %>%
gather(Type, TH, "BBN", "4kHz", "8kHz", "16kHz", "32kHz") %>%
mutate(Type = fct_relevel(Type, "BBN", "4kHz", "8kHz", "16kHz", "32kHz"))
# Change in D' ------------------------------------------------------------
# Calculate difference between pre- and post-hearing loss d'
dprime <-
TH_data %>%
filter(Condition %in% c("Baseline", "Post HHL")) %>%
filter(!(ID %in% HL_not_done)) %>%
select(ID, Sex, Condition, Type, BG_Type, BG_Intensity, `Dur (ms)`, dprime, dB) %>%
spread(Condition, dprime) %>%
mutate(dprime_change = `Post HHL` - Baseline,
Type = fct_relevel(Type, "BBN", "4kHz", "8kHz", "16kHz", "32kHz"),
BG = case_when(BG_Type == "NA" & BG_Intensity == "NA" ~ "Quiet",
BG_Type == "PNK" & BG_Intensity == "30" ~ "30dB Pink Noise Background",
BG_Type == "PNK" & BG_Intensity == "50" ~ "50dB Pink Noise Background",
BG_Type == "WN" & BG_Intensity == "50" ~ "50dB White Noise Background",
BG_Type == "PKN" & BG_Intensity == "30" ~ "30dB Pink Noise Background",
BG_Type == "PKN" & BG_Intensity == "50" ~ "50dB Pink Noise Background",
TRUE ~ "ISSUE") %>%
fct_relevel("Quiet", "30dB Pink Noise Background", "50dB Pink Noise Background", "50dB White Noise Background")) %>%
gather(Condition, dprime, "dprime_change", "Baseline", `Post HHL`)
# Reaction time calculation -----------------------------------------------
writeLines("Calculating average RXN time")
TH_filter <- function(df) {
# print(df)
ID = unique(df$ID) # %>% print
Condition = unique(df$Condition) # %>% print
BG_Intensity = unique(df$BG_Intensity) # %>% print
Dur = unique(df$`Dur (ms)`) # %>% print
kHz = unique(df$`Freq (kHz)`)
kHz = if_else(kHz == "0", "BBN", paste0(kHz,"kHz")) # %>% print
# print(TH)
# print(paste(ID))
cuttoff = TH %>% # have to use UQ to force the evaluation of the variable
filter(ID == UQ(ID) & Condition == UQ(Condition) & BG_Intensity == UQ(BG_Intensity) & `Dur (ms)` == UQ(Dur)) %>%
pull(UQ(kHz)) #%>% print
cuttoff = ifelse(identical(cuttoff, numeric(0)), -99, cuttoff) # %>% print
# ifelse(identical(cuttoff, numeric(0)), df, filter(df, `Inten (dB)` >= UQ(cuttoff))) %>% print
df %>%
filter(`Inten (dB)` >= UQ(cuttoff))
}
Data_over_TH <-
Analysis_data %>%
ungroup() %>%
# select(-data_trimmed, -Blocks_trimmed) %>%
# filter(ID == "Green 1") %>%
# filter(Stim == "BBN") %>%
filter(Type == 1 & Response == "Hit") %>%
filter(`Inten (dB)` != -100) %>%
mutate(Rat = .$ID, Con = .$Condition, BG = .$BG_Intensity, Dur = .$`Dur (ms)`, kHz = .$`Freq (kHz)`) %>%
group_by(Rat, Sex, Con, BG, Dur, kHz) %>%
nest %>%
mutate(data = map(data, TH_filter)) # %>% print
Rxn_overall <-
Data_over_TH %>%
ungroup() %>%
unnest(data) %>%
filter(Type == 1 & Response == "Hit") %>%
filter(`Inten (dB)` != -100) %>%
# Filter by TH table so that only reaction times above thresholds are included
group_by(ID, Sex, Condition, BG_Type, BG_Intensity, Stim, `Dur (ms)`, `Freq (kHz)`, `Inten (dB)`) %>%
do(describe(.$`R Time (ms)`)) %>%
as_tibble() %>%
select(-vars)
# Change in reaction time
Rxn_change <-
Rxn_overall %>%
filter(Condition %in% c("Baseline", "Post HHL")) %>%
filter(!(ID %in% HL_not_done)) %>%
select(ID, Sex, Condition, BG_Type, BG_Intensity, Stim, `Dur (ms)`, `Freq (kHz)`, `Inten (dB)`, mean) %>%
spread(Condition, mean) %>%
mutate(Rxn_change = `Post HHL` - Baseline) %>%
gather(Condition, mean, "Rxn_change")
# React time comparison between overall and daily averaging was basically identical, so daily dropped
Rxn_daily <-
Data_over_TH %>%
ungroup() %>%
unnest(data) %>%
# filter(Date > "2022-2-25" & Date < "2022-2-26") %>%
# filter(ID == "Green 1") %>%
group_by(ID, Date, Condition, BG_Type, BG_Intensity, Stim, `Dur (ms)`, `Freq (kHz)`, `Inten (dB)`) %>%
do(describe(.$`R Time (ms)`)) %>%
group_by(ID, Condition, BG_Type, BG_Intensity, `Dur (ms)`, `Freq (kHz)`, `Inten (dB)`) %>%
summarise(mean = mean(mean), .groups = "keep")
# React time comparison between averaging methods - basically identical
Rxn_overall %>%
select(ID:mean) %>%
left_join(., Rxn_daily,
by = c("ID", "Condition", "Dur (ms)", "Freq (kHz)", "Inten (dB)", "BG_Type", "BG_Intensity"),
suffix = c("", "_daily"))
# Rxn Comparison ----------------------------------------------------------
# ANOVA
Rxn.aov = aov(mean ~ Condition * `Freq (kHz)` * `Inten (dB)` * BG_Intensity, data = Rxn_overall)
# Parametric check
Rxn.aov$residuals %>%
shapiro.test()
# Summary
summary(Rxn.aov)
# TukeyHSD(Rxn.aov)