PandemicLP

library(PandemicLP)
#> Loading required package: rstan
#> Loading required package: StanHeaders
#> Loading required package: ggplot2
#> rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
#> For execution on a local, multicore CPU with excess RAM we recommend calling
#> options(mc.cores = parallel::detectCores()).
#> To avoid recompilation of unchanged Stan programs, we recommend calling
#> rstan_options(auto_write = TRUE)
#> Loading required package: rstantools
#> This is rstantools version 2.1.1

In this vignette, we present the main functionality of the PandemicLP package. The package uses the theory presented in http://est.ufmg.br/covidlp/home/pt/. As the model evolves, new versions of the package are released. This vignette, however, aims to only show the five basic functions of the package.

Loading data

Function load_covid() loads Covid-19 data from online repositories. The first argument is a string with the country’s name. Use country_list() for a complete list of possibilities. The second argument is another string with the state’s name if the country is “Brazil”. Use state_list() for a similar list. Finally, you can use the last_date argument if you don’t want to use all the data and compare the predictions with updated data. After loading the data using load_covid(), it can be plotted using the plot generic function.

MGdata <- load_covid("Brazil","MG", last_date = "2020-11-09")
#> Downloading COVID-19 data from the official Brazilian repository: https://covid.saude.gov.br/
#> Please, be patient...
#>  Done!
plot(MGdata)$new
#> Plotting Data.
#> The generated plot(s) can be stored in a variable.
#> New Cases plot: variable$new

External data can also be loaded and estimated using the package. For that purpose, it needs to be put in an adequate format. Please read help("covid19BH") for an example of using external data to fit and predict one of the package models.

Estimating model

The model is estimated with function pandemic_model. It currently uses rstan to draw samples from the posterior distribution. The first argument can be either the output of the load_covid() function or a list with the proper elements. Customized control of the stan algorithm can be also set in this function. You can choose to fit the ‘confirmed’ or ‘deaths’ series. Use the option covidLPconfig = TRUE to use the same configuration of the app in https://dest-ufmg.shinyapps.io/app_COVID19/.

#MGestimated <- pandemic_model(MGdata, case_type = "deaths", covidLPconfig = TRUE)
## Using pre-generated results for speed
temp <- tempfile(fileext = ".rda")
d <- download.file("https://drive.google.com/u/2/uc?id=165mXm5DbtPENGJlVLVJvzvVfFFWxT_dr&export=download",
  temp, mode = "wb", quiet = TRUE)

# Try to use downloaded version. If not available, run the model
if (!d) load(temp) else {
  warning("Data failed to download from drive. Please check internet connection and try again.")
  knitr::knit_exit()
}

By calling the output of the function, a small summary of the estimation is given.

MGestimated
#> pandemic_model
#>  Family      :        poisson
#>  Mean function form:  static generalized logistic
#>  Type of Case:        deaths
#>  Location:            Brazil_MG
#>  0bservations:        259 
#> 
#> ------
#> Parameters:
#>    mean  2.5%   50% 97.5%   n_eff  Rhat
#> a 1.436 0.794 1.388 2.360 830.144 1.002
#> b 0.019 0.012 0.018 0.029 601.976 1.003
#> c 0.027 0.025 0.027 0.028 558.865 1.003
#> f 2.243 1.894 2.224 2.642 666.962 1.003
#> 
#> ------
#> Priors:
#> 
#>  a   ~  Gamma(0.1, 0.1)
#>  b   ~  LogNormal(0, 20)
#>  c   ~  Gamma(2, 9)
#>  f   ~  Gamma(0.01, 0.01)
#> 
#> Restrictions:
#>  1:  a/b^f < 0.02 *population
#>  2:  f > 1
#> 
#> ------
#> *For help interpreting the printed output see ?print.pandemicEstimated
#> *For more information see ?'summary.pandemicEstimated
#> *For details on the model, priors and restrictions, see ?models

Convergence diagnostics can be used on the ‘fit’ element of the output. In particular, the relevant parameters are ‘a’, ‘b’, ‘c’ and ‘f’.

traceplot(MGestimated)+theme(legend.position = "")

density(MGestimated)

Note: traceplot can be used directly on the output, without a reference to the ‘fit’ element nor the parameters.

Prediction

In order to make the predictions using the model fitted from pandemic_model, use the rstantools::posterior_predict generic function. Also, set custom prediction horizons with options horizonLong and horizonShort. The difference is that the long term prediction uses daily predictions and the short term uses accumulated predictions.

MGpredicted <- posterior_predict(MGestimated, horizonLong=200)
MGpredicted
#> 
#> Predicted pandemic death cases for Brazil_MG. Can be plotted with plot().
#> 
#> Showing predictive stats::median for the long-term predictions for Brazil_MG.
#> 
#> 2020-11-10 2020-11-11 2020-11-12 
#>         28         27         26 
#>    ...
#> 
#> 2021-05-26 2021-05-27 2021-05-28 
#>          0          0          0 
#> 
#> *For customized view, see help(print.pandemicPredicted)
#> **For more details, see help(pandemicPredicted-xs)

The output can also be printed for a small summary. Note that the full predictive distribution sample is returned in this function.

Useful statistics

Some useful statistics from the CovidLP app have been programmed into a function. Use this function to obtain short and long term predictions, predicted total number of cases, predicted peak date and the date when the pandemic is predicted to end.

pandemic_stats(MGpredicted)
#> 
#> 95% Credible Intervals for death cases in Brazil_MG
#> 
#> Short-term Predictions:
#>         date     q2.5      med    q97.5     mean
#> 1 2020-11-10 9222.000     9232 9243.000 9231.899
#> 2 2020-11-11 9245.000     9259 9275.000 9259.260
#> 3 2020-11-12 9267.000     9286 9305.000 9285.913
#>    ...                                                 
#> 12 2020-11-21 9460.950     9501 9543.000 9501.132
#> 13 2020-11-22 9478.975     9523 9567.025 9522.431
#> 14 2020-11-23 9498.000     9544 9589.025 9543.174
#> 
#> ------
#> Long-term Predictions:
#>         date              q2.5               med             q97.5
#> 1 2020-11-10            18.000                28            39.000
#> 2 2020-11-11            17.000                27            38.025
#> 3 2020-11-12            17.000                26            38.000
#>                mean
#> 1            27.899
#> 2            27.361
#> 3            26.653
#>    ...                                                                 
#> 198 2021-05-26            0.000                0            1.000
#> 199 2021-05-27            0.000                0            1.000
#> 200 2021-05-28            0.000                0            1.000
#>                     
#> 198            0.165
#> 199            0.166
#> 200            0.160
#> 
#> ------
#> Total Number of Cases:
#>      q2.5   med    q97.5
#>  9715.925 10313 11300.27
#> 
#> ------
#> Peak Dates:
#>        q2.5        med      q97.5
#>  2020-08-08 2020-08-22 2020-09-06
#> 
#> ------
#> End Dates:
#>        q2.5        med      q97.5
#>  2021-02-03 2021-02-11 2021-02-21
#> 
#> ------
#> *Use plot() to see these statistics in a graph format.
#> *For more information, see ?'pandemicStats-xs'.
#> *For details on the calculations, see ?pandemic_stats.

Plotting

Generic function plot can be used on the prediction to plot the predictions using the plotly package, whose figure is interactive. Up to two plots are outputted, which can be stored for later use.

MGplots <- plot(MGpredicted,term="both")
#> Plotting deaths cases
#> Short term plot created successfully
#> Long term plot created successfully
#> The generated plot(s) can be stored in a variable.
#> Long term plot: variable$long
#> Short term plot: variable$short
MGplots$long
MGplots$short