-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_multinomial.R
47 lines (33 loc) · 1.17 KB
/
example_multinomial.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
# load packages ----------------------
source("packages/load_packages.R")
# ---------------------------------------------------------------------
# VISUALISATION OF A MULTINOMIAL DISTRIBUTION
# ---------------------------------------------------------------------
#set seed for replication
set.seed(1)
# Calculation of n=10.000 different election results
y <- NULL;
for (i in 1:10000)
{
tmp <- rmultin(191765, c(0.4, 0.27, 0.33) )
y <- rbind(y, tmp)
}
wk_10 <- data.frame(y)
# calculate the difference between first-placed and second-placed
wk_10 <- wk_10 %>% mutate(diff = X1 - X2)
#Density plot for the difference between first-placed and second-placed candidate
d <- density(wk_10$diff)
vp <- (wk_10 %>% prop(diff < 24517))
#rename visualisation
pdf("graphics/multinomial_distribution.pdf", 10, 5)
plot(d,
xlab= "difference (in absolute votes) between first and second placed candidate",
ylab="density", family = "serif",
main=NA) # plots the results
#set value for absolute number of citizen with migration background
value <- 24517
polygon(c(d$x[d$x <= value ], value),
c(d$y[d$x <= value ], 0),
col = "grey",
border = 1)
dev.off()