-
Notifications
You must be signed in to change notification settings - Fork 0
/
contrast.Rmd
342 lines (276 loc) · 9.9 KB
/
contrast.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
---
title: "Analysis_ABM"
author: "Lina Walkowiak"
date: "2024-05-22"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Download packages
```{r}
pacman::p_load(hrbrthemes, tidyverse, dplyr, dagitty, caret, pacman, rmarkdown, knitr)
if (!require("pacman")) install.packages("pacman")
pacman::p_load(magrittr, dplyr, purrr, forcats, tidyr, modelr, tidybayes, tidybayes.rethinking,
ggplot2, cowplot, rstan, ggrepel, RColorBrewer, gganimate, brms)
```
```{r}
data <- read.csv("/Users/lina/Documents/4thsemester/dynamics/all_things_exam/sim_data2.csv")
```
```{r}
data$name <- as.integer(factor(data$name))
data$intervention <- as.integer(data$intervention+1)
# 0 no interevention
# 3 +6 taxes
# 2 +4 taxes
# 1 +2 taxes
dat_list <- list(
agent = data$name,
decision = data$decision,
intervention = data$intervention
)
# MODEL
# Fit the model
model <- ulam(
alist(
decision ~ dbinom(1, p),
logit(p) <- a[agent] + b[intervention],
a[agent] ~ dnorm(0, 1),
b[intervention] ~ dnorm(0, 1)
),
data = dat_list,
chains = 4,
cores = 4,
log_lik = TRUE
)
# Summarize the model
model_summary <- precis(model, depth = 2)
# Convert summary to data frame
summary_df <- as.data.frame(model_summary)
library(xtable)
xt = xtable(summary_df)
print(xt,
tabular.environment = "longtable",
floating = FALSE
)
```
```{r}
# Select the all agents
selected_agents <- 1:61
# Simulate data for posterior predictions for all four interventions for the selected agents
dat_list <- lapply(1:4, function(i) list(agent=selected_agents, intervention=rep(i, length(selected_agents))))
logit_posts <- lapply(dat_list, function(dat) link(model, data=dat, n=1e4))
# Calculate mean logit probabilities and credible intervals for all interventions
logit_mu <- sapply(logit_posts, function(post) apply(post, 2, mean))
ci_logit <- lapply(logit_posts, function(post) apply(post, 2, function(x) HPDI(x, prob=0.95)))
# Convert to data frame for plotting
logit_df <- data.frame(
agent = rep(selected_agents, each=4),
intervention = rep(1:4, times=length(selected_agents)),
logit_mu = as.vector(logit_mu),
lower = unlist(lapply(ci_logit, function(ci) ci[1, ])),
upper = unlist(lapply(ci_logit, function(ci) ci[2, ]))
)
# Adjust x-axis positions for interventions with more spread
logit_df$x <- rep(1:length(selected_agents), each=4) + rep(seq(-0.3, 0.3, length.out=4), times=length(selected_agents))
# Define the color palette
palette <- c('#33576E', '#BBCF9B', '#C7DBE2', '#498B6D')
# Plot the results
plot(NULL, xlim=c(0.5, length(selected_agents) + 0.5), ylim=c(min(logit_df$lower), max(logit_df$upper)),
xlab="Agent", ylab="Logit Probability", xaxt="n", yaxt="n")
axis(1, at=1:length(selected_agents), labels=selected_agents, las=2)
axis(2, at=seq(min(logit_df$lower), max(logit_df$upper), length.out=5))
abline(h=0, lty=2)
# Add error bars, points, and lines connecting points for each agent
for (i in selected_agents) {
agent_data <- subset(logit_df, agent == i)
for (j in 1:4) {
points(agent_data$x[j], agent_data$logit_mu[j], pch=16, col=palette[j])
arrows(x0=agent_data$x[j], y0=agent_data$lower[j],
x1=agent_data$x[j], y1=agent_data$upper[j],
angle=90, code=3, length=0.05, col=palette[j])
}
lines(agent_data$x, agent_data$logit_mu, col="gray")
}
# Optional: add labels or additional text
# text(x, y, labels, pos, cex) can be used to add text if needed
```
# Model plots
```{r}
post <- extract.samples(model)
intervention <- inv_logit( post$b )
intervention_0 <- intervention[,1]
intervention_1 <- intervention[,2]
intervention_2 <- intervention[,3]
intervention_3 <- intervention[,4]
contrast_1 <- intervention[,2] - intervention[,1]
contrast_2 <- intervention[,3] - intervention[,1]
contrast_3 <- intervention[,4] - intervention[,1]
# some plotting
plot( precis( as.data.frame(intervention) ) , xlim=c(0,1), main ="Intervention effects" )
precis(as.data.frame(intervention), xlim=c(0,1), main ="Intervention effects" )
# Load necessary packages
library(gt)
# Create a data frame with the summary statistics
summary_data <- data.frame(
Variable = c("Baseline", "+2%", "+4%", "+6%"),
Mean = c(0.2, 0.2, 0.2, 0.2),
SD = c(0.05, 0.06, 0.05, 0.05),
Percentile_5.5 = c(0.12, 0.12, 0.12, 0.12),
Percentile_94.5 = c(0.29, 0.29, 0.29, 0.29),
Histogram = c("▁▃▇▇▂▁▁▁", "▁▃▇▅▂▁▁▁", "▁▃▇▅▂▁▁▁", "▁▃▇▅▂▁▁▁")
)
# Set the color palette
my_palette <- '#BBCF9B' #'#C7DBE2' #'#498B6D'
# Create a nice table using gt
summary_data %>%
gt() %>%
tab_header(
title = "Posterior estimates"
) %>%
tab_style(
style = cell_fill(color = my_palette[1]),
locations = cells_body()
) %>%
tab_style(
style = cell_text(color = "white"),
locations = cells_body()
) %>%
tab_style(
style = cell_text(weight = "bold"),
locations = cells_column_labels()
)
```
```{r}
library(ridgeline)
library(ggridges)
palette <- c('#33576E', '#BBCF9B', '#498B6D', '#C7DBE2')
# In nicer format
intervention_df <- data.frame(
intervention_0 = intervention_0,
intervention_1 = intervention_1,
intervention_2 = intervention_2,
intervention_3 = intervention_3
)
contrast_df <- data.frame(
contrast_1 = contrast_1,
contrast_2 = contrast_2,
contrast_3 = contrast_3
)
intervention_long <- pivot_longer(intervention_df, everything(), names_to = "intervention", values_to = "estimate")
contrast_long <- pivot_longer(contrast_df, everything(), names_to = "contrast", values_to = "estimate")
# First plot
ridgeline(intervention_long$estimate, intervention_long$intervention,
palette = palette,
border = palette,
labels = c("No Tax", "+2%", "+4%", "+6%"),
main = "Posterior Estimates: Intervention Effects")
# Labels
intervention_labels <- c( "+2%", "+4%", "+6%")
# Create ggplot
ggplot(contrast_long, aes(x = estimate, y = contrast, fill = contrast)) +
geom_density_ridges(scale = 1) +
scale_fill_manual(values = palette) +
labs(title = "Posterior Estimates: Intervention Effects",
x = "Posterior Estimate",
y = "Intervention") +
scale_y_discrete(labels = intervention_labels) +
theme_ipsum() +
theme(legend.position = "none")
# Plot
# Create ggplot
ggplot(contrast_long, aes(x = estimate, color = contrast)) +
geom_density(alpha = 0.2) +
#scale_fill_manual(values = palette) +
scale_color_manual(values = palette, labels = c("+2%", "+4%", "+6%")) +
labs(title = "",
subtitle = "Contrasts between intervention and baseline on the probability scale",
x = "Posterior Estimate",
y = "Density",
fill = "Intervention",
color = "Intervention") +
geom_vline(xintercept = 0, linetype = "dashed") +
theme_ipsum() +
theme(legend.position = "right")
```
# Exploration
```{r}
exp <- read.csv("/Users/lina/Documents/4thsemester/dynamics/all_things_exam/gradual_df_3.csv")
exp$iteration <- 1:293
exp$price_development <- (price_development_5+14 /14) -1
exp$per_vegans <- exp$num_vegans_5/61*100
```
```{r}
# Find the iterations where per_vegans is 50, 75, and the max per_vegans
iteration_50 <- exp$iteration[which.min(abs(exp$per_vegans - 50))]
iteration_75 <- exp$iteration[which.min(abs(exp$per_vegans - 75))]
iteration_max <- exp$iteration[which.max(exp$per_vegans)]
# Extract the corresponding y-values
y_price_50 <- exp$price_development[which.min(abs(exp$per_vegans - 50))] * 100
y_price_75 <- exp$price_development[which.min(abs(exp$per_vegans - 75))] * 100
y_price_max <- exp$price_development[which.max(exp$per_vegans)] * 100
# Extract the y-values for the vegan milk consumers
y_vegans_50 <- exp$per_vegans[which.min(abs(exp$per_vegans - 50))]
y_vegans_75 <- exp$per_vegans[which.min(abs(exp$per_vegans - 75))]
y_vegans_max <- exp$per_vegans[which.max(exp$per_vegans)]
# Plotting
ggplot(exp, aes(x=iteration)) +
geom_line(aes(y=per_vegans), size=1, color='#33576E', alpha=0.9) +
geom_line(aes(y=price_development*100), size=0.8, color='#498B6D') +
scale_y_continuous(
sec.axis = sec_axis(~./100, name= "Relative price of dairy milk")
) +
labs(
x = "Iteration",
y = "Vegan milk consumers (%)",
title = " ",
subtitle = "Percentage of vegans over time (continuous increase of dairy price)",
color = "Variable"
) +
theme(
legend.position = "bottom",
axis.title.y.right = element_text(colour="black")
) +
theme_ipsum() +
annotate("text", label = "% vegans", x = 130, y = 70, size = 4, colour = "#33576E", family = "mono") +
annotate("text", label = "Price", x = 130, y = 21, size = 4, colour = "#498B6D", family = "mono") +
geom_segment(aes(x=iteration_50, xend=iteration_50, y=y_price_50, yend=y_vegans_50), linetype="dashed") +
geom_segment(aes(x=iteration_75, xend=iteration_75, y=y_price_75, yend=y_vegans_75), linetype="dashed") +
geom_segment(aes(x=iteration_max, xend=iteration_max, y=y_price_max, yend=y_vegans_max), linetype="dashed")
```
```{r}
summary_data <- data.frame(
Iteration = c(iteration_50, iteration_75, iteration_max),
Relative_Price = c(y_price_50, y_price_75, y_price_max),
Percentage_Vegans = c(y_vegans_50, y_vegans_75, y_vegans_max)
)
# Set the color palette
my_palette <- '#BBCF9B'
# Create a nice table using gt
summary_data %>%
gt() %>%
tab_header(
title = "Exploratory simulation"
) %>%
tab_style(
style = cell_fill(color = my_palette),
locations = cells_body()
) %>%
tab_style(
style = cell_text(color = "white"),
locations = cells_body()
) %>%
tab_style(
style = cell_text(weight = "bold"),
locations = cells_column_labels()
)
```
```{r}
# Survey data for calculations
data <- read.csv("/Users/lina/Documents/4thsemester/dynamics/all_things_exam/data1/dataset.csv")
df <- data[,-c(1,2, 6, 34:43) ]
df <- na.omit(df)
sum(df$dairymilk_week+df$plantmilk_week)/length(df$dairymilk_week)*52
sum(df$dairymilk_week)/length(df$dairymilk_week)*52
sum(df$plantmilk_week)/length(df$dairymilk_week)*52
```