An R package to analyze the parliamentary records of the 19th legislative period of the Bundestag, the German parliament.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.4KB

  1. ---
  2. title: "funwithdata"
  3. output: rmarkdown::html_vignette
  4. vignette: >
  5. %\VignetteIndexEntry{funwithdata}
  6. %\VignetteEngine{knitr::rmarkdown}
  7. %\VignetteEncoding{UTF-8}
  8. ---
  9. ```{r, include = FALSE}
  10. knitr::opts_chunk$set(
  11. collapse = TRUE,
  12. comment = "#>"
  13. )
  14. ```
  15. ```{r setup}
  16. library(hateimparlament)
  17. library(dplyr)
  18. library(ggplot2)
  19. library(stringr)
  20. library(tidyr)
  21. ```
  22. ## Preparation of data
  23. First, you need to download all records of the current legislative period.
  24. ```r
  25. fetch_all("../inst/records/") # path to directory where records should be stored
  26. ```
  27. Second, those `.xml` files, need to be parsed into `R` `tibbles`. This is accomplished by:
  28. ```r
  29. read_all("../inst/records/") %>% repair() -> res
  30. ```
  31. We also used `repair` to fix a bunch of formatting issues in the records and unpacked
  32. the result into more descriptive variables.
  33. For development purposes, we load the tables from csv files.
  34. ```{r}
  35. res <- read_from_csv('../inst/csv/')
  36. ```
  37. and unpack our tibbles
  38. ```{r}
  39. comments <- res$comments
  40. speeches <- res$speeches
  41. speaker <- res$speaker
  42. talks <- res$talks
  43. applause <- res$applause
  44. ```
  45. ## Analysis
  46. Now we can start analysing our parsed dataset, e.g. find out which party gives the most talks:
  47. ```{r, fig.width=7}
  48. join_speaker(res$speeches, res) %>%
  49. group_by(fraction) %>%
  50. summarize(n = n()) %>%
  51. arrange(n) %>%
  52. bar_plot_fractions(title="Number of speeches given by fraction",
  53. ylab="Number of speeches")
  54. ```
  55. or counting the occurences of a given word:
  56. ```{r, fig.width=7}
  57. find_word(res, "Kohleausstieg") %>%
  58. filter(occurences > 0) %>%
  59. join_speaker(res) %>%
  60. select(content, fraction) %>%
  61. filter(!is.na(fraction)) %>%
  62. group_by(fraction) %>%
  63. summarize(n = n()) %>%
  64. arrange(desc(n)) %>%
  65. bar_plot_fractions(title = "Parties using the word 'Kohleausstieg' the most (absolutely)",
  66. ylab = "Number of uses of 'Kohleausstieg'",
  67. flipped = F)
  68. ```
  69. ### Who gives the most speeches?
  70. ```{r}
  71. res$speeches %>%
  72. group_by(speaker) %>%
  73. summarize(n = n()) %>%
  74. arrange(-n) %>%
  75. left_join(res$speaker, by=c("speaker" = "id")) %>%
  76. head(10)
  77. ```
  78. ### Who talks the longest?
  79. ```{r}
  80. res$talks %>%
  81. mutate(content_len = str_length(content)) %>%
  82. group_by(speaker) %>%
  83. summarize(avg_content_len = mean(content_len)) %>%
  84. arrange(-avg_content_len) %>%
  85. left_join(res$speaker, by=c("speaker" = "id")) %>%
  86. head(10)
  87. ```
  88. ### Which party gives the most applause to which parties?
  89. ```{r}
  90. res$applause %>%
  91. left_join(res$speaker, by=c("on_speaker" = "id")) %>%
  92. select(on_fraction = fraction, where(is.logical)) %>%
  93. group_by(on_fraction) %>%
  94. arrange(on_fraction) %>%
  95. summarize("AfD" = sum(`AfD`),
  96. "BÜNDNIS 90 / DIE GRÜNEN" = sum(`BUENDNIS_90_DIE_GRUENEN`),
  97. "CDU/CSU" = sum(`CDU_CSU`),
  98. "DIE LINKE" = sum(`DIE_LINKE`),
  99. "FDP" = sum(`FDP`),
  100. "SPD" = sum(`SPD`)) -> tb
  101. ```
  102. For plotting our results we reorganize them a bit and produce a bar plot:
  103. ```{r, fig.width=7}
  104. pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
  105. filter(!is.na(on_fraction)) %>%
  106. bar_plot_fractions(x_variable = on_fraction,
  107. y_variable = value,
  108. fill = by_fraction,
  109. title = "Number of rounds of applauses from fractions to fractions",
  110. xlab = "Applauded fraction",
  111. ylab = "Rounds of applauses",
  112. filllab = "Applauding fraction",
  113. flipped = FALSE)
  114. ```
  115. ### Which party comments the most on which parties?
  116. ```{r}
  117. res$comments %>%
  118. left_join(res$speaker, by=c("on_speaker" = "id")) %>%
  119. select(by_fraction = fraction.x, on_fraction = fraction.y) %>%
  120. group_by(on_fraction) %>%
  121. summarize(`AfD` = sum(str_detect(by_fraction, "AfD"), na.rm=T),
  122. `BÜNDNIS 90 / DIE GRÜNEN` = sum(str_detect(by_fraction, "BÜNDNIS 90/DIE GRÜNEN"), na.rm=T),
  123. `CDU/CSU` = sum(str_detect(by_fraction, "CDU/CSU"), na.rm = T),
  124. `DIE LINKE` = sum(str_detect(by_fraction, "DIE LINKE"), na.rm=T),
  125. `FDP` = sum(str_detect(by_fraction, "FDP"), na.rm=T),
  126. `SPD` = sum(str_detect(by_fraction, "SPD"), na.rm=T)) -> tb
  127. ```
  128. Analogously we plot the results:
  129. ```{r, fig.width=7}
  130. pivot_longer(tb, where(is.numeric), "by_fraction", "count") %>%
  131. filter(!is.na(on_fraction)) %>%
  132. bar_plot_fractions(x_variable = on_fraction,
  133. y_variable = value,
  134. fill = by_fraction,
  135. title = "Number of comments from fractions to fractions",
  136. xlab = "Commented fraction",
  137. ylab = "Number of comments",
  138. filllab = "Commenting fraction",
  139. flipped = FALSE)
  140. ```
  141. ### When are which topics discussed the most?
  142. ```{r, fig.width=7}
  143. pandemic_pattern <- "(?i)virus|corona|covid|lockdown"
  144. climate_pattern <- "(?i)klimawandel|erderwärmung|co2|treibhaus|methan|kyoto-protokoll|klimaabkommen"
  145. pension_pattern <- "(?i)rente|pension|altersarmut"
  146. word_usage_by_date(res, c(pandemic = pandemic_pattern,
  147. climate = climate_pattern,
  148. pension = pension_pattern)) %>%
  149. ggplot(aes(x = date, y = count, color = pattern)) +
  150. xlab("date of session") +
  151. ylab("occurence of word per session") +
  152. labs(color = "Topic") +
  153. geom_point()
  154. ```