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.
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.
= load_covid("Brazil","MG")
MGdata plot(MGdata)$new
#> Plotting Data
#> The generated plot(s) can be stored in a variable.
#> New Cases plot: variable$new
#> Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
#> Please use `arrange()` instead.
#> See vignette('programming') for more help
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
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.
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/.
# Commenting to reduce vignette runtime
# MGestimated = pandemic_model(MGdata, case_type = "deaths", covidLPconfig = TRUE)
# Load precomputed MCMC
download.file("http://github.com/CovidLP/PandemicLP/raw/master/temp/PandemicLP.rda","./PandemicLP.rda")
load("./PandemicLP.rda")
By calling the output of the function, a small summary of the estimation is given.
MGestimated#> pandemic_model
#> Distribution: poisson
#> Mean function form: static generalized logistic
#> Type of Case: deaths
#> Location: Brazil_MG
#> 0bservations: 275
#>
#> ------
#> Parameters:
#> mean 2.5% 50% 97.5% n_eff Rhat
#> a 0.747 0.394 0.721 1.305 696.147 1.000
#> b 0.056 0.037 0.055 0.083 516.922 1.003
#> c 0.023 0.021 0.023 0.024 538.510 1.005
#> f 3.350 2.749 3.310 4.105 540.332 1.002
#>
#> ------
#> 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.
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.
= posterior_predict(MGestimated,horizonLong=200)
MGpredicted
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-26 2020-11-27 2020-11-28
#> 26 25 25
#> ...
#>
#> 2021-06-11 2021-06-12 2021-06-13
#> 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.
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-26 9875.000 9884 9894.000 9883.804
#> 2 2020-11-27 9896.000 9909 9923.000 9909.308
#> 3 2020-11-28 9916.975 9934 9952.025 9934.328
#> ...
#> 12 2020-12-07 10101.000 10139 10181.000 10139.182
#> 13 2020-12-08 10120.000 10160 10201.025 10160.030
#> 14 2020-12-09 10138.000 10181 10223.000 10180.203
#>
#> ------
#> Long-term Predictions:
#> date q2.5 med q97.5
#> 1 2020-11-26 17.000 26.0 36.000
#> 2 2020-11-27 16.000 25.0 35.025
#> 3 2020-11-28 15.000 25.0 36.000
#> mean
#> 1 25.804
#> 2 25.504
#> 3 25.020
#> ...
#> 198 2021-06-11 0.000 0.0 2.000 0.351
#> 199 2021-06-12 0.000 0.0 2.000 0.378
#> 200 2021-06-13 0.000 0.0 2.000 0.357
#>
#> ------
#> Total Number of Cases:
#> q2.5 med q97.5
#> 10400.85 11064.5 12163.17
#>
#> ------
#> Peak Dates:
#> q2.5 med q97.5
#> 2020-08-08 2020-08-24 2020-09-11
#>
#> ------
#> End Dates:
#> q2.5 med q97.5
#> 2021-03-08 2021-03-16 2021-03-26
#>
#> ------
#> *Use plot() to see these statistics in a graph format.
#> *For more information, see ?'pandemicStats-xs'.
#> *For details on the calculations, see ?pandemic_stats.
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.
= plot(MGpredicted,term="both")
MGplots #> 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
$long MGplots
$short MGplots