This vignette details the structure and construction of the incidence_fit and incidence_fit_list classes, which are produced by the fit() and fit_optim_split() functions, respectively. By the end of this tutorial, you should be able to construct incidence_fit and incidence_fit_list objects for use with your own models.

Structure of an incidence_fit object

An incidence_fit object contains three elements:

$model: The model fit to an incidence object. Currently, this represents a log-linear model, but it can be any model.

$info: Information derived from the model

r The growth rate

r.conf the confidence interval of r

pred a data frame containing the predictions of the model using the true dates (dates), their numeric version used in the model (dates.x), the predicted value (fit), and the lower (lwr) and upper (upr) bounds of the associated confidence interval.

doubling the predicted doubling time in days (only if r is positive)

doubling.conf the confidence interval of the doubling time

halving the predicted halving time in days (only if r is negative)

halving.conf the confidence interval of the halving time

$origin: the date corresponding to day ‘0’

Internally, when fit() is run, these elements are constructed by function incidence:::extract_info(). First we need to setup data. We will use simulated Ebola outbreak data from the outbreaks package over weekly intervals and calculate the fit for the first 20 weeks:

This will be important later when we combine several incidence_fit objects into a single incidence_fit_list.

Building an incidence_fit object from scratch

The incidence_fit object can be constructed from any model from which you can derive the daily growth rate, doubling/halving times, predictions, and confidence intervals. The following three steps show roughly how it is done from model fitting to construction.

Step 1: create the model

The default model for fit() is a log-linear model on the intervals between dates. To fit this model, we will need to create a data frame with the counts and the midpoints of the intervals:

Structure of an incidence_fit_list object

There are several reasons for having multiple fits to a single incidence object. One may want to have a separate fit for different groups represented in the object, or one may want to split the fits at the peak of the epidemic. To aid in plotting and summarizing the different fits, we’ve created the incidence_fit_list class. This class has two defining features:

It consists of a named list containing one or more incidence_fit objects or lists containing incidence_fit objects.

An attribute called “locations” contains a list whose length is equal to the number of incidence_fit objects in the object. Each list element contains a vector that defines where an incidence_fit object is within the incidence_fit_list.

The reason for this structure is because it is sometimes necessary to nest lists of incidence_fit objects within lists. When this happens, accessing individual elements of the objects cumbersome. To alleviate this, each object has a distinct path within the named list in the “locations” attribute that allows one to access the object directly since R allows you to traverse the elements of a nested list by subsetting with a vector:

Here you can see that the object looks very similar to the incidence_fit object, but it has extra information. The first thing you may notice is the fact that both “doubling” and “halving” are shown. This is because the two fits have different signs for the daily growth rate. The second thing you may notice is the fact that there is something called attr(x, 'locations'). This attribute gives the location of the incidence_fit objects within the list. We can illustrate how this works if we look at the structure of the object:

Example: Nested incidence_fit

Above, we showed the example of a basic incidence_fit_list class with two objects representing the fits before and after the peak of an epicurve. However, it is often useful evaluate fits for different groups separately. Here, we will construct an incidence object, but define groups by gender:

Because all that defines an incidence_fit_list is the class definition and the ‘locations’ attribute that defines the positions of the incidence_fit objects within the nesting, then it’s also possible to define the output of fit_optim_split() as an incidence_fit_list class: