knitr::opts_chunk$set(echo = TRUE)
library("hyper2")
Takes a couple of hours to run with no cache
Here I analyse Formula 1 results in a range of years using
Plackett-Luce likelihoods as implemented in the hyper2
package.
Files like formula1_2017.txt
are directly copied from Wikipedia
(with slight whitespace changes).
years <- 2001:2015
f <- function(M){M[,-ncol(M)]} # removes 'points' column o <- function(k){ordertable2supp(f(read.table(k)))} filenamemaker <- function(year){paste("formula1_",year,".txt", sep="")} F1_list <- list() for(year in years){ filename <- filenamemaker(year) jj <- paste("y",year,sep="") F1_list[[jj]] <- o(filename) }
F1_total <- hyper2() for(i in F1_list){F1_total <- F1_total + i}
m <- maxp(F1_total) pie(m) dotchart(m,pch=16,main=paste("Formula 1, years ",min(years)," to ",max(years)))
M <- matrix(NA,nrow=length(pnames(F1_total)),ncol=length(years)) rownames(M) <- pnames(F1_total) colnames(M) <- paste("y",years,sep="") for(i in seq_len(nrow(M))){ for(j in seq_len(ncol(M))){ if(rownames(M)[i] %in% pnames(F1_list[[j]])){M[i,j] <- 1} } } jj <- order(rowSums(M,na.rm=T),decreasing=T) M <- M[jj,] image(x=years,y=seq_along(pnames(F1_total)),t(M))
howmanyraces <- sort(rowSums(M,na.rm=TRUE),decreasing=TRUE) plot(howmanyraces)
select <- function(F1tab,minraces=5){ wanted <- rownames(M)[howmanyraces >= minraces] ordertable2supp(F1tab[rownames(F1tab) %in% wanted,]) }
freqracers <- function(minraces){ # minimum number of seasons out <- hyper2() for(year in years){ out <- out + select(f(read.table(filenamemaker(year),minraces))) } return(out) } comp <- function(m,mr){ par(pty="s") # plot(m,mr,asp=1,xlim=c(0,0.2),ylim=c(0,0.2)) plot(m,mr,asp=1) abline(0,1) }
F1_top1 <- freqracers(1) m1 <- maxp(F1_top1) pnames(F1_top1) <- rev(pnames(F1_top1)) m1r <- rev(maxp(F1_top1)) # reverse of MLE with names reversed
pie(m1) comp(m1,m1r)
F1_top2 <- freqracers(2) m2 <- maxp(F1_top2) pnames(F1_top2) <- rev(pnames(F1_top2)) m2r <- rev(maxp(F1_top2)) # reverse of MLE with names reversed
pie(m2) comp(m2,m2r)
F1_top3 <- freqracers(3) m3 <- maxp(F1_top3) pnames(F1_top3) <- rev(pnames(F1_top3)) m3r <- rev(maxp(F1_top3)) # reverse of MLE with names reversed
pie(m3) comp(m3,m3r)
F1_top4 <- freqracers(4) m4 <- maxp(F1_top4) pnames(F1_top4) <- rev(pnames(F1_top4)) m4r <- rev(maxp(F1_top4)) # reverse of MLE with names reversed
pie(m4) comp(m4,m4r)
F1_top5 <- freqracers(5) m5 <- maxp(F1_top5) pnames(F1_top5) <- rev(pnames(F1_top5)) m5r <- rev(maxp(F1_top5)) # reverse of MLE with names reversed
pie(m5) comp(m5,m5r)
F1_top6 <- freqracers(6) m6 <- maxp(F1_top6) pnames(F1_top6) <- rev(pnames(F1_top6)) m6r <- rev(maxp(F1_top6)) # reverse of MLE with names reversed
pie(m6) comp(m6,m6r)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.