Base functions

epifitter provides functions for the fit of (non-flexible) two-parameter population dynamics models to disease progress curve (DPC) data: exponential, monomolecular, logistic and Gompertz.

The goal of fitting these models to DPCs is to estimate numerically meaningful parameters: y0 that represents primary inoculum and r, representing the apparent infection rate. Hence, the importance of choosing the model that best describe the epidemics to better understand its proterties and compare epidemics.

Two approaches can be used to obtain the parameters:

  • Linear regression models that are fitted to a specific transformation of the disease data according to each of the models.
  • Non-linear regression models fitted to original data

Both approaches are available in epifitter. The simplest way to fit these models to single epidemic data is using the fit_lin() and fit_nlin() functions. For the latter, the alternative fit_nlin2() allows estimating a third parameter, the upper asymptote, when maximum disease intensity is not close to 100%. The fit_multi() function is the most flexible and allows to fit all models to multiple epidemic datasets.

First, we need to load the packages we’ll need for this tutorial.

Basic usage

Dataset

To use epifitter data at least two variables are needed, one representing the time of each assessment of disease intensity during the course of the epidemics and the other representing a disease intensity variable as proportion (e.g. incidence, severity, prevalence). For the case of designed experiments with replicates, a third variable is needed.

Let’s simulate a DPC dataset for one epidemics measured at replicated plots. The simulated data resemble a polyciclic epidemics of sigmoid shape. We can do that using the epifitter sim_logistic() function of epifitter (more about ?sim_logistic here).

dpcL <- sim_logistic(
  N = 100, # duration of the epidemics in days
  y0 = 0.01, # disease intensity at time zero
  dt = 10, # interval between assessments
  r = 0.1, # apparent infection rate
  alpha = 0.2, # level of noise
  n = 7 # number of replicates
)

Let’s give a look at the simulated dataset.

head(dpcL)
##   replicates time          y   random_y
## 1          1    0 0.01000000 0.01000000
## 2          1   10 0.02672668 0.01723613
## 3          1   20 0.06946279 0.06154274
## 4          1   30 0.16868385 0.13693906
## 5          1   40 0.35549349 0.39359715
## 6          1   50 0.59989486 0.56812740

The dpc_L object generated using sim_logistic() is a dataframe with four columns. The y variable is a vector for the disease intensity as proportion (0 < y < 1). To facilitate visualization, let’s make a plot using the ggplot function of the ggplot2 package.

ggplot(
  dpcL,
  aes(time, y,
    group = replicates
  )
) +
  geom_point(aes(time, random_y), shape = 1) + # plot the replicate values
  geom_point(color = "steelblue", size = 2) +
  geom_line(color = "steelblue") +
  labs(
    title = "Simulated 'complete' epidemics of sigmoid shape",
    subtitle = "Produced using sim_logistic()"
  )+
  theme_minimal_hgrid()

Linear regression using fit_lin()

The fit_lin() requires at least the time and y arguments. In the example, we will call the random_y which represents the replicates. A quick way to call these variables attached to the dataframe is shown below.

f_lin <- fit_lin(
  time = dpcL$time,  
  y = dpcL$random_y
)

fit_lin() outputs a list object which contains several elements. Three elements of the list are shown by default: stats of model fit, Infection rate and Initial Inoculum

f_lin
## Results of fitting population models 
## 
## Stats:
##                  CCC r_squared    RSE
## Logistic      0.9986    0.9971 0.1736
## Gompertz      0.9776    0.9562 0.4939
## Monomolecular 0.9350    0.8779 0.6661
## Exponential   0.9111    0.8368 0.6384
## 
##  Infection rate:
##                 Estimate    Std.error      Lower      Upper
## Logistic      0.10086264 0.0006256741 0.09961623 0.09961623
## Gompertz      0.07198494 0.0017800392 0.06843892 0.06843892
## Monomolecular 0.05575383 0.0024005851 0.05097162 0.05097162
## Exponential   0.04510881 0.0023005411 0.04052590 0.04052590
## 
##  Initial inoculum:
##                    Estimate Linearized     lin.SE         Lower         Upper
## Logistic       9.715847e-03  -4.624234 0.03701538  9.031430e-03  0.0104515832
## Gompertz       2.589362e-05  -2.357217 0.10530854  2.200752e-06  0.0001910769
## Monomolecular -1.825121e+00  -1.038551 0.14202053 -2.748931e+00 -1.1289562738
## Exponential    2.771775e-02  -3.585682 0.13610184  2.113529e-02  0.0363502712

Model fit stats

The Stats element of the list shows how each of the four models predicted the observations based on three measures:

  • Lin’s concordance correlation coefficient CCC (Lin 2000), a measure of agreement that takes both bias and precision into account
  • Coefficient of determination r_squared (R2), a measure of precision
  • Residual standard deviation RSE for each model.

The four models are sorted from the high to the low CCC. As expected because the sim_logistic function was used to create the synthetic epidemic data, the the Logistic model was superior to the others.

Model coefficients

The estimates, and respective standard error and upper and lower 95% confidence interval, for the two coefficients of interest are shown in the Infection rate and Initial inoculum elements. For the latter, both the back-transformed (estimate) and the linearized estimate are shown.

Global stats

The element f_lin$stats_all provides a wide format dataframe with all the stats for each model.

f_lin$stats_all
## # A tibble: 4 x 14
##   best_model model      r    r_se r_ci_lwr r_ci_upr    v0  v0_se r_squared   RSE
##        <int> <chr>  <dbl>   <dbl>    <dbl>    <dbl> <dbl>  <dbl>     <dbl> <dbl>
## 1          1 Logi~ 0.101  6.26e-4   0.0996   0.102  -4.62 0.0370     0.997 0.174
## 2          2 Gomp~ 0.0720 1.78e-3   0.0684   0.0755 -2.36 0.105      0.956 0.494
## 3          3 Mono~ 0.0558 2.40e-3   0.0510   0.0605 -1.04 0.142      0.878 0.666
## 4          4 Expo~ 0.0451 2.30e-3   0.0405   0.0497 -3.59 0.136      0.837 0.638
## # ... with 4 more variables: CCC <dbl>, y0 <dbl>, y0_ci_lwr <dbl>,
## #   y0_ci_upr <dbl>

Model predictions

The predicted values are stored as a dataframe in the data element called using the same $ operator as above. Both the observed and (y) and the back-transformed predictions (predicted) are shown for each model. The linearized value and the residual are also shown.

head(f_lin$data)
##   time          y         model  linearized     predicted      residual
## 1    0 0.01000000   Exponential -4.60517019  2.771775e-02 -0.0177177469
## 2    0 0.01000000 Monomolecular  0.01005034 -1.825121e+00  1.8351211709
## 3    0 0.01000000      Logistic -4.59511985  9.715847e-03  0.0002841529
## 4    0 0.01000000      Gompertz -1.52717963  2.589362e-05  0.0099741064
## 5   10 0.01723613   Exponential -4.06074730  4.351741e-02 -0.0262812725
## 6   10 0.01723613 Monomolecular  0.01738641 -6.177123e-01  0.6349484227

Plot of predictions

The plot_fit() produces, by default, a panel of plots depicting the observed and predicted values by all fitted models. The arguments pont_size and line_size that control for the size of the dots for the observation and the size of the fitted line, respectively.

plot_lin <- plot_fit(f_lin,
  point_size = 2,
  line_size = 1
) 

# Default plots
plot_lin 

Publication-ready plots

The plots are ggplot2 objects which can be easily customized by adding new layers that override plot paramaters as shown below. The argument models allows to select the models(s) to be shown on the plot. The next plot was customized for the logistic model.

# Customized plots

plot_fit(f_lin,
  point_size = 2,
  line_size = 1,
  models = "Logistic")+
  theme_minimal_hgrid(font_size =18) +
  scale_x_continuous(limits = c(0,100))+
  scale_color_grey()+
   theme(legend.position = "none")+
  labs(
    x = "Time",
    y = "Proportion disease "
    
  )

Non-linear regression

Two-parameters

The fit_nlin() function uses the Levenberg-Marquardt algorithm for least-squares estimation of nonlinear parameters. In addition to time and disease intensity, starting values for y0 and r should be given in the starting_par argument. The output format and interpretation is analogous to the fit_lin().

NOTE: If you encounter error messages saying “matrix at initial parameter estimates”, try to modify the starting values for the parameters to solve the problem.

f_nlin <- fit_nlin(
  time = dpcL$time,
  y = dpcL$random_y,
  starting_par = list(y0 = 0.01, r = 0.03)
)

f_nlin
## Results of fitting population models 
## 
## Stats:
##                  CCC r_squared    RSE
## Logistic      0.9989    0.9979 0.0187
## Gompertz      0.9973    0.9957 0.0299
## Monomolecular 0.9144    0.8665 0.1547
## Exponential   0.8871    0.8249 0.1753
## 
##  Infection rate:
##                 Estimate   Std.error      Lower      Upper
## Logistic      0.10100422 0.001546405 0.09792362 0.09792362
## Gompertz      0.07119455 0.001694747 0.06781844 0.06781844
## Monomolecular 0.02276982 0.001502074 0.01977754 0.01977754
## Exponential   0.01901896 0.001393512 0.01624294 0.01624294
## 
##  Initial inoculum:
##                    Estimate    Std.error         Lower         Upper
## Logistic       9.564961e-03 6.928700e-04  8.184693e-03  1.094523e-02
## Gompertz       6.327240e-08 7.938704e-08 -9.487469e-08  2.214195e-07
## Monomolecular -1.929188e-01 4.629696e-02 -2.851470e-01 -1.006905e-01
## Exponential    1.812723e-01 2.099534e-02  1.394474e-01  2.230972e-01

We can check the results using plot_fit.

plot_fit(f_nlin) +
  theme_minimal_hgrid()#changing plot theme

Estimating K (maximum disease)

In many epidemics the last measure (final time) of a DPC does not reach the maximum intensity and, for this reason, estimation of maximum asymptote (carrying capacity K) may be necessary. The fin_lin2() provides an estimation of K in addition to the estimates provided by fit_lin().

Before demonstrating the function, we can transform our simulated data by creating another variable with y_random2 with maximum about 0.8 (80%). Simplest way is to multiply the y_random by 0.8.

dpcL2 = dpcL %>% 
  mutate(random_y = random_y * 0.8)

Then we run the fit_nlin2() for the new dataset.

f_nlin2 <- fit_nlin2(
  time = dpcL2$time,
  y = dpcL2$random_y,
  starting_par = list(y0 = 0.01, r = 0.2, K =  0.6)
)
f_nlin2
## Results of fitting population models 
## 
## Stats:
##                  CCC r_squared    RSE
## Logistic      0.9989    0.9979 0.0150
## Gompertz      0.9576    0.9612 0.0894
## Monomolecular 0.9469    0.9058 0.1014
## 
##  Infection rate:
##                 Estimate   Std.error      Lower      Upper
## Logistic      0.10130088 0.001884182 0.09754657 0.09754657
## Gompertz      0.10521025 0.016617407 0.07209934 0.07209934
## Monomolecular 0.01625929 0.003083416 0.01011545 0.01011545
## 
##  Initial inoculum:
##                    Estimate    Std.error         Lower         Upper
## Logistic       7.568782e-03 6.270115e-04  6.319435e-03  8.818130e-03
## Gompertz       3.127783e-18 7.532715e-17 -1.469648e-16  1.532204e-16
## Monomolecular -1.462800e-01 3.231788e-02 -2.106748e-01 -8.188519e-02
## 
##  Maximum disease intensity:
##                Estimate   Std.error     Lower     Upper
## Logistic      0.7989610 0.003674896 0.7916386 0.8062834
## Gompertz      0.6480742 0.018342803 0.6115254 0.6846231
## Monomolecular 1.0000000 0.107222594 0.7863543 1.2136457
plot_fit(f_nlin2)

NOTE: The exponential model is not included because it doesn’t have a maximum asymptote. The estimated value of K is the expected 0.8.

Fit models to multiple DPCs

Most commonly, there are more than one epidemics to analyse either from observational or experimental studies. When the goal is to fit a common model to all curves, the fit_multi() function is in hand. Each DPC needs an unique identified to further combined in a single data frame.

Data

Let’s use the sim_ family of functions to create three epidemics and store the data in a single data.frame. The Gompertz model was used to simulate these data. Note that we allowed to the y0 and r parameter to differ the DPCs. We should combine the three DPCs using the bind_rows() function and name the identifier (.id), automatically created as a character vector, for each epidemics as ‘DPC’.

epi1 <- sim_gompertz(N = 60, y0 = 0.001, dt = 5, r = 0.1, alpha = 0.4, n = 4)
epi2 <- sim_gompertz(N = 60, y0 = 0.001, dt = 5, r = 0.12, alpha = 0.4, n = 4)
epi3 <- sim_gompertz(N = 60, y0 = 0.003, dt = 5, r = 0.14, alpha = 0.4, n = 4)

multi_epidemic <- bind_rows(epi1,
  epi2,
  epi3,
  .id = "DPC"
)
head(multi_epidemic)
##   DPC replicates time          y   random_y
## 1   1          1    0 0.00100000 0.00100000
## 2   1          1    5 0.01515505 0.01007585
## 3   1          1   10 0.07878459 0.06413633
## 4   1          1   15 0.21411521 0.25593106
## 5   1          1   20 0.39266393 0.33195906
## 6   1          1   25 0.56723412 0.61641071

We can visualize the three DPCs in a same plot

p_multi <- ggplot(multi_epidemic,
       aes(time, y, shape = DPC, group = DPC))+
  geom_point(size =2)+
  geom_line()+
  theme_minimal_grid(font_size =18) +
   labs(
    x = "Time",
    y = "Proportion disease "
    
  )
p_multi

Or use facet_wrap() for ploting them separately.

p_multi +
  facet_wrap(~ DPC, ncol = 1)

Using fit_multi()

fit_multi() requires at least four arguments: time, disease intensity (as proportion), data and the curve identifier (strata_cols). The latter argument accepts one or more strata include as c("strata1",strata2"). In the example below, the stratum name is DPC, the name of the variable.

By default, the linear regression is fitted to data but adding another argument nlin = T, the non linear regressions is fitted instead.

multi_fit <- fit_multi(
  time_col = "time",
  intensity_col = "random_y",
  data = multi_epidemic,
  strata_cols = "DPC"
)

All parameters of the list can be returned using the $ operator as below.

head(multi_fit$Parameters)
##   DPC best_model         model          r        r_se   r_ci_lwr   r_ci_upr
## 1   1          1      Gompertz 0.10102759 0.002329434 0.09634879 0.10570640
## 2   1          2 Monomolecular 0.07299717 0.002971135 0.06702947 0.07896487
## 3   1          3      Logistic 0.15916441 0.007231719 0.14463908 0.17368975
## 4   1          4   Exponential 0.08616725 0.008836459 0.06841869 0.10391580
## 5   2          1      Gompertz 0.12064521 0.002045528 0.11653664 0.12475377
## 6   2          2 Monomolecular 0.09420589 0.003221497 0.08773532 0.10067646
##           v0      v0_se r_squared       RSE       CCC            y0
## 1 -1.9311512 0.08235792 0.9741061 0.3142578 0.9868832  0.0010103624
## 2 -0.6119105 0.10504548 0.9235035 0.4008280 0.9602307 -0.8439508205
## 3 -4.5503386 0.25567988 0.9064380 0.9756122 0.9509232  0.0104532036
## 4 -3.9384281 0.31241602 0.6553827 1.1921035 0.7918201  0.0194788093
## 5 -1.9689969 0.07232035 0.9858302 0.2759569 0.9928646  0.0007743496
## 6 -0.7582087 0.11389713 0.9447603 0.4346037 0.9715956 -1.1344493369
##       y0_ci_lwr    y0_ci_upr
## 1  0.0002921556  0.002891901
## 2 -1.2770950279 -0.493198389
## 3  0.0062812407  0.017347785
## 4  0.0104001594  0.036482519
## 5  0.0002526702  0.002039670
## 6 -1.6831133263 -0.697980449

Similarly, all data can be returned.

head(multi_fit$Data)
##   DPC time          y         model  linearized    predicted      residual
## 1   1    0 0.00100000   Exponential -6.90775528  0.019478809 -1.847881e-02
## 2   1    0 0.00100000 Monomolecular  0.00100050 -0.843950820  8.449508e-01
## 3   1    0 0.00100000      Logistic -6.90675478  0.010453204 -9.453204e-03
## 4   1    0 0.00100000      Gompertz -1.93264473  0.001010362 -1.036237e-05
## 5   1    5 0.01007585   Exponential -4.59761341  0.029968997 -1.989314e-02
## 6   1    5 0.01007585 Monomolecular  0.01012696 -0.280082621  2.901585e-01

If nonlinear regression is preferred, the nlim argument should be set to TRUE

multi_fit2 <- fit_multi(
  time_col = "time",
  intensity_col = "random_y",
  data = multi_epidemic,
  strata_cols = "DPC",
  nlin = TRUE)
## Warning in log(y0/1): NaNs produzidos

## Warning in log(y0/1): NaNs produzidos

## Warning in log(y0/1): NaNs produzidos

## Warning in log(y0/1): NaNs produzidos

## Warning in log(y0/1): NaNs produzidos
head(multi_fit2$Parameters)
##   DPC         model           y0        y0_se          r        r_se df
## 1   1      Gompertz  0.001285655 0.0009068164 0.09790763 0.004497741 50
## 2   1      Logistic  0.033066900 0.0061158199 0.13988035 0.007508601 50
## 3   1 Monomolecular -0.179984958 0.0455413612 0.04258735 0.002709457 50
## 4   1   Exponential  0.227470772 0.0289006034 0.02766148 0.002631392 50
## 5   2      Gompertz  0.001594229 0.0008054165 0.11381729 0.003917603 50
## 6   2      Logistic  0.034074174 0.0050456688 0.16353669 0.007093510 50
##         CCC r_squared        RSE     y0_ci_lwr    y0_ci_upr   r_ci_lwr
## 1 0.9905505 0.9813924 0.05238858 -5.357394e-04  0.003107049 0.08887365
## 2 0.9879959 0.9776336 0.05880710  2.078291e-02  0.045350886 0.12479888
## 3 0.9468662 0.9128863 0.11821003 -2.714575e-01 -0.088512442 0.03714525
## 4 0.8662004 0.7923232 0.17951461  1.694222e-01  0.285519342 0.02237617
## 5 0.9954249 0.9909629 0.03659580 -2.349813e-05  0.003211955 0.10594856
## 6 0.9933308 0.9878864 0.04401047  2.393965e-02  0.044208698 0.14928896
##     r_ci_upr best_model
## 1 0.10694161          1
## 2 0.15496182          2
## 3 0.04802946          3
## 4 0.03294678          4
## 5 0.12168603          1
## 6 0.17778443          2

Want to estimate K?

If you want to estimate K, set nlin = TRUE and estimate_K = TRUE.

NOTE: If you do not set both arguments TRUE, K will not be estimated, because nlin defaut is FALSE. Also remember that when estimating K, we don’t fit the Exponential model.

multi_fit_K <- fit_multi(
  time_col = "time",
  intensity_col = "random_y",
  data = multi_epidemic,
  strata_cols = "DPC",
  nlin = T,
  estimate_K = T
)
## Warning in log(y0/K): NaNs produzidos

## Warning in log(y0/K): NaNs produzidos

## Warning in log(y0/K): NaNs produzidos

## Warning in log(y0/K): NaNs produzidos

## Warning in log(y0/K): NaNs produzidos

## Warning in log(y0/K): NaNs produzidos

## Warning in log(y0/K): NaNs produzidos
head(multi_fit_K$Parameters)
##   DPC         model           y0        y0_se          r        r_se K
## 1   1      Gompertz  0.001285666 0.0011436084 0.09790758 0.006922048 1
## 2   1      Logistic  0.033067145 0.0069921277 0.13988010 0.009488550 1
## 3   1 Monomolecular -0.179990399 0.0504098328 0.04258786 0.007096232 1
## 4   2      Gompertz  0.001594224 0.0009504817 0.11381732 0.005358546 1
## 5   2      Logistic  0.034073808 0.0055301536 0.16353715 0.008331526 1
## 6   2 Monomolecular -0.169866500 0.0464104274 0.05047215 0.006672790 1
##         K_se df       CCC r_squared        RSE     y0_ci_lwr    y0_ci_upr
## 1 0.01975755 49 0.9905505 0.9813924 0.05292046 -0.0010125014  0.003583833
## 2 0.01824093 49 0.9879959 0.9776336 0.05940414  0.0190159387  0.047118352
## 3 0.07151026 49 0.9468665 0.9128849 0.11941016 -0.2812927506 -0.078688047
## 4 0.01088041 49 0.9954249 0.9909629 0.03696735 -0.0003158408  0.003504288
## 5 0.01116260 49 0.9933308 0.9878863 0.04445729  0.0229605478  0.045187067
## 6 0.04965422 49 0.9575543 0.9311096 0.10758524 -0.2631317461 -0.076601255
##     r_ci_lwr   r_ci_upr  K_ci_lwr K_ci_upr best_model
## 1 0.08399720 0.11181795 0.9602957 1.039704          1
## 2 0.12081214 0.15894805 0.9633435 1.036657          2
## 3 0.02832745 0.05684827 0.8562948 1.143705          3
## 4 0.10304892 0.12458572 0.9781350 1.021865          1
## 5 0.14679432 0.18027997 0.9775679 1.022432          2
## 6 0.03706267 0.06388162 0.9002161 1.099784          3

Graphical outputs

Use ggplot2 to produce elegant data visualizations of models curves and the estimated parameters.

DPCs and fitted curves

The original data and the predicted values by each model are stored in multi_fit$Data. A nice plot can be produced as follows:

multi_fit$Data %>%
  ggplot(aes(time, predicted, color = DPC)) +
  geom_point(aes(time, y), color = "gray") +
  geom_line(size = 1) +
  facet_grid(DPC ~ model, scales = "free_y") +
  theme_minimal_hgrid()+
  coord_cartesian(ylim = c(0, 1))

Using the dplyr function filter only the model of interest can be chosen for plotting.

multi_fit$Data %>%
  filter(model == "Gompertz") %>%
  ggplot(aes(time, predicted, color = DPC)) +
  geom_point(aes(time, y),
    color = "gray",
    size = 2
  ) +
  geom_line(size = 1.2) +
  theme_minimal_hgrid() +
  labs(
    x = "Time",
    y = "Disease Intensity"
  )

Apparent infection rate

The multi_fit$Parameters element is where all stats and parameters as stored. Let’s plot the estimates of the apparent infection rate.

multi_fit$Parameters %>%
  filter(model == "Gompertz") %>%
  ggplot(aes(DPC, r)) +
  geom_point(size = 3) +
  geom_errorbar(aes(ymin = r_ci_lwr, ymax = r_ci_upr),
    width = 0,
    size = 1
  ) +
  labs(
    x = "Time",
    y = "Apparent infection rate"
  ) +
  theme_minimal_hgrid()

References

Lin L (2000). A note on the concordance correlation coefficient. Biometrics 56: 324 - 325.