Fitting and selecting models

Kaique S Alves

2020-11-25

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:

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 to estimate a third parameter, 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.

library(epifitter)
library(ggplot2)
library(dplyr)
library(magrittr)
library(cowplot)

Basic usage

Dataset

To use epifitter data at least two variables are needed, one representing the time when each of the multiple assessments of disease intensity were performed during the course of the epidemics and the other representing a disease intensity variable normalized to proportion (e.g. incidence, severity, prevalence). For the case of designed experiments with replicates, a third variable is needed (eg. rep).

Let’s produce a fake 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.02096918
## 3          1   20 0.06946279 0.09405380
## 4          1   30 0.16868385 0.16150663
## 5          1   40 0.35549349 0.35940309
## 6          1   50 0.59989486 0.68811887

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.9977    0.9955 0.2151
## Gompertz      0.9770    0.9550 0.4937
## Monomolecular 0.9353    0.8784 0.6533
## Exponential   0.9147    0.8429 0.6185
## 
##  Infection rate:
##                 Estimate    Std.error      Lower      Upper
## Logistic      0.09952459 0.0007751504 0.09798041 0.09798041
## Gompertz      0.07096875 0.0017792735 0.06742426 0.06742426
## Monomolecular 0.05481379 0.0023544805 0.05012343 0.05012343
## Exponential   0.04471079 0.0022289423 0.04027051 0.04027051
## 
##  Initial inoculum:
##                    Estimate Linearized     lin.SE         Lower         Upper
## Logistic       1.013941e-02  -4.581135 0.04585852  9.262372e-03  0.0110985533
## Gompertz       3.340641e-05  -2.332800 0.10526324  3.016688e-06  0.0002347365
## Monomolecular -1.764110e+00  -1.016719 0.13929295 -2.648093e+00 -1.0943285144
## Exponential    2.831352e-02  -3.564416 0.13186600  2.177252e-02  0.0368195819

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.0995 7.75e-4   0.0980   0.101  -4.58 0.0459     0.995 0.215
## 2          2 Gomp~ 0.0710 1.78e-3   0.0674   0.0745 -2.33 0.105      0.955 0.494
## 3          3 Mono~ 0.0548 2.35e-3   0.0501   0.0595 -1.02 0.139      0.878 0.653
## 4          4 Expo~ 0.0447 2.23e-3   0.0403   0.0492 -3.56 0.132      0.843 0.619
## # ... 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.831352e-02 -0.0183135155
## 2    0 0.01000000 Monomolecular  0.01005034 -1.764110e+00  1.7741101599
## 3    0 0.01000000      Logistic -4.59511985  1.013941e-02 -0.0001394053
## 4    0 0.01000000      Gompertz -1.52717963  3.340641e-05  0.0099665936
## 5   10 0.02096918   Exponential -3.86470157  4.427620e-02 -0.0233070176
## 6   10 0.02096918 Monomolecular  0.02119216 -5.977252e-01  0.6186943352

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.9978    0.9956 0.0270
## Gompertz      0.9962    0.9936 0.0356
## Monomolecular 0.9111    0.8613 0.1577
## Exponential   0.8863    0.8235 0.1762
## 
##  Infection rate:
##                 Estimate   Std.error      Lower      Upper
## Logistic      0.10192149 0.002269048 0.09740132 0.09740132
## Gompertz      0.07227542 0.002062995 0.06816572 0.06816572
## Monomolecular 0.02259819 0.001518930 0.01957232 0.01957232
## Exponential   0.01913312 0.001409556 0.01632514 0.01632514
## 
##  Initial inoculum:
##                    Estimate    Std.error         Lower         Upper
## Logistic       8.913406e-03 9.530964e-04  7.014740e-03  1.081207e-02
## Gompertz       1.787614e-08 2.959276e-08 -4.107566e-08  7.682794e-08
## Monomolecular -1.928208e-01 4.711556e-02 -2.866798e-01 -9.896181e-02
## Exponential    1.791008e-01 2.100483e-02  1.372570e-01  2.209446e-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.9978    0.9956 0.0217
## Gompertz      0.9504    0.9507 0.0962
## Monomolecular 0.9438    0.9007 0.1043
## 
##  Infection rate:
##                 Estimate   Std.error       Lower       Upper
## Logistic      0.10260831 0.002775405 0.097078197 0.097078197
## Gompertz      0.10564947 0.018276704 0.069232346 0.069232346
## Monomolecular 0.01616372 0.003174514 0.009838362 0.009838362
## 
##  Initial inoculum:
##                    Estimate    Std.error         Lower         Upper
## Logistic       6.949942e-03 8.539140e-04  5.248482e-03  8.651403e-03
## Gompertz       2.390538e-17 5.909359e-16 -1.153560e-15  1.201371e-15
## Monomolecular -1.465927e-01 3.321715e-02 -2.127793e-01 -8.040609e-02
## 
##  Maximum disease intensity:
##                Estimate  Std.error     Lower     Upper
## Logistic      0.7976794 0.00529755 0.7871238 0.8082350
## Gompertz      0.6399612 0.01953210 0.6010427 0.6788798
## Monomolecular 1.0000000 0.11146221 0.7779067 1.2220933
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.001000000
## 2   1          1    5 0.01515505 0.002540419
## 3   1          1   10 0.07878459 0.078347457
## 4   1          1   15 0.21411521 0.280294070
## 5   1          1   20 0.39266393 0.483810099
## 6   1          1   25 0.56723412 0.662778563

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.10446308 0.002407946 0.09962658 0.10929958
## 2   1          2 Monomolecular 0.07539519 0.003145689 0.06907688 0.08171349
## 3   1          3      Logistic 0.16657089 0.007649502 0.15120641 0.18193536
## 4   1          4   Exponential 0.09117570 0.009388898 0.07231754 0.11003386
## 5   2          1      Gompertz 0.12372518 0.004002443 0.11568604 0.13176432
## 6   2          2 Monomolecular 0.09701825 0.004366213 0.08824846 0.10578805
##           v0      v0_se r_squared       RSE       CCC            y0
## 1 -2.0024861 0.08513375 0.9741208 0.3248497 0.9868908  0.0006067164
## 2 -0.6420453 0.11121690 0.9199301 0.4243766 0.9582954 -0.9003637464
## 3 -4.7923080 0.27045072 0.9046107 1.0319742 0.9499166  0.0082250813
## 4 -4.1502627 0.33194767 0.6535087 1.2666316 0.7904508  0.0157602758
## 5 -1.9231255 0.14150773 0.9502773 0.5399591 0.9745048  0.0010676337
## 6 -0.7072548 0.15436893 0.9080441 0.5890343 0.9518062 -1.0284152550
##       y0_ci_lwr    y0_ci_upr
## 1  0.0001524253  0.001943661
## 2 -1.3760299964 -0.519922886
## 3  0.0047942879  0.014076226
## 4  0.0080910303  0.030698970
## 5  0.0001126065  0.005802328
## 6 -1.7657557913 -0.487647051

Similarly, all data can be returned.

head(multi_fit$Data)
##   DPC time           y         model   linearized     predicted      residual
## 1   1    0 0.001000000   Exponential -6.907755279  0.0157602758 -0.0147602758
## 2   1    0 0.001000000 Monomolecular  0.001000500 -0.9003637464  0.9013637464
## 3   1    0 0.001000000      Logistic -6.906754779  0.0082250813 -0.0072250813
## 4   1    0 0.001000000      Gompertz -1.932644734  0.0006067164  0.0003932836
## 5   1    5 0.002540419   Exponential -5.975426135  0.0248627594 -0.0223223401
## 6   1    5 0.002540419 Monomolecular  0.002543652 -0.3035214105  0.3060618298

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

## 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.0006677891 0.0004096371 0.10397384 0.003640479 50
## 2   1      Logistic  0.0285661142 0.0047423546 0.14980000 0.006906875 50
## 3   1 Monomolecular -0.1854781248 0.0448218470 0.04360531 0.002712166 50
## 4   1   Exponential  0.2328410135 0.0297890566 0.02731916 0.002655711 50
## 5   2      Gompertz  0.0003519250 0.0004297816 0.13053490 0.008091928 50
## 6   2      Logistic  0.0243982824 0.0064288062 0.19060202 0.013334735 50
##         CCC r_squared        RSE     y0_ci_lwr    y0_ci_upr   r_ci_lwr
## 1 0.9949141 0.9898765 0.03883797 -0.0001549912  0.001490569 0.09666172
## 2 0.9918646 0.9855788 0.04896495  0.0190408147  0.038091414 0.13592713
## 3 0.9498231 0.9178813 0.11565980 -0.2755054541 -0.095450796 0.03815776
## 4 0.8607374 0.7849349 0.18359974  0.1730079325  0.292674095 0.02198501
## 5 0.9875808 0.9753595 0.06160218 -0.0005113168  0.001215167 0.11428179
## 6 0.9856079 0.9730944 0.06609130  0.0114856451  0.037310920 0.16381841
##     r_ci_upr best_model
## 1 0.11128596          1
## 2 0.16367287          2
## 3 0.04905286          3
## 4 0.03265331          4
## 5 0.14678802          1
## 6 0.21738562          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

## 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.0006677880 0.0005092515 0.1039739 0.005392412 1
## 2   1      Logistic  0.0285660564 0.0053514206 0.1498001 0.008482487 1
## 3   1 Monomolecular -0.1854812218 0.0494668598 0.0436056 0.006932552 1
## 4   2      Gompertz  0.0003518778 0.0004945555 0.1305358 0.010389371 1
## 5   2      Logistic  0.0243982316 0.0069279411 0.1906022 0.015084537 1
## 6   2 Monomolecular -0.1755089982 0.0538757339 0.0532146 0.007781455 1
##         K_se df       CCC r_squared        RSE     y0_ci_lwr    y0_ci_upr
## 1 0.01366745 49 0.9949141 0.9898765 0.03923227 -0.0003555912  0.001691167
## 2 0.01420046 49 0.9918646 0.9855788 0.04946207  0.0178119740  0.039320139
## 3 0.06738013 49 0.9498233 0.9178805 0.11683404 -0.2848885983 -0.086073845
## 4 0.01632896 49 0.9875808 0.9753596 0.06222760 -0.0006419686  0.001345724
## 5 0.01525084 49 0.9856079 0.9730944 0.06676230  0.0104760128  0.038320450
## 6 0.05307023 49 0.9452726 0.9092389 0.12402382 -0.2837763390 -0.067241657
##     r_ci_lwr   r_ci_upr  K_ci_lwr K_ci_upr best_model
## 1 0.09313739 0.11481031 0.9725342 1.027466          1
## 2 0.13275387 0.16684626 0.9714631 1.028537          2
## 3 0.02967412 0.05753709 0.8645946 1.135405          3
## 4 0.10965756 0.15141400 0.9671857 1.032814          1
## 5 0.16028866 0.22091568 0.9693523 1.030648          2
## 6 0.03757718 0.06885202 0.8933514 1.106649          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.