Skip to content

Commit

Permalink
Further tidying
Browse files Browse the repository at this point in the history
Option for exponential MCMC prior removed (unused); package recompiled with new version of odin.dust to get rid of error.
  • Loading branch information
KeithJF82 committed Jun 25, 2024
1 parent bff7a22 commit 4398f7a
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 37 deletions.
2 changes: 1 addition & 1 deletion R/dust.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ SEIRV_Model <- R6::R6Class(
}
))
class(SEIRV_Model) <- c("dust_generator", class(SEIRV_Model))
## Generated by odin.dust (version 0.3.10) - do not edit
## Generated by odin.dust (version 0.3.11) - do not edit
SEIRV_Model$set("public", "transform_variables", function(y) {
info <- self$info()
set_dim <- function(x, dimx) {
Expand Down
42 changes: 22 additions & 20 deletions R/mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
#' If type = "zero", prior probability is always zero \cr
#' If type = "flat", prior probability is zero if log parameter values in designated ranges log_params_min and log_params_max, \cr
#' -Inf otherwise; log_params_min and log_params_max included in prior_settings as vectors of same length as log_params_ini
#' If type = "exp", prior probability is given by dexp calculation on FOI/R0 values \cr
#' If type = "norm", prior probability is given by dnorm calculation on parameter values with settings based on vectors of values
#' in prior_settings; norm_params_mean and norm_params_sd (mean and standard deviation values applied to log FOI/R0
#' parameters and to actual values of additional parameters) + R0_mean + R0_sd (mean + standard deviation of computed R0, single values)
#' in prior_settings: \cr
#' norm_params_mean and norm_params_sd (vectors of mean and standard deviation values applied to log FOI/R0
#' parameters and to actual values of additional parameters) \cr
#' + FOI_mean + FOI_sd (mean + standard deviation of computed FOI, single values) \cr
#' + R0_mean + R0_sd (mean + standard deviation of computed R0, single values) \cr
#' @param dt time increment in days (must be 1 or 5)
#' @param n_reps Number of times to repeat calculations to get average likelihood at each step
#' @param enviro_data Data frame containing values of environmental covariates
Expand Down Expand Up @@ -287,10 +289,12 @@ single_posterior_calc <- function(log_params_prop = c(), input_data = list(), ob
#' If type = "zero", prior probability is always zero \cr
#' If type = "flat", prior probability is zero if log parameter values in designated ranges log_params_min and log_params_max,
#' -Inf otherwise; log_params_min and log_params_max included in prior_settings as vectors of same length as log_params_ini \cr
#' If type = "exp", prior probability is given by dexp calculation on FOI/R0 values \cr
#' If type = "norm", prior probability is given by dnorm calculation on parameter values with settings based on vectors of values
#' in prior_settings; norm_params_mean and norm_params_sd (mean and standard deviation values applied to log FOI/R0
#' parameters and to actual values of additional parameters) + R0_mean + R0_sd (mean + standard deviation of computed R0, single values)
#' in prior_settings: \cr
#' norm_params_mean and norm_params_sd (vectors of mean and standard deviation values applied to log FOI/R0
#' parameters and to actual values of additional parameters) \cr
#' + FOI_mean + FOI_sd (mean + standard deviation of computed FOI, single values) \cr
#' + R0_mean + R0_sd (mean + standard deviation of computed R0, single values) \cr
#' @param enviro_data Values of environmental covariates
#' @param add_values List of parameters in addition to those governing FOI/R0, either giving a fixed value or giving NA to
#' indicate that they are part of the parameter set to be estimated \cr
Expand All @@ -309,7 +313,7 @@ mcmc_checks <- function(log_params_ini = c(), n_regions = 1, prior_settings = li
param_names = names(log_params_ini)
n_params = length(log_params_ini)
assert_that(is.null(param_names) == FALSE, msg = "Parameters should be named using create_param_labels")
assert_that(prior_settings$type %in% c("zero", "flat", "exp", "norm"), msg = "Check prior_settings$type")
assert_that(prior_settings$type %in% c("zero", "flat", "norm"), msg = "Check prior_settings$type")
if(prior_settings$type == "flat"){
assert_that(length(prior_settings$log_params_min) == n_params, msg = "Check prior_settings$log_params_min")
assert_that(length(prior_settings$log_params_max) == n_params, msg = "Check prior_settings$log_params_max")
Expand Down Expand Up @@ -391,10 +395,12 @@ param_prop_setup <- function(log_params = c(), chain_cov = 1, adapt = 0){
#' If type = "zero", prior probability is always zero \cr
#' If type = "flat", prior probability is zero if log parameter values in designated ranges log_params_min and log_params_max,
#' -Inf otherwise; log_params_min and log_params_max included in prior_settings as vectors of same length as log_params_ini \cr
#' If type = "exp", prior probability is given by dexp calculation on FOI/R0 values \cr
#' If type = "norm", prior probability is given by dnorm calculation on parameter values with settings based on vectors of values
#' in prior_settings; norm_params_mean and norm_params_sd (mean and standard deviation values applied to log FOI/R0
#' parameters and to actual values of additional parameters) + R0_mean + R0_sd (mean + standard deviation of computed R0, single values)
#' in prior_settings: \cr
#' norm_params_mean and norm_params_sd (vectors of mean and standard deviation values applied to log FOI/R0
#' parameters and to actual values of additional parameters) \cr
#' + FOI_mean + FOI_sd (mean + standard deviation of computed FOI, single values) \cr
#' + R0_mean + R0_sd (mean + standard deviation of computed R0, single values) \cr
#' @param regions Vector of region names
#' @param log_params_prop Proposed values of parameters (natural logarithm of actual parameters)
#' @param enviro_data Environmental data frame, containing only relevant environmental covariate values
Expand Down Expand Up @@ -427,12 +433,6 @@ mcmc_FOI_R0_setup <- function(prior_settings = list(type = "zero"), regions = ""
if(log_params_prop[i]<prior_settings$log_params_min[i]){prior = -Inf}
if(log_params_prop[i]>prior_settings$log_params_max[i]){prior = -Inf}
}
} else {
if(prior_settings$type == "exp"){
prior_FOI = dexp(FOI_values, rate = 1, log = TRUE)
prior_R0 = dexp(R0_values, rate = 1, log = TRUE)
prior = prior+sum(prior_FOI)+sum(prior_R0)
}
}
}

Expand Down Expand Up @@ -468,10 +468,12 @@ mcmc_FOI_R0_setup <- function(prior_settings = list(type = "zero"), regions = ""
#' If type = "zero", prior probability is always zero \cr
#' If type = "flat", prior probability is zero if log parameter values in designated ranges log_params_min and log_params_max,
#' -Inf otherwise; log_params_min and log_params_max included in prior_settings as vectors of same length as log_params_ini \cr
#' If type = "exp", prior probability is given by dexp calculation on FOI/R0 values \cr
#' If type = "norm", prior probability is given by dnorm calculation on parameter values with settings based on vectors of values
#' in prior_settings; norm_params_mean and norm_params_sd (mean and standard deviation values applied to log FOI/R0
#' parameters and to actual values of additional parameters) + R0_mean + R0_sd (mean + standard deviation of computed R0, single values)
#' in prior_settings: \cr
#' norm_params_mean and norm_params_sd (vectors of mean and standard deviation values applied to log FOI/R0
#' parameters and to actual values of additional parameters) \cr
#' + FOI_mean + FOI_sd (mean + standard deviation of computed FOI, single values) \cr
#' + R0_mean + R0_sd (mean + standard deviation of computed R0, single values) \cr
#' @param dt time increment in days (must be 1 or 5)
#' @param n_reps Number of repetitions
#' @param enviro_data Values of environmental variables (if in use)
Expand Down Expand Up @@ -499,7 +501,7 @@ mcmc_prelim_fit <- function(n_iterations = 1, n_param_sets = 1, n_bounds = 1, lo
#TODO - Add assertthat functions
assert_that(mode_start %in% c(0, 1, 3), msg = "mode_start must have value 0, 1 or 3")
assert_that(length(log_params_min) == length(log_params_max), msg = "Parameter limit vectors must have same lengths")
assert_that(prior_settings$type %in% c("zero", "exp", "norm"), msg = "Prior type must be 'zero', 'exp' or 'norm'")
assert_that(prior_settings$type %in% c("zero", "norm"), msg = "Prior type must be 'zero', or 'norm'")

best_fit_results = list()
n_params = length(log_params_min)
Expand Down
4 changes: 2 additions & 2 deletions inst/dust/SEIRV_Model.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated by odin.dust (version 0.3.10) - do not edit
// Generated by odin.dust (version 0.3.11) - do not edit
template <typename real_type, typename container>
__host__ __device__ real_type odin_sum1(const container x, size_t from, size_t to);
template <typename real_type, typename container>
Expand Down Expand Up @@ -325,7 +325,7 @@ void user_check_array_dim(cpp11::sexp x, const char *name,
cpp11::integers dim = cpp11::as_cpp<cpp11::integers>(x.attr("dim"));
for (size_t i = 0; i < N; ++i) {
if (dim[(int)i] != dim_expected[i]) {
Rf_error("Incorrect size of dimension %d of '%s' (expected %d)",
Rf_error("Incorrect size of dimension %zu of '%s' (expected %d)",
i + 1, name, dim_expected[i]);
}
}
Expand Down
8 changes: 5 additions & 3 deletions man/MCMC.Rd

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

8 changes: 5 additions & 3 deletions man/mcmc_FOI_R0_setup.Rd

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

8 changes: 5 additions & 3 deletions man/mcmc_checks.Rd

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

8 changes: 5 additions & 3 deletions man/mcmc_prelim_fit.Rd

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

4 changes: 2 additions & 2 deletions src/SEIRV_Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void dust_cpu_SEIRV_Model_set_stochastic_schedule(SEXP ptr, SEXP time);
SEXP dust_cpu_SEIRV_Model_ode_statistics(SEXP ptr);
#include <dust/r/dust.hpp>

// Generated by odin.dust (version 0.3.10) - do not edit
// Generated by odin.dust (version 0.3.11) - do not edit
template <typename real_type, typename container>
__host__ __device__ real_type odin_sum1(const container x, size_t from, size_t to);
template <typename real_type, typename container>
Expand Down Expand Up @@ -399,7 +399,7 @@ void user_check_array_dim(cpp11::sexp x, const char *name,
cpp11::integers dim = cpp11::as_cpp<cpp11::integers>(x.attr("dim"));
for (size_t i = 0; i < N; ++i) {
if (dim[(int)i] != dim_expected[i]) {
Rf_error("Incorrect size of dimension %d of '%s' (expected %d)",
Rf_error("Incorrect size of dimension %zu of '%s' (expected %d)",
i + 1, name, dim_expected[i]);
}
}
Expand Down
Binary file modified src/SEIRV_Model.o
Binary file not shown.
Binary file modified src/YEP.dll
Binary file not shown.
Binary file modified src/cpp11.o
Binary file not shown.

0 comments on commit 4398f7a

Please sign in to comment.