forked from KennethEnevoldsen/Bachelor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlotCode.R
261 lines (189 loc) · 7.13 KB
/
PlotCode.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
p_load(RColorBrewer, Hmisc)
#loading large datasets
load(file = "result_df_full.rda")
load(file = "result_df_full_selfsim.rda")
load(file = "result_df_distribution.rda")
#loading 1v1 datasets
load(file = "result_df_RB_0ToM.rda")
load(file = "result_df_0ToM_2ToM.rda")
load(file = "result_df_2ToM_5ToM.rda")
result_df = result_df
### CREATING PLOTTING FUNCTIONS ###,l
#add ID function
add_ID <- function(result_df, add_op = F){
#DESCRIPTION
#INPUT
#OUTPUT
n_rounds <- max(result_df$round_nr)
n_players <- nrow(result_df)/n_rounds
result_df$ID <- as.numeric( #as. numeric since sapply returns a matrix
sapply(seq(1, n_players, by = 2), #creating a list of the first agent in each pair (jumping by two since it is a pair)
function(x) rep(c(x, x+1), n_rounds))) #creating ID for each pair equal to x and x+1 and repeating for each round
if (add_op){
result_df$op <- sapply(seq(nrow(result_df)),
function(x)
str_split(result_df$pair[x], " / ")[[1]][str_split(result_df$pair[x], " / ")[[1]] != result_df$player[x]])
}
return(result_df)
}
result_df <- add_ID(result_df, add_op = T)
result_sum <- result_df %>%
group_by(ID, player, op) %>%
summarise(total_point = sum(points), mean_choice = mean(choice)) %>%
arrange(desc(total_point))
# result_sum <- result_df %>%
# group_by(sim, player, op) %>%
# summarise(total_point = sum(points), mean_choice = mean(choice)) %>%
# arrange(desc(total_point))
result_sum
result_sum_sub <- subset(result_sum, !(op %in% c("RB", "SoftmaxTitTat", "WSLS")) & !(player %in% c("RB", "SoftmaxTitTat", "WSLS")) )
#create a compete matrix
quick_heatmap <- function(result_df, return_plot_df = F){
#DESCRIPTION
#INPUT
#OUTPUT
tmp_list <- NULL; i = 1
for (c_pair in unique(result_df$pair)){ #loop through all the pairs
p_subset <- subset(result_df, pair == c_pair)
#Player 1
player <- p_subset$player[1]
op <- p_subset$player[2]
points <- sum(p_subset$points[seq(1, nrow(p_subset), by = 2)]) #calculate points
tmp_list[[i]] <- data_frame(player = player, op = op, points = points) #save the variables
#Player 2
player <- p_subset$player[2]
op <- p_subset$player[1]
points <- sum(p_subset$points[seq(2, nrow(p_subset), by = 2)]) #calculate points
tmp_list[[i+1]] <- data_frame(player = player, op = op, points = points) #save the variables
i = i + 2
}
i
tmp_df <- tmp_list %>% bind_rows() #save the variables
unique(tmp_df$player)
p <- ggplot(tmp_df, aes(player, op)) +
geom_tile(aes(fill = points), colour = "white") +
scale_fill_gradient(low = "lightsteelblue1", high = "steelblue") +
theme_classic()
if (return_plot_df){
return(list(plot = p, plot_df = tmp_df))
} else{
return(p)
}
}
quick_heatmap(result_df, return_plot_df = F)
result_df_sub <- subset(result_df, !(op %in% c("RB", "TFT", "WSLS")) & !(player %in% c("RB", "TFT", "WSLS")))
quick_heatmap(result_df_sub, return_plot_df = F)
#
#parameter values plots #!#
#P-K plot
#INPUTS
quick_p_k_plot <- function(result_df, ID, blue = T){
p_k_v <- c()
hidden_states <- result_df$hidden_states[result_df$ID == ID]
for (i in 1:length(hidden_states)){
p_k_v <- c(p_k_v, hidden_states[[i]]$own_hidden_states$p_k)
}
level <- length(p_k_v)/length(hidden_states)
plot_df <- data_frame(round = seq(length(hidden_states)))
for (i in 1:level){
plot_df[,i+1] <- p_k_v[seq(i, length(p_k_v), by= level)]
colnames(plot_df)[i+1] <- paste("P(", i-1, ")", sep = "")
}
plot_df1 <- melt(plot_df , id.vars = 'round', variable.name = "level")
p <- ggplot(plot_df1, aes(round, value, color = level)) + geom_line()
if (blue){ #add color palette
p <- p + scale_color_brewer(palette="Blues", direction = -1) + theme_classic()
}
return(p)
}
quick_p_k_plot(result_df, ID = 2, blue = T)
#estimation errors
#we need to save prob of choosing 1
#and estimate for prob of op choosing 1
result_df = result_df
ID1 = 28
ID2 = 27
ID1_hidden_states <- result_df$hidden_states[result_df$ID == ID1]
ID2_hidden_states = result_df$hidden_states[result_df$ID == ID2]
e_p_op_1 <- c()
a_p_op_1 <- c()
for (i in 1:length(hidden_states)){
e_p_op_1 <- c(e_p_op_1, p_op_1_k_fun(ID1_hidden_states[[i]])) #estimate probability of op. choosing 1
a_p_op_1 <- c(a_p_op_1, basic_p_op_1_fun(ID2_hidden_states[[i]])) #actual probability of op. choosing 1
}
plot(seq(length(hidden_states)), #the trials
a_p_op_1-e_p_op_1 #the predictions error
)
length(a_p_op_1)
length(e_p_op_1)
dens(p_op_1)
#0-ToM plots
#Variance
variance_basic <- c()
hidden_states <- result_df$hidden_states[result_df$ID == "6"]
for (i in 1:length(hidden_states)){
variance_basic <- c(variance_basic, result_df$hidden_states[result_df$ID == "6"][[i]]$own_hidden_states$variance_basic)
}
plot(seq(length(hidden_states)), #the trials
exp(variance_basic)
)
#mean
mean_basic <- c()
for (i in 1:length(hidden_states)){
mean_basic <- c(mean_basic, result_df$hidden_states[result_df$ID == "6"][[i]]$own_hidden_states$mean_basic)
}
plot(seq(length(hidden_states)), #the trials
inv.logit(mean_basic)
)
#p_op_1
e_p_op_1 <- c()
for (i in 1:length(hidden_states)){
e_p_op_1 <- c(e_p_op_1, basic_p_op_1_fun(hidden_states[[i]]))
}
plot(seq(length(hidden_states)), #the trials
(e_p_op_1)
)
###Repeated simulations
quick_pwon_plot <- function(d, agent){
d <- dplyr::select(d, -c(hidden_states))
#create a commulative sum and percentage won
d <- d %>%
group_by(sim, player) %>%
mutate(cumsum = cumsum(points), p_won = ((round_nr + cumsum(points))/2)/round_nr)
dp <- subset(d, player == agent)
dp <- dp %>% group_by(round_nr) %>% summarise(m_p_won = mean(p_won)) %>% merge(., dp)
ggplot(dp, aes(round_nr, p_won)) +
stat_summary(fun.data=mean_cl_boot, geom="ribbon", alpha = 0.25, fill = "steelblue") +
stat_summary(fun.data=mean_cl_boot, geom="ribbon", alpha = 0.30, fill = "steelblue", fun.args = list(conf.int = .5)) +
geom_line(aes(round_nr, m_p_won), size = 0.3) + labs(x = "Round", y = "Win %") + theme_linedraw()
}
#0-ToM vs RB
#fixing sim
d <- result_df_RB_0ToM
d$sim <- sort(rep(seq(1,100), 400))
quick_pwon_plot(d = d , agent = "0-ToM") + labs(title = "0-ToM vs RB - 0 ToM Perspevtive")
ggsave("0ToMvsRB.png", width = 20, height = 18, units = "cm", path = "plots")
#2-ToM vs 5
#fixing sim
d <- result_df_2ToM_5ToM
d$sim <- sort(rep(seq(1,100), 400))
quick_pwon_plot(d = d , agent = "5-ToM") + labs(title = "2-ToM vs 5-ToM - 5 ToM perspective")
ggsave("2ToMvs5ToM.png", width = 20, height = 18, units = "cm", path = "plots")
#0-ToM vs 2
#fixing sim
d <- result_df_0ToM_2ToM
d$sim <- sort(rep(seq(1,100), 400))
quick_pwon_plot(d = d , agent = "2-ToM") + labs(title = "0-ToM vs 2-ToM - 2 ToM Perspevtive")
ggsave("0ToMvs2ToM.png", width = 20, height = 18, units = "cm", path = "plots")
#lignende for p_k med estimation af modstanderens reelle level
# plots vi skal have
#cumsum plot
#RB vs 0ToM
#2 vs 5
#heatmap
#all
#self sim
#level estimation
#5 vs 2 - conf int
#parameter estimation
#0-ToM vs RB