---
title: "soling"
output: bookdown::html_document2
---

```{r setup, include=FALSE}
set.seed(0)
knitr::opts_chunk$set(echo = TRUE)
library("hyper2")
library("magrittr")
options("digits" = 5)
```

<p style="text-align: right;">
![](`r system.file("help/figures/hyper2.png", package = "hyper2")`){width=10%}
</p>

The Soling is a type of boat raced in the 2000 Summer Olympics.  Here
I analyse race data taken from
[wikipedia](https://en.wikipedia.org/w/index.php?title=Sailing_at_the_2000_Summer_Olympics_\%E2\%80\%93_Soling&oldid=945362535)
using a Plackett-Luce extension of the Bradley-Terry model.


```{r loadlib}
soling_table <- read.table("soling.txt")
soling_table
```

Thus we see, reading column 1, that in race 1, `norway` came fifth,
`nz` third, `netherlands` first, and so on.  We can present this
information in a rank table format:

```{r interpretsoling}
ordertable_to_ranktable(soling_table)
```

and also convert it to a likelihood function:

```{r solingtolikelihood}
soling <- ordertable2supp(soling_table)
head(soling)
```

We can also consider observations from the round robins and quarter finals:

```{r,rrqf}
soling_rr1 <- read.table("soling_rr1.txt")
soling_rr2 <- read.table("soling_rr2.txt")
soling_qf  <- read.table("soling_qf.txt" )
soling_rr1
```

Thus, for example, taking `soling_rr1[1,2] == TRUE` we see that
Germany beat Sweden in round robin 1.  To incorporate this information
we need a little bespoke function:

```{r, definef}
rr <- function(H,M){
	for(i in seq_len(nrow(M)-1)){
		for(j in (i+1):ncol(M)){
			if(M[i,j]){
				winner <- rownames(M)[i]
				loser  <- rownames(M)[j]
			} else {
				winner <- rownames(M)[j]
				loser <-  rownames(M)[i]
			}
			H[winner] %<>% inc
			H[c(winner,loser)] %<>% dec
		}
	}
	return(H)
}
```

We can see the effect on the evaluate of incorporating the additional
observations into the Plackett-Luce likelihoods:

```{r showdiff,fig.cap="Evaluate before and after incorporation round robin and quarter final information"}
H <- soling  # before new information
soling_maxp <- maxp(H) 
H %<>% rr(soling_rr1)
H %<>% rr(soling_rr2)
H %<>% rr(soling_qf)
soling_after <- H # after new information
soling_after_maxp <- maxp(soling_after)
par(pty="s")
plot(soling_maxp,soling_after_maxp,asp=1,pch=16,xlim=c(0,0.3),ylim=c(0,0.3))
abline(0,1)
```

Figure \@ref(fig:showdiff) shows a modest change.  We might ask
whether the additional observations are consistent with the original
Placket-Luce likelihood:

```{r}
(soling_diff <- soling_after - soling)
```

So `soling_diff` is the _difference_ between `H`--- which is `soling`
plus the observations from the roundrobins and quarter finals---and
`soling` without the extra information.  In other words, the
information in the roundrobins and quarter finals.

```{r,testH2equalp}
equalp.test(soling_diff)
```


### Package dataset {-}

Following lines create `soling.rda`, residing in the `data/`
directory of the package.

```{r,label=savesolingdataset}
save(soling,soling_after, soling_maxp,soling_after_maxp,soling_table,
     soling_rr1, soling_rr2, soling_qf,
  file="soling.rda")
```

