Skip to content

Instantly share code, notes, and snippets.

@rexarski
Created September 1, 2020 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexarski/98c508322124962ce9293196ffa6f87c to your computer and use it in GitHub Desktop.
Save rexarski/98c508322124962ce9293196ffa6f87c to your computer and use it in GitHub Desktop.
library(dplyr)
Wilson <- function(n, k, alpha=0.95) {
phat <- k/n
score <- qnorm(1-(1-alpha)/2)
lbound <- (phat+score**2/(2*n)-score*sqrt((phat*(1-phat)+score**2/(4*n))/n))/(1+score**2/n)
return(lbound)
}
seed(2019)
x <- sample(1:500,200,replace=T)
y <- sample(1:500,200,replace=T)
votes <- data.frame(x,y) %>%
rowwise() %>%
mutate(pos=min(x,y),total=max(x,y)) %>%
select(total, pos) %>%
mutate(Wilson=Wilson(total,pos)) %>%
arrange(desc(Wilson))
head(votes)
tail(votes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment