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 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.
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.
## replicates time y random_y
## 1 1 0 0.01000000 0.01288201
## 2 1 10 0.02672668 0.03209673
## 3 1 20 0.06946279 0.05593189
## 4 1 30 0.16868385 0.11354925
## 5 1 40 0.35549349 0.37117859
## 6 1 50 0.59989486 0.58523236
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()
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.
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
## Results of fitting population models
##
## Stats:
## CCC r_squared RSE
## Logistic 0.9980 0.9961 0.2014
## Gompertz 0.9772 0.9554 0.4940
## Monomolecular 0.9358 0.8793 0.6541
## Exponential 0.9158 0.8447 0.6175
##
## Infection rate:
## Estimate Std.error Lower Upper
## Logistic 0.10005022 0.0007259609 0.09860403 0.09860403
## Gompertz 0.07131991 0.0017801659 0.06777364 0.06777364
## Monomolecular 0.05509536 0.0023571854 0.05039960 0.05039960
## Exponential 0.04495487 0.0022253651 0.04052171 0.04052171
##
## Initial inoculum:
## Estimate Linearized lin.SE Lower Upper
## Logistic 9.853887e-03 -4.609987 0.04294843 9.053185e-03 0.0107246390
## Gompertz 2.876842e-05 -2.347198 0.10531603 2.505435e-06 0.0002081296
## Monomolecular -1.787216e+00 -1.025043 0.13945297 -2.679761e+00 -1.1111625562
## Exponential 2.773824e-02 -3.584943 0.13165437 2.133914e-02 0.0360562783
The Stats
element of the list shows how each of the four
models predicted the observations based on three measures:
CCC
(Lin
2000), a measure of agreement that takes both bias and precision into
accountr_squared
(R2),
a measure of precisionRSE
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.
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.
The element f_lin$stats_all
provides a wide format
dataframe with all the stats for each model.
## # A tibble: 4 × 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.100 7.26e-4 0.0986 0.101 -4.61 0.0429 0.996 0.201
## 2 2 Gomp… 0.0713 1.78e-3 0.0678 0.0749 -2.35 0.105 0.955 0.494
## 3 3 Mono… 0.0551 2.36e-3 0.0504 0.0598 -1.03 0.139 0.879 0.654
## 4 4 Expo… 0.0450 2.23e-3 0.0405 0.0494 -3.58 0.132 0.845 0.618
## # ℹ 4 more variables: CCC <dbl>, y0 <dbl>, y0_ci_lwr <dbl>, y0_ci_upr <dbl>
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.
## time y model linearized predicted residual
## 1 0 0.01288201 Exponential -4.35192321 2.773824e-02 -0.014856228
## 2 0 0.01288201 Monomolecular 0.01296571 -1.787216e+00 1.800098239
## 3 0 0.01288201 Logistic -4.33895751 9.853887e-03 0.003028127
## 4 0 0.01288201 Gompertz -1.47061787 2.876842e-05 0.012853245
## 5 10 0.03209673 Exponential -3.43900124 4.348259e-02 -0.011385867
## 6 10 0.03209673 Monomolecular 0.03262312 -6.065512e-01 0.638647916
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.
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.
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.9981 0.9961 0.0253
## Gompertz 0.9960 0.9931 0.0367
## Monomolecular 0.9105 0.8605 0.1584
## Exponential 0.8871 0.8249 0.1758
##
## Infection rate:
## Estimate Std.error Lower Upper
## Logistic 0.10172790 0.002117684 0.09750925 0.09750925
## Gompertz 0.07144988 0.002091371 0.06728365 0.06728365
## Monomolecular 0.02253934 0.001520224 0.01951090 0.01951090
## Exponential 0.01921620 0.001411105 0.01640514 0.01640514
##
## Initial inoculum:
## Estimate Std.error Lower Upper
## Logistic 8.754409e-03 8.786398e-04 7.004069e-03 1.050475e-02
## Gompertz 2.728028e-08 4.488888e-08 -6.214296e-08 1.167035e-07
## Monomolecular -1.938651e-01 4.730158e-02 -2.880947e-01 -9.963555e-02
## Exponential 1.777736e-01 2.088804e-02 1.361625e-01 2.193847e-01
We can check the results using plot_fit
.
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.
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.9981 0.9961 0.0204
## Gompertz 0.9696 0.9689 0.0771
## Monomolecular 0.9434 0.9001 0.1048
##
## Infection rate:
## Estimate Std.error Lower Upper
## Logistic 0.10138711 0.002556884 0.096292409 0.096292409
## Gompertz 0.10070566 0.013061303 0.074680441 0.074680441
## Monomolecular 0.01614014 0.003187261 0.009789388 0.009789388
##
## Initial inoculum:
## Estimate Std.error Lower Upper
## Logistic 7.093591e-03 8.082882e-04 5.483042e-03 8.704141e-03
## Gompertz 2.076096e-18 4.146682e-17 -8.054835e-17 8.470054e-17
## Monomolecular -1.476832e-01 3.336607e-02 -2.141666e-01 -8.119985e-02
##
## Maximum disease intensity:
## Estimate Std.error Lower Upper
## Logistic 0.8012138 0.005039044 0.7911733 0.8112543
## Gompertz 0.6747932 0.016549955 0.6418167 0.7077697
## Monomolecular 1.0000000 0.112276709 0.7762838 1.2237162
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.
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.
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.001153009
## 2 1 1 5 0.01515505 0.018754560
## 3 1 1 10 0.07878459 0.083523945
## 4 1 1 15 0.21411521 0.144475412
## 5 1 1 20 0.39266393 0.280676654
## 6 1 1 25 0.56723412 0.520068774
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.
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.
## DPC best_model model r r_se r_ci_lwr r_ci_upr
## 1 1 1 Gompertz 0.10217034 0.002547829 0.09705288 0.10728781
## 2 1 2 Monomolecular 0.07433077 0.003205973 0.06789138 0.08077015
## 3 1 3 Logistic 0.15894431 0.006906309 0.14507258 0.17281604
## 4 1 4 Exponential 0.08461354 0.008484865 0.06757119 0.10165589
## 5 2 1 Gompertz 0.12556890 0.002551448 0.12044416 0.13069363
## 6 2 2 Logistic 0.17832883 0.006771428 0.16472802 0.19192965
## v0 v0_se r_squared RSE CCC y0
## 1 -1.9439806 0.09007935 0.9698447 0.3437209 0.9846916 0.0009242703
## 2 -0.6326009 0.11334825 0.9149006 0.4325093 0.9555594 -0.8825003234
## 3 -4.5064108 0.24417491 0.9137427 0.9317121 0.9549274 0.0109174989
## 4 -3.8738100 0.29998528 0.6654323 1.1446709 0.7991106 0.0207790504
## 5 -2.0457351 0.09020731 0.9797742 0.3442092 0.9897838 0.0004373212
## 6 -4.3827047 0.23940615 0.9327557 0.9135156 0.9652081 0.0123374141
## y0_ci_lwr y0_ci_upr
## 1 2.312188e-04 0.002937497
## 2 -1.363793e+00 -0.499203846
## 3 6.713821e-03 0.017706278
## 4 1.137488e-02 0.037958121
## 5 9.408764e-05 0.001575754
## 6 7.663708e-03 0.019804492
Similarly, all data can be returned.
## DPC time y model linearized predicted residual
## 1 1 0 0.001153009 Exponential -6.765380462 0.0207790504 -0.0196260417
## 2 1 0 0.001153009 Monomolecular 0.001153674 -0.8825003234 0.8836533322
## 3 1 0 0.001153009 Logistic -6.764226789 0.0109174989 -0.0097644901
## 4 1 0 0.001153009 Gompertz -1.911818500 0.0009242703 0.0002287384
## 5 1 5 0.018754560 Exponential -3.976318334 0.0317220807 -0.0129675203
## 6 1 5 0.018754560 Monomolecular 0.018932657 -0.2981588931 0.3169134535
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 produced
## Warning in log(y0/1): NaNs produced
## Warning in log(y0/1): NaNs produced
## Warning in log(y0/1): NaNs produced
## Warning in log(y0/1): NaNs produced
## Warning in log(y0/1): NaNs produced
## Warning in log(y0/1): NaNs produced
## DPC model y0 y0_se r r_se df
## 1 1 Gompertz 0.0005425827 0.0004332554 0.10260336 0.004498526 50
## 2 1 Logistic 0.0270177252 0.0049330294 0.14841131 0.007405287 50
## 3 1 Monomolecular -0.1833580420 0.0471072174 0.04263708 0.002797586 50
## 4 1 Exponential 0.2265791949 0.0293175295 0.02776921 0.002677972 50
## 5 2 Gompertz 0.0012970838 0.0007355385 0.11456556 0.004233988 50
## 6 2 Logistic 0.0315972992 0.0046459447 0.16604574 0.006987654 50
## CCC r_squared RSE y0_ci_lwr y0_ci_upr r_ci_lwr
## 1 0.9919604 0.9840347 0.04891711 -0.0003276363 0.001412802 0.09356781
## 2 0.9904185 0.9820832 0.05326701 0.0171094441 0.036926006 0.13353735
## 3 0.9438356 0.9082860 0.12223897 -0.2779756727 -0.088740411 0.03701797
## 4 0.8633812 0.7886074 0.18255246 0.1676932038 0.285465186 0.02239035
## 5 0.9947982 0.9897488 0.03916555 -0.0001802887 0.002774456 0.10606134
## 6 0.9938617 0.9885615 0.04242983 0.0222656446 0.040928954 0.15201063
## r_ci_upr best_model
## 1 0.11163892 1
## 2 0.16328526 2
## 3 0.04825620 3
## 4 0.03314808 4
## 5 0.12306977 1
## 6 0.18008086 2
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, becausenlin
defaut isFALSE
. 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 produced
## Warning in log(y0/K): NaNs produced
## Warning in log(y0/K): NaNs produced
## Warning in log(y0/K): NaNs produced
## Warning in log(y0/K): NaNs produced
## Warning in log(y0/K): NaNs produced
## Warning in log(y0/K): NaNs produced
## DPC model y0 y0_se r r_se K
## 1 1 Gompertz 0.0005425893 0.0005441714 0.10260330 0.006762012 1
## 2 1 Logistic 0.0270177764 0.0055987127 0.14841121 0.009165249 1
## 3 1 Monomolecular -0.1833655527 0.0521354807 0.04263778 0.007318205 1
## 4 2 Gompertz 0.0012970819 0.0008689429 0.11456556 0.005783373 1
## 5 2 Logistic 0.0315972080 0.0050901281 0.16604587 0.008182928 1
## 6 2 Monomolecular -0.1696667805 0.0477631767 0.05030028 0.006864940 1
## K_se df CCC r_squared RSE y0_ci_lwr y0_ci_upr
## 1 0.01776785 49 0.9919604 0.9840347 0.04941374 -0.0005509641 0.001636143
## 2 0.01583066 49 0.9904185 0.9820832 0.05380780 0.0157667420 0.038268811
## 3 0.07380870 49 0.9438361 0.9082840 0.12348000 -0.2881357236 -0.078595382
## 4 0.01163812 49 0.9947982 0.9897488 0.03956318 -0.0004491243 0.003043288
## 5 0.01071660 49 0.9938617 0.9885615 0.04286060 0.0213682127 0.041826203
## 6 0.05137919 49 0.9551460 0.9273358 0.11077045 -0.2656504776 -0.073683083
## r_ci_lwr r_ci_upr K_ci_lwr K_ci_upr best_model
## 1 0.08901453 0.11619208 0.9642942 1.035706 1
## 2 0.12999295 0.16682947 0.9681871 1.031813 2
## 3 0.02793129 0.05734426 0.8516759 1.148324 3
## 4 0.10294344 0.12618769 0.9766123 1.023388 1
## 5 0.14960166 0.18249008 0.9784642 1.021536 2
## 6 0.03650467 0.06409589 0.8967496 1.103250 3
Use ggplot2
to produce elegant data visualizations of models curves and the
estimated parameters.
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))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Using the dplyr function filter
only the model
of interest can be chosen for plotting.
The multi_fit$Parameters
element is where all stats and
parameters as stored. Let’s plot the estimates of the apparent infection
rate.
Lin L (2000). A note on the concordance correlation coefficient. Biometrics 56: 324 - 325.