Created
June 20, 2017 14:35
-
-
Save chrishanretty/941f6f3599dedee33db498c00f7f0279 to your computer and use it in GitHub Desktop.
Get stats on minority government from ParlGov
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
### Make sure to download files from www.parlgov.org! | |
elec <- read.csv("view_election.csv") | |
cab <- read.csv("view_cabinet.csv") | |
party <- read.csv("view_party.csv") | |
### Date | |
cab$start_date <- as.Date(cab$start_date) | |
### Filter to post-1992 cabinets | |
cab <- subset(cab, start_date > as.Date("1992-01-01")) | |
nUnique <- length(unique(cab$cabinet_id)) | |
### For each cabinet, get whether minority or not | |
cab <- cab %>% | |
group_by(cabinet_id) %>% | |
mutate(cabinet_seats = sum(seats[which(cabinet_party == 1)], na.rm = TRUE), | |
isMinority = cabinet_seats < 0.5 * election_seats_total) | |
### What proportion of cabinets are minority cabinets? | |
min_pct <- cab %>% | |
distinct(cabinet_id, isMinority) %>% | |
ungroup() %>% | |
summarize(percentage = mean(isMinority)) | |
min_pct2 <- cab %>% | |
filter(caretaker == 0) %>% | |
distinct(cabinet_id, isMinority) %>% | |
ungroup() %>% | |
summarize(percentage = mean(isMinority)) | |
### Of these, what proportion are single party? | |
min_nparties <- cab %>% | |
filter(isMinority == TRUE & cabinet_party == 1) %>% | |
distinct(cabinet_id, party_id) %>% | |
group_by(cabinet_id) %>% | |
summarize(nParties = length(unique(party_id))) %>% | |
group_by(nParties) %>% | |
summarize(n = n()) %>% | |
mutate(freq = n / sum(n)) | |
### What about their duration? | |
### We need first to create a duration variable | |
### See https://gist.github.com/hdigital/9bdca6a2f605a27d5d28ca90b7752759 | |
cab_dur <- cab %>% | |
mutate(start_date = as.Date(start_date)) %>% | |
select(cabinet_id, previous_cabinet_id, start_date) %>% | |
distinct | |
cab_dur <- cab_dur %>% | |
inner_join(cab_dur, by = c('cabinet_id' = 'previous_cabinet_id')) %>% | |
mutate(duration = start_date.y - start_date.x) %>% | |
select(cabinet_id, duration) | |
cab <- cab %>% left_join(cab_dur) | |
min_durat <- cab %>% | |
distinct(isMinority, cabinet_id, duration) %>% | |
group_by(isMinority) %>% | |
summarize(n = n(), | |
meanDurat = mean(duration, na.rm = TRUE), | |
OverFour = sum(duration > 365.25 * 4, na.rm = TRUE)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment