Skip to content

Instantly share code, notes, and snippets.

@chrishanretty
Created July 5, 2024 07:58
Show Gist options
  • Save chrishanretty/ded2d7227ec8270ff481cebf57cbaf8c to your computer and use it in GitHub Desktop.
Save chrishanretty/ded2d7227ec8270ff481cebf57cbaf8c to your computer and use it in GitHub Desktop.
Quick proportionality calculation
library(tidyverse)
dat <- tribble(~party, ~seat_share, ~vote_share,
"Lab", 411/632, .338,
"Con", 119/632, .237,
"LDem", 71/632, .122,
"SNP", 9/632, .024,
"Ind", 6/632, 0.02,
"Ref", 4/632, .143,
"Green", 4/632, .068,
"Plaid", 4/632, .007,
"Other", 0/631, .04)
d_gall <- function(v, s) {
sqrt(1/2 * sum((v - s)^2))
}
d_sl <- function(v, s) {
good <- which(v > 0)
v <- v[good]
s <- s[good]
sum((s - v)^2 / v)
}
gallagher <- d_gall(dat$vote_share, dat$seat_share)
sainte_lague <- d_sl(dat$vote_share, dat$seat_share)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment