In this vignette, we show how custom functions for priors, likelihood, or movement of parameters and augmented data can be used in outbreaker2. In all these functions, the process will be similar:

  1. write your own function with the right arguments
  2. pass this function as an argument to a custom... function
  3. pass the result to outbreaker2

Note that 2-3 can be a single step if passing the function to the arguments of outbreaker2 directly. Also note that all priors and likelihoods are expected on a log scale. Finally, also note that while the various custom... functions will try to some extent to check that the provided functions are valid, such tests are very difficult to implement. In short: you are using these custom features at your own risks - make sure these functions work before passing them to outbreaker2.


Customising priors

Priors of outbreaker2 must be a function of an outbreaker_param list (see ?outbreaker_param). Here, we decide to use a step function rather than the default Beta function as a prior for pi, the reporting probability, and a flat prior between 0 and 1 for the mutation rate (which is technically a probability in the basic genetic model used in outbreaker2).

We start by defining two functions: an auxiliary function f which returns values on the natural scale, and which we can use for plotting the prior distribution, and then a function f_pi which will be used for the customisation.

f <- function(pi) {
    ifelse(pi < 0.8, 0, 5)
}

f_pi <- function(param) { 
    log(f(param$pi))
}

plot(f, type = "s", col = "blue", 
     xlab = expression(pi), ylab = expression(p(pi)), 
     main = expression(paste("New prior for ", pi)))

While f is a useful function to visualise the prior, f_pi is the function which will be passed to outbreaker. To do so, we pass it to custom_priors:

library(outbreaker2)

f_mu <- function(param) {
  if (param$mu < 0 || param$mu > 1) {
    return(-Inf)
  } else {
    return(0.0)
  }
  
}

priors <- custom_priors(pi = f_pi, mu = f_mu)
priors
#> 
#> 
#>  ///// outbreaker custom priors ///
#> 
#> class: custom_priors list
#> number of items: 4 
#> 
#> /// custom priors set to NULL (default used) //
#> $eps
#> NULL
#> 
#> $lambda
#> NULL
#> 
#> /// custom priors //
#> $mu
#> function (param) 
#> {
#>     if (param$mu < 0 || param$mu > 1) {
#>         return(-Inf)
#>     }
#>     else {
#>         return(0)
#>     }
#> }
#> 
#> $pi
#> function (param) 
#> {
#>     log(f(param$pi))
#> }

Note that custom_priors does more than just adding the custom function to a list. For instance, the following customisations are all wrong, and rightfully rejected:


## wrong: not a function
## should be pi = function(x){0.0}
custom_priors(pi = 0.0)
#> Error in custom_priors(pi = 0): The following priors are not functions: pi

## wrong: two arguments
custom_priors(pi = function(x, y){0.0})
#> Error in custom_priors(pi = function(x, y) {: The following priors dont' have a single argument: pi

We can now use the new priors to run outbreaker on the fake_outbreak data (see introduction vignette):


dna <- fake_outbreak$dna
dates <- fake_outbreak$sample
w <- fake_outbreak$w
data <- outbreaker_data(dna = dna, dates = dates, w_dens = w)

## we set the seed to ensure results won't change
set.seed(1)


res <- outbreaker(data = data, priors = priors)

We can check the results first by looking at the traces, and then by plotting the posterior distributions of pi and mu, respectively:


plot(res)

plot(res, "pi", burnin = 500)

plot(res, "mu", burnin = 500)

plot(res, "pi", type = "density", burnin = 500)

plot(res, "mu", type = "hist", burnin = 500)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Note that we are using density and histograms here for illustrative purposes, but there is no reason to prefer one or the other for a specific parameter.

Interestingly, the trace of pi suggests that the MCMC oscillates between two different states, on either bound of the interval on which the prior is positive (it is -Inf outside (0.8; 1)). This may be a consequence of the step function, which causes sharp ‘cliffs’ in the posterior landscape. What shall one do to derive good samples from the posterior distribution in this kind of situation? There are several options, which in fact apply to typical cases of multi-modal posterior distributions:

  • Avoid ‘cliffs’, i.e. sharp drops in the posterior landscape, typically created by using step-functions in likelihoods and in priors.

  • Use larger samples, i.e. run more MCMC iterations.

  • Use a different sampler, better than Metropolis-Hasting at deriving samples from multi-modal distributions.

Because we know what the real transmission tree is for this dataset, we can assess how the new priors impacted the inference of the transmission tree.


summary(res, burnin = 500)
#> $step
#>    first     last interval  n_steps 
#>      550    10000       50      190 
#> 
#> $post
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  -699.5  -618.8  -586.0  -580.3  -547.9  -458.3 
#> 
#> $like
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  -701.2  -620.4  -587.6  -581.9  -549.5  -459.9 
#> 
#> $prior
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   1.609   1.609   1.609   1.609   1.609   1.609 
#> 
#> $mu
#>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
#> 3.838e-05 6.505e-05 7.780e-05 8.419e-05 9.993e-05 1.770e-04 
#> 
#> $pi
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.8002  0.8030  0.8093  0.8254  0.8232  0.9940 
#> 
#> $tree
#>    from to time   support generations
#> 1    NA  1   -1        NA          NA
#> 2     1  2    1 0.9947368           1
#> 3     2  3    3 0.9947368           1
#> 4    NA  4    2        NA          NA
#> 5     3  5    4 0.9894737           1
#> 6     9  6    6 0.9894737           1
#> 7     4  7    5 1.0000000           1
#> 8     5  8    6 0.9631579           1
#> 9     4  9    5 0.9789474           1
#> 10    6 10    7 1.0000000           1
#> 11    7 11    7 0.5210526           1
#> 12    5 12    7 0.8210526           1
#> 13    9 13    6 1.0000000           1
#> 14    5 14    8 0.7947368           1
#> 15    5 15    7 0.8315789           1
#> 16    4 16    8 0.5052632           2
#> 17    4 17    7 0.5684211           2
#> 18    5 18    9 0.4473684           2
#> 19    9 19    9 0.9894737           2
#> 20   10 20   11 0.9789474           2
#> 21   11 21   10 0.9842105           2
#> 22   11 22   11 1.0000000           2
#> 23   13 23    9 1.0000000           2
#> 24   13 24   10 0.9947368           2
#> 25   13 25    9 0.9947368           3
#> 26   17 26    9 0.8789474           3
#> 27   17 27   11 1.0000000           3
#> 28   NA 28    9        NA          NA
#> 29   10 29   12 1.0000000           3
#> 30   13 30   11 0.9631579           3
tree <- summary(res, burnin = 500)$tree

comparison <- data.frame(case = 1:30,
                         inferred = paste(tree$from),
             true = paste(fake_outbreak$ances),
             stringsAsFactors = FALSE)
             
comparison$correct <- comparison$inferred == comparison$true
comparison
#>    case inferred true correct
#> 1     1       NA   NA    TRUE
#> 2     2        1    1    TRUE
#> 3     3        2    2    TRUE
#> 4     4       NA   NA    TRUE
#> 5     5        3    3    TRUE
#> 6     6        9    4   FALSE
#> 7     7        4    4    TRUE
#> 8     8        5    5    TRUE
#> 9     9        4    6   FALSE
#> 10   10        6    6    TRUE
#> 11   11        7    7    TRUE
#> 12   12        5    8   FALSE
#> 13   13        9    9    TRUE
#> 14   14        5    5    TRUE
#> 15   15        5    5    TRUE
#> 16   16        4    7   FALSE
#> 17   17        4    7   FALSE
#> 18   18        5    8   FALSE
#> 19   19        9    9    TRUE
#> 20   20       10   10    TRUE
#> 21   21       11   11    TRUE
#> 22   22       11   11    TRUE
#> 23   23       13   13    TRUE
#> 24   24       13   13    TRUE
#> 25   25       13   13    TRUE
#> 26   26       17   17    TRUE
#> 27   27       17   17    TRUE
#> 28   28       NA   NA    TRUE
#> 29   29       10   10    TRUE
#> 30   30       13   13    TRUE
mean(comparison$correct)
#> [1] 0.8


Customizing likelihood

Likelihood functions customisation works identically to prior functions. The only difference is that custom functions will take two arguments (data and param) instead of one in the prior functions. The function used to specify custom likelihood is custom_likelihoods. Each custom function will correspond to a specific likelihood component:


custom_likelihoods()
#> 
#> 
#>  ///// outbreaker custom likelihoods ///
#> 
#> class: custom_likelihoods list
#> number of items: 5 
#> 
#> /// custom likelihoods set to NULL (default used) //
#> $genetic
#> NULL
#> 
#> $reporting
#> NULL
#> 
#> $timing_infections
#> NULL
#> 
#> $timing_sampling
#> NULL
#> 
#> $contact
#> NULL

see ?custom_likelihoods for details of these components, and see the section ‘Extending the model’ for new, other components. As for custom_priors, a few checks are performed by custom_likelihoods:


## wrong: not a function
custom_likelihoods(genetic = "fubar")
#> Error in custom_likelihoods(genetic = "fubar"): The following likelihoods are not functions: genetic

## wrong: only one argument
custom_likelihoods(genetic = function(x){ 0.0 })
#> Error in custom_likelihoods(genetic = function(x) {: The following likelihoods dont' have two arguments: genetic

A trivial customisation is to disable some or all of the likelihood components of the model by returning a finite constant. Here, we apply this to two cases: first, we will disable all likelihood components as a sanity check, making sure that the transmission tree landscape is explored freely by the MCMC. Second, we will recreate the Wallinga & Teunis (1994) model, by disabling specific components.

A null model


f_null <- function(data, param) {
   return(0.0)
}

null_model <- custom_likelihoods(genetic = f_null,
                                 timing_sampling = f_null,
                                 timing_infections = f_null,
                                 reporting = f_null,
                                 contact = f_null)

null_model
#> 
#> 
#>  ///// outbreaker custom likelihoods ///
#> 
#> class: custom_likelihoods list
#> number of items: 5 
#> 
#> /// custom likelihoods //
#> $genetic
#> function (data, param) 
#> {
#>     return(0)
#> }
#> 
#> $reporting
#> function (data, param) 
#> {
#>     return(0)
#> }
#> 
#> $timing_infections
#> function (data, param) 
#> {
#>     return(0)
#> }
#> 
#> $timing_sampling
#> function (data, param) 
#> {
#>     return(0)
#> }
#> 
#> $contact
#> function (data, param) 
#> {
#>     return(0)
#> }

We also specify settings via the config argument to avoid detecting imported cases, reduce the number of iterations and sampling each of them:


null_config <- list(find_import = FALSE,
n_iter = 500,
sample_every = 1)

set.seed(1)

res_null <- outbreaker(data = data,
config = null_config,
likelihoods = null_model)

plot(res_null)

plot(res_null, "pi")

plot(res_null, "mu")

By typical MCMC standards, these traces look appaling, as they haven’t reach stationarity (i.e. same mean and variance over time), and are grossly autocorrelated in parts. Fair enough, as these are only the first 500 iterations of the MCMC, so that autocorrelation is expected. In fact, what we observe here literally is the random walk across the posterior landscape, which in this case is only impacted by the priors.

We can check that transmission trees are indeed freely explored:


plot(res_null, type = "alpha")

Do not try to render the corresponding network using plot(..., type = "network") as the force-direction algorithm will go insane. However, this network can be visualised using igraph, extracting the edges and nodes from the plot (without displaying it):


## extract nodes and edges from the visNetwork object
temp <- plot(res_null, type = "network", min_support = 0)
class(temp)
#> [1] "visNetwork" "htmlwidget"
head(temp$x$edges)
#>   from to value arrows   color
#> 1    1  2 0.004     to #CCDDFF
#> 2    1  3 0.002     to #CCDDFF
#> 3    1  4 0.008     to #CCDDFF
#> 4    1  5 0.010     to #CCDDFF
#> 5    1  6 0.004     to #CCDDFF
#> 6    1  7 0.006     to #CCDDFF
head(temp$x$nodes)
#>   id label value   color shape shaped
#> 1  1     1 0.206 #CCDDFF   dot   <NA>
#> 2  2     2 0.276 #B2D9E3   dot   <NA>
#> 3  3     3 0.266 #98D6C7   dot   <NA>
#> 4  4     4 0.336 #7ED2AC   dot   <NA>
#> 5  5     5 0.356 #99CAA9   dot   <NA>
#> 6  6     6 0.384 #C2C0AD   dot   <NA>

## make an igraph object
library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union

net_null <- graph.data.frame(temp$x$edges,
                             vertices = temp$x$nodes[1:4])

plot(net_null, layout = layout.circle,
     main = "Null model, posterior trees")

We can derive similar diagnostics for the number of generations betweens cases (kappa), only constrained by default settings to be between 1 and 5, and for the infection dates (t_inf):


plot(res_null, type = "kappa")

plot(res_null, type = "t_inf")

Finally, we can verify that the distributions of mu and pi match their priors, respectively an exponential distribution with rate 1000 and a beta with parameters 10 and 1. Here, we get a qualitative assessment by comparing the observed distribution (histograms) to the densities of similar sized random samples from the priors:


par(xpd=TRUE)
hist(res_null$mu, prob = TRUE, col = "grey",
     border = "white",
     main = "Distribution of mu")

invisible(replicate(30,
     points(density(rexp(500, 1000)), type = "l", col = "blue")))



hist(res_null$pi, prob = TRUE, col = "grey",
     border = "white", main = "Distribution of pi")

invisible(replicate(30,
     points(density(rbeta(500, 10, 1)), type = "l", col = "blue")))


A model using symptom onset only

We can use data and likelihood customisation to change the default outbreaker2 model into a Wallinga & Teunis (1994) model. To do so, we need to:

  • Remove the DNA sequences from the data; alternatively we could also specify a ‘null’ function (i.e. returning a finite constant, as above) for the genetic likelihood.

  • Disable all likelihood components other than timing_infections using custom_likelihoods. This means that the dates provided will be treated as dates of symptom onset, and the timing distribution w will be taken as the serial interval.

  • Disable the detection of imported cases, and forcing all kappa values to be

While these are fairly major changes, they are straightforward to implement. We first create the dataset and custom likelihood functions:


onset_data <- outbreaker_data(dates = fake_outbreak$onset,
                          w_dens = fake_outbreak$w)

wt_model <- custom_likelihoods(timing_sampling = f_null,
                               reporting = f_null)

To fix parameters or augmented data (here, fix all kappa values to 1), we set the initial states to the desired values and disable the corresponding moves:


wt_config <- create_config(init_kappa = 1, move_kappa = FALSE,
                           init_pi = 1, move_pi = FALSE,
                           move_mu = FALSE)

We can now run the analyses for this new model:


set.seed(1)
res_wt <- outbreaker(data = onset_data,
                     config = wt_config,
                     likelihoods = wt_model)
#> Can't use seqTrack initialization with missing DNA sequences; using a star-like tree

plot(res_wt)

plot(res_wt, burnin = 500)

plot(res_wt, burnin = 500, type = "alpha")

summary(res_wt)
#> $step
#>    first     last interval  n_steps 
#>        1    10000       50      201 
#> 
#> $post
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#> -354.93  -34.75  -33.49  -34.88  -31.70  -27.79 
#> 
#> $like
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#> -357.23  -37.05  -35.79  -37.18  -34.00  -30.09 
#> 
#> $prior
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   2.302   2.302   2.302   2.302   2.302   2.302 
#> 
#> $mu
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   1e-04   1e-04   1e-04   1e-04   1e-04   1e-04 
#> 
#> $pi
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>       1       1       1       1       1       1 
#> 
#> $tree
#>    from to time    support generations
#> 1    11  1   19 0.06965174          NA
#> 2     8  2   19 0.06467662          NA
#> 3    18  3   20 0.08955224          NA
#> 4    14  4   18 0.07960199          NA
#> 5    12  5   17 0.07960199          NA
#> 6    12  6   18 0.08457711          NA
#> 7    25  7   16 0.07960199          NA
#> 8     9  8   14 0.06467662          NA
#> 9    23  9   16 0.07462687          NA
#> 10   16 10   13 0.06965174          NA
#> 11   15 11   14 0.07960199          NA
#> 12   15 12   17 0.07960199          NA
#> 13   18 13   17 0.07960199           1
#> 14   20 14   13 0.07960199          NA
#> 15   16 15   14 0.07462687          NA
#> 16   21 16   12 0.08955224          NA
#> 17   20 17   12 0.07960199          NA
#> 18   25 18   13 0.09452736           1
#> 19   21 19   11 0.09452736          NA
#> 20   26 20   11 0.08457711          NA
#> 21   24 21    9 0.09452736          NA
#> 22   25 22    9 0.11940299          NA
#> 23   29 23    8 0.14427861          NA
#> 24   28 24    7 0.13432836          NA
#> 25   29 25    7 0.12437811          NA
#> 26   28 26    6 0.16417910          NA
#> 27   28 27    5 0.15920398          NA
#> 28   29 28    5 0.15422886          NA
#> 29   30 29    4 0.21890547          NA
#> 30   29 30    3 0.10945274          NA

As before for the ‘null’ model, the transmission tree is very poorly resolved in this case. We use the same approach to visualise it: extract nodes and edges from the visNetork object, use this information to create an igraph object, and visualise the result using a circular layout:


## extract nodes and edges from the visNetwork object
temp <- plot(res_wt, type = "network", min_support = 0.05)
class(temp)
#> [1] "visNetwork" "htmlwidget"
head(temp$x$edges)
#>     from to      value arrows   color
#> 53     3  2 0.05472637     to #98D6C7
#> 77     4  2 0.05970149     to #7ED2AC
#> 105    5  1 0.05472637     to #99CAA9
#> 107    5  3 0.07462687     to #99CAA9
#> 135    6  3 0.05970149     to #C2C0AD
#> 137    6  5 0.05472637     to #C2C0AD
head(temp$x$nodes)
#>   id label     value   color shape shaped
#> 1  1     1 0.3432836 #CCDDFF   dot   <NA>
#> 2  2     2 0.3233831 #B2D9E3   dot   <NA>
#> 3  3     3 0.3582090 #98D6C7   dot   <NA>
#> 4  4     4 0.4527363 #7ED2AC   dot   <NA>
#> 5  5     5 0.5472637 #99CAA9   dot   <NA>
#> 6  6     6 0.5174129 #C2C0AD   dot   <NA>

## make an igraph object

net_wt <- graph.data.frame(temp$x$edges,
                             vertices = temp$x$nodes[1:4])
                 
plot(net_wt, layout = layout.circle,
     main = "WT model, posterior trees")


Customising movements

Customising movements works in similar ways to priors and likelihoods. In practice, this type of customisation is more complex as it most likely will require evaluation of likelihoods and priors, and therefore require the user to know which functions to all, and how. These are documented in the API vignette. In the following, we provide two examples:

  • a (fake) Gibbs sampler for the movement of the mutation rate mu

  • a new ‘naive’ movement of ancestries in which infectors are picked at random from all cases

But before getting into these, we need to explicit how movements are happening in outbreaker2.

Movements in outbreaker2

At the core of the outbreaker function, movements are implemented as a list of functions, which are all evaluated in turn during every iteration of the MCMC. All movement functions must obey two rules:

  • The first argument must be an outbreaker_param object (typically called param in the original code); see ?create_param for details.

  • All movement functions must return a valid, outbreaker_param object.

However, a new difficulty compared to prior or likelihood customisation is that different movements may require different components of the model, and a different set of arguments after param. In fact, this can be seen by examining the arguments of all the default movement functions:


lapply(custom_moves(),  args)
#> $mu
#> function (param, data, config, custom_ll = NULL, custom_prior = NULL) 
#> NULL
#> 
#> $pi
#> function (param, data, config, custom_ll = NULL, custom_prior = NULL) 
#> NULL
#> 
#> $eps
#> function (param, data, config, custom_ll = NULL, custom_prior = NULL) 
#> NULL
#> 
#> $lambda
#> function (param, data, config, custom_ll = NULL, custom_prior = NULL) 
#> NULL
#> 
#> $alpha
#> function (param, data, list_custom_ll = NULL) 
#> NULL
#> 
#> $swap_cases
#> function (param, data, list_custom_ll = NULL) 
#> NULL
#> 
#> $t_inf
#> function (param, data, list_custom_ll = NULL) 
#> NULL
#> 
#> $kappa
#> function (param, data, config, list_custom_ll = NULL) 
#> NULL

To handle this difficulty, outbreaker2 transforms every movement function before running the MCMC into a new function with a single parameter param, attaching all other required argument to the function’s environment. The function achieving this transformation is called bind_moves. This function ‘knows’ what these components are for known moves listed aboves. For new, unknown moves, it attaches the following components, provided they are used as arguments of the new function:

See examples in ?bind_moves for details of how this process works.

A (fake) Gibbs sampler for mu

A Gibbs sampler supposes that the conditional distribution of a parameter is known and can directly be sampled from. Here, we use this principle to provide a toy example of custom movement for mu, assuming that this conditional distribution is always an Exponential distribution with a rate of 1000. This is easy to implement; to make sure that the function is actually used, we set a global variable changed when the function is called.


move_mu <- function(param, config) {

  NEW_MOVE_HAS_BEEN_USED <<- TRUE
  param$mu <- rexp(1, 1000)  
  return(param)
  
}

moves <- custom_moves(mu = move_mu)

quick_config <- list(n_iter = 500, sample_every = 1, find_import = FALSE)

Note that the new movement function move_mu has two arguments, and that we do not specify config. Internally, what happens is:


## bind quick_config to function
move_mu_intern <- bind_to_function(move_mu, config = quick_config)

## new function has just one argument
move_mu_intern
#> function (param) 
#> {
#>     NEW_MOVE_HAS_BEEN_USED <<- TRUE
#>     param$mu <- rexp(1, 1000)
#>     return(param)
#> }
#> <environment: 0x40d8d98>

## 'config' is in the function's environment
names(environment(move_mu_intern))
#> [1] "config"

## 'config' is actually 'quick_config'
identical(environment(move_mu_intern)$config, quick_config)
#> [1] TRUE

We perform a quick run using this new movement:


NEW_MOVE_HAS_BEEN_USED <- FALSE

set.seed(1)
res_move_mu <- outbreaker(data, quick_config, moves = moves)
NEW_MOVE_HAS_BEEN_USED
#> [1] TRUE

plot(res_move_mu)

plot(res_move_mu, "pi")

plot(res_move_mu, "mu")

This short, full trace, clearly hasn’t mixed well (as is to be expected). But while we see the effect of accept/reject sampling (Metropolis algorithm) for pi with a lot of autocorrelation, the trace of mu shows complete independence between successive values. While the Gibbs sampler used here is not correct, this result is: a Gibbs sampler will be more efficient than the classical Metropolis(-Hasting) algorithm for a given number a iterations.


A new movement of ancestries

Moves of ancestries are done in two ways in outbreaker: by picking ancestors at random from any prior case, and by swapping cases from a transmission link. Here, we implement a new move, which will propose infectors which have been infected on the same day of the current infector. As before, we will use global variables to keep track of the resulting movements (see N_ACCEPT and N_REJECT).


api <- get_cpp_api()

new_move_ances <- function(param, data, custom_likelihoods = NULL) {

for (i in 1:data$N) {
    current_ances <- param$alpha[i]
    if (!is.na(current_ances)) {
      ## find cases infected on the same days
      current_t_inf <- param$t_inf[current_ances]
      pool <- which(param$t_inf == current_t_inf)
      pool <- setdiff(pool, i)
      
      if (length(pool) > 0) {
        ## propose new ancestor
        current_ll <- api$cpp_ll_all(data, param, i = i, custom_likelihoods)
        param$alpha[i] <- sample(pool, 1)
        new_ll <- api$cpp_ll_all(data, param, i = i, custom_likelihoods)

        ## likelihood ratio - no correction, move is symmetric
        ratio <- exp(new_ll - current_ll)

        ## accept / reject
        if (runif(1) <= ratio) { # accept
          N_ACCEPT <<- N_ACCEPT + 1
        } else { # reject
          N_REJECT <<- N_REJECT + 1
          param$alpha[i] <- current_ances
        }
      }
    }
  }
  return(param)
}

moves <- custom_moves(new_move = new_move_ances)

We can now use this new move in our transmission tree reconstruction. We will use a shorter chain than the defaults as this new move is likely to be computer intensive.


N_ACCEPT <- 0
N_REJECT <- 0

set.seed(1)
res_new_move <- outbreaker(data, list(move_kappa = FALSE), moves = moves)

N_ACCEPT
#> [1] 149856
N_REJECT
#> [1] 265088

plot(res_new_move)

plot(res_new_move, type = "alpha")

summary(res_new_move)
#> $step
#>    first     last interval  n_steps 
#>        1    10000       50      201 
#> 
#> $post
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#> -1087.1  -474.2  -470.7  -469.8  -462.6  -431.4 
#> 
#> $like
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#> -1088.4  -476.3  -472.8  -471.9  -464.7  -433.6 
#> 
#> $prior
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.8666  2.0377  2.1538  2.0799  2.2386  2.3013 
#> 
#> $mu
#>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
#> 9.182e-05 1.348e-04 1.510e-04 1.521e-04 1.678e-04 2.332e-04 
#> 
#> $pi
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.8525  0.9710  0.9836  0.9759  0.9929  0.9999 
#> 
#> $tree
#>    from to time     support generations
#> 1    NA  1   -1 0.004975124          NA
#> 2     1  2    1 0.995024876          NA
#> 3     2  3    3 1.000000000           1
#> 4    NA  4    3          NA          NA
#> 5     3  5    4 0.970149254           1
#> 6     9  6    6 0.900497512           1
#> 7     4  7    5 1.000000000           1
#> 8     5  8    6 0.970149254           1
#> 9     4  9    5 0.885572139           1
#> 10    6 10    7 0.995024876           1
#> 11    7 11    7 0.656716418           1
#> 12    5 12    7 0.870646766           1
#> 13    9 13    7 1.000000000           1
#> 14    5 14    7 0.691542289           1
#> 15    5 15    7 0.736318408           1
#> 16    7 16    8 0.791044776           1
#> 17   26 17    8 0.671641791           1
#> 18    8 18    9 0.437810945           1
#> 19    9 19    8 1.000000000           1
#> 20   10 20   10 0.965174129           1
#> 21   11 21   10 0.955223881           1
#> 22   11 22   10 1.000000000           1
#> 23   13 23    9 1.000000000           1
#> 24   13 24    9 1.000000000           1
#> 25   13 25    9 1.000000000           1
#> 26    7 26    7 0.407960199           1
#> 27   17 27   11 1.000000000           1
#> 28   NA 28    9          NA          NA
#> 29   10 29   11 1.000000000           1
#> 30   13 30   10 1.000000000           1

Results show a switch to a new mode at about 5000 iterations. Let us compare the consensus tree to the actual one (store in fake_outbreak$ances):


summary(res_new_move, burnin = 5000)
#> $step
#>    first     last interval  n_steps 
#>     5050    10000       50      100 
#> 
#> $post
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  -480.6  -470.5  -462.6  -460.5  -457.6  -431.4 
#> 
#> $like
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  -482.2  -472.6  -464.6  -462.6  -459.7  -433.6 
#> 
#> $prior
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   1.129   1.993   2.134   2.070   2.232   2.301 
#> 
#> $mu
#>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
#> 9.182e-05 1.337e-04 1.507e-04 1.510e-04 1.670e-04 2.021e-04 
#> 
#> $pi
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.8778  0.9662  0.9814  0.9748  0.9922  0.9999 
#> 
#> $tree
#>    from to time support generations
#> 1    NA  1   -1    0.01          NA
#> 2     1  2    1    0.99          NA
#> 3     2  3    3    1.00           1
#> 4    NA  4    3      NA          NA
#> 5     3  5    4    0.98           1
#> 6     9  6    6    0.81           1
#> 7     4  7    5    1.00           1
#> 8     5  8    6    0.97           1
#> 9     4  9    5    0.80           1
#> 10    6 10    7    1.00           1
#> 11    7 11    7    0.66           1
#> 12    5 12    7    0.89           1
#> 13    9 13    7    1.00           1
#> 14    5 14    7    0.73           1
#> 15    5 15    7    0.74           1
#> 16    7 16    8    0.86           1
#> 17    7 17    7    0.38           1
#> 18    8 18    9    0.45           1
#> 19    9 19    8    1.00           1
#> 20   10 20   10    0.97           1
#> 21   11 21   10    0.99           1
#> 22   11 22   10    1.00           1
#> 23   13 23    9    1.00           1
#> 24   13 24    9    1.00           1
#> 25   13 25    9    1.00           1
#> 26   17 26    8    0.62           1
#> 27   17 27   10    1.00           1
#> 28   NA 28    9      NA          NA
#> 29   10 29   11    1.00           1
#> 30   13 30   10    1.00           1
tree2 <- summary(res_new_move, burnin = 5000)$tree

comparison <- data.frame(case = 1:30,
                         inferred = paste(tree2$from),
             true = paste(fake_outbreak$ances),
             stringsAsFactors = FALSE)
             
comparison$correct <- comparison$inferred == comparison$true
comparison
#>    case inferred true correct
#> 1     1       NA   NA    TRUE
#> 2     2        1    1    TRUE
#> 3     3        2    2    TRUE
#> 4     4       NA   NA    TRUE
#> 5     5        3    3    TRUE
#> 6     6        9    4   FALSE
#> 7     7        4    4    TRUE
#> 8     8        5    5    TRUE
#> 9     9        4    6   FALSE
#> 10   10        6    6    TRUE
#> 11   11        7    7    TRUE
#> 12   12        5    8   FALSE
#> 13   13        9    9    TRUE
#> 14   14        5    5    TRUE
#> 15   15        5    5    TRUE
#> 16   16        7    7    TRUE
#> 17   17        7    7    TRUE
#> 18   18        8    8    TRUE
#> 19   19        9    9    TRUE
#> 20   20       10   10    TRUE
#> 21   21       11   11    TRUE
#> 22   22       11   11    TRUE
#> 23   23       13   13    TRUE
#> 24   24       13   13    TRUE
#> 25   25       13   13    TRUE
#> 26   26       17   17    TRUE
#> 27   27       17   17    TRUE
#> 28   28       NA   NA    TRUE
#> 29   29       10   10    TRUE
#> 30   30       13   13    TRUE
mean(comparison$correct)
#> [1] 0.9