From 3f62de6f0794181dfde3d51a7e931cc478709c92 Mon Sep 17 00:00:00 2001 From: Tess-LaCoil Date: Wed, 8 Jan 2025 13:02:28 +1100 Subject: [PATCH 1/5] Changed extract_estimates argument order so the output from run() can be piped into the first arg as a fit. --- R/hmde_extract_estimates.R | 4 ++-- tests/testthat/test-hmde_extract_estimates.R | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/R/hmde_extract_estimates.R b/R/hmde_extract_estimates.R index 261a0d6..a89887a 100644 --- a/R/hmde_extract_estimates.R +++ b/R/hmde_extract_estimates.R @@ -9,8 +9,8 @@ #' @import dplyr #' @importFrom stats quantile -hmde_extract_estimates <- function(model = NULL, - fit = NULL, +hmde_extract_estimates <- function(fit = NULL, + model = NULL, input_measurement_data = NULL){ #Check for fit if(is.null(fit)){ diff --git a/tests/testthat/test-hmde_extract_estimates.R b/tests/testthat/test-hmde_extract_estimates.R index f14202f..1c47960 100644 --- a/tests/testthat/test-hmde_extract_estimates.R +++ b/tests/testthat/test-hmde_extract_estimates.R @@ -7,16 +7,14 @@ test_that("Execution and output: extract_estimates function", { ) suppressWarnings( - constant_single_fit <- hmde_model("constant_single_ind") |> + output <- hmde_model("constant_single_ind") |> hmde_assign_data(data = data) |> hmde_run(chains = 1, iter = 20, cores = 1, - verbose = FALSE, show_messages = FALSE) + verbose = FALSE, show_messages = FALSE) |> + hmde_extract_estimates(model = "constant_single_ind", + input_measurement_data = data) ) - output <- hmde_extract_estimates(model = "constant_single_ind", - fit = constant_single_fit, - input_measurement_data = data) - expect_named(output) expect_visible(output) From 5c808f3c79a48198f3a8f4a87ebb7cbe4d37c81f Mon Sep 17 00:00:00 2001 From: Tess-LaCoil Date: Wed, 8 Jan 2025 13:20:17 +1100 Subject: [PATCH 2/5] Updated documentation. --- man/hmde_extract_estimates.Rd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/man/hmde_extract_estimates.Rd b/man/hmde_extract_estimates.Rd index 93b89b4..9005f09 100644 --- a/man/hmde_extract_estimates.Rd +++ b/man/hmde_extract_estimates.Rd @@ -4,13 +4,13 @@ \alias{hmde_extract_estimates} \title{Extract samples and return measurement, individual, and population-level estimates} \usage{ -hmde_extract_estimates(model = NULL, fit = NULL, input_measurement_data = NULL) +hmde_extract_estimates(fit = NULL, model = NULL, input_measurement_data = NULL) } \arguments{ -\item{model}{model name character string} - \item{fit}{fitted model Stan fit} +\item{model}{model name character string} + \item{input_measurement_data}{data used to fit the model with ind_id, y_obs, time, obs_index tibble} } \value{ From 0ac05a82d9f4759ace630dc71eb1583ab8a80324 Mon Sep 17 00:00:00 2001 From: Tess-LaCoil Date: Tue, 21 Jan 2025 11:31:31 +1100 Subject: [PATCH 3/5] In the extract_estimates function, extract model name from the fit object as it's part of the stan fit structure anyway. Also now includes the model name as an item in the returned estimates list. --- R/hmde_extract_estimates.R | 7 ++++--- vignettes/constant-growth.Rmd | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/R/hmde_extract_estimates.R b/R/hmde_extract_estimates.R index a89887a..53015fc 100644 --- a/R/hmde_extract_estimates.R +++ b/R/hmde_extract_estimates.R @@ -10,7 +10,6 @@ #' @importFrom stats quantile hmde_extract_estimates <- function(fit = NULL, - model = NULL, input_measurement_data = NULL){ #Check for fit if(is.null(fit)){ @@ -21,9 +20,11 @@ hmde_extract_estimates <- function(fit = NULL, stop("Fit not S4 stanfit type.") } + model <- fit@model_name #Check for model if(!model %in% hmde_model_names()){ - stop("Model name not recognised. Run hmde_model_names() to see available models.") + stop(paste0("Model name not recognised: ", model, + " Run hmde_model_names() to see available models.")) } #Check for input measurement data @@ -33,7 +34,7 @@ hmde_extract_estimates <- function(fit = NULL, } } - estimate_list <- list() + estimate_list <- list(model_name = model) par_names <- hmde_model_pars(model) if(grepl("multi", model)){ #Get n_ind for multi-individual diff --git a/vignettes/constant-growth.Rmd b/vignettes/constant-growth.Rmd index d2a1d32..61ec46a 100644 --- a/vignettes/constant-growth.Rmd +++ b/vignettes/constant-growth.Rmd @@ -215,7 +215,7 @@ trout_constant_fit <- hmde_model("constant_multi_ind") |> Once the model has finished running, we can extract the model estimates and have a look at the distribution of estimated sizes, estimated growth increments, and annualised growth rates at the level of sizes over time. ```{r} -trout_constant_estimates <- hmde_extract_estimates(model = "constant_multi_ind", +trout_constant_estimates <- hmde_extract_estimates( fit = trout_constant_fit, input_measurement_data = Trout_Size_Data) ``` From f6713d5892cf722bf53c8c37f2abb89af2231ca7 Mon Sep 17 00:00:00 2001 From: Tess-LaCoil Date: Tue, 21 Jan 2025 12:21:27 +1100 Subject: [PATCH 4/5] Updated calls of extract_estimates to not have the model name. --- inst/stan/affine_single_ind.stan | 4 ++-- man/hmde_extract_estimates.Rd | 6 +++--- tests/testthat/test-hmde_extract_estimates.R | 3 +-- tests/testthat/test-hmde_plot_de_pieces.R | 6 ++---- vignettes/canham.Rmd | 3 +-- vignettes/here_be_dragons.Rmd | 3 +-- vignettes/hmde_for_mathematicians.Rmd | 3 +-- vignettes/von-bertalanffy.Rmd | 3 +-- 8 files changed, 12 insertions(+), 19 deletions(-) diff --git a/inst/stan/affine_single_ind.stan b/inst/stan/affine_single_ind.stan index 04425fc..5e58c1b 100644 --- a/inst/stan/affine_single_ind.stan +++ b/inst/stan/affine_single_ind.stan @@ -66,8 +66,8 @@ data { real time[n_obs]; real y_bar; int int_method; - real prior_means[2]; #vector of means for beta parameter priors - real prior_sds[2]; #Vector of SDs for beta parameter priors + real prior_means[2]; //vector of means for beta parameter priors + real prior_sds[2]; //Vector of SDs for beta parameter priors } // The parameters accepted by the model. diff --git a/man/hmde_extract_estimates.Rd b/man/hmde_extract_estimates.Rd index 9005f09..040b010 100644 --- a/man/hmde_extract_estimates.Rd +++ b/man/hmde_extract_estimates.Rd @@ -4,14 +4,14 @@ \alias{hmde_extract_estimates} \title{Extract samples and return measurement, individual, and population-level estimates} \usage{ -hmde_extract_estimates(fit = NULL, model = NULL, input_measurement_data = NULL) +hmde_extract_estimates(fit = NULL, input_measurement_data = NULL) } \arguments{ \item{fit}{fitted model Stan fit} -\item{model}{model name character string} - \item{input_measurement_data}{data used to fit the model with ind_id, y_obs, time, obs_index tibble} + +\item{model}{model name character string} } \value{ named list with data frames for measurement, individual, population-level, and error parameter estimates diff --git a/tests/testthat/test-hmde_extract_estimates.R b/tests/testthat/test-hmde_extract_estimates.R index 1c47960..3f54d5f 100644 --- a/tests/testthat/test-hmde_extract_estimates.R +++ b/tests/testthat/test-hmde_extract_estimates.R @@ -11,8 +11,7 @@ test_that("Execution and output: extract_estimates function", { hmde_assign_data(data = data) |> hmde_run(chains = 1, iter = 20, cores = 1, verbose = FALSE, show_messages = FALSE) |> - hmde_extract_estimates(model = "constant_single_ind", - input_measurement_data = data) + hmde_extract_estimates(input_measurement_data = data) ) expect_named(output) diff --git a/tests/testthat/test-hmde_plot_de_pieces.R b/tests/testthat/test-hmde_plot_de_pieces.R index 9d7dfbd..d0de442 100644 --- a/tests/testthat/test-hmde_plot_de_pieces.R +++ b/tests/testthat/test-hmde_plot_de_pieces.R @@ -6,8 +6,7 @@ test_that("Execution and output: plot_de_pieces function", { verbose = FALSE, show_messages = FALSE) ) - output <- hmde_extract_estimates(model = "constant_multi_ind", - fit = multi_ind_trout, + output <- hmde_extract_estimates(fit = multi_ind_trout, input_measurement_data = Trout_Size_Data) plot <- hmde_plot_de_pieces(model = "constant_multi_ind", @@ -33,8 +32,7 @@ test_that("Execution and output: bad input", { verbose = FALSE, show_messages = FALSE) ) - output <- hmde_extract_estimates(model = "constant_multi_ind", - fit = multi_ind_trout, + output <- hmde_extract_estimates(fit = multi_ind_trout, input_measurement_data = Trout_Size_Data) expect_error( diff --git a/vignettes/canham.Rmd b/vignettes/canham.Rmd index 0f36d69..16b0301 100644 --- a/vignettes/canham.Rmd +++ b/vignettes/canham.Rmd @@ -83,8 +83,7 @@ tree_canham_fit <- hmde_model("canham_multi_ind") |> hmde_assign_data(data = Tree_Size_Data) |> hmde_run(chains = 4, cores = 4, iter = 2000) -tree_canham_estimates <- hmde_extract_estimates(model = "canham_multi_ind", - fit = tree_canham_fit, +tree_canham_estimates <- hmde_extract_estimates(fit = tree_canham_fit, input_measurement_data = Tree_Size_Data) #saveRDS(tree_canham_fit, "tree_canham_fit.rds") diff --git a/vignettes/here_be_dragons.Rmd b/vignettes/here_be_dragons.Rmd index 3086047..8fff756 100644 --- a/vignettes/here_be_dragons.Rmd +++ b/vignettes/here_be_dragons.Rmd @@ -167,8 +167,7 @@ for(i in 1:runs){ ) #Extract parameter estimates - ests <- hmde_extract_estimates(model = "affine_single_ind", - fit = fit, + ests <- hmde_extract_estimates(fit = fit, input_measurement_data = obs_data_frame) temp <- tibble( diff --git a/vignettes/hmde_for_mathematicians.Rmd b/vignettes/hmde_for_mathematicians.Rmd index 639c2b5..1b691c1 100644 --- a/vignettes/hmde_for_mathematicians.Rmd +++ b/vignettes/hmde_for_mathematicians.Rmd @@ -104,8 +104,7 @@ canham_multi_ind_fit <- hmde_model("canham_multi_ind") |> hmde_assign_data(data = Tree_Size_Data) |> hmde_run(chains = 1, cores = 1, iter = 1000) -Tree_Size_Ests <- hmde_extract_estimates(model = "canham_multi_ind", - fit = canham_multi_ind_fit, +Tree_Size_Ests <- hmde_extract_estimates(fit = canham_multi_ind_fit, input_measurement_data = Tree_Size_Data) ``` diff --git a/vignettes/von-bertalanffy.Rmd b/vignettes/von-bertalanffy.Rmd index 01a037e..9997f2e 100644 --- a/vignettes/von-bertalanffy.Rmd +++ b/vignettes/von-bertalanffy.Rmd @@ -100,8 +100,7 @@ lizard_vb_fit <- hmde_model("vb_multi_ind") |> hmde_assign_data(data = Lizard_Size_Data) |> hmde_run(chains = 4, cores = 1, iter = 2000) -lizard_estimates <- hmde_extract_estimates(model = "vb_multi_ind", - fit = lizard_vb_fit, +lizard_estimates <- hmde_extract_estimates(fit = lizard_vb_fit, input_measurement_data = Lizard_Size_Data) ``` From db3ad897d06afac0c25026793e99af305ab99270 Mon Sep 17 00:00:00 2001 From: Tess-LaCoil Date: Tue, 21 Jan 2025 12:38:05 +1100 Subject: [PATCH 5/5] Removed model name from args for extract_estimates function. --- R/hmde_extract_estimates.R | 1 - man/hmde_extract_estimates.Rd | 2 -- 2 files changed, 3 deletions(-) diff --git a/R/hmde_extract_estimates.R b/R/hmde_extract_estimates.R index 53015fc..df82d31 100644 --- a/R/hmde_extract_estimates.R +++ b/R/hmde_extract_estimates.R @@ -1,6 +1,5 @@ #' Extract samples and return measurement, individual, and population-level estimates #' -#' @param model model name character string #' @param fit fitted model Stan fit #' @param input_measurement_data data used to fit the model with ind_id, y_obs, time, obs_index tibble #' diff --git a/man/hmde_extract_estimates.Rd b/man/hmde_extract_estimates.Rd index 040b010..be77cc4 100644 --- a/man/hmde_extract_estimates.Rd +++ b/man/hmde_extract_estimates.Rd @@ -10,8 +10,6 @@ hmde_extract_estimates(fit = NULL, input_measurement_data = NULL) \item{fit}{fitted model Stan fit} \item{input_measurement_data}{data used to fit the model with ind_id, y_obs, time, obs_index tibble} - -\item{model}{model name character string} } \value{ named list with data frames for measurement, individual, population-level, and error parameter estimates