-
Notifications
You must be signed in to change notification settings - Fork 3
/
modeling_analyses.Rmd
162 lines (133 loc) · 6.61 KB
/
modeling_analyses.Rmd
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
---
title: "Prob switch analyses"
output: html_notebook
---
# Set switches
```{r Set up parameters}
library("ggplot2"); theme_set(theme_bw()); library("plyr"); library("reshape2"); library("R.matlab"); library("zoo")
gg_save = T
data_set = "PS"
parameter_names = c("alpha", "alpha_high", "beta", "beta_high", "epsilon", "TS_bias", "forget")
data_dir = paste0("C:/Users/maria/MEGAsync/SLCN/", data_set, "GenRecCluster/fit_par")
plot_dir = file.path(data_dir, "plots")
if (!dir.exists(plot_dir)) {
dir.create(plot_dir)
}
```
# Read in data
```{r Read in ages}
ages = read.csv("C:/Users/maria/MEGAsync/SLCNdata/SLCNinfo.csv")
ages$age_group = "Adults"
ages$age_group[ages$PreciseYrs < 18] = "Teens"
ages$age_group[ages$PreciseYrs < 13] = "Children"
ages$age_group = factor(ages$age_group, levels = c("Children", "Teens", "Adults"))
ages$PDS[ages$age_group == "Adults"] = 6
colnames(ages)[colnames(ages) == "ID"] = "sID"
ages
```
```{r Read in task data}
filenames = list.files(data_dir, pattern = "*[0-9]*.csv")
all_files = data.frame()
for(filename in filenames) {
# Read in subj file, add trial_index and ACC
subj_file = read.csv(file.path(data_dir, filename))
subj_file$trial_index = 1:nrow(subj_file)
subj_file$ACC = with(subj_file, selected_box == correct_box)
# Add columns for missing parameters
for (colname in c(parameter_names, paste0(parameter_names, "_rec"))) {
if (!colname %in% colnames(subj_file)) {
subj_file[,colname] = 0
}
}
# Attach to all_files
all_files = as.data.frame(rbind(all_files, subj_file))
}
for (col_name in c("model_name", "LL", parameter_names, paste0(parameter_names, "_rec"))) {
if (!col_name %in% colnames(all_files)) {
all_files[,col_name] = 0
}
}
all_files$X = all_files$Unnamed..0 = NULL
summary(all_files)
```
```{r Get parameter_dat}
# Get one row per dataset
parameter_dat = ddply(all_files, c("sID", "model_name_rec", "learning_style", "NLL_rec", parameter_names, paste0(parameter_names, "_rec")), summarize,
NLL = -LL[trial_index == max(trial_index)],
AIC = AIC_rec[1]) # NLL in the last trial of InitialLearning
# Split generated and recovered into two separate columns
parameter_long_gen = melt(subset(parameter_dat, select = c(parameter_names, "NLL", "sID", "model_name_rec")),
measure.vars = c(parameter_names, "NLL"), variable.name = "parameter", value.name = "gen_par")
parameter_long_rec = melt(subset(parameter_dat, select = c(paste0(parameter_names, "_rec"), "NLL_rec", "sID", "model_name_rec")),
measure.vars = c(paste0(parameter_names, "_rec"), "NLL_rec"), variable.name = "parameter", value.name = "rec_par")
parameter_long_rec$parameter = gsub("_rec", "", parameter_long_rec$parameter)
parameter_long = merge(parameter_long_gen, parameter_long_rec, by = c("sID", "model_name_rec", "parameter"))
# Make sure that the resulting dimensions are right
assertthat::assert_that(dim(parameter_long)[1] == (length(parameter_names) + 1) * dim(parameter_dat)[1])
parameter_AIC = reshape(subset(parameter_dat, select = c("AIC", "sID", "model_name_rec")),
direction = "wide", timevar = "model_name_rec", idvar = "sID")
parameter_AIC$counter_minus_Bayes = with(parameter_AIC, AIC.counter_flat_alpha_beta_epsilon - AIC.Bayes_epsilon)
parameter_AIC$simple_minus_Bayes = with(parameter_AIC, AIC.simple_flat_alpha_beta_epsilon - AIC.Bayes_epsilon)
parameter_AIC_long = melt(subset(parameter_AIC, select = c("sID", "counter_minus_Bayes", "simple_minus_Bayes")),
measure.vars = c("counter_minus_Bayes", "simple_minus_Bayes"), variable.name = "model", value.name = "AIC")
```
# Analyze
```{r Plot AIC scores of all models}
gg_AICs = ggplot(parameter_dat, aes(model_name_rec, AIC, color = learning_style)) +
stat_summary(fun.data = mean_se, geom = "bar") +
stat_summary(fun.data = mean_se, geom = "pointrange") +
geom_point(position = "jitter", alpha = 0.3) +
theme(legend.position = "none")
gg_AICs_rel = ggplot(parameter_AIC_long, aes(model, AIC)) +
stat_summary(fun.data = mean_se, geom = "bar") +
stat_summary(fun.data = mean_se, geom = "pointrange") +
geom_point(position = "jitter", alpha = 0.3)
if (gg_save) {
ggsave(file.path(plot_dir, "gg_AICs.png"), gg_AICs, width = 1 + length(unique(parameter_dat$learning_style)), height = 3)
ggsave(file.path(plot_dir, "gg_AICs_rel.png"), gg_AICs_rel, width = 1 + length(unique(parameter_dat$learning_style)), height = 3)
}
```
```{r Plot parameters over age}
parameter_long_age = merge(parameter_long, ages, by = "sID", all.x = T)
assertthat::assert_that(dim(parameter_long_age)[1] == dim(parameter_long)[1])
gg_params_across_age = ggplot(subset(parameter_long_age, rec_par != 0), aes(PreciseYrs, rec_par)) +
geom_point() +
geom_smooth() +
facet_wrap(model_name_rec ~ parameter, scales = 'free')
if (gg_save) {
ggsave(file.path(plot_dir, "gg_params_across_age.png"), gg_params_across_age, width = 6, height = 6)
}
```
```{r plot generated against recovered parameters}
# Find outliers
outliers = list()
parameter_long$gen_minus_rec = with(parameter_long, gen_par - rec_par)
for (par_name in unique(parameter_long$parameter)) {
par_rows = parameter_long$parameter == par_name
cutoff = quantile(abs(parameter_long[par_rows, "gen_minus_rec"]), .99)
outliers = c(outliers, subset(parameter_long, par_rows & (abs(gen_minus_rec) > cutoff))$sID)
}
parameter_long$outlier = parameter_long$sID %in% outliers
# Plot
gg_gr =
ggplot(subset(parameter_long, gen_par != 0 & rec_par != 0), aes(gen_par, rec_par - gen_par, color = outlier)) +
geom_point() +
geom_text(aes(label = ifelse(outlier, sID, ""), hjust = 0)) +
theme(legend.position = "none") +
facet_wrap(model_name_rec ~ parameter, scale = "free", ncol = 3)
gg_gen_rec = gg_gr + geom_abline(slope = 0, linetype = "dotted")
gg_gen_rec2 = gg_gr + aes(y = rec_par) + geom_abline(slope = 1, linetype = "dotted")
gg_gen_rec_lim = gg_gen_rec
gg_gen_rec_lim$data = subset(gg_gen_rec_lim$data, abs(gen_minus_rec) < 5)
gg_gen_rec2_lim = gg_gen_rec2
gg_gen_rec2_lim$data = subset(gg_gen_rec2_lim$data, abs(gen_minus_rec) < 5)
# Save plots
if (gg_save) {
ggsave(file.path(plot_dir, "gg_gen_rec.png"), gg_gen_rec, width = 6, height = 3 * length(unique(parameter_dat$model_name_rec)))
ggsave(file.path(plot_dir, "gg_gen_rec2.png"), gg_gen_rec2, width = 6, height = 3 * length(unique(parameter_dat$model_name_rec)))
ggsave(file.path(plot_dir, "gg_gen_rec_lim.png"), gg_gen_rec_lim, width = 6, height = 3 * length(unique(parameter_dat$model_name_rec)))
ggsave(file.path(plot_dir, "gg_gen_rec2_lim.png"), gg_gen_rec2_lim, width = 6, height = 3 * length(unique(parameter_dat$model_name_rec)))
}
```
```{r start button}
```