Skip to contents

Plots a 2D simplex, a triangle with unit sides centered at the origin, onto which marginal posterior probabilities of relapse, reinfection and recrudescence (or any other vector of three numbers in zero to one summing to one) can be projected; see project2D() and examples below.

Usage

plot_simplex(
  v.labels = c("Recrudescence", "Relapse", "Reinfection"),
  v.cutoff = 0.5,
  v.colours = c("yellow", "purple", "red"),
  plot.tri = T,
  p.coords = NULL,
  p.labels = rownames(p.coords),
  pos = 3
)

Arguments

v.labels

A vector of labels that annotate vertices anticlockwise from top (default: "Recrudescence", "Relapse", "Reinfection"). If NULL, vertices are not annotated.

v.cutoff

An arbitrary number between 0.5 and 1 that separates regions of lower and higher probability. Beware the use of cut-offs for probable recrudescence classification and probable reinfection classification; see "Understand posterior estimates".

v.colours

A vector of colours associated with the vertices anticlockwise from top; see example below.

plot.tri

Whether to plot the triangular boundary (default true).

p.coords

Matrix of simplex coordinates (3D) to plot with points, one row per point. If a vector is given, this is converted to a matrix with a single row.

p.labels

Labels of the points p.coords. If labels are undesired, set this to NA. The default is to use the row names of p.coords.

pos

Which side to plot the p.labels. Values of 1, 2, 3 and 4, respectively indicate positions below, to the left of, above and to the right of the points. Can be either a single integer (default 3) or a vector of integers.

Examples

# Plot 2D simplex
plot_simplex(p.coords = diag(3),
             p.labels = c("(1,0,0)", "(0,1,0)", "(0,0,1)"),
             pos = c(1,3,3))


# ==============================================================================
# Given data on an enrollment episode and a recurrence,
# compute the posterior probabilities of the 3Rs and plot the deviation of the
# posterior from the prior
# ==============================================================================

# Some data:
y <- list(list(m1 = c('a', 'b'), m2 = c('c', 'd')), # Enrollment episode
          list(m1 = c('a'), m2 = c('c'))) # Recurrent episode

# Some allele frequencies:
fs <- list(m1 = setNames(c(0.4, 0.6), c('a', 'b')),
           m2 = setNames(c(0.2, 0.8), c('c', 'd')))

# A vector of prior probabilities:
prior <- array(c(0.2, 0.3, 0.5), dim = c(1,3),
               dimnames = list(NULL, c("C", "L", "I")))

# Compute posterior probabilities
post <- compute_posterior(y, fs, prior)
#> Number of valid relationship graphs (RGs) is 9
#> ========
#> =========
#> ========
#> =========
#> ========
#> =========
#> ========
#> =========
#> =========
#> |
#> Computing log p(Y|RG) for 9 RGs
#> ========
#> =========
#> ========
#> =========
#> ========
#> =========
#> ========
#> =========
#> =========
#> |
#> Finding log-likelihood of each vector of recurrence states
#> ========
#> =========
#> ========
#> =========
#> ========
#> =========
#> ========
#> =========
#> =========
#> |
#> 

p.coords <- rbind(prior, post$marg)

# Plot simplex with probability greater than 0.8 considered relatively
# certain
plot_simplex(v.cutoff = 0.8, p.coords = p.coords,
             p.labels = c("Prior", "Posterior"))

# Plot the deviation of the posterior from the prior, this requires manually
# obtaining 2D coordinates
xy_prior <- project2D(as.vector(prior))
xy_post <- project2D(as.vector(post$marg))
arrows(x0 = xy_prior["x"], x1 = xy_post["x"],
       y0 = xy_prior["y"], y1 = xy_post["y"], length = 0.1)