/
diagnostic.Rmd
61 lines (50 loc) · 1.24 KB
/
diagnostic.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
---
title: "Diagnostic"
author: "Andreas Bender"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Diagnostic}
%\VignetteEngine{knitr::rmarkdown}
---
```{r, echo = FALSE}
library(knitr)
opts_chunk$set(
fig.align = "center",
fig.width = 4,
fig.height = 4,
crop = TRUE)
```
```{r message=FALSE, warning=FALSE}
library(tidyr)
library(purrr)
library(dplyr)
library(coalitions)
library(ggplot2)
theme_set(theme_bw())
```
```{r, warning = FALSE}
# load latest emnid data
temp <- scrape_wahlrecht() %>% slice(1) %>% collapse_parties()
temp %>% unnest("survey")
# draw 10k samples from posterior
set.seed(29072017)
draws <- map(temp$survey, draw_from_posterior, nsim=1e4, correction=0.01) %>%
flatten_df()
draws_long <- gather(draws, party, percent, cdu:others) %>%
group_by(party) %>%
mutate(sim = row_number()) %>% ungroup()
```
```{r, fig.width=6, fig.height=6}
ggplot(draws_long, aes(x=party, y=percent)) +
geom_boxplot() +
geom_hline(yintercept = 0.05, lty=2, col=2)
## chains
ggplot(draws_long, aes(x=sim, y=percent)) +
geom_path() +
geom_hline(yintercept = 0.05, lty=2, col=2) +
facet_wrap(~party, nrow=2)
draws_long %>%
group_by(party) %>%
summarize(entryprob = sum(percent >= 0.05)/n())
```