Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024-08-07 update : adding matrix normalization func. and using AR1 in spatial func. #53

22 changes: 12 additions & 10 deletions R/generate_simulated_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
#' @param scaling_factor Scaling factor for aux site
#' @param aux_site_bool Boolean to use the aux site framework with
#' scaling factor.
#' @param init_bool Boolean for making initial value of AUX site AR(1)
#' process stationary( 1 or 0 ).
cbernalz marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @return a list containing three dataframes. hosp_data is a dataframe
#' containing the number of daily hospital admissions by day for a theoretical
Expand Down Expand Up @@ -101,7 +103,8 @@
#' phi_rt = 0.6,
#' sigma_eps = sqrt(0.02),
#' scaling_factor = 0.01,
#' aux_site_bool = TRUE
#' aux_site_bool = TRUE,
#' init_bool = TRUE
cbernalz marked this conversation as resolved.
Show resolved Hide resolved
#' )
#' hosp_data <- sim_data$hosp_data
#' ww_data <- sim_data$ww_data
Expand Down Expand Up @@ -161,7 +164,8 @@ generate_simulated_data <- function(r_in_weeks = # nolint
phi_rt = 0.6,
sigma_eps = sqrt(0.02),
scaling_factor = 0.01,
aux_site_bool = TRUE) {
aux_site_bool = TRUE,
init_bool = TRUE) {
cbernalz marked this conversation as resolved.
Show resolved Hide resolved
# Some quick checks to make sure the inputs work as expected
stopifnot(
"weekly R(t) passed in isn't long enough" =
Expand Down Expand Up @@ -367,18 +371,16 @@ generate_simulated_data <- function(r_in_weeks = # nolint
# Auxiliary Site
if (aux_site_bool) {
state_deviation_noise_vec <- state_deviation_noise_vec_aux_rng(
scaling_factor,
sigma_eps,
1,
1,
n_weeks
cbernalz marked this conversation as resolved.
Show resolved Hide resolved
)
state_deviation_init <- rnorm(
n = 1,
mean = 0,
sd = scaling_factor * sigma_eps
)
log_r_site_aux <- construct_aux_rt(log_r_state_week,
state_deviation_ar_coeff = phi_rt,
state_deviation_noise_vec
scaling_factor,
sigma_eps,
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
state_deviation_noise_vec,
init_bool
cbernalz marked this conversation as resolved.
Show resolved Hide resolved
)
log_r_site <- rbind(
log_r_site,
Expand Down
Binary file modified data/hosp_data.rda
Binary file not shown.
Binary file modified data/hosp_data_eval.rda
Binary file not shown.
16 changes: 11 additions & 5 deletions inst/stan/functions/aux_site_process_rng.stan
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@
* of spatial epsilon.
* @param state_deviation_ar_coeff Coefficient for AR(1) temporal correlation on
* state deviations for aux site.
* @param init_bool Boolean for making initial value stationary( 1 or 0 ).
cbernalz marked this conversation as resolved.
Show resolved Hide resolved
* @return A vector for unadjusted Rt for aux site where columns are time points.
*/
vector aux_site_process_rng(vector log_state_rt,
real scaling_factor,
real sigma_eps,
real state_deviation_ar_coeff) {
real state_deviation_ar_coeff,
int init_bool) {
cbernalz marked this conversation as resolved.
Show resolved Hide resolved


//Presets
int n_time = size(log_state_rt);

vector[n_time] state_deviation_noise_vec = state_deviation_noise_vec_aux_rng(scaling_factor,
sigma_eps,
n_time);
// creates vector of standard normal for contruct aux rt.
vector[n_time] z = state_deviation_noise_vec_aux_rng(1,
1,
n_time);
cbernalz marked this conversation as resolved.
Show resolved Hide resolved
vector[n_time] log_aux_site_rt = construct_aux_rt(log_state_rt,
state_deviation_ar_coeff,
state_deviation_noise_vec);
scaling_factor,
sigma_eps,
z,
init_bool);
cbernalz marked this conversation as resolved.
Show resolved Hide resolved

return log_aux_site_rt;
}
35 changes: 17 additions & 18 deletions inst/stan/functions/construct_aux_rt.stan
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@
* @param log_state_rt "State" level unadjusted Rt, on log scale.
* @param state_deviation_ar_coeff Coefficient for AR(1) temporal correlation on
* subpopulation deviations.
* @param state_deviation_init Initial value of the spatial deviation
* @param state_deviation_noise_vec vector with random vectors of noise
* for state devaiation process for aux site.
* @param scale_factor Scaling factor for aux site process.
* @param sigma_eps Parameter for construction of covariance matrix
* of spatial epsilon.
* @param z Vector with random vectors of noise on standard normal.
* @param init_bool Boolean for making initial value stationary( 1 or 0 ).
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
* @return A vector for unadjusted Rt for aux site where columns are time points.
*/
vector construct_aux_rt(vector log_state_rt,
real state_deviation_ar_coeff,
vector state_deviation_noise_vec) {
real scaling_factor,
real sigma_eps,
vector z,
int init_bool) {
cbernalz marked this conversation as resolved.
Show resolved Hide resolved
cbernalz marked this conversation as resolved.
Show resolved Hide resolved


// presets
int n_time = dims(state_deviation_noise_vec)[1];
vector[n_time] log_aux_site_rt;
real state_deviation_t_i = 0;
// To explain state_deviation_t_i = 0 initialization,
// we need a state_deviation_t_i value for t_1 = 1, so we will
// initialize the process with index 1 of spatial
// deviation noise matrix w/out spatial_deviation ar coeff, so we set
// state_deviation_t_i to 0 initially.

for (t_i in 1:n_time) {
state_deviation_t_i = state_deviation_ar_coeff * state_deviation_t_i +
state_deviation_noise_vec[t_i];
log_aux_site_rt[t_i] = log_state_rt[t_i] + state_deviation_t_i;
}
int n_time = dims(z)[1];
vector[n_time] log_aux_site_rt = ar1(
log_state_rt,
state_deviation_ar_coeff,
scaling_factor * sigma_eps,
z,
init_bool
dylanhmorris marked this conversation as resolved.
Show resolved Hide resolved
);

return log_aux_site_rt;
}
11 changes: 11 additions & 0 deletions inst/stan/functions/matrix_normalization.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Normalizes a matrix using its determinant to the power of 1 / n.
* @param matrx A matrix, usually the correlation matrix need to normalize
* @return A matrix that has been normalized.
*
*/
matrix matrix_normalization(matrix matrx) {
int n = cols(matrx);
matrix[n,n] norm_matrx = matrx / pow(determinant(matrx), 1.0 / n);
return norm_matrx;
}
2 changes: 2 additions & 0 deletions inst/stan/functions/spatial_functions.stan
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* File to call all desired spatial functions for use with expose_functions()
*/
functions{
#include ar1.stan
#include exponential_decay_corr_func.stan
#include independence_corr_func.stan
#include construct_spatial_rt.stan
Expand All @@ -10,4 +11,5 @@ functions{
#include construct_aux_rt.stan
#include state_deviation_noise_vec_aux_rng.stan
#include aux_site_process_rng.stan
#include matrix_normalization.stan
}
9 changes: 7 additions & 2 deletions man/generate_simulated_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading