From 251d6e3d8c01245a2d147165c483ec52c48fb2aa Mon Sep 17 00:00:00 2001 From: Andrea-Havron-NOAA Date: Fri, 31 May 2024 16:14:33 -0700 Subject: [PATCH] fix bugs --- .../distributions/functors/density_components_base.hpp | 3 --- inst/include/distributions/functors/lognormal_lpdf.hpp | 5 ++++- inst/include/distributions/functors/multinomial_lpmf.hpp | 6 +++++- inst/include/distributions/functors/normal_lpdf.hpp | 6 ++++++ 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/inst/include/distributions/functors/density_components_base.hpp b/inst/include/distributions/functors/density_components_base.hpp index d52946922..c441c4497 100644 --- a/inst/include/distributions/functors/density_components_base.hpp +++ b/inst/include/distributions/functors/density_components_base.hpp @@ -39,9 +39,6 @@ struct DensityComponentBase : public fims_model_object::FIMSObject { std::string nll_type; //prior, re, data bool osa_flag = false; bool simulate_flag = false; - #ifdef TMB_MODEL - ::objective_function *of; - #endif /** @brief Constructor. */ diff --git a/inst/include/distributions/functors/lognormal_lpdf.hpp b/inst/include/distributions/functors/lognormal_lpdf.hpp index f19bd4882..dc8f7904b 100644 --- a/inst/include/distributions/functors/lognormal_lpdf.hpp +++ b/inst/include/distributions/functors/lognormal_lpdf.hpp @@ -1,4 +1,4 @@ -#ifndef LOGNORMALl_LPDF +#ifndef LOGNORMAL_LPDF #define LOGNORMAL_LPDF #include "density_components_base.hpp" @@ -55,8 +55,10 @@ namespace fims_distributions for (size_t i = 0; i < this->observed_values.size(); i++) { if(!is_na[i]){ + #ifdef TMB_MODEL // this->nll_vec[i] = this->keep[i] * -dnorm(this->observed_values[i], mu[i], sd[i], do_log); this->nll_vec[i] = -dnorm(log(this->observed_values[i]), mu[i], sd[i], do_log) - log(this->observed_values[i]); + nll += this->nll_vec[i]; if (this->simulate_flag) { @@ -66,6 +68,7 @@ namespace fims_distributions this->observed_values[i] = exp(rnorm(mu[i], sd[i])); } } + #endif /* osa not working yet if(osa_flag){//data observation type implements osa residuals diff --git a/inst/include/distributions/functors/multinomial_lpmf.hpp b/inst/include/distributions/functors/multinomial_lpmf.hpp index e20f4a366..641965adb 100644 --- a/inst/include/distributions/functors/multinomial_lpmf.hpp +++ b/inst/include/distributions/functors/multinomial_lpmf.hpp @@ -16,6 +16,9 @@ namespace fims_distributions Type nll = 0.0; fims::Vector dims; std::vector is_na; + #ifdef TMB_MODEL + ::objective_function *of; + #endif // data_indicator , Type> keep; MultinomialLPMF() : DensityComponentBase() @@ -42,7 +45,7 @@ namespace fims_distributions } #ifdef TMB_model this->nll_vec[i] = -dmultinom((vector)observed_vector, (vector)expected_vector, do_log); - #endif + nll += this->nll_vec[i]; /* if (this->simulate_flag) @@ -61,6 +64,7 @@ namespace fims_distributions } } */ + #endif } } #ifdef TMB_MODEL diff --git a/inst/include/distributions/functors/normal_lpdf.hpp b/inst/include/distributions/functors/normal_lpdf.hpp index a527514b3..17b048274 100644 --- a/inst/include/distributions/functors/normal_lpdf.hpp +++ b/inst/include/distributions/functors/normal_lpdf.hpp @@ -16,6 +16,9 @@ struct NormalLPDF : public DensityComponentBase { fims::Vector sd; Type nll = 0.0; std::vector is_na; + #ifdef TMB_MODEL + ::objective_function *of; + #endif //data_indicator , Type> keep; NormalLPDF() : DensityComponentBase() { @@ -43,13 +46,16 @@ struct NormalLPDF : public DensityComponentBase { for(size_t i=0; iobserved_values.size(); i++){ if(!is_na[i]){ // this->nll_vec[i] = this->keep[i] * -dnorm(this->observed_values[i], mu[i], sd[i], do_log); + #ifdef TMB_MODEL this->nll_vec[i] = -dnorm(this->observed_values[i], mu[i], sd[i], do_log); + nll += this->nll_vec[i]; if(this->simulate_flag){ FIMS_SIMULATE_F(this->of){ this->observed_values[i] = rnorm(mu[i], sd[i]); } } + #endif /* osa not working yet if(osa_flag){//data observation type implements osa residuals //code for osa cdf method