This tutorial provides a worked example of outbreak reconstruction using outbreaker2. For installation guidelines, a general overview of the package’s functionalities as well as other resources, see the ‘overview’ vignette:

vignette("Overview", package = "outbreaker2")

We will be analysing a small simulated outbreak distributed with the package, fake_outbreak. This dataset contains simulated dates of onsets, partial contact tracing data and pathogen genome sequences for 30 cases:

Here, we will use the dates of case isolation $sample, DNA sequences $dna, contact tracing data $ctd and the empirical distribution of the generation time $w, which can be visualised as:


Running the analysis with defaults

By default, outbreaker2 uses the settings defined by create_config(); see the documentation of this function for details. Note that the main function of outbreaker2 is called outbreaker (without number). The function’s arguments are:

The only mandatory input really is the data. For most cases, customising the method will be done through config and the function create_config(), which creates default and alters settings such as prior parameters, length and rate of sampling from the MCMC, and definition of which parameters should be estimated (‘moved’). The last arguments of outbreaker are used to specify custom prior, likelihood, and movement functions, and are detailed in the ‘Customisation’ vignette.

Let us run the analysis with default settings:

This analysis will take around 40 seconds on a modern computer. Note that outbreaker2 is slower than outbreaker for the same number of iterations, but the two implementations are actually different. In particular, outbreaker2 performs many more moves than the original package for each iteration of the MCMC, resulting in more efficient mixing. In short: outbreaker2 is slower, but it requires far less iterations.

Results are stored in a data.frame with the special class outbreaker_chains:

Each row of res contains a sample from the MCMC. For each, informations about the step (iteration of the MCMC), log-values of posterior, likelihood and priors, and all parameters and augmented data are returned. Ancestries (i.e. indices of the most recent ancestral case for a given case), are indicated by alpha_[index of the case], dates of infections by t_inf_[index of the case], and number of generations between cases and their infector / ancestor by kappa_[index of the case]:


Analysing the results

Graphics

Results can be visualised using plot, which has several options and can be used to derive various kinds of graphics (see ?plot.outbreaker_chains). The basic plot shows the trace of the log-posterior values, which is useful to assess mixing:


plot(res)

The second argument of plot can be used to visualise traces of any other column in res:


plot(res, "prior")

plot(res, "mu")

plot(res, "t_inf_15")

burnin can be used to discard the first iterations prior to mixing:


## compare this to plot(res)
plot(res, burnin = 2000)

type indicates the type of graphic to plot; roughly:

  • trace for traces of the MCMC (default)

  • hist, density to assess distributions of quantitative values

  • alpha, network to visualise ancestries / transmission tree; note that network opens up an interactive plot and requires a web browser with Javascript enabled; the argument min_support is useful to select only the most supported ancestries and avoid displaying too many links

  • kappa to visualise the distributions generations between cases and their ancestor / infector

Here are a few examples:


plot(res, "mu", "density", burnin = 2000)


plot(res, type = "alpha", burnin = 2000)


plot(res, type = "t_inf", burnin = 2000)


plot(res, type = "kappa", burnin = 2000)


plot(res, type = "network", burnin = 2000, min_support = 0.01)

Using summary

The summary of results derives various distributional statistics for posterior, likelihood and prior densities, as well as for the quantitative parameters. It also builds a consensus tree, by finding for each case the most frequent infector / ancestor in the posterior samples. The corresponding frequencies are reported as ‘support’. The most frequent value of kappa is also reported as ‘generations’:


Customising settings and priors

As said before, most customisation can be achieved via create_config. In the following, we make the following changes to the defaults:

  • increase the number of iterations to 30,000

  • set the sampling rate to 20

  • use a star-like initial tree

  • disable to movement of kappa, so that we assume that all cases have observed

  • set a lower rate for the exponential prior of mu (10 instead of 1000)

plot(res2, burnin = 2000)

We can see that the burnin is around 2,500 iterations (i.e. after the initial step corresponding to a local optimum). We get the consensus tree from the new results, and compare the inferred tree to the actual ancestries stored in the dataset (fake_outbreak$ances):


summary(res2, burnin = 3000)
#> $step
#>    first     last interval  n_steps 
#>     3020    30000       20     1350 
#> 
#> $post
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  -545.5  -534.6  -532.0  -532.3  -529.6  -523.0 
#> 
#> $like
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  -550.1  -538.9  -536.3  -536.6  -534.0  -527.6 
#> 
#> $prior
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   3.073   4.260   4.431   4.362   4.539   4.604 
#> 
#> $mu
#>      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
#> 6.365e-05 1.249e-04 1.387e-04 1.404e-04 1.555e-04 2.313e-04 
#> 
#> $pi
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.8436  0.9625  0.9810  0.9739  0.9928  1.0000 
#> 
#> $tree
#>    from to time   support generations
#> 1    NA  1   -1        NA          NA
#> 2     1  2    1 1.0000000           1
#> 3     2  3    3 1.0000000           1
#> 4    NA  4    2        NA          NA
#> 5     3  5    4 1.0000000           1
#> 6     4  6    5 0.9681481           1
#> 7     4  7    5 1.0000000           1
#> 8     5  8    6 0.9333333           1
#> 9    13  9    7 1.0000000           1
#> 10    6 10    7 1.0000000           1
#> 11    7 11    7 0.8503704           1
#> 12    8 12    8 0.8229630           1
#> 13    6 13    6 1.0000000           1
#> 14    5 14    7 0.9829630           1
#> 15    5 15    7 0.7429630           1
#> 16    7 16    8 0.9955556           1
#> 17    7 17    7 0.9911111           1
#> 18    8 18    9 0.9829630           1
#> 19    9 19    9 1.0000000           1
#> 20   10 20   10 0.9674074           1
#> 21   11 21   10 1.0000000           1
#> 22   11 22   10 1.0000000           1
#> 23   13 23    9 1.0000000           1
#> 24   13 24    9 0.9992593           1
#> 25   13 25    8 1.0000000           1
#> 26   17 26    9 1.0000000           1
#> 27   17 27   10 1.0000000           1
#> 28   NA 28    9        NA          NA
#> 29   10 29   11 1.0000000           1
#> 30   13 30   10 1.0000000           1
tree2 <- summary(res2, burnin = 3000)$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        4    4    TRUE
#> 7     7        4    4    TRUE
#> 8     8        5    5    TRUE
#> 9     9       13    6   FALSE
#> 10   10        6    6    TRUE
#> 11   11        7    7    TRUE
#> 12   12        8    8    TRUE
#> 13   13        6    9   FALSE
#> 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.9333333

Let’s visualise the posterior trees:


plot(res2, type = "network", burnin = 3000, min_support = 0.01)