---
title: "genderequality"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{genderequality}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(hateimparlament)
library(dplyr)
library(ggplot2)
library(stringr)
library(tidyr)
library(xml2)
```

## Preparation of data

First, you need to download all records of the current legislative period.
```r
fetch_all("../records/") # path to directory where records should be stored
```
Second, those `.xml` files, need to be parsed into `R` `tibbles`. This is accomplished by:
```r
read_all("../records/") %>% repair() -> res
```
We also used `repair` to fix a bunch of formatting issues in the records.

For development purposes, we load the tables from csv files.
```{r}
res <- read_from_csv('../inst/csv/')
```
and unpack our tibbles
```{r}
comments <- res$comments
speeches <- res$speeches
speaker <- res$speaker
talks <- res$talks
```

Bevor we can do our analysis, we have to assign a gender to our politicans. We do this
by reading the gender from the master data of all members of parliament, which is
fetched from bundestag.de.

```{r}
xml_get <- function(node, name) {
    res <- xml_text(xml_find_all(node, name))
    if (length(res) == 0) NA_character_
    else res
}

x <- read_xml("../inst/masterdata.xml")
mdbs <- xml_find_all(x, "MDB")

ids <- c()
genders <- c()
for (mdb in mdbs) {
    xml_get(mdb, "ID") -> mdb_id
    xml_find_first(mdb, "BIOGRAFISCHE_ANGABEN") %>%
        xml_get("GESCHLECHT") ->
        mdb_gender
    ids <- c(ids, mdb_id)
    genders <- c(genders, if (mdb_gender == "männlich") "male" else "female")
}

gender <- tibble(id = ids, gender = genders)
speaker_with_gender <- left_join(res$speaker, gender)
```

## Analyse

First, let's look at the relative distribution of the sexes throughout the whole Bundestag.

```{r}
speaker_with_gender %>% 
  select(gender) %>%
  group_by(gender) %>% 
  summarise("count" = n()) %>% 
  filter(gender %in% c("male", "female")) %>% 
  mutate(portion = 100*count/sum(count)) -> 
  plot1

bp <- ggplot(plot1, aes(x = "", y = portion, fill = gender))+
  geom_bar(width = 1, stat = "identity")
pie <- bp + coord_polar("y", start=0)
pie + 
  scale_fill_manual(values=c("pink", "blue")) +
  ggtitle("Relative distribution of sexes") +
  xlab("") +
  ylab("")
```

Next, we look at the individual distributions between men and women in the different fractions.

```{r, fig.width=7}
speaker_with_gender %>%
  group_by(fraction) %>%
  summarize(n = n()) ->
  fraction_size

  speaker_with_gender %>% 
  filter(gender=="female") %>% 
  group_by(fraction) %>% 
  summarize(n_female = n()) %>% 
  left_join(fraction_size) %>% 
  mutate(q = n_female/n) -> women_per_fraction
  bar_plot_fractions(women_per_fraction, x_variable=fraction, y_variable=q, title="Frauenanteil nach Partei")
```

Prepared with this knowledge, we can now analyse the relative amount of speeches by gender and fraction.

```{r, fig.width=7}
speaker_with_gender %>% transmute(speaker_id = id, gender, fraction) -> simple_speaker_with_gender
speeches %>% 
  transmute(id, speaker_id = speaker) %>% 
  inner_join(simple_speaker_with_gender) %>%
  group_by(fraction) %>%
  summarize(speeches=n()) -> 
  fraction_speeches_size

speeches %>% 
  transmute(id, speaker_id = speaker) %>% 
  inner_join(simple_speaker_with_gender) %>%
  filter(gender=='female') %>%
  group_by(fraction) %>%
  summarize(female_speeches=n()) %>%
  left_join(fraction_speeches_size) %>%
  left_join(women_per_fraction) %>%
  mutate(q_speeches = female_speeches/speeches) -> speech_distribution
  #bar_plot_fractions(speech_distribution, x_variable=fraction, y_variable=q_speeches, title="Redeanteil Frauen nach Partei")


party_order <- factor(c("Fraktionslos", "AfD&Fraktionslos",
                        "DIE LINKE", "BÜNDNIS 90/DIE GRÜNEN", "SPD", "CDU/CSU",
                        "FDP", "AfD", NA_character_))

speech_distribution %>% 
  mutate("Frauenanteil" = q, "Redenanteil Frauen" = q_speeches) %>% 
  pivot_longer(c(Frauenanteil, "Redenanteil Frauen"), "type") %>% 
  ggplot(aes(x=factor(fraction, levels = party_order), y=value, fill=factor(type, levels = factor(c("Frauenanteil", "Redenanteil Frauen"))))) + scale_fill_manual(values= c("Frauenanteil"="gray", "Redenanteil Frauen"="red")) + coord_flip() + geom_bar(stat="identity", position="dodge") + labs(fill="Kategorie")
  
```

For comparison, let's analyze the total differences in the amount of speeches given.
```{r}

speeches %>%
  group_by(speaker) %>%
  summarize(n = n()) %>%
  ungroup() %>% 
  arrange(-n) %>%
  join_speaker(res) %>%
  left_join(gender, by=c("speaker"="id")) %>% 
  group_by(gender) %>% 
  summarise(absolute=sum(n)) %>%
  filter(gender %in% c("female", "male")) %>%
  mutate(absolute2=absolute/sum(absolute)) %>% 
  mutate(portion=c(0.32, 0.68)) %>% 
  mutate(relative=absolute*(1-portion)) %>% 
  mutate(relative2=relative/sum(relative)) -> 
  plot3
```

At first lets take a look at the absolute difference in the amount of speeches by the two sexes.
```{r,fig.width=7}
barplot(plot3$absolute2,
        ylab = "amount of speeches",
        main = "Absolute comparison of speech shares",
        las = 1, 
        names.arg = c("women", "men"), 
        col = c("pink", "darkblue"),
        font.main = 4,
        cex.axis = 0.7)
```

Since there are more men represented in the German Bundestag, we now consider the relative proportions of speeches, depending on the ratio of men and women.
```{r, fig.width=7}
barplot(plot3$relative2,
        ylab = "amount of speeches",
        main = "Relative comparison of speech shares",
        las = 1, 
        names.arg = c("women", "men"), 
        col = c("pink", "darkblue"),
        font.main = 4,
        cex.axis = 0.7)
```
