---
title: "Resolution and accuracy in Congreve & Lamsdell matrices"
author: "Martin R. Smith <martin.smith@durham.ac.uk>"
date: "`r Sys.Date()`"
output: 
  bookdown::pdf_document2:
    toc: yes
    includes:
      in_header: ../inst/preamble.tex
  html_document:
    default: yes
bibliography: ../inst/REFERENCES.bib
csl: https://raw.githubusercontent.com/citation-style-language/styles/master/dependent/biology-letters.csl
link-citations: yes
github-repo: ms609/CongreveLamsdell2016
vignette: >
  %\VignetteIndexEntry{Results: Method choice}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
---

This page depicts the analytical results of all 100 matrices generated by
Congreve & Lamsdell [-@Congreve2016] using a ternary plotting approach
[@Smith2019], with quartets and partitions used as distance metrics.

```{r initialize, echo=FALSE, message=FALSE}
library("Ternary")
library("CongreveLamsdell2016")
data('clBremPartitions', 'clBremQuartets', 'clMkvPartitions', 'clMkvQuartets',
     'clBootFreqPartitions', 'clBootFreqQuartets', 
     'clBootGcPartitions', 'clBootGcQuartets',
     'clJackFreqPartitions', 'clJackFreqQuartets',
     'clJackGcPartitions', 'clJackGcQuartets', 
     'clCI')
```

```{r initialize-variables, echo=FALSE}
PCH <- c(
  brem  = 2,
  bootG = 0,
  bootF = 5,
  jackG = 3,
  jackF = 4,
  none  = NA,
  
  mk     = 1,
  markov = 1,
  equal  = 61, #'='
  eq     = 61,
  dot    = 183, #'.'
  
  k1         = 4,
  implied1   = 4,
  k2         = 183, 
  implied2   = 183,
  k3         = 183, 
  implied3   = 183,
  k5         = 3,
  implied5   = 3,
  kX         = 183,
  implied10  = 183,
  implied20  = 183,
  implied200 = 183,
  kC = 17,
  impliedC = 17  #triupfilled
)

COL <- c(
  black      = paste0(cbPalette8[1], '99'),
  markov     = paste0(cbPalette8[4], '99'),
  mk         = paste0(cbPalette8[4], '99'),
  
  equal      = paste0(cbPalette8[8], '99'),
  eq        = paste0(cbPalette8[8], '99'),
  implied1   = paste0(cbPalette8[6], '42'),
  k1         = paste0(cbPalette8[6], '42'),
  implied2   = paste0(cbPalette8[6], '42'),
  k2         = paste0(cbPalette8[6], '42'),
  implied3   = paste0(cbPalette8[6], '42'),
  k3         = paste0(cbPalette8[6], '42'),
  implied5   = paste0(cbPalette8[6], '42'),
  k5         = paste0(cbPalette8[6], '42'),
  implied10  = paste0(cbPalette8[6], '99'),
  kX         = paste0(cbPalette8[6], '99'),
  implied20  = paste0(cbPalette8[6], '42'),
  implied200 = paste0(cbPalette8[6], '42'),
  kC         = paste0(cbPalette8[2], '99'),
  impliedC   = paste0(cbPalette8[2], '99')
)

MARGINS <- c(2.8, 0.3, 0.3, 0.3)
ROWS <- c(1, 3)

TernaryQuarts<-function(TREE=TREE, zoom=1, padding=0.1) {
  clInitializeTernaryQuarts(zoom, padding)
  clPlotQuartets(clBootGcQuartets, TREE, cex=1.1, pch=PCH)
  clPlotQuartets(clMkvQuartets, TREE, cex=1.1, pch=PCH['mk'])

  # Return:
  invisible()
}


TernaryParts<-function(TREE=TREE) {
  clInitializeTernarySplits()
  clPlotSplits(clBootGcPartitions, TREE, cex=1.1, pch=PCH)
  clPlotSplits(clMkvPartitions, TREE, cex=1.1, pch=PCH['mk'])
  
  # Return:
  invisible()
}

AddLegend <- function(pos='bottomright') {
  legend(pos, cex=0.8, bty='n',
         lty=1,
         pch=PCH[c('mk', 'eq', 'kX', 'k5', 'k3', 'k1', 'kC')], pt.cex=1.1,
         col=COL[c('mk', 'eq', 'kX', 'k5', 'k3', 'k1', 'kC')],
         legend=c('Markov', 'Equal weights', 
                  paste0('Implied, k=', c(10, 5, '2, 3', 1, '2..10'))))
}

Plottem <- function (i) { 
  TernaryQuarts(TREE=i)
  
  title(main=paste0("\nQuartets"), cex.main=0.8)
  arrows(sqrt(3/4) * 0.5, 0.5, sqrt(3/4) * 0.8, 0.5, length=0.1)
  text  (sqrt(3/4) * 0.65, 0.5, pos=3, 'Decreasing resolution', cex=0.8)
  
  arrows(sqrt(3/4) * 0.98, 0.40, sqrt(3/4) * 0.98, 0.20, length=0.1)
  text  (sqrt(3/4) * 1.01, 0.30, pos=3, 'Increasing divergence', cex=0.8, srt=270)
  
  TernaryQuarts(TREE=i, zoom=3, padding=0.01)
  title(main=paste0("\nDataset ", i, ": CI=",round(clCI[i], 2)), cex.main=1.2)
  
  TernaryParts(TREE=i)
  
  arrows(sqrt(3/4) * 0.98, 0.40, sqrt(3/4) * 0.98, 0.20, length=0.1)
  text  (sqrt(3/4) * 1.01, 0.30, pos=3, 'Increasing RF distance', cex=0.8, srt=270)
  
  AddLegend()
}
```

## Summary
```{R Summary, echo=FALSE, fig.height=3, fig.width=9, warning=FALSE}
x <- Sys.setlocale("LC_ALL", "C") # allows PCH=183
par(mfrow=c(1, ROWS[2]), mar=MARGINS)

clInitializeTernaryQuarts()
clPlotAverageQuartets(clBootGcQuartets, cex=1.1, pch=PCH)
clPlotAverageQuartets(clMkvQuartets, cex=1.1, pch=PCH['mk'])

title(main=paste0("\nQuartets"), cex.main=0.8)
arrows(sqrt(3/4) * 0.5, 0.5, sqrt(3/4) * 0.8, 0.5, length=0.1)
text  (sqrt(3/4) * 0.65, 0.5, pos=3, 'Decreasing resolution', cex=0.8)

arrows(sqrt(3/4) * 0.98, 0.40, sqrt(3/4) * 0.98, 0.20, length=0.1)
text  (sqrt(3/4) * 1.01, 0.30, pos=3, 'Increasing divergence', cex=0.8, srt=270)


clInitializeTernaryQuarts(zoom=3, padding=0.01)
clPlotAverageQuartets(clBootGcQuartets, cex=1.1, pch=PCH)
clPlotAverageQuartets(clMkvQuartets, cex=1.1, pch=PCH['mk'])
title("\nResults for all datasets (means)", cex.main=1.2)

clInitializeTernarySplits()
clPlotAverageSplits(clBootGcPartitions, cex=1.1, pch=PCH)
clPlotAverageSplits(clMkvPartitions, cex=1.1, pch=PCH['mk'])

arrows(sqrt(3/4) * 0.98, 0.40, sqrt(3/4) * 0.98, 0.20, length=0.1)
text  (sqrt(3/4) * 1.01, 0.30, pos=3, 'Increasing RF distance', cex=0.8, srt=270)

AddLegend()
```


## Trees 1--10
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(1)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(2)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(3)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(4)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(5)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(6)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(7)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(8)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(9)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(10)
```

## Trees 11--20
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(11)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(12)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(13)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(14)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(15)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(16)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(17)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(18)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(19)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(20)
```


## Trees 21--30
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(21)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(22)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(23)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(24)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(25)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(26)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(27)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(28)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(29)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(30)
```

## Trees 31--40
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(31)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(32)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(33)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(34)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(35)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(36)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(37)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(38)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(39)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(40)
```

## Trees 41--50
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(41)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(42)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(43)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(44)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(45)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(46)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(47)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(48)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(49)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(50)
```

## Trees 51--60
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(51)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(52)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(53)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(54)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(55)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(56)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(57)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(58)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(59)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(60)
```

## Trees 61--70
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(61)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(62)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(63)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(64)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(65)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(66)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(67)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(68)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(69)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(70)
```

## Trees 71--80
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(71)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(72)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(73)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(74)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(75)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(76)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(77)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(78)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(79)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(80)
```

## Trees 81--90
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(81)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(82)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(83)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(84)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(85)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(86)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(87)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(88)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(89)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(90)
```

## Trees 91--100
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(91)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(92)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(93)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(94)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(95)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(96)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(97)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(98)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(99)
```
```{R, echo=FALSE, fig.width=9, fig.height=6, warning=FALSE}
par(mfrow=ROWS, mar=MARGINS); x <- Plottem(100)
```

## References
