| Title: | Dynamic Linear Model for Wastewater-Based Epidemiology |
|---|---|
| Description: | Implement dynamic linear models outlined in Shumway and Stoffer (2025) <doi:10.1007/978-3-031-70584-7>. Two model structures for data smoothing and forecasting are considered. The specific models proposed will be added once the manuscript is published. |
| Authors: | Difan Ouyang [aut, cre], Lappui Chung [aut], Charles Doss [aut] |
| Maintainer: | Difan Ouyang <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.0 |
| Built: | 2026-05-12 05:21:25 UTC |
| Source: | https://github.com/cran/dlmwwbe |
Fits a dynamic linear model (DLM) using maximum likelihood estimation.
dllm( data, obs_cols, S = c("univariate", "kvariate"), log10 = FALSE, date = NULL, prior = list(), equal.state.var = FALSE, equal.obs.var = FALSE, init_params = NULL, auto_init = TRUE, control = list(maxit = 500) )dllm( data, obs_cols, S = c("univariate", "kvariate"), log10 = FALSE, date = NULL, prior = list(), equal.state.var = FALSE, equal.obs.var = FALSE, init_params = NULL, auto_init = TRUE, control = list(maxit = 500) )
data |
A data frame containing observed time series data. |
obs_cols |
Character vector of column names in |
S |
Character; the structure of latent states. |
log10 |
logical; if TRUE, a log10 transformation is applied to the whole data. |
date |
Optional; the name of the column in |
prior |
A list of prior specifications. Default priors are used if not supplied. |
equal.state.var |
logical; if TRUE the variance is the same across all wastewater state. |
equal.obs.var |
logical; if TRUE the variance is the same across all wastewater observation. |
init_params |
Optional numeric vector of initial parameters. |
auto_init |
Logical; if |
control |
List of control parameters for the optimization routine ( |
The function prepares the data, validates inputs, and (if necessary) automatically initializes parameters.
It then defines a helper function to build the model via build_dlm and fits the model using
maximum likelihood estimation (dlmMLE). Filtering and smoothing are applied to obtain state estimates.
An object of class dllm containing the fitted model, filtered and smoothed estimates,
along with fit statistics (log-likelihood, AIC, BIC) and other diagnostic information.
The input data.
The input vector of date.
Character vector of column names in data to be used as observations.
Character; the structure of latent states.
A list of estimated parameters by maximum likelihood estimation.
The loglikelihood of the fitted model.
AIC of the fitted model.
BIC of the fitted model.
An integer code returned by optim
An dlm object of the fitted dynamic linear model.
The corresponding dynamic linear filter returned by dlmFilter
The corresponding dynamic linear smoother returned by dlmSmooth
A matrix of the filtered observed response variables.
A matrix of the smoothed observed response variables.
data<- wastewater[wastewater$Code == "TC",] data$SampleDate <- as.Date(data$SampleDate) fit <- dllm( equal.state.var=TRUE, equal.obs.var=FALSE, log10=TRUE, data = data, date = "SampleDate", obs_cols = c("ORFlab", "Nlab"), S = 'kvariate') summary(fit) plot(fit, type='smoother', plot_data = TRUE)data<- wastewater[wastewater$Code == "TC",] data$SampleDate <- as.Date(data$SampleDate) fit <- dllm( equal.state.var=TRUE, equal.obs.var=FALSE, log10=TRUE, data = data, date = "SampleDate", obs_cols = c("ORFlab", "Nlab"), S = 'kvariate') summary(fit) plot(fit, type='smoother', plot_data = TRUE)
Constructs a dynamic linear model (DLM) object using the dlm package.
pdlm( data, formula, lags = 0, log10 = TRUE, date = NULL, prior = list(), equal.state.var = TRUE, equal.obs.var = TRUE, init_params = list(), auto_init = TRUE, control = list(maxit = 500) )pdlm( data, formula, lags = 0, log10 = TRUE, date = NULL, prior = list(), equal.state.var = TRUE, equal.obs.var = TRUE, init_params = list(), auto_init = TRUE, control = list(maxit = 500) )
data |
A data frame containing the variables in the model. |
formula |
An object of class "formula" describing the model to be fitted. |
lags |
A nonnegative integer indicating the lag of the latent state in the model. |
log10 |
Logical; if TRUE, a log10 transformation is applied to the entire dataset. |
date |
An optional vector of date indices of the data. |
prior |
An optional list specifying the prior mean vector and covariance structure of the latent state. If not provided, a naive prior is used. |
equal.state.var |
Logical; if TRUE, the same variance is assumed across all state components. |
equal.obs.var |
Logical; if TRUE, the same variance is assumed across all observation components. |
init_params |
An optional list of initial parameters for the model. Should include Ft, Wt, and Vt: transition coefficients, state variance, and observation variance components respectively. |
auto_init |
Logical; if TRUE, naive initial parameters are used. |
control |
An optional list of control parameters for |
A dlm object with additional attributes:
The fitted formula.
Number of lags.
The input data.
The input vector of dates.
A list of estimated parameters.
Log-likelihood of the fitted model.
Akaike Information Criterion.
Bayesian Information Criterion.
The convergence code from optim.
The final dlm object.
Output from dlmFilter.
One-step-ahead predictions.
Variance of the predictions.
data <- wastewaterhealthworker[wastewaterhealthworker$Code == "TC",] data$SampleDate <- as.Date(data$SampleDate) fit <- pdlm( formula=HealthWorkerCaseCount~WW.tuesday+WW.thursday, data = data, lags = 2, equal.state.var=FALSE, equal.obs.var=FALSE, log10=TRUE, date = "SampleDate") summary(fit) plot(fit, conf.int = TRUE)data <- wastewaterhealthworker[wastewaterhealthworker$Code == "TC",] data$SampleDate <- as.Date(data$SampleDate) fit <- pdlm( formula=HealthWorkerCaseCount~WW.tuesday+WW.thursday, data = data, lags = 2, equal.state.var=FALSE, equal.obs.var=FALSE, log10=TRUE, date = "SampleDate") summary(fit) plot(fit, conf.int = TRUE)
Produces a plot for an object of class dllm (typically created by dllm).
The function displays the observed data along with the fitted curves computed using filtered and/or
smoothed state estimates.
## S3 method for class 'dllm' plot( x, type = c("smoother", "filter"), plot_data = TRUE, obs_cols = NULL, obs_colors = NULL, filter_colors = NULL, smoother_colors = NULL, conf.int = FALSE, sig.level = 0.95, ... )## S3 method for class 'dllm' plot( x, type = c("smoother", "filter"), plot_data = TRUE, obs_cols = NULL, obs_colors = NULL, filter_colors = NULL, smoother_colors = NULL, conf.int = FALSE, sig.level = 0.95, ... )
x |
An object of class |
type |
Character; one of |
plot_data |
Logical; if |
obs_cols |
Character; an optional argument specifying the variables to be plotted. If |
obs_colors |
Optional character vector specifying custom colors for observed data. If not supplied, a default palette is used. |
filter_colors |
Optional character vector specifying custom colors for filtered curves. If not supplied, a default palette is used. |
smoother_colors |
Optional character vector specifying custom colors for smoothed curves. If not supplied, a default palette is used. |
conf.int |
Logical; if |
sig.level |
Numeric; significance level for confidence intervals (default: 0.95). |
... |
Additional graphical parameters to pass to the underlying plotting functions. |
This function produces a plot of the fitted DLM and returns NULL invisibly.
Produces a plot for an object of class pdlm (typically created by pdlm).
The function displays the observed data along with the fitted curves computed using filtered and/or
smoothed state estimates.
## S3 method for class 'pdlm' plot(x, plot_data = TRUE, conf.int = FALSE, sig.level = 0.95, ...)## S3 method for class 'pdlm' plot(x, plot_data = TRUE, conf.int = FALSE, sig.level = 0.95, ...)
x |
An object of class |
plot_data |
Logical; if |
conf.int |
Logical; if |
sig.level |
Numeric; significance level for confidence intervals (default: 0.95). |
... |
Additional graphical parameters to pass to the underlying plotting functions. |
This function produces a plot of the fitted DLM and returns NULL invisibly.
Provides a brief summary of the fitted dynamic local level model, including parameter estimates and log-likelihood.
## S3 method for class 'dllm' summary(object, ...)## S3 method for class 'dllm' summary(object, ...)
object |
An object of class |
... |
Additional arguments (not used). |
The object is returned invisibly.
Provides a brief summary of the fitted predictive dynamic linear model, including parameter estimates and log-likelihood.
## S3 method for class 'pdlm' summary(object, ...)## S3 method for class 'pdlm' summary(object, ...)
object |
An object of class |
... |
Additional arguments (not used). |
The object is returned invisibly.
A dataset containing the long format of daily wastewater data collected in Minnesota from March 2022 to February 2023. The wastewater was collected twice a week with possible missing values.
wastewaterwastewater
A data frame with 1348 rows and 4 variables:
Character. The code name of the treatment plant where the wastewater was sampled.
Date. The sample collection date.
The ORF target.
The N target.
A dataset containing the wide format of weekly wastewater and clinical case data collected in Minnesota from March 2022 to Feburary 2023. The wastewater was collected twice a week with possible missing values.
wastewaterhealthworkerwastewaterhealthworker
A data frame with 196 rows and 5 variables:
Character. The code name of the treatment plant where the wastewater was sampled.
Date. The sample collection date.
Integer. Reported weekly Covid-19 positive case counts.
Flow adjusted wastewater measurement from Tuesday samples.
Flow adjusted wastewater measurement from Thursday samples.