--- 
title: "Inapplicable data"
author: "Martin Brazeau (m.brazeau@imperial.ac.uk), Thomas Guillerme (guillert@tcd.ie) and Martin Smith (martin.smith@durham.ac.uk)"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
output: bookdown::gitbook
documentclass: book
bibliography: [../References.bib, packages.bib]
csl: https://raw.githubusercontent.com/citation-style-language/styles/master/apa-old-doi-prefix.csl
link-citations: yes
github-repo: TGuillerme/Inapp
description: "Inapplicable data in a parsimony setting"
---


```{r setup, include=FALSE}
library('Inapp')
library('ape')
knitr::opts_chunk$set(echo = TRUE)
labelRow <- function(text) text(-3, 9, text, font=2, pos=4, cex=1.2)
goloPar <- function () par(mfrow=c(1, 2), mar=c(0.5, 0.2, 0.6, 0.2), oma=c(0, 0, 2, 0), cex=0.5)
goloPlot <- function (tai, clr, nodeLabels=FALSE) {
    goloPar()
    vignettePlot(comb, tai, legend.pos='topleft', FALSE, passes=2, state.labels=ap)
    vignettePlot(comb, clr, legend.pos='topleft', passes=4, state.labels = rb)
    if (nodeLabels) {
        text((1:15) - 1 , 1:15, LETTERS[1:15])
        text((1:14) + 2.5 , (1:14) - 0.5, 1:14)
    }
}
SankoffMatrix <- function (states) {
  nr <- prod(vapply(states, length, 0L)) + 1L
  nchar <- length(states)
  ret <- matrix(0, nr, nr)
  ret[1, -1] <- nchar + 1L # +1 for gain of character
  ret[-1, 1] <- 1L # Just a loss
  
  listing <- vapply(seq_len(nr - 1L) - 1L, function (x) {
      ret <- integer(nchar)
      if (x == 0) return (ret)
      bin <- TreeSearch::AsBinary(x)
      ret[seq_along(bin)] <- bin
      ret
  }, double(nchar))
  rownames(ret) <- c('(0), absent', vapply(seq_len(nr - 1L), function (x) {
      paste0(paste0('(', x, '), present, '), 
             paste(vapply(seq_len(nchar), 
                          function (char) states[[char]][listing[char, x]+ 1L],
                          character(1)),
                   collapse=', ')
             )
      }, character(1)))
  ret[-1, -1] <- vapply(seq_len(nr-1), function (i) 
      vapply(seq_len(nr-1), function (j) sum(listing[, i] != listing[, j]), 0L),
      integer(nr - 1L))
  colnames(ret) <- paste0('**', seq_len(nr) - 1L, '**')
  
  # Return: 
  ret
}
ap <- c('Absent', 'Present')
rb <- c('Red', 'Blue')
oap <- c('', ap)
orb <- c('', rb)
whitey <- '#ffffff42'
greeny <- '#7fbf7ba7'
purply <- '#af8dc3e0'
number <- function (n) ifelse (n %in% 1:14, c('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen')[n], n)

```

# Inapplicable data in a parsimony setting {-}

This document `r if(knitr::is_latex_output()) "(also available as an [HTML webpage*](https://rawgit.com/TGuillerme/Inapp/master/inst/gitbook/_book/index.html))"` provides a detailed explanation of the algorithm for handling
inapplicable data proposed by @Brazeau2018.

We first discuss how the [Fitch algorithm](#fitch) works and introduce
the [problems](#problems) that it encounters in the face of inapplicable character states.

We then introduce our [solution](#solution), a new [algorithm](#algorithm), implemented in
various [software packages](#software), and discuss its
implications for the coding of [characters](#coding) and [ambiguity](#ambiguity).

We close with some [example](#examples) trees that demonstrate how our algorithm behaves in more complicated cases.

 `r if(knitr::is_latex_output()) "&#42; [rawgit.com/TGuillerme/Inapp/master/inst/gitbook/_book/](https://rawgit.com/TGuillerme/Inapp/master/inst/gitbook/_book/index.html)"`
