---
title: "Domestic cooking and likelihood"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Suppose there are five people in a family.  Each day for five days,
one person cooks a meal.  After the fifth day, each person considers
the four meals cooked by other family members, and puts them in order
of preference.  This very short vignette shows how to apply
generalized Bradley-Terry to this situation using the `hyper2`
package.

```{r loadlib}
library(hyper2)

```

Now add some data:

```{r data}

H <- hyper2() + 
  race(c("p5","p2","p3","p4")) + # person 1 says p5 is the best, p4 the worst.
  race(c("p1","p5","p3","p4")) + # person 2 says p1 is the best, p4 the worst.
  race(c("p1","p4","p2","p5")) + # person 3 says p1 is the best, p5 the worst.
  race(c("p1","p2","p3","p5")) + # person 4 says p1 is the best, p5 the worst.
  race(c("p2","p1","p3","p4"))   # person 5 says p2 is the best, p4 the worst.
H
```

Note that noone is allowed to vote for themselves.  We may find
maximum likelihood estimate for the strengths,
$\operatorname{argmax}\mathcal{L}\left(p_1,\ldots,p_5\right)$:

```{r}
maxp(H)
```

Graphically:

```{r maxlike, echo=FALSE}
dotchart(maxp(H),pch=16,main="probabilities")
dotchart(log(maxp(H)),pch=16,main="log probabilities")
```

We can assess the hypothesis that all players have the same strength:

```{r}
equalp.test(H)
```

Thus we reject the hypothesis of equal strength.  Now we can assess
the hypothesis that person `a` does in fact have the highest strength
of the five.  We can follow the reasoning in the _icons_
demonstration and test $H_0\colon p_1=p_2$:

```{r}
samep.test(H,c("p1","p2"))
```

There is no strong evidence to support the assertion that person 1 is
actually stronger than person 2 in the sense that $p_1>p_2$.

## Suspect observation

Suppose that we subsequently observe order statistic `p5,p4,p3,p2,p1`,
that is, person 5 is the best, 4 is the second best, and so on to 1
being the worst.  Is this observation consistent with the previous
dataset?

It is possible to perform a permutation test on this as follows.
First, calculate the probability of each of the $5!=120$ possible
observations (up to a constant):

```{r}
library(partitions)
M <- perms(5)
M[] <- paste("p",M,sep="")
imH <- indep(maxp(H))
f <- function(o){loglik(imH,race(o),log=FALSE)}
LL <- apply(perms(5),2,f)
length(LL)
sum(LL)
head(LL)
```

So `LL` is the probability of observing each permutation of 5 objects.
We then calculate the $p$-value as the probability of obtaining the
observation or an observation more extreme; in this case we define a
permutation to be "more extreme" if it has a smaller probability than
the observed permutation of `5,4,3,2,1` (under the null).  This is
easily calculated:

```{r}
obs <- c(5,4,3,2,1)
(pval <- sum(LL[LL <= f(obs)])/sum(LL))
```

Thus it is reasonable to reject the hypothesis that this particular
observation was drawn from the same distribution as the others, and
infer that it was suspect.


## Changes in cooking strength.

We now consider a sequence of observations where one person, say
person 1, is suspected of an increase in strength (perhaps they
attended a domestic cookery course).  We make some observations before
and after the training and seek evidence that it was effective.
Unlike the previous cases, here we have a full order statistic without
the complication of not being allowed to vote for oneself (perhaps a
professional chef puts the dishes in order).  First, before:

```{r,beforetraining}
# observations before training:
Hbefore <- 
  race(c("p5","p2","p3","p4","p1")) + #  5 is the best, 4 the worst.
  race(c("p5","p3","p2","p4","p1")) + #  5 is the best, 1 the worst.
  race(c("p4","p5","p3","p2","p1")) + #  4 is the best, 1 the worst.
  race(c("p5","p3","p4","p1","p2")) + #  5 is the best, 2 the worst.
  race(c("p2","p5","p3","p1","p4"))   #  2 is the best, 4 the worst.
Hbefore
```

Now, after the training we have two finishing order observations:
`p1,p2,p3,p4,p5` and `p3,p1,p5,p2,p4`.  Thus in the first observation,
`p1` comes first and in the second observation, `p1` comes second
second (after `p3`).  The likelihood for the first observation would
be

\[
\frac{p_1+S}{p_1+S+p_2+p_3+p_4+p_5}\cdot
\frac{p_2  }{      p_2+p_3+p_4+p_5}\cdot
\frac{p_3  }{          p_3+p_4+p_5}\cdot
\frac{p_4  }{              p_4+p_5}\cdot
\frac{p_5  }{                  p_5}\cdot
\]

and the likelihood for the second observation would be

\[
\frac{p_3  }{p_1+S+p_2+p_3+p_4+p_5}\cdot
\frac{p_1+S}{p_1+S+p_2+    p_4+p_5}\cdot
\frac{p_5  }{      p_2+    p_4+p_5}\cdot
\frac{p_2  }{      p_2+    p_4    }\cdot
\frac{p_4  }{              p_4    }\cdot
\]

We can translate these into package idiom with function `pwa()`
("person with advantage"):

```{r,usepwafortraining}
Hafter <- race(c("p1","p2","p3","p4","p5")) + race(c("p3","p1","p5","p2","p4"))
Hafter %<>%  pwa("p1")
Hafter
```

In the above, the additional strength conferred by the training is
represented by `S`.  We can estimate the effect of training:

```{r,addbeforeandafter}
maxp(Hbefore+Hafter)
```

[In the above, note the use of the overloaded "`+`", meaning to add
two likelihood functions for independent observations].  The evaluate
suggests that the training had a sizable effect; we may use
\code{specficp.gt.test()} to test a null of zero strength:

```{r,testnullzerotrainingeffect}
specificp.gt.test(Hbefore+Hafter, "S", 0)
```

thus we have strong evidence for the training being effective.
