---
title: "Sushi monster"
author: "R. K. S. Hankin"
output: bookdown::html_document2
bibliography: hyper2.bib
---

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

```{r out.width='20%', out.extra='style="float:right; padding:10px"',echo=FALSE}
knitr::include_graphics(system.file("help/figures/hyper2.png", package = "hyper2"))
```


<font size="1"> most of the time is taken calculating the
`calclikesushi` and `maxlike`chunks.  With a sample size of 50 in
chunk `tryH`, and 11 points on the graph, it takes about half an hour
to run without cache. </font>

To cite the `hyper2` package in publications, please use @hankin2017_rmd.

This short document applies `hyper3` formalism to a dataset
obtained from the `preflib` library [@MaWa13a] in which ten
different types of sushi were placed in order of preference by 5000
judges.  Three of the sushis (maguro, toro, tekka-maki) were made with
tuna, and it is reasonable to view these three types as being
exchangeable in much the same way as the red and blue buses.  To
proceed, we need to define equivalence classes of sushi---one for
tuna-based sushis and one for everything else.  Of course, one can
also imagine the opposite phenomenon whereby the judge decides he has
had enough tuna for the moment and this disincentivises further tuna
choices.  This mechanism would be modelled by having $\lambda<1$.


```{r}
a <- as.matrix(read.table("sushi.txt", skip=22))
set.seed(0)
wanted <- sample(seq_len(nrow(a)),50,replace=TRUE,prob=a[,1])
a <- a[wanted,-1]
colnames(a) <- c("first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth")
```

```{r}
sushi_types <- c("ebi", "anago", "maguro", "ika", "uni", "sake", "tamago", "toro", "tekka", "kappa")
a[] <- sushi_types[a]
noquote(a)
sushi_eq_classes <-
    c("ebi"  = 1, "anago" = 1, "maguro" = 2, "ika" = 1, "uni" = 1,
      "sake" = 1, "tamago" = 1, "toro" = 2, "tekka" = 2,
      "kappa" = 1)
```
	

```{r,defcalculate_sushi_H}
make_sushi_H <- function(a, lambda){
    H <- hyper3()

    for(i in seq_len(nrow(a))){
        H <- H + cheering3(a[i,], e=sushi_eq_classes, h=c(1,lambda))
    }
    return(H)
}
```

```{r tryH, cache=TRUE}
H1 <- make_sushi_H(a, lambda=1)
H2 <- make_sushi_H(a, lambda=2)
```


```{r maxpsushiH,cache=TRUE}
mH1 <- maxp(H1,give=1)
mH2 <- maxp(H2,give=1)
```


```{r showsushiH}
mH1
mH2
```

```{r calclikesushi, cache=TRUE}
f <- function(lambda){
   H <- make_sushi_H(a, lambda = lambda)
   maxp(H,give=1)$`Log-likelihood`
}

s <- exp(seq(from = log(0.9), to = log(6), len = 11))
L <- sapply(s,f)
```

```{r maxlike, cache=TRUE}
(jj <- optimize(f, 2:3, maximum=TRUE))
```

```{r plotsushilike}
plot(log(s), L-jj$objective, type='b')
```

```{r calc_sushi, cache=TRUE}
sushi <- make_sushi_H(a, lambda = jj$maximum)
```

```{r calc_sushimaxp, cache=TRUE}
(sushi_maxp <- maxp(sushi))
```

### Package dataset {-}

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

```{r,label=savesushidataset}
sushi_table <- a
save(sushi_table,  sushi, sushi_maxp, sushi_eq_classes, file="sushi.rda")
```

## References {-}
