| Title: | Dynamic Bayesian Learning for Spatiotemporal Mechanistic Models |
|---|---|
| Description: | Provides tools for Bayesian learning of spatiotemporal dynamical mechanistic models. Includes methods for parameter estimation, simulation, and inference using hierarchical and state-space modeling approaches, following Banerjee, Chen, Frankenburg and Zhou (2025) <https://jmlr.org/papers/v26/22-0896.html>. |
| Authors: | Xiang Chen [aut, cre], Sudipto Banerjee [aut] |
| Maintainer: | Xiang Chen <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.2 |
| Built: | 2026-06-09 11:12:19 UTC |
| Source: | https://github.com/cran/spDBL |
Performs the backward sampling pass of the FFBS algorithm using the filtered
distributions produced by FF. Iterates from time back
to , drawing smoothed state samples at each step via
BS_1step_cpp.
BS(res_ff, G_ls, nT, delta = 1, verbose = FALSE)BS(res_ff, G_ls, nT, delta = 1, verbose = FALSE)
res_ff |
List of length |
G_ls |
Either a single numeric matrix (state transition matrix |
nT |
Integer. Number of time steps. |
delta |
Numeric scalar. Right-variance discount factor. Defaults to
|
verbose |
Logical. If |
A named list with:
Array of dimension c(p, q, nT) containing the smoothed
state means.
Array of dimension c(p, p, nT) containing the smoothed
state left-covariance matrices.
Computes the posterior mean vector and covariance matrix
of the discrepancy given the current predictive mean
, observations , and variance .
cal_Bt_bt(mut, invSigma, tau2t, zt_ut, I_Stilde)cal_Bt_bt(mut, invSigma, tau2t, zt_ut, I_Stilde)
mut |
Numeric vector. Predictive mean at calibration locations. |
invSigma |
Numeric matrix. Inverse of the predictive spatial covariance. |
tau2t |
Numeric scalar. Current variance |
zt_ut |
Numeric vector. Observation residuals |
I_Stilde |
Numeric matrix. Identity-like matrix for the discrepancy
prior precision ( |
A named list with:
Numeric matrix. Posterior covariance of the discrepancy.
Numeric vector. Posterior mean of the discrepancy.
For each row of the matrix X, computes the median and the 2.5th and
97.5th percentiles across columns (samples).
cal_errorbar(X)cal_errorbar(X)
X |
Numeric matrix. Rows correspond to spatial locations or parameters; columns correspond to posterior samples. |
A data frame with columns:
Row-wise median.
Row-wise 2.5th percentile.
Row-wise 97.5th percentile.
For each row of the matrix X, computes the mean and the 2.5th and
97.5th percentiles across columns (samples).
cal_errorbar_mean(X)cal_errorbar_mean(X)
X |
Numeric matrix. Rows correspond to spatial locations or parameters; columns correspond to posterior samples. |
A data frame with columns:
Row-wise mean.
Row-wise 2.5th percentile.
Row-wise 97.5th percentile.
Computes the log (or raw) absolute Jacobian of the transformation from the
constrained parameter eta (bounded in
[eta_limit_low, eta_limit_high]) to the unconstrained logit scale.
Zero entries of eta are excluded from the product.
cal_jacobian_logit_uniform(eta, eta_limit_low, eta_limit_high, log = FALSE)cal_jacobian_logit_uniform(eta, eta_limit_low, eta_limit_high, log = FALSE)
eta |
Numeric vector. Constrained parameter values. |
eta_limit_low |
Numeric vector. Lower bounds for each element of
|
eta_limit_high |
Numeric vector. Upper bounds for each element of
|
log |
Logical. If |
Numeric scalar (log absolute Jacobian or absolute Jacobian).
Verifies that a matrix is approximately symmetric (within a relative and absolute tolerance). If so, symmetrises it and, if still not positive definite, adds a small diagonal ridge. Raises an error if the asymmetry exceeds the tolerance.
check_pds(C, eps = 10^(-5), per = 0.05)check_pds(C, eps = 10^(-5), per = 0.05)
C |
Numeric matrix to check. |
eps |
Numeric scalar. Absolute tolerance for the Frobenius norm of
|
per |
Numeric scalar. Relative tolerance: the ratio of the asymmetry
norm to the mean of |
The (possibly corrected) positive definite symmetric matrix.
Evaluates the log (or raw) density of the matrix-T distribution that arises
when the right covariance is and .
dMTig(Y, m, M, nu, d, R, log = TRUE)dMTig(Y, m, M, nu, d, R, log = TRUE)
Y |
Numeric matrix. Observation matrix ( |
m |
Numeric matrix. Mean matrix ( |
M |
Numeric matrix. Left covariance matrix ( |
nu |
Numeric scalar. Degrees of freedom (shape parameter of the
inverse-gamma on |
d |
Numeric scalar. Rate parameter of the inverse-gamma on
|
R |
Numeric matrix. Right correlation matrix ( |
log |
Logical. If |
Numeric scalar. Log density (or density if log = FALSE).
A list of example training and testing objects used to illustrate the emulation workflow in the package.
dt_emulationdt_emulation
A list with 4 elements:
Training set of PDE parameter values.
Test set of PDE parameter values.
Training set of PDE-based outputs corresponding to
pde_para_train.
Test set of PDE-based outputs corresponding to
pde_para_test.
Simulated data / package example data
Fits a dynamic linear emulator to training PDE output using Forward Filtering
Backward Sampling (FFBS). The wrapper constructs the input-parameter Gaussian
process covariance, partitions spatial output into episode blocks, builds
autoregressive design matrices, runs FFBS, and draws posterior
state samples with FFBS_sampling.
emulator_learn( pde_para_train, dt_pde_train, Nx, Ny, F_ls_train = "default", F_ls_test = "default", N_people = 10000, nsam = 10, AR_choice = 2, episode_window = 1, gp_tune = 0.5, gp_sigma2 = 1.1, gp_tau2 = 10^(-4) )emulator_learn( pde_para_train, dt_pde_train, Nx, Ny, F_ls_train = "default", F_ls_test = "default", N_people = 10000, nsam = 10, AR_choice = 2, episode_window = 1, gp_tune = 0.5, gp_sigma2 = 1.1, gp_tau2 = 10^(-4) )
pde_para_train |
Numeric matrix. Training input parameters, with rows corresponding to simulator runs and columns to parameters. |
dt_pde_train |
List of length |
Nx |
Integer. Number of spatial grid cells in the x-direction. |
Ny |
Integer. Number of spatial grid cells in the y-direction. |
F_ls_train |
Either |
F_ls_test |
Deprecated/unused argument retained for compatibility. |
N_people |
Numeric scalar. Population size associated with the PDE
simulator. Stored in the returned setup list. Defaults to |
nsam |
Integer. Number of posterior state samples drawn by
|
AR_choice |
Integer. Autoregressive design order. Use |
episode_window |
Integer. Number of y-direction columns per episode
block. Defaults to |
gp_tune |
Numeric scalar. Tuning factor used to set the exponential
kernel range from the maximum pairwise input distance. Defaults to
|
gp_sigma2 |
Numeric scalar. Marginal variance parameter for the input
Gaussian process kernel. Defaults to |
gp_tau2 |
Numeric scalar. Nugget variance parameter for the input
Gaussian process kernel. Defaults to |
A named list with:
Output of FFBS.
Posterior state samples returned by
FFBS_sampling.
A list of training data, blocking information, kernel
parameters, generated design matrices, and other quantities needed by
emulator_predict.
emulator_predict, FFBS,
FFBS_sampling, gen_F_ls_AR1_EP,
gen_F_ls_AR2_EP
Uses a fitted emulator returned by emulator_learn to predict
PDE output at new input parameter values. Prediction can use either the exact
posterior predictive mean from FFBS_predict_exact or Monte
Carlo posterior predictive draws from FFBS_predict_MC.
emulator_predict( emulator, input_new, dt_pde_test, F_new_ls = "default", MC = FALSE )emulator_predict( emulator, input_new, dt_pde_test, F_new_ls = "default", MC = FALSE )
emulator |
Named list. Fitted emulator object returned by
|
input_new |
Numeric matrix. New input parameter values at which to predict, with rows corresponding to new simulator settings. |
dt_pde_test |
List of length |
F_new_ls |
Either |
MC |
Logical. If |
If F_new_ls = "default", the function constructs the required
autoregressive design matrices for the new inputs from dt_pde_test
using the autoregressive order and episode blocking structure stored in
emulator.
If MC = FALSE, a named list of length nT_ori, where
each element is a matrix of exact posterior predictive means for one
original time step. If MC = TRUE, a named list of length
nT_ori, where each element is an array of Monte Carlo predictive
draws for one original time step.
emulator_learn, FFBS_predict_exact,
FFBS_predict_MC, recover_from_EP_exact,
recover_from_EP_MC
Computes the logistic sigmoid .
expit(x)expit(x)
x |
Numeric vector or matrix. |
Numeric vector or matrix with values in .
Runs the full forward filtering pass over nT time steps under the
Matrix Normal Inverse Wishart (MNIW) model. At each step, calls
FF_1step_cpp to update the state mean and covariance as well as the
inverse-Wishart parameters for the right-covariance matrix .
FF(Y, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, delta = 1, verbose = FALSE)FF(Y, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, delta = 1, verbose = FALSE)
Y |
List of length |
F_ls |
Either a single |
G_ls |
Either a single |
W_ls |
Either a single |
V_ls |
Either a single |
m0 |
Numeric matrix. Prior mean of the state |
M0 |
Numeric matrix. Prior left-covariance of |
n0 |
Numeric scalar. Prior degrees of freedom of the inverse-Wishart on
|
D0 |
Numeric matrix. Prior scale matrix of the inverse-Wishart on
|
nT |
Integer. Number of time steps. |
delta |
Numeric scalar. Discount factor for the right-variance matrix.
Defaults to |
verbose |
Logical. If |
A named list of length nT + 1. Elements "T1" through
"T<nT>" each contain a list with filtered parameters
nt, Dt, at, At, mt, Mt.
The additional element prior stores m0, M0,
n0, D0.
Performs one step of the forward filter under the model where the right
covariance of the state and observation is the identity matrix (no
inverse-Wishart update for ).
FF_1step_R_I(Yt, Ft, Gt, Wt, Vt, mt_1, Mt_1, delta = 1)FF_1step_R_I(Yt, Ft, Gt, Wt, Vt, mt_1, Mt_1, delta = 1)
Yt |
Numeric matrix. Data matrix at time |
Ft |
Numeric matrix. Covariate matrix at time |
Gt |
Numeric matrix. State transition matrix ( |
Wt |
Numeric matrix. State noise left-covariance ( |
Vt |
Numeric matrix. Observation noise left-covariance ( |
mt_1 |
Numeric matrix. Filtered state mean at |
Mt_1 |
Numeric matrix. Filtered state left-covariance at |
delta |
Numeric scalar. Discount factor. Defaults to |
A named list with:
One-step-ahead state mean.
One-step-ahead state left-covariance.
Filtered state mean.
Filtered state left-covariance.
The discount factor passed in.
Performs one step of the forward filter under the model where the right
covariance of is with .
The inverse of (Rinv) must be precomputed and passed in.
FF_1step_R_sigma2R(Yt, Ft, Gt, Wt, Vt, mt_1, Mt_1, nt_1, Dt_1, Rinv, delta = 1)FF_1step_R_sigma2R(Yt, Ft, Gt, Wt, Vt, mt_1, Mt_1, nt_1, Dt_1, Rinv, delta = 1)
Yt |
Numeric matrix. Data matrix at time |
Ft |
Numeric matrix. Covariate matrix at time |
Gt |
Numeric matrix. State transition matrix at time |
Wt |
Numeric matrix. State noise left-covariance at time |
Vt |
Numeric matrix. Observation noise left-covariance at time |
mt_1 |
Numeric matrix. Filtered state mean at |
Mt_1 |
Numeric matrix. Filtered state left-covariance at |
nt_1 |
Numeric scalar. Shape parameter of |
Dt_1 |
Numeric scalar. Rate parameter of |
Rinv |
Numeric matrix. Precomputed inverse of the spatial correlation
matrix |
delta |
Numeric scalar. Right-variance discount factor. Defaults to
|
A named list with updated filtering parameters:
Updated shape parameter.
Updated rate parameter.
One-step-ahead state mean ().
One-step-ahead state left-covariance ().
Filtered state mean ().
Filtered state left-covariance ().
The discount factor passed in.
Runs the forward filtering pass for large datasets that are partitioned into
spatial blocks and stored as CSV files on disk. At each time step the spatial
domain is traversed block-by-block using a snake traversal order (see
generate.grid.rowsnake), and results are written to disk to
avoid exhausting memory.
FF_bigdata_R( Y_ls, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, fnrow, fncol, bnrow, bncol, path_out, delta = 1, verbose = FALSE )FF_bigdata_R( Y_ls, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, fnrow, fncol, bnrow, bncol, path_out, delta = 1, verbose = FALSE )
Y_ls |
Character vector of length |
F_ls |
Either a single file path (constant |
G_ls |
Either a single file path (constant |
W_ls |
Either a single file path (constant |
V_ls |
Either a single file path (constant |
m0 |
Numeric matrix. Prior state mean ( |
M0 |
Numeric matrix. Prior left-covariance of the state ( |
n0 |
Numeric scalar. Prior degrees of freedom of the inverse-Wishart. |
D0 |
Numeric matrix. Prior scale matrix of the inverse-Wishart. |
nT |
Integer. Number of time steps (files). |
fnrow |
Integer. Total number of rows in each data file. |
fncol |
Integer. Total number of columns in each data file. |
bnrow |
Integer. Number of rows per spatial block. |
bncol |
Integer. Number of columns per spatial block. |
path_out |
Character. Directory path where output CSV files are written. |
delta |
Numeric scalar. Right-variance discount factor. Defaults to
|
verbose |
Logical. If |
A named list of character vectors with paths to the output CSV files:
Paths to filtered degrees-of-freedom files.
Paths to filtered scale-matrix files.
Paths to filtered state-mean files.
Paths to filtered left-covariance files.
Paths to predicted state-mean files.
Paths to predicted left-covariance files.
Path to the covariate matrix at the final time step.
Path to the noise covariance matrix at the final time step.
Runs the full forward filtering pass over nT time steps under the
model where the right covariance is fixed at the identity (no
inverse-Wishart update). Calls FF_1step_R_I at each step.
FF_I(Y, F_ls, G_ls, W_ls, V_ls, m0, M0, nT, delta = 1, verbose = FALSE)FF_I(Y, F_ls, G_ls, W_ls, V_ls, m0, M0, nT, delta = 1, verbose = FALSE)
Y |
List of length |
F_ls |
Covariate matrix or list of matrices (see |
G_ls |
State transition matrix or list (see |
W_ls |
State noise left-covariance matrix or list (see |
V_ls |
Observation noise left-covariance matrix or list
(see |
m0 |
Numeric matrix. Prior state mean. |
M0 |
Numeric matrix. Prior state left-covariance. |
nT |
Integer. Number of time steps. |
delta |
Numeric scalar. Discount factor. Defaults to |
verbose |
Logical. If |
A named list of length nT + 1. Elements "T1" through
"T<nT>" contain at, At, mt, Mt. The
element prior stores m0 and M0.
Runs the full forward filtering pass over nT time steps under the
model where the right covariance of is with
.
FF_sigma2R( Y, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, R, delta = 1, verbose = FALSE )FF_sigma2R( Y, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, R, delta = 1, verbose = FALSE )
Y |
List of length |
F_ls |
Covariate matrix or list of matrices (see |
G_ls |
State transition matrix or list (see |
W_ls |
State noise left-covariance matrix or list (see |
V_ls |
Observation noise left-covariance matrix or list
(see |
m0 |
Numeric matrix. Prior state mean ( |
M0 |
Numeric matrix. Prior state left-covariance ( |
n0 |
Numeric scalar. Prior shape of |
D0 |
Numeric scalar. Prior rate of |
nT |
Integer. Number of time steps. |
R |
Numeric matrix. Fixed spatial correlation matrix ( |
delta |
Numeric scalar. Right-variance discount factor. Defaults to
|
verbose |
Logical. If |
A named list of length nT + 1 (same structure as
FF) with an additional scalar Dt in place of a matrix.
FF_1step_R_sigma2R, FFBS_sigma2R
Runs the complete FFBS algorithm under the Matrix Normal Inverse Wishart
(MNIW) model: first applies the forward filter (FF) and then
the backward sampler (BS).
FFBS(Y, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, delta = 1, verbose = FALSE)FFBS(Y, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, delta = 1, verbose = FALSE)
Y |
List of length |
F_ls |
Covariate matrix or list of matrices (see |
G_ls |
State transition matrix or list (see |
W_ls |
State noise left-covariance matrix or list (see |
V_ls |
Observation noise left-covariance matrix or list
(see |
m0 |
Numeric matrix. Prior state mean ( |
M0 |
Numeric matrix. Prior state left-covariance ( |
n0 |
Numeric scalar. Prior degrees of freedom of the inverse-Wishart. |
D0 |
Numeric matrix. Prior scale matrix of the inverse-Wishart. |
nT |
Integer. Number of time steps. |
delta |
Numeric scalar. Right-variance discount factor. Defaults to
|
verbose |
Logical. If |
A named list with:
Output of FF: filtered distributions for each
time step.
Output of BS: smoothed state means (st)
and left-covariances (St).
Runs the complete FFBS algorithm with the right covariance fixed at the
identity matrix (no inverse-Wishart update for ).
FFBS_I(Y, F_ls, G_ls, W_ls, V_ls, m0, M0, nT, delta = 1, verbose = FALSE)FFBS_I(Y, F_ls, G_ls, W_ls, V_ls, m0, M0, nT, delta = 1, verbose = FALSE)
Y |
List of length |
F_ls |
Covariate matrix or list (see |
G_ls |
State transition matrix or list (see |
W_ls |
State noise left-covariance matrix or list (see |
V_ls |
Observation noise left-covariance matrix or list
(see |
m0 |
Numeric matrix. Prior state mean. |
M0 |
Numeric matrix. Prior state left-covariance. |
nT |
Integer. Number of time steps. |
delta |
Numeric scalar. Discount factor. Defaults to |
verbose |
Logical. If |
A named list with ff (output of FF_I) and
bs (output of BS).
Computes the analytical posterior predictive mean at new spatial locations
using the smoothed state means from FFBS and an
exponential GP kernel for the cross-covariance.
FFBS_predict_exact( Y, para_ffbs, F_ls, F_new_ls, input, input_new, nT, phi_para, gp_sigma2 = 1.1, gp_tau2 = 10^(-4), delta = 1 )FFBS_predict_exact( Y, para_ffbs, F_ls, F_new_ls, input, input_new, nT, phi_para, gp_sigma2 = 1.1, gp_tau2 = 10^(-4), delta = 1 )
Y |
List of length |
para_ffbs |
List. Output of |
F_ls |
Covariate matrix or list for observed locations. |
F_new_ls |
Covariate matrix or list for new locations. |
input |
Numeric matrix. Coordinates of observed locations. |
input_new |
Numeric matrix or vector. Coordinates of new locations. |
nT |
Integer. Number of time steps. |
phi_para |
Numeric scalar. Range parameter of the exponential kernel. |
gp_sigma2 |
Numeric scalar. Variance parameter of the GP kernel.
Defaults to |
gp_tau2 |
Numeric scalar. Nugget variance of the GP kernel. Defaults to
|
delta |
Numeric scalar. Discount factor. Defaults to |
A named list of length nT. Each element "T<t>" is a
matrix of dimension c(N_new, q) with the posterior predictive mean
at the new locations.
FFBS_predict_MC, gen_exp_kernel
Estimates posterior predictive means at new spatial locations using Monte
Carlo integration over the posterior samples of the state .
Uses an exponential GP kernel to compute the cross-covariance between
observed and new locations.
FFBS_predict_MC( nsam, Y, res_ffbs, F_ls, F_new_ls, input, input_new, nT, phi_para, gp_sigma2 = 1.1, gp_tau2 = 10^(-4), delta = 1 )FFBS_predict_MC( nsam, Y, res_ffbs, F_ls, F_new_ls, input, input_new, nT, phi_para, gp_sigma2 = 1.1, gp_tau2 = 10^(-4), delta = 1 )
nsam |
Integer. Number of posterior samples to average over. |
Y |
List of length |
res_ffbs |
List. Posterior samples of |
F_ls |
Covariate matrix or list for observed locations. |
F_new_ls |
Covariate matrix or list for new (prediction) locations. |
input |
Numeric matrix. Coordinates of observed locations
( |
input_new |
Numeric matrix or vector. Coordinates of new locations. |
nT |
Integer. Number of time steps. |
phi_para |
Numeric scalar. Range parameter of the exponential kernel. |
gp_sigma2 |
Numeric scalar. Variance parameter of the GP kernel.
Defaults to |
gp_tau2 |
Numeric scalar. Nugget variance of the GP kernel. Defaults to
|
delta |
Numeric scalar. Discount factor. Defaults to |
A named list of length nT. Each element "T<t>" is an
array of dimension c(N_new, q, nsam) with posterior predictive mean
samples at the new locations.
FFBS_predict_exact, gen_exp_kernel
Given the filtered and smoothed distributions from FFBS,
draws nsam joint posterior samples of the state matrices
and the covariance using the
MNIW sampler.
FFBS_sampling(nsam, para_ffbs, F_ls, G_ls, nT, delta = 1, verbose = FALSE)FFBS_sampling(nsam, para_ffbs, F_ls, G_ls, nT, delta = 1, verbose = FALSE)
nsam |
Integer. Number of posterior samples to draw. |
para_ffbs |
List. Output of |
F_ls |
Covariate matrix or list of matrices (see |
G_ls |
State transition matrix or list (see |
nT |
Integer. Number of time steps. |
delta |
Numeric scalar. Discount factor. Defaults to |
verbose |
Logical. If |
A named list of length nT + 1. Elements "T1" through
"T<nT>" are arrays of dimension c(p, q, nsam) containing
posterior samples of . The element Sigma is an array
of dimension c(q, q, nsam) containing posterior samples of
.
Draws nsam posterior samples of the state from the
smoothed distributions produced by FFBS_I, assuming an
identity right-covariance matrix.
FFBS_sampling_I(nsam, para_ffbs, F_ls, G_ls, nT, delta = 1, verbose = FALSE)FFBS_sampling_I(nsam, para_ffbs, F_ls, G_ls, nT, delta = 1, verbose = FALSE)
nsam |
Integer. Number of posterior samples. |
para_ffbs |
List. Output of |
F_ls |
Covariate matrix or list (see |
G_ls |
State transition matrix or list (see |
nT |
Integer. Number of time steps. |
delta |
Numeric scalar. Discount factor. Defaults to |
verbose |
Logical. If |
A named list of length nT. Each element "T<t>" is an
array of dimension c(p, q, nsam) with posterior samples of the
state at time .
Draws nsam posterior samples of the state and the
scalar variance using the MNIG sampler, given the FFBS output
under the scalar-sigma model.
FFBS_sampling_sigma2R( nsam, para_ffbs, F_ls, G_ls, nT, R, delta = 1, verbose = FALSE )FFBS_sampling_sigma2R( nsam, para_ffbs, F_ls, G_ls, nT, R, delta = 1, verbose = FALSE )
nsam |
Integer. Number of posterior samples. |
para_ffbs |
List. Output of |
F_ls |
Covariate matrix or list (see |
G_ls |
State transition matrix or list (see |
nT |
Integer. Number of time steps. |
R |
Numeric matrix. Fixed spatial correlation matrix. |
delta |
Numeric scalar. Discount factor. Defaults to |
verbose |
Logical. If |
A named list of length nT + 1. Elements "T1" through
"T<nT>" are arrays of dimension c(p, q, nsam) with state
samples. The element Sigma is a vector of length nsam with
samples of .
Runs the complete FFBS algorithm under the model where the right covariance
is with .
FFBS_sigma2R( Y, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, R, delta = 1, verbose = FALSE )FFBS_sigma2R( Y, F_ls, G_ls, W_ls, V_ls, m0, M0, n0, D0, nT, R, delta = 1, verbose = FALSE )
Y |
List of length |
F_ls |
Covariate matrix or list (see |
G_ls |
State transition matrix or list (see |
W_ls |
State noise left-covariance matrix or list (see |
V_ls |
Observation noise left-covariance matrix or list
(see |
m0 |
Numeric matrix. Prior state mean. |
M0 |
Numeric matrix. Prior state left-covariance. |
n0 |
Numeric scalar. Prior shape of |
D0 |
Numeric scalar. Prior rate of |
nT |
Integer. Number of time steps. |
R |
Numeric matrix. Fixed spatial correlation matrix. |
delta |
Numeric scalar. Discount factor. Defaults to |
verbose |
Logical. If |
A named list with ff (output of FF_sigma2R) and
bs (output of BS).
FF_sigma2R, BS,
FFBS_sampling_sigma2R
Simulates observed calibration data by adding a spatially
correlated random discrepancy and an independent noise term to
the computer model output . The variance decays
over time via the discount factor .
gen_calibrate_data(ycal_mat, para_gen_cal, U_gen)gen_calibrate_data(ycal_mat, para_gen_cal, U_gen)
ycal_mat |
Numeric matrix of dimension |
para_gen_cal |
Named list with elements:
|
U_gen |
Numeric matrix. Spatial covariance matrix for the discrepancy
( |
A named list with:
Matrix of dimension c(t_cal, s_cal) of discrepancy
realisations.
Matrix of dimension c(t_cal, s_cal) of simulated
observations.
Numeric vector of length t_cal with sampled
variances.
Like gen_calibrate_data but uses independent (uncorrelated)
discrepancy increments drawn from a univariate normal with variance
.
gen_calibrate_data_uncorr(ycal_mat, para_gen_cal, U_gen)gen_calibrate_data_uncorr(ycal_mat, para_gen_cal, U_gen)
ycal_mat |
Numeric matrix. Computer model output ( |
para_gen_cal |
Named list (same structure as in
|
U_gen |
Numeric matrix. Only the |
A named list with:
Matrix of dimension c(t_cal, s_cal) of simulated
observations.
Numeric vector of length t_cal.
Evaluates the exponential kernel
for all pairs of rows in loc.
gen_exp_kernel(loc, phi, sigma2 = 1, tau2 = 0)gen_exp_kernel(loc, phi, sigma2 = 1, tau2 = 0)
loc |
Numeric matrix. Spatial locations ( |
phi |
Numeric scalar. Range (decay) parameter. |
sigma2 |
Numeric scalar. Marginal variance. Defaults to |
tau2 |
Numeric scalar. Nugget variance. Defaults to |
A symmetric positive definite matrix of dimension c(n, n).
gen_gp_kernel, gen_expsq_kernel
Evaluates the squared-exponential kernel
for all pairs of rows in loc.
gen_expsq_kernel(loc, phi, sigma2 = 1, tau2 = 0)gen_expsq_kernel(loc, phi, sigma2 = 1, tau2 = 0)
loc |
Numeric matrix. Spatial locations ( |
phi |
Numeric scalar. Inverse length-scale parameter. |
sigma2 |
Numeric scalar. Marginal variance. Defaults to |
tau2 |
Numeric scalar. Nugget variance. Defaults to |
A symmetric positive definite matrix of dimension c(n, n).
Constructs the time-varying covariate list for a first-order
autoregressive model: (with ).
gen_F_ls_AR1(Y, nT)gen_F_ls_AR1(Y, nT)
Y |
List of length |
nT |
Integer. Number of time steps. |
A list of length nT where element is the covariate
matrix .
Generates a flattened covariate list for the episode-partition (EP) model
with AR(1) lags. The output list is indexed by ,
where is the block index and is the time index.
gen_F_ls_AR1_EP(Y, nT, n_b, ind)gen_F_ls_AR1_EP(Y, nT, n_b, ind)
Y |
List of length |
nT |
Integer. Number of time steps. |
n_b |
Integer. Number of spatial blocks. |
ind |
Data frame of block indices as returned by
|
A list of length nT * n_b with block-wise AR(1) covariate
matrices.
Constructs the time-varying covariate list for a second-order
autoregressive model by column-binding the two most recent lags:
(with special handling for ).
gen_F_ls_AR2(Y, nT)gen_F_ls_AR2(Y, nT)
Y |
List of length |
nT |
Integer. Number of time steps. |
A list of length nT where element is the
covariate matrix .
Generates a flattened covariate list for the episode-partition (EP) model with AR(2) lags. Both lag-1 and lag-2 column blocks are extracted and column-bound for each spatial block.
gen_F_ls_AR2_EP(Y, nT, n_b, ind)gen_F_ls_AR2_EP(Y, nT, n_b, ind)
Y |
List of length |
nT |
Integer. Number of time steps. |
n_b |
Integer. Number of spatial blocks. |
ind |
Data frame of block indices as returned by
|
A list of length nT * n_b with block-wise AR(2) covariate
matrices (each of width ).
Simulates data from the MNIW dynamic linear model for nT time steps
and writes the observations and all model parameters
to CSV files in path.
gen_ffbs_csv(N, S, p, nT, path)gen_ffbs_csv(N, S, p, nT, path)
N |
Integer. Number of spatial locations (rows of |
S |
Integer. Number of response variables (columns of |
p |
Integer. Dimension of the state vector (must be |
nT |
Integer. Number of time steps. |
path |
Character. Directory path (with trailing |
Invisibly returns NULL. Files written: m0.csv,
MM0.csv, G0.csv, F0.csv, Sigma.csv,
RSigma.csv, V0.csv, RV0.csv, W0.csv, and
Y1.csv through Y<nT>.csv.
Simulates data from the MNIW dynamic linear model for nT time steps
and returns the generated observations and model parameters as in-memory
objects.
gen_ffbs_data(N, S, p, nT)gen_ffbs_data(N, S, p, nT)
N |
Integer. Number of spatial locations (rows of |
S |
Integer. Number of response variables (columns of |
p |
Integer. Dimension of the state vector (must be |
nT |
Integer. Number of time steps. |
A named list with:
Named list of length nT ("Y1", ...,
"Y<nT>"), each element an data matrix.
Named list of model parameters: loc, dist,
m0, M0, G0, F0, Sigma,
RSigma, V0, RV0, W0, n0,
D0.
Evaluates the isotropic GP exponential kernel
for all pairs of rows in loc.
gen_gp_kernel(loc, phi, sigma2, tau2)gen_gp_kernel(loc, phi, sigma2, tau2)
loc |
Numeric matrix. Spatial locations ( |
phi |
Numeric scalar. Range (decay) parameter of the exponential kernel. |
sigma2 |
Numeric scalar. Marginal variance parameter. |
tau2 |
Numeric scalar. Nugget (white-noise) variance. |
A symmetric positive definite matrix of dimension c(n, n).
gen_exp_kernel, gen_expsq_kernel
Builds the full covariance matrix over observed and new locations using an
exponential GP kernel, then extracts the
cross-covariance block .
gen_Jt(input, input_new, phi_para, gp_sigma2 = 1.1, gp_tau2 = 10^(-4))gen_Jt(input, input_new, phi_para, gp_sigma2 = 1.1, gp_tau2 = 10^(-4))
input |
Numeric matrix. Coordinates of observed locations
( |
input_new |
Numeric matrix or vector. Coordinates of new locations
( |
phi_para |
Numeric scalar. Range parameter of the exponential kernel. |
gp_sigma2 |
Numeric scalar. Marginal variance of the kernel. Defaults
to |
gp_tau2 |
Numeric scalar. Nugget variance. Defaults to |
Numeric matrix of dimension c(N, N_new).
Constructs a positive definite matrix of dimension dim by forming
from a random normal matrix and then rescaling so the
maximum entry is 1.
gen_pd_matrix(dim)gen_pd_matrix(dim)
dim |
Integer. Number of rows (and columns) of the output matrix. |
A symmetric positive definite numeric matrix of dimension
c(dim, dim).
Numerically integrates the spatial SIR ODE (SIR) on a
two-dimensional grid using deSolve::ode.2D and returns the log(1 + I)
values of the infected compartment.
gen_pde(eta, Nx, Ny, N, nT_ori)gen_pde(eta, Nx, Ny, N, nT_ori)
eta |
Numeric vector of length 5. PDE parameters: transmission rate, recovery rate, and diffusion coefficients for S, I, R. |
Nx |
Integer. Number of grid cells in the x-direction (rows). |
Ny |
Integer. Number of grid cells in the y-direction (columns). |
N |
Numeric. Total population size. |
nT_ori |
Integer. Number of time steps to simulate (times
|
Numeric matrix of dimension c(nT_ori, Nx * Ny) containing
for each time step and grid cell.
Simulates a prior trajectory of the spatial
discrepancy field and the associated inverse-gamma variance sequence
from the dynamic calibration model.
gen_prior_u_tau2(n0, d0, b, m0, M0, U, nT_cal)gen_prior_u_tau2(n0, d0, b, m0, M0, U, nT_cal)
n0 |
Numeric scalar. Prior shape of |
d0 |
Numeric scalar. Prior scale of |
b |
Numeric scalar. Discount factor in |
m0 |
Numeric vector. Prior mean of the initial discrepancy |
M0 |
Numeric matrix. Prior covariance of |
U |
Numeric matrix. Spatial covariance matrix for the discrepancy increments. |
nT_cal |
Integer. Number of calibration time steps. |
A named list with:
Numeric vector. Sampled initial discrepancy.
Matrix of dimension c(nT_cal, s_cal) of discrepancy
realisations.
Numeric scalar. Sampled .
Numeric vector of length nT_cal.
Draws a matrix of independent standard normal entries and divides by the
absolute maximum, so all entries lie in .
gen_ran_matrix(nrow, ncol)gen_ran_matrix(nrow, ncol)
nrow |
Integer. Number of rows. |
ncol |
Integer. Number of columns. |
A numeric matrix of dimension c(nrow, ncol).
Partitions a matrix or file into rectangular blocks and returns a data frame of block indices numbered from 1 to K (total number of blocks). Supports exact and flexible partitioning, and multiple traversal orders.
generate_grid( fnrow, fncol, bnrow, bncol, traversal.mode = "rowsnake", is.flexible = NULL, is.flexible.row = FALSE, is.flexible.col = FALSE )generate_grid( fnrow, fncol, bnrow, bncol, traversal.mode = "rowsnake", is.flexible = NULL, is.flexible.row = FALSE, is.flexible.col = FALSE )
fnrow |
Integer. Number of rows in the file or matrix. |
fncol |
Integer. Number of columns in the file or matrix. |
bnrow |
Integer. Number of rows per block. |
bncol |
Integer. Number of columns per block. |
traversal.mode |
Character. Traversal order for the grid. |
is.flexible |
Logical or |
is.flexible.row |
Logical. If |
is.flexible.col |
Logical. If |
A data.frame with columns:
Block index from 1 to K.
Upper (first) row index of the block.
Lower (last) row index of the block.
Left (first) column index of the block.
Right (last) column index of the block.
# Exact partition with snake traversal generate_grid(100, 100, 10, 10) # Flexible partition (block size does not divide file size evenly) generate_grid(105, 103, 10, 10, is.flexible = TRUE)# Exact partition with snake traversal generate_grid(100, 100, 10, 10) # Flexible partition (block size does not divide file size evenly) generate_grid(105, 103, 10, 10, is.flexible = TRUE)
Computes block indices when block dimensions divide file dimensions exactly. Uses vectorised operations rather than a loop, making it fast for large grids.
generate.grid.exact(fnrow, fncol, bnrow, bncol, traversal.mode)generate.grid.exact(fnrow, fncol, bnrow, bncol, traversal.mode)
fnrow |
Integer. Number of rows in the file or matrix. |
fncol |
Integer. Number of columns in the file or matrix. |
bnrow |
Integer. Number of rows per block. Must divide |
bncol |
Integer. Number of columns per block. Must divide |
traversal.mode |
Character. |
A data.frame with columns block_no, U, D,
L, R. See generate_grid for column descriptions.
generate_grid, generate.grid.rowsnake,
generate.grid.lr
Iteratively computes block indices using a strict left-to-right traversal order across all rows. Handles cases where block dimensions do not evenly divide file dimensions by allowing the last block in each row or column to be smaller.
generate.grid.lr(fnrow, fncol, bnrow, bncol)generate.grid.lr(fnrow, fncol, bnrow, bncol)
fnrow |
Integer. Number of rows in the file or matrix. |
fncol |
Integer. Number of columns in the file or matrix. |
bnrow |
Integer. Number of rows per block. |
bncol |
Integer. Number of columns per block. |
A data.frame with columns block_no, U, D,
L, R. See generate_grid for column descriptions.
generate_grid, generate.grid.exact,
generate.grid.rowsnake
Iteratively computes block indices using a snake (boustrophedon) traversal order. Handles cases where block dimensions do not evenly divide file dimensions by allowing the last block in each row or column to be smaller.
generate.grid.rowsnake(fnrow, fncol, bnrow, bncol)generate.grid.rowsnake(fnrow, fncol, bnrow, bncol)
fnrow |
Integer. Number of rows in the file or matrix. |
fncol |
Integer. Number of columns in the file or matrix. |
bnrow |
Integer. Number of rows per block. |
bncol |
Integer. Number of columns per block. |
A data.frame with columns block_no, U, D,
L, R. See generate_grid for column descriptions.
generate_grid, generate.grid.exact,
generate.grid.lr
Computes using chol followed by chol2inv for
numerical stability.
inv_chol(X)inv_chol(X)
X |
Numeric symmetric positive definite matrix. |
The inverse of X.
Computes the log posterior predictive density of at a single time
step under the model with an identity right-covariance matrix, using a
matrix-normal density.
lppd_id_1t(Yt, Ft, Vt, st, St)lppd_id_1t(Yt, Ft, Vt, st, St)
Yt |
Numeric matrix. Observed data at time |
Ft |
Numeric matrix. Covariate matrix at time |
Vt |
Numeric matrix. Observation noise left-covariance ( |
st |
Numeric matrix. Smoothed state mean at time |
St |
Numeric matrix. Smoothed state left-covariance at time |
Numeric scalar. Log posterior predictive density.
Computes the log posterior predictive density of at a single time
step under the scalar-sigma model (), using
dMTig.
lppd_IG_1t(Yt, Ft, Vt, st, St, nt, Dt, R)lppd_IG_1t(Yt, Ft, Vt, st, St, nt, Dt, R)
Yt |
Numeric matrix. Observed data at time |
Ft |
Numeric matrix. Covariate matrix at time |
Vt |
Numeric matrix. Observation noise left-covariance ( |
st |
Numeric matrix. Smoothed state mean at time |
St |
Numeric matrix. Smoothed state left-covariance at time |
nt |
Numeric scalar. Filtered shape parameter at time |
Dt |
Numeric scalar. Filtered rate parameter at time |
R |
Numeric matrix. Fixed right correlation matrix ( |
Numeric scalar. Log posterior predictive density.
Computes the log posterior predictive density of at a single time
step under the MNIW model, evaluated at the smoothed state parameters from
FFBS.
lppd_IW_1t(Yt, Ft, Vt, st, St, nt, Dt)lppd_IW_1t(Yt, Ft, Vt, st, St, nt, Dt)
Yt |
Numeric matrix. Observed data at time |
Ft |
Numeric matrix. Covariate matrix at time |
Vt |
Numeric matrix. Observation noise left-covariance ( |
st |
Numeric matrix. Smoothed state mean at time |
St |
Numeric matrix. Smoothed state left-covariance at time |
nt |
Numeric scalar. Filtered degrees of freedom at time |
Dt |
Numeric matrix. Filtered scale matrix at time |
Numeric scalar. Log posterior predictive density.
Symmetrises a matrix by averaging it with its transpose and, if the result is not positive definite, adds a small diagonal ridge.
make_pds(C, eps = 10^(-4))make_pds(C, eps = 10^(-4))
C |
Numeric matrix. |
eps |
Numeric scalar. Size of the diagonal ridge added when |
The corrected positive definite symmetric matrix.
Draws nsam posterior samples of the regression coefficient matrix
and the scalar variance under the MNIG model where
the right covariance of is :
MNIG_sampler(nsam, X, v, S, C, Vb, R)MNIG_sampler(nsam, X, v, S, C, Vb, R)
nsam |
Integer. Number of samples to draw. |
X |
Numeric matrix. Covariate (design) matrix ( |
v |
Numeric scalar. Shape parameter of the inverse-gamma prior on
|
S |
Numeric scalar. Rate parameter of the inverse-gamma prior on
|
C |
Numeric matrix. Prior mean of |
Vb |
Numeric matrix. Prior row covariance of |
R |
Numeric matrix. Fixed right correlation matrix ( |
A named list with:
Numeric vector of length nsam with samples of
.
Array of dimension c(p, q, nsam) with samples of
.
MNIW_sampler, FFBS_sampling_sigma2R
Computes the posterior parameters of the Matrix Normal Inverse Wishart (MNIW)
model using crossprod for improved numerical efficiency.
MNIW_R(X, Y, newX = NULL, newY = NULL, H, v, S, C, Vb)MNIW_R(X, Y, newX = NULL, newY = NULL, H, v, S, C, Vb)
X |
Numeric matrix. Covariate (design) matrix of dimension |
Y |
Numeric matrix. Response matrix of dimension |
newX |
Unused legacy parameter. Defaults to |
newY |
Unused legacy parameter. Defaults to |
H |
Numeric matrix. Row covariance matrix of the error term ( |
v |
Numeric scalar. Prior degrees of freedom of the inverse-Wishart on |
S |
Numeric matrix. Prior scale matrix of the inverse-Wishart on |
C |
Numeric matrix. Prior mean matrix of |
Vb |
Numeric matrix. Prior row covariance matrix of |
The model is , where ,
, and .
A named list with posterior parameters:
Posterior degrees of freedom.
Posterior scale matrix of the inverse-Wishart.
Posterior mean matrix of .
Posterior row covariance matrix of .
Computes the posterior parameters of the Matrix Normal Inverse Wishart (MNIW)
model using explicit matrix transposes. Equivalent to MNIW_R but
without crossprod optimisations. Intended for reference and testing.
MNIW_R_naiive(X, Y, newX = NULL, newY = NULL, H, v, S, C, Vb)MNIW_R_naiive(X, Y, newX = NULL, newY = NULL, H, v, S, C, Vb)
X |
Numeric matrix. Covariate (design) matrix of dimension |
Y |
Numeric matrix. Response matrix of dimension |
newX |
Unused legacy parameter. Defaults to |
newY |
Unused legacy parameter. Defaults to |
H |
Numeric matrix. Row covariance matrix of the error term ( |
v |
Numeric scalar. Prior degrees of freedom of the inverse-Wishart on |
S |
Numeric matrix. Prior scale matrix of the inverse-Wishart on |
C |
Numeric matrix. Prior mean matrix of |
Vb |
Numeric matrix. Prior row covariance matrix of |
The model is , where ,
, and .
A named list with posterior parameters:
Posterior degrees of freedom.
Posterior scale matrix of the inverse-Wishart.
Posterior mean matrix of .
Posterior row covariance matrix of .
Draws nsam posterior samples of the regression coefficient matrix
and the covariance matrix under the MNIW model:
MNIW_sampler(nsam, X, v, S, C, Vb)MNIW_sampler(nsam, X, v, S, C, Vb)
nsam |
Integer. Number of samples to draw. |
X |
Numeric matrix. Covariate (design) matrix ( |
v |
Numeric scalar. Degrees of freedom of the inverse-Wishart prior on
|
S |
Numeric matrix. Scale matrix of the inverse-Wishart prior
( |
C |
Numeric matrix. Prior mean of |
Vb |
Numeric matrix. Prior row covariance of |
A named list with:
Array of dimension c(q, q, nsam) with samples of
.
Array of dimension c(p, q, nsam) with samples of
.
Produces nine raster heatmaps arranged in a 3-by-3 grid, one for each time
stamp in tstamp. Each panel shows the spatial field for a given input
(row) index and time step.
plot_panel_heatmap_9( dat, input_num, tstamp, max_y, Nx, Ny, nT, filename = "plot_panel", savei = TRUE, col_bgr = spdbl_default_col_bgr(), path_fig = "." )plot_panel_heatmap_9( dat, input_num, tstamp, max_y, Nx, Ny, nT, filename = "plot_panel", savei = TRUE, col_bgr = spdbl_default_col_bgr(), path_fig = "." )
dat |
List of length |
input_num |
Integer. Row index within each time-step matrix to plot. |
tstamp |
Integer vector of length 9. Time step indices to display. |
max_y |
Numeric scalar. Upper limit of the colour scale. |
Nx |
Integer. Number of grid points in the x-direction. |
Ny |
Integer. Number of grid points in the y-direction. |
nT |
Integer. Total number of time steps in |
filename |
Character. Base name for the saved PNG (used when
|
savei |
Logical. Whether to save the panel to disk. Defaults to
|
col_bgr |
Character vector of colours for the heatmap gradient. |
path_fig |
Character. Directory path where the file is saved when
|
A list of nine ggplot objects (one per panel).
Produces nine raster heatmaps for calibration data arranged in a 3-by-3
grid, one for each time stamp in tstamp. Locations are specified via
loc_cal rather than a regular grid.
plot_panel_heatmap_9_cal( dat, tstamp, max_y, loc_cal, Nx, Ny, filename = "plot_panel", savei = TRUE, col_bgr = spdbl_default_col_bgr(), path_fig = "." )plot_panel_heatmap_9_cal( dat, tstamp, max_y, loc_cal, Nx, Ny, filename = "plot_panel", savei = TRUE, col_bgr = spdbl_default_col_bgr(), path_fig = "." )
dat |
Numeric matrix of dimension |
tstamp |
Integer vector of length 9. Time-step indices (rows of
|
max_y |
Numeric scalar. Upper limit of the colour scale. |
loc_cal |
Data frame with columns |
Nx |
Integer. Number of grid points in the x-direction (used for reference only). |
Ny |
Integer. Number of grid points in the y-direction (used for reference only). |
filename |
Character. Base name for the saved PNG. Defaults to
|
savei |
Logical. Whether to save the panel to disk. Defaults to
|
col_bgr |
Character vector of colours for the heatmap gradient. |
path_fig |
Character. Directory path where the file is saved when
|
A list of nine ggplot objects.
plot_panel_heatmap_9_cal_nolab
Like plot_panel_heatmap_9_cal but suppresses x- and y-axis
labels and supports a custom lower colour-scale limit (min_y).
plot_panel_heatmap_9_cal_nolab( dat, tstamp, min_y = 0, max_y, loc_cal, Nx, Ny, filename = "plot_panel", savei = TRUE, col_bgr = spdbl_default_col_bgr(), path_fig = "." )plot_panel_heatmap_9_cal_nolab( dat, tstamp, min_y = 0, max_y, loc_cal, Nx, Ny, filename = "plot_panel", savei = TRUE, col_bgr = spdbl_default_col_bgr(), path_fig = "." )
dat |
Numeric matrix of dimension |
tstamp |
Integer vector of length 9. Time-step indices to display. |
min_y |
Numeric scalar. Lower limit of the colour scale. Defaults to
|
max_y |
Numeric scalar. Upper limit of the colour scale. |
loc_cal |
Data frame with columns |
Nx |
Integer. Number of grid points in the x-direction. |
Ny |
Integer. Number of grid points in the y-direction. |
filename |
Character. Base name for the saved PNG. Defaults to
|
savei |
Logical. Whether to save the panel to disk. Defaults to
|
col_bgr |
Character vector of colours for the heatmap gradient. |
path_fig |
Character. Directory path where the file is saved when
|
A list of nine ggplot objects.
Reads a matrix of PDE input parameters and a set of PDE output CSV files, splits the inputs into training and testing sets, and reshapes the simulator outputs into time-indexed lists for emulator fitting and evaluation.
prepare_data(file_para, file_data, prop_train = 0.8)prepare_data(file_para, file_data, prop_train = 0.8)
file_para |
Character scalar. Path to a CSV file containing the PDE input parameter matrix. Rows correspond to simulator runs. |
file_data |
Character vector. Paths to CSV files containing PDE outputs,
one file per simulator run. The length should match the number of rows in
|
prop_train |
Numeric scalar in |
The function assumes that each file in file_data contains a matrix
whose rows correspond to time steps and whose columns correspond to spatial
locations. The number of time steps is inferred from the first PDE output
file.
A named list with:
Numeric matrix of training input parameters.
Numeric matrix of testing input parameters.
Named list of training PDE outputs by time step.
Named list of testing PDE outputs by time step.
Integer. Number of training simulator runs.
Integer. Number of testing simulator runs.
emulator_learn, emulator_predict
Creates a ggplot2 raster heatmap using the col_bgr colour
palette with a squished colour scale capped at max_y.
quick_heat(dt, max_y, col_bgr = spdbl_default_col_bgr())quick_heat(dt, max_y, col_bgr = spdbl_default_col_bgr())
dt |
Data frame with columns |
max_y |
Numeric scalar. Upper limit of the colour scale. Values above this are squished to the maximum colour. |
col_bgr |
Character vector of colours for the heatmap gradient. |
A ggplot object.
Saves plot to a PNG file in path_fig whose name is
filename followed by the current Unix timestamp, ensuring unique
file names across repeated calls.
quick_save(filename, path_fig, plot)quick_save(filename, path_fig, plot)
filename |
Character. Base name for the output file (no extension). |
path_fig |
Character. Directory path where the file is saved. |
plot |
A |
Invisibly returns NULL (called for its side effect).
Efficiently reads a contiguous rectangular block of rows and columns from a
CSV file using readr::read_csv. Useful for processing big data in
chunks without loading the entire file into memory.
read_big_csv_quick(filename, rows = c(1, Inf), cols = c(1, Inf), header = TRUE)read_big_csv_quick(filename, rows = c(1, Inf), cols = c(1, Inf), header = TRUE)
filename |
Character. Path to the CSV file. |
rows |
Integer vector of length 2 giving the first and last row indices
to read (1-based, excluding the header row if |
cols |
Integer vector of length 2 giving the first and last column
indices to read. Use |
header |
Logical. Whether the file has a header row. Defaults to
|
A tibble (from readr) containing the requested block.
Reassembles a flattened episode-partitioned list (as produced by the EP
model) back into a list indexed by the original nT_ori time steps.
Assumes the number of blocks per season n_block = nT / nT_ori is an
integer.
recover_from_EP_exact(dat_EP, nT_ori, nT)recover_from_EP_exact(dat_EP, nT_ori, nT)
dat_EP |
List of length |
nT_ori |
Integer. Number of original (non-partitioned) time steps. |
nT |
Integer. Total number of episode time steps
( |
A named list of length nT_ori ("T1", ...,
"T<nT_ori>"), each element a matrix covering one original time step.
Reassembles a flattened episode-partitioned list of posterior sample arrays
back into a list indexed by the original nT_ori time steps.
recover_from_EP_MC(dat_EP, nT_ori, nT, nsam)recover_from_EP_MC(dat_EP, nT_ori, nT, nsam)
dat_EP |
List of length |
nT_ori |
Integer. Number of original time steps. |
nT |
Integer. Total number of episode time steps. |
nsam |
Integer. Number of posterior samples. |
A named list of length nT_ori. Each element is an array of
dimension c(p, bncol * n_block, nsam).
Samples using the upper-triangular
Cholesky factors RM and RSigma of and
respectively: , where
has i.i.d. standard normal entries.
rmn_chol(m, RM, RSigma)rmn_chol(m, RM, RSigma)
m |
Numeric matrix. Mean matrix ( |
RM |
Numeric matrix. Upper-triangular Cholesky factor of the row
covariance |
RSigma |
Numeric matrix. Upper-triangular Cholesky factor of the column
covariance |
A numeric matrix of dimension c(p, S).
Draws nsam independent samples from using
the Cholesky factors of and .
rmn_chol_more(nsam, m, RM, RSigma)rmn_chol_more(nsam, m, RM, RSigma)
nsam |
Integer. Number of samples to draw. |
m |
Numeric matrix. Mean matrix ( |
RM |
Numeric matrix. Upper-triangular Cholesky factor of the row
covariance |
RSigma |
Numeric matrix. Upper-triangular Cholesky factor of the column
covariance |
An array of dimension c(p, S, nsam).
Draws one sample from the predictive distribution
at each calibration time step, given
precomputed mean and covariance (e.g., from
update_muSigma_eta_one).
sample_y_eta_one(ind_sam, nT_cal, N_sp_train, mu_arry_train, Sigma_eta_train)sample_y_eta_one(ind_sam, nT_cal, N_sp_train, mu_arry_train, Sigma_eta_train)
ind_sam |
Integer. Sample index (used only for loop iteration; single sample is drawn). |
nT_cal |
Integer. Number of calibration time steps. |
N_sp_train |
Integer. Number of training spatial locations. |
mu_arry_train |
Numeric array of dimension |
Sigma_eta_train |
Numeric matrix. Predictive covariance
( |
Numeric matrix of dimension c(nT_cal, N_sp_train) with one
draw per time step.
Maps x from the unit interval back to the original range
: .
scale_back_uniform(x, low, high)scale_back_uniform(x, low, high)
x |
Numeric vector or matrix. Values in |
low |
Numeric. Lower bound of the target range. |
high |
Numeric. Upper bound of the target range. |
Numeric vector or matrix rescaled to .
Maps x to .
Raises an error if x is outside [low, high]. Sets
NaN results to 0.
scale_uniform(x, low, high)scale_uniform(x, low, high)
x |
Numeric vector or matrix. Values to scale. |
low |
Numeric. Lower bound of the original range. |
high |
Numeric. Upper bound of the original range. |
Numeric vector or matrix with values in .
Defines the right-hand side of a spatial SIR (Susceptible-Infected-Recovered)
partial differential equation suitable for use with deSolve::ode.2D.
Diffusion is implemented via ReacTran::tran.2D with zero-flux
boundary conditions.
SIR(t, y, parms)SIR(t, y, parms)
t |
Numeric. Current time (passed by the ODE solver). |
y |
Numeric vector. Flattened state vector of length |
parms |
Named list of parameters:
|
A list with one element: a numeric vector of derivatives of the same
length as y, as required by deSolve.
Like update_y_eta_one but skips the final sampling step,
returning only the predictive mean and covariance. Useful when sampling is
done separately (e.g., in sample_y_eta_one).
update_muSigma_eta_one( eta, ind_sam, Nx, Ny, N_people, nT_ori, nT, nT_cal, N_sp, N_sp_train, AR_choice, bncol, res_ffbs, n_b_cal, ind_block_cal, t_VinvY_FTheta_ls, Vinv, R_cal_train, input, input_cal, phi_para, gp_sigma2, gp_tau2, ind_train_cal )update_muSigma_eta_one( eta, ind_sam, Nx, Ny, N_people, nT_ori, nT, nT_cal, N_sp, N_sp_train, AR_choice, bncol, res_ffbs, n_b_cal, ind_block_cal, t_VinvY_FTheta_ls, Vinv, R_cal_train, input, input_cal, phi_para, gp_sigma2, gp_tau2, ind_train_cal )
eta |
Numeric vector. PDE parameters (length 5). |
ind_sam |
Integer. Index of the FFBS sample to use. |
Nx |
Integer. Grid cells in x-direction. |
Ny |
Integer. Grid cells in y-direction. |
N_people |
Numeric. Total population size. |
nT_ori |
Integer. Number of original time steps. |
nT |
Integer. Number of episode time steps. |
nT_cal |
Integer. Number of calibration time steps. |
N_sp |
Integer. Total number of spatial locations. |
N_sp_train |
Integer. Number of training spatial locations. |
AR_choice |
Integer. AR order (1 or 2). |
bncol |
Integer. Number of columns per block. |
res_ffbs |
List. FFBS posterior samples. |
n_b_cal |
Integer. Number of calibration blocks. |
ind_block_cal |
Data frame. Block indices for calibration. |
t_VinvY_FTheta_ls |
List. Precomputed |
Vinv |
Numeric matrix. Inverse of the observation covariance. |
R_cal_train |
Numeric matrix. Correlation matrix for training locations. |
input |
Numeric matrix. Observed location coordinates. |
input_cal |
Numeric matrix. Calibration location coordinates. |
phi_para |
Numeric scalar. GP range parameter. |
gp_sigma2 |
Numeric scalar. GP marginal variance. |
gp_tau2 |
Numeric scalar. GP nugget variance. |
ind_train_cal |
Integer vector. Indices of training locations. |
A named list with mu and Sigma.
update_y_eta_one, sample_y_eta_one
For a given candidate PDE parameter vector eta, simulates the
computer model output, computes the posterior predictive mean and covariance
at calibration locations using all nsam FFBS samples, and draws new
observations from the resulting predictive distribution.
update_y_eta( eta, nsam, Nx, Ny, N_people, nT_ori, nT, nT_cal, N_sp, N_sp_train, AR_choice, bncol, res_ffbs, n_b_cal, ind_block_cal, t_VinvY_FTheta_ls, Vinv, R_cal_train, input, input_cal, phi_para, gp_sigma2, gp_tau2, ind_train_cal )update_y_eta( eta, nsam, Nx, Ny, N_people, nT_ori, nT, nT_cal, N_sp, N_sp_train, AR_choice, bncol, res_ffbs, n_b_cal, ind_block_cal, t_VinvY_FTheta_ls, Vinv, R_cal_train, input, input_cal, phi_para, gp_sigma2, gp_tau2, ind_train_cal )
eta |
Numeric vector. PDE parameters (length 5). |
nsam |
Integer. Number of posterior samples. |
Nx |
Integer. Grid cells in x-direction. |
Ny |
Integer. Grid cells in y-direction. |
N_people |
Numeric. Total population size. |
nT_ori |
Integer. Number of original time steps. |
nT |
Integer. Number of episode time steps. |
nT_cal |
Integer. Number of calibration time steps. |
N_sp |
Integer. Total number of spatial locations. |
N_sp_train |
Integer. Number of training spatial locations. |
AR_choice |
Integer. AR order (1 or 2). |
bncol |
Integer. Number of columns per block. |
res_ffbs |
List. FFBS posterior samples. |
n_b_cal |
Integer. Number of calibration blocks. |
ind_block_cal |
Data frame. Block indices for calibration. |
t_VinvY_FTheta_ls |
List. Precomputed |
Vinv |
Numeric matrix. Inverse of the observation covariance. |
R_cal_train |
Numeric matrix. Correlation matrix for training locations. |
input |
Numeric matrix. Observed location coordinates. |
input_cal |
Numeric matrix. Calibration location coordinates. |
phi_para |
Numeric scalar. GP range parameter. |
gp_sigma2 |
Numeric scalar. GP marginal variance. |
gp_tau2 |
Numeric scalar. GP nugget variance. |
ind_train_cal |
Integer vector. Indices of training locations within calibration locations. |
A named list with:
Array c(nT_cal, N_sp_train, nsam) of predictive samples.
Array c(nT_cal, N_sp_train, nsam) of predictive means.
Numeric scalar. Predictive variance constant.
Numeric matrix. Predictive covariance matrix.
Like update_y_eta but uses only one FFBS sample (indexed by
ind_sam) instead of averaging over all samples, making it faster for
MCMC proposals.
update_y_eta_one( eta, ind_sam, Nx, Ny, N_people, nT_ori, nT, nT_cal, N_sp, N_sp_train, AR_choice, bncol, res_ffbs, n_b_cal, ind_block_cal, t_VinvY_FTheta_ls, Vinv, R_cal_train, input, input_cal, phi_para, gp_sigma2, gp_tau2, ind_train_cal )update_y_eta_one( eta, ind_sam, Nx, Ny, N_people, nT_ori, nT, nT_cal, N_sp, N_sp_train, AR_choice, bncol, res_ffbs, n_b_cal, ind_block_cal, t_VinvY_FTheta_ls, Vinv, R_cal_train, input, input_cal, phi_para, gp_sigma2, gp_tau2, ind_train_cal )
eta |
Numeric vector. PDE parameters (length 5). |
ind_sam |
Integer. Index of the FFBS sample to use. |
Nx |
Integer. Grid cells in x-direction. |
Ny |
Integer. Grid cells in y-direction. |
N_people |
Numeric. Total population size. |
nT_ori |
Integer. Number of original time steps. |
nT |
Integer. Number of episode time steps. |
nT_cal |
Integer. Number of calibration time steps. |
N_sp |
Integer. Total number of spatial locations. |
N_sp_train |
Integer. Number of training spatial locations. |
AR_choice |
Integer. AR order (1 or 2). |
bncol |
Integer. Number of columns per block. |
res_ffbs |
List. FFBS posterior samples. |
n_b_cal |
Integer. Number of calibration blocks. |
ind_block_cal |
Data frame. Block indices for calibration. |
t_VinvY_FTheta_ls |
List. Precomputed |
Vinv |
Numeric matrix. Inverse of the observation covariance. |
R_cal_train |
Numeric matrix. Correlation matrix for training locations. |
input |
Numeric matrix. Observed location coordinates. |
input_cal |
Numeric matrix. Calibration location coordinates. |
phi_para |
Numeric scalar. GP range parameter. |
gp_sigma2 |
Numeric scalar. GP marginal variance. |
gp_tau2 |
Numeric scalar. GP nugget variance. |
ind_train_cal |
Integer vector. Indices of training locations. |
A named list with y, mu, and Sigma.