-
Notifications
You must be signed in to change notification settings - Fork 2
/
03_visualize_cond.R
212 lines (181 loc) · 9.65 KB
/
03_visualize_cond.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
# Group-level visualizations
# 9.13.19 KLS update 8.17.20
# load required packages
library(here)
library(tidyverse)
library(ggplot2)
# load source functions
source(here::here('scr', 'add_tt_number.R'))
# set hard-coded variables
# read in and concatenate data for group visualization
dt <- read.csv(here::here('data', 'socialAL_clean_data.csv'))
d1 <- read.csv(here::here('output', 'model_comparison.csv'))[c(1,23)]
dt <- merge(dt, d1, by = 'id')
rm(d1)
# reorder trial_type and age group factors
dt$trial_type <- factor(dt$trial_type, levels = c('Untrustworthy', 'Neutral', 'Trustworthy'))
dt$agegrp <- factor(dt$agegrp, levels = c('Younger', 'Older'))
#learners
dt$learn <- ifelse(dt$win == 'baseline', 0, 1)
dt$learn <- factor(dt$learn)
## Graph 1 - Group means ####
## ---------------------
# calculate individual means
indiv_means <- dt %>%
group_by(id, agegrp, trial_type) %>%
summarise(avg_amount = mean(amount_shared, na.rm = TRUE))
# calculate age group means
se <- function(sd,n) {sd/sqrt(n())}
grpmeans <- indiv_means %>%
group_by(agegrp, trial_type) %>%
summarise(mean_amount = mean(avg_amount), sd_amount = sd(avg_amount),
se_amount = sd(avg_amount)/sqrt(n()))
# graph constants
lg = 18 # text size
sm = 14
custom_plot = list(theme(
plot.title = element_text(size = 24),
axis.title.x = element_text(size = lg), axis.text.x = element_text(size = sm),
axis.title.y = element_text(size = lg), axis.text.y = element_text(size = sm),
legend.title = element_text(size = lg), legend.text = element_text(size = sm),
strip.text.x = element_text(size = lg))
)
# graph group means
age_grp_means <- ggplot(grpmeans, aes(trial_type, mean_amount, colour = agegrp, fill= agegrp)) +
geom_bar(position=position_dodge(), stat='identity') +
geom_errorbar(aes(ymin=mean_amount - se_amount, ymax = mean_amount + se_amount),
width = .2, position=position_dodge(.9)) + theme_minimal() +
scale_fill_brewer(palette="Set1", name="Age Group") +
scale_colour_brewer(palette="Set1", name="Age Group") +
xlab('Trial Type') + ylab('Average $ Shared') + coord_cartesian(ylim=c(0, 9)) +
scale_y_continuous(breaks = c(0,3, 6, 9)) + custom_plot #+
#ggtitle('Baseline Model') + theme(plot.title = element_text(hjust = 0.5))
age_grp_means
#ggsave(here::here('figs', 'age_grp_means.png'))
ggsave(here::here('figs', 'baseline_beh_age_grp_means.png'))
# pretty graph
beh <- ggplot() +
geom_point(data = indiv_means, aes(x = trial_type, y = avg_amount, colour = agegrp),
position = position_jitterdodge(jitter.height = .25), alpha = .5) +
geom_bar(data = grpmeans, aes(x = trial_type, y = mean_amount, colour = agegrp, fill = agegrp),
position=position_dodge(), stat= 'identity', alpha = 0.3) +
geom_errorbar(data = grpmeans, aes(x = trial_type, y = mean_amount, ymin = mean_amount-se_amount, ymax = mean_amount + se_amount, colour = agegrp),
position = position_dodge(.9), width = .2) +
xlab('Partner Type') + ylab('Average $ Shared') + coord_cartesian(ylim=c(0, 9)) +
scale_y_continuous(breaks = c(0,3, 6, 9)) +
scale_fill_brewer(palette="Set1", name="Age Group") + theme_minimal() +
scale_colour_brewer(palette="Set1", name="Age Group") + custom_plot
saveRDS(beh, here::here('figs', 'bbg.RDS'))
## Graph 2 - Change over time ####
## --------------------------
dt <- add_tt_number(dt)
# calculate age group x trial_type x trial means
d3 <- dt %>%
dplyr::group_by(agegrp, trial_type, tt_number) %>%
summarise(mean_amount = mean(amount_shared, na.rm = TRUE),
sd_amount = sd(amount_shared, na.rm = TRUE),
se_amount = sd(amount_shared, na.rm = TRUE)/sqrt(n()))
# graph trial_type over time
trial_type_by_time <- ggplot(d3, aes(tt_number, mean_amount, colour = trial_type, fill = trial_type)) +
geom_point() + geom_smooth(method=lm) + facet_grid(. ~ agegrp) +
xlab('Trial') + ylab('Average $ Shared') + theme_minimal() +
scale_fill_brewer(palette="Dark2", name="Condition") +
scale_colour_brewer(palette="Dark2", name="Condition") +
scale_x_continuous(breaks = c(3, 6, 9, 12, 15)) +
coord_cartesian(ylim=c(0, 9)) + scale_y_continuous(breaks = c(3, 6, 9)) + custom_plot +
theme(strip.text.x = element_text(size=lg), legend.position="bottom")
trial_type_by_time
ggsave(here::here('figs', 'grp_means_over_time.png'))
# graph raw data
# need to add something for overplotting
behxtime <- ggplot(dt, aes(tt_number, amount_shared, colour = trial_type, fill = trial_type)) +
geom_smooth(method=lm) + facet_grid(. ~ agegrp) +
xlab('Trial') + ylab('Amount $ Shared') + geom_jitter(size=1, alpha=0.2, width=0.3) +
scale_fill_brewer(palette="Dark2", name="Partner Type") +
scale_colour_brewer(palette="Dark2", name="Partner Type") +
scale_x_continuous(breaks = c(3, 6, 9, 12, 15)) +
coord_cartesian(ylim=c(-1, 10)) + scale_y_continuous(breaks = c(0,3, 6, 9)) +
theme_minimal() + custom_plot + theme(strip.text.x = element_text(size=lg)) +
theme(legend.position = 'bottom')
#ggsave(here::here('figs', 'all_data_over_time_no_legend.png'))
ggsave(here::here('figs', 'baseline_all_beh_over_time.png'))
saveRDS(behxtime, here::here('figs', 'bot.RDS'))
## Graph 3 - Change over stage ####
dt <- add_stage(dt)
# calculate individual mean summary table by stage
indiv_means_stage <- dt %>%
dplyr::group_by(id, agegrp, trial_type, stage) %>%
summarize(avg_amount = mean(amount_shared, na.rm = TRUE))
grp_means_stage <- indiv_means_stage %>%
dplyr::group_by(agegrp, trial_type, stage) %>%
summarize(sd_amount = sd(avg_amount), se_amount = sd(avg_amount)/sqrt(n()),
avg_amount = mean(avg_amount))
# graph trial_type over stage ####
ggplot(grp_means_stage, aes(stage, avg_amount, colour = trial_type)) +
geom_point() + geom_line() +
geom_errorbar(aes(ymin = avg_amount - se_amount, ymax =avg_amount + se_amount, width=.1)) +
facet_grid(.~agegrp) + xlab('Trial Bin') + ylab('Average $ Shared') + theme_minimal() +
scale_fill_brewer(palette="Dark2", name="Condition") +
scale_colour_brewer(palette="Dark2", name="Condition") +
scale_x_continuous(breaks = c(1,2,3)) +
coord_cartesian(ylim=c(0, 9)) + scale_y_continuous(breaks = c(0, 3, 6, 9)) + custom_plot
#ggsave(here::here('figs', 'grp_means_over_stage.png'))
# pretty graph ####
behxstage <- ggplot() +
geom_point(data = indiv_means_stage, aes(x = stage, y = avg_amount, color = trial_type),
position = position_jitterdodge(jitter.height = .25), alpha = .5) +
geom_bar(data = grp_means_stage, aes(x = stage, y = avg_amount, color = trial_type, fill = trial_type),
position=position_dodge(), stat= 'identity', alpha = 0.3) +
geom_errorbar(data = grp_means_stage, aes(x = stage, y = avg_amount, ymin = avg_amount - se_amount, ymax = avg_amount + se_amount, color = trial_type),
position = position_dodge(.9), width = .2) +
facet_grid(.~agegrp) + xlab('Trial Bin') + ylab('Average $ Shared') + theme_minimal() +
scale_fill_brewer(palette="Dark2", name="Partner Type") +
scale_colour_brewer(palette="Dark2", name="Partner Type") + scale_x_continuous(breaks = c(1,2,3)) +
coord_cartesian(ylim=c(0, 9)) + scale_y_continuous(breaks = c(0, 3, 6, 9)) +
theme(legend.position="bottom") + custom_plot
saveRDS(behxstage, here::here('figs', 'bbs.RDS'))
# Graph 4 - Difference scores (by Age Group) ####
library(tidyr)
dw <- pivot_wider(indiv_means, id_cols = c(id, agegrp), names_from = trial_type, values_from = avg_amount)
dw$tudiff <- dw$Trustworthy - dw$Untrustworthy
dw$tndiff <- dw$Trustworthy - dw$Neutral
ggplot(dw, aes(agegrp, tudiff, color = agegrp)) + geom_violin(trim= FALSE) + geom_point(alpha = .5) +
scale_color_brewer(palette="Set1") + theme_minimal() +
xlab('Age Group') + ylab('Difference in Trust \n(Trustworthy - Untrustworthy)') +
geom_hline(aes(yintercept = 0)) + theme(legend.position = 'none') + custom_plot
#ggsave(here::here('figs', 'grp_means_tu_diff_score.png'))
#ggsave(here::here('figs', 'baseline_grp_means_tu_diff_score.png'))
ggplot(dw, aes(agegrp, tndiff, color = agegrp)) + geom_violin(trim= FALSE) + geom_point(alpha = .5) +
scale_color_brewer(palette="Set1") + theme_minimal() +
xlab('Age Group') + ylab('Difference in Trust \n(Trustworthy - Neutral)') +
geom_hline(aes(yintercept = 0)) + theme(legend.position = 'none') + custom_plot
# calculate learner group means ####
# calculate individual means
indiv_means <- dt %>%
group_by(id, learn, trial_type) %>%
summarise(avg_amount = mean(amount_shared, na.rm = TRUE))
se <- function(sd,n) {sd/sqrt(n())}
lgrpmeans <- indiv_means %>%
group_by(learn, trial_type) %>%
summarise(mean_amount = mean(avg_amount), sd_amount = sd(avg_amount),
se_amount = sd(avg_amount)/sqrt(n()))
# graph constants
lg = 18 # text size
sm = 14
custom_plot = list(theme(
plot.title = element_text(size = 24),
axis.title.x = element_text(size = lg), axis.text.x = element_text(size = sm),
axis.title.y = element_text(size = lg), axis.text.y = element_text(size = sm),
legend.title = element_text(size = lg), legend.text = element_text(size = sm),
strip.text.x = element_text(size = lg))
)
# graph learner group means
learn_grp_means <- ggplot(lgrpmeans, aes(trial_type, mean_amount, colour = learn, fill= learn)) +
geom_bar(position=position_dodge(), stat='identity') +
geom_errorbar(aes(ymin=mean_amount - se_amount, ymax = mean_amount + se_amount),
width = .2, position=position_dodge(.9)) + theme_minimal() +
scale_fill_brewer(palette="Set1", name="Learner Group") +
scale_colour_brewer(palette="Set1", name="Learner Group") +
xlab('Trial Type') + ylab('Average $ Shared') + coord_cartesian(ylim=c(0, 9)) +
scale_y_continuous(breaks = c(0,3, 6, 9)) + custom_plot #+
learn_grp_means