From 2e01a1ba6f1e5eed8b1edcbfc116e0179778b1b3 Mon Sep 17 00:00:00 2001 From: athowes Date: Tue, 9 May 2023 16:24:53 +0100 Subject: [PATCH] Add generation of objfull (without Laplace approximation) to naomi-simple_fit. Useful for #41 --- src/naomi-simple_fit/functions.R | 6 +++++- src/naomi-simple_fit/orderly.yml | 4 ++++ src/naomi-simple_fit/script.R | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/naomi-simple_fit/functions.R b/src/naomi-simple_fit/functions.R index 6459a48..f0b12f2 100644 --- a/src/naomi-simple_fit/functions.R +++ b/src/naomi-simple_fit/functions.R @@ -1,5 +1,5 @@ #' A local version of naomi::make_tmb_obj, edited to work with DLL = "naomi_simple" -local_make_tmb_obj <- function(data, par, calc_outputs = 0L, inner_verbose, progress = NULL, map = NULL, DLL = "naomi_simple") { +local_make_tmb_obj <- function(data, par, calc_outputs = 0L, inner_verbose, progress = NULL, map = NULL, DLL = "naomi_simple", laplace = TRUE) { # Begin expose naomi:::make_tmb_obj # https://github.com/mrc-ide/naomi/blob/e9de40f12cf2e652f78966bb351fa5718ecd7867/R/tmb-model.R#L496 data$calc_outputs <- as.integer(calc_outputs) @@ -29,6 +29,10 @@ local_make_tmb_obj <- function(data, par, calc_outputs = 0L, inner_verbose, prog integrate_out <- "x_minus_i" } + if(!laplace) { + integrate_out <- NULL + } + obj <- TMB::MakeADFun( data = data, parameters = par, diff --git a/src/naomi-simple_fit/orderly.yml b/src/naomi-simple_fit/orderly.yml index 77ac397..63da494 100755 --- a/src/naomi-simple_fit/orderly.yml +++ b/src/naomi-simple_fit/orderly.yml @@ -8,6 +8,10 @@ artefacts: description: Inference output filenames: - out.rds + - data: + description: Full objective function (without Laplace approximation) + filenames: + - objfull.rds parameters: #' Run inference with TMB? TRUE or FALSE diff --git a/src/naomi-simple_fit/script.R b/src/naomi-simple_fit/script.R index 1a3e1ce..673823e 100755 --- a/src/naomi-simple_fit/script.R +++ b/src/naomi-simple_fit/script.R @@ -82,6 +82,11 @@ tmb_inputs_simple <- local_exclude_inputs(tmb_inputs) #' The number of hyperparameters is 24 (as compared with 31 for the full model) n_hyper <- 24 +#' Create version of the objective function with no Laplace approximation +#' This will be used in later reports, such as to do PSIS +objfull <- local_make_tmb_obj(tmb_inputs$data, tmb_inputs$par_init, calc_outputs = FALSE, inner_verbose = FALSE, DLL = "naomi_simple", laplace = FALSE) +saveRDS(objfull, file = "objfull.rds") + if(tmb) { start <- Sys.time()