nowcast | R Documentation |
Nowcasting can help to obtain up-to-date information on trends during a situation where reports about events arrive with delay. For example in public health reporting, reports about important indicators (such as occurrence of cases) are prone to be delayed due to for example manual quality checking and reporting system hierarchies. Altogether, the delays are subject to a delay distribution, which may, or may not, vary over time.
nowcast(now, when, data, dEventCol="dHospital", dReportCol="dReport",
method=c("bayes.notrunc", "bayes.notrunc.bnb", "lawless",
"bayes.trunc", "unif", "bayes.trunc.ddcp"),
aggregate.by="1 day",
D=15,
m=NULL, m.interpretation=c("hoehle_anderheiden2014", "lawless1994"),
control=list(
dRange=NULL, alpha=0.05, nSamples=1e3,
N.tInf.prior=c("poisgamma","pois","unif"),
N.tInf.max=300, gd.prior.kappa=0.1,
ddcp=list(ddChangepoint=NULL,
cp_order=c("zero","one"),
Wextra=NULL,
logLambda=c("iidLogGa","tps","rw1","rw2"),
responseDistr=c("poisson", "negbin"),
mcmc=c(burnin=2500, sample=10000, thin=1, adapt=1000,
store.samples=FALSE)),
score=FALSE, predPMF=FALSE))
now |
an object of class |
when |
a vector of |
data |
A data frame with one row per case – for each case on needs information on the day of the event (e.g. hospitalization) and the day of report of this event. |
dEventCol |
The name of the column in |
dReportCol |
Name of the column in |
method |
A vector of strings denoting the different methods for doing the nowcasting. Note that results of the first name in this list are officially returned by the function. However, it is possible to specify several methods here, e.g., in order to compare score evaluations. Details of the methods are described in Höhle and an der Heiden (2014).
|
aggregate.by |
Time scale used for the temporal aggregation of
the records in the data |
D |
Maximum possible or maximum relevant delay (unit:
|
m |
Size of the moving window for the estimation of the delay
distribution. Default: |
m.interpretation |
This parameter controls the interpretation of
the sliding window used to estimate the delay distribution. If
Alternatively, when |
control |
A list with named arguments controlling the functionality of the nowcasting.
|
The methodological details of the nowcasting procedures are described in Höhle M and an der Heiden M (2014).
nowcast
returns an object of "stsNC"
. The
upperbound
slot contains the median of the method specified at
the first position the argument method
. The slot pi
(for
prediction interval)
contains the equal tailed (1-\alpha
)*100% prediction
intervals, which are calculated based on the predictive distributions
in slot predPMF
.
Furthermore, slot truth
contains an sts
object
containing the true number of cases (if possible to compute it is based on
the data in data
). Finally, slot SR
contains the results
for the proper scoring rules (requires truth to be calculable).
Note: The bayes.trunc.ddcp
uses the JAGS software together with
the R package runjags to handle the parallelization of
the MCMC using the "rjparallel"
method of
run.jags
, which additionally requires the
rjags package. You need to manually install
JAGS on your computer for the package to work – see
https://mcmc-jags.sourceforge.io/
and the documentation of runjags for details.
Note: The function is still under development and might change in the future. Unfortunately, little emphasis has so far been put on making the function easy to understand and use.
Michael Höhle
Höhle, M. and an der Heiden, M. (2014): Bayesian nowcasting
during the STEC O104:H4 outbreak in Germany, 2011. Biometrics
70(4):993-1002. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1111/biom.12194")}.
A preprint is available as
https://staff.math.su.se/hoehle/pubs/hoehle_anderheiden2014-preprint.pdf.
Günther, F. and Bender, A. and Katz, K. and
Küchenhoff, H. and Höhle, M. (2020):
Nowcasting the COVID-19 pandemic in Bavaria.
Biometrical Journal. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1002/bimj.202000112")}
Preprint available at \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1101/2020.06.26.20140210")}.
data("husO104Hosp")
#Extract the reporting triangle at a specific day
t.repTriangle <- as.Date("2011-07-04")
#Use 'void' nowcasting procedure (we just want the reporting triangle)
nc <- nowcast(now=t.repTriangle,when=t.repTriangle,
dEventCol="dHosp",dReportCol="dReport",data=husO104Hosp,
D=15,method="unif")
#Show reporting triangle
reportingTriangle(nc)
#Perform Bayesian nowcasting assuming the delay distribution is stable over time
nc.control <- list(N.tInf.prior=structure("poisgamma",
mean.lambda=50,var.lambda=3000),
nSamples=1e2)
t.repTriangle <- as.Date("2011-06-10")
when <- seq(t.repTriangle-3,length.out=10,by="-1 day")
nc <- nowcast(now=t.repTriangle,when=when,
dEventCol="dHosp",dReportCol="dReport",data=husO104Hosp,
D=15,method="bayes.trunc",control=nc.control)
#Show time series and posterior median forecast/nowcast
plot(nc,xaxis.tickFreq=list("%d"=atChange,"%m"=atChange),
xaxis.labelFreq=list("%d"=at2ndChange),xaxis.labelFormat="%d-%b",
xlab="Time (days)",lty=c(1,1,1,1),lwd=c(1,1,2))
## Not run:
### Using runjags to do a Bayesian model with changepoint(s)
### -- this might take a while
nc.control.ddcp <- modifyList(nc.control,
list(gd.prior.kappa=0.1,
ddcp=list(ddChangepoint=as.Date(c("2011-05-23")),
logLambda="tps",
tau.gamma=1,
mcmc=c(burnin=1000,sample=1000,thin=1,
adapt=1000,store.samples=FALSE))))
nc.ddcp <- nowcast(now=t.repTriangle,when=when,
dEventCol="dHosp",dReportCol="dReport",
data=husO104Hosp, aggregate.by="1 day",
method="bayes.trunc.ddcp", D=15,
control=nc.control.ddcp)
plot(nc.ddcp,legend.opts=NULL,
xaxis.tickFreq=list("%d"=atChange,"%m"=atChange),
xaxis.labelFreq=list("%d"=at2ndChange),xaxis.labelFormat="%d-%b",
xlab="Time (days)",lty=c(1,1,1,1),lwd=c(1,1,2))
lambda <- attr(delayCDF(nc.ddcp)[["bayes.trunc.ddcp"]],"model")$lambda
showIdx <- seq(which( max(when) == epoch(nc.ddcp))) #seq(ncol(lambda))
matlines( showIdx,t(lambda)[showIdx,],col="gray",lwd=c(1,2,1),lty=c(2,1,2))
legend(x="topright",c(expression(lambda(t)),"95% CI"),col="gray",lwd=c(2,1),lty=c(1,2))
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.