From d6c7c5f2f70a9d7a93a92747d53acb3fc73ce7dd Mon Sep 17 00:00:00 2001 From: Laura Marshall Date: Wed, 19 Mar 2025 11:22:29 +0000 Subject: [PATCH 1/3] Fixes #113 --- DESCRIPTION | 2 +- NEWS.md | 1 + R/ddf.ds.R | 15 +++++++++++++++ R/mcds_tools.R | 23 ++++++++++++++++++++++- 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9709ad72..f4773867 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,7 @@ Description: Animal abundance estimation via conventional, multiple covariate fitting is performed via maximum likelihood. Also included are diagnostics and plotting for fitted detection functions. Abundance estimation is via a Horvitz-Thompson-like estimator. -Version: 3.0.0.9001 +Version: 3.0.0.9003 URL: https://github.com/DistanceDevelopment/mrds/ BugReports: https://github.com/DistanceDevelopment/mrds/issues Depends: diff --git a/NEWS.md b/NEWS.md index 231f16b3..c4e45f95 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,7 @@ Bug Fixes * Fixed formatting issue in flnl.grad help +* Now displays a warning if the user tries to fit a detection function with covariates using MCDS.exe which is not either a half-normal or a hazard rate model. (Issue #113) # mrds 3.0.0 diff --git a/R/ddf.ds.R b/R/ddf.ds.R index 47974550..cb5278bc 100644 --- a/R/ddf.ds.R +++ b/R/ddf.ds.R @@ -224,6 +224,21 @@ ddf.ds <-function(dsmodel, mrmodel = NULL, if(control$optimizer == "MCDS" && system.file("MCDS.exe", package="mrds") == ""){ stop("You have chosen to use the MCDS.exe optimizer but it cannot be found!", call. = FALSE) } + + # Validate options are suitable for MCDS + if(control$optimizer %in% c("MCDS","both") && system.file("MCDS.exe", package="mrds") != ""){ + MCDS.options.valid <- validate.MCDS.options(model) + # If NULL there are no issues + if(!is.null(MCDS.options.valid)){ + R.opt.message <- ifelse(control$optimizer == "MCDS", + "The R optimizer will be used instead.", + "Only the R optimizer will be used.") + # Otherwise there are issues so use the R optimizer + warning(paste(MCDS.options.valid, R.opt.message, sep = " "), + immediate. = TRUE, call. = FALSE) + control$optimizer <- "R" + } + } # run MCDS.exe if it's there if(control$optimizer %in% c("MCDS","both") && system.file("MCDS.exe", package="mrds")!=""){ diff --git a/R/mcds_tools.R b/R/mcds_tools.R index 5895d0b2..8d5af1e1 100644 --- a/R/mcds_tools.R +++ b/R/mcds_tools.R @@ -253,7 +253,7 @@ create.command.file <- function(dsmodel=call(), data, cat("HAZARD", file=command.file.name, append=TRUE) }else if(mod.vals$key == "unif"){ cat("UNIFORM", file=command.file.name, append=TRUE) - }else{ + }else if(mod.vals$key == "gamma"){ cat("NEXPON", file=command.file.name, append=TRUE) } @@ -639,3 +639,24 @@ create.data.file <- function(data, dsmodel, data.file){ cluster = cluster) return(output) } + + +validate.MCDS.options <- function(model){ + # Validates the options are suitable for MCDS + + # Extract values from the formula + mod_paste <- paste(model) + mod.vals <- try(eval(parse(text=mod_paste[2:length(mod_paste)]))) + + # Check if there are covariates + if(!is.null(mod.vals$formula)){ + # if so only HN and HR permitted + if(!mod.vals$key %in% c("hn", "hr")){ + return("MCDS can only fit covariates with the half-normal and hazard rate key functions.") + } + } + + # If no issue return NULL + return(NULL) +} + From c9f21a6b766836d7a7cefd225aed2058806d7405 Mon Sep 17 00:00:00 2001 From: Laura Marshall Date: Wed, 19 Mar 2025 13:27:29 +0000 Subject: [PATCH 2/3] Fixed check for model formula --- R/mcds_tools.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/mcds_tools.R b/R/mcds_tools.R index 8d5af1e1..14b16434 100644 --- a/R/mcds_tools.R +++ b/R/mcds_tools.R @@ -649,7 +649,7 @@ validate.MCDS.options <- function(model){ mod.vals <- try(eval(parse(text=mod_paste[2:length(mod_paste)]))) # Check if there are covariates - if(!is.null(mod.vals$formula)){ + if(mod.vals$formula != ~1){ # if so only HN and HR permitted if(!mod.vals$key %in% c("hn", "hr")){ return("MCDS can only fit covariates with the half-normal and hazard rate key functions.") From 32bbdf6785b22725288398eebff41979ac3509be Mon Sep 17 00:00:00 2001 From: Laura Marshall Date: Sun, 23 Mar 2025 11:52:47 +0000 Subject: [PATCH 3/3] Do not fit a negative exponential model in place of a gamma Reference issue #113 --- DESCRIPTION | 2 +- NEWS.md | 1 + R/mcds_tools.R | 11 ++++++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f4773867..9cfb92b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,7 @@ Description: Animal abundance estimation via conventional, multiple covariate fitting is performed via maximum likelihood. Also included are diagnostics and plotting for fitted detection functions. Abundance estimation is via a Horvitz-Thompson-like estimator. -Version: 3.0.0.9003 +Version: 3.0.0.9004 URL: https://github.com/DistanceDevelopment/mrds/ BugReports: https://github.com/DistanceDevelopment/mrds/issues Depends: diff --git a/NEWS.md b/NEWS.md index c4e45f95..7c632173 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ Bug Fixes * Fixed formatting issue in flnl.grad help * Now displays a warning if the user tries to fit a detection function with covariates using MCDS.exe which is not either a half-normal or a hazard rate model. (Issue #113) +* Fixed so that the MCDS.exe does not try to fit a negative exponential in place of a gamme key function. (Issue #113) # mrds 3.0.0 diff --git a/R/mcds_tools.R b/R/mcds_tools.R index 14b16434..628783af 100644 --- a/R/mcds_tools.R +++ b/R/mcds_tools.R @@ -253,8 +253,8 @@ create.command.file <- function(dsmodel=call(), data, cat("HAZARD", file=command.file.name, append=TRUE) }else if(mod.vals$key == "unif"){ cat("UNIFORM", file=command.file.name, append=TRUE) - }else if(mod.vals$key == "gamma"){ - cat("NEXPON", file=command.file.name, append=TRUE) + }else{ + stop("Unrecognised key function for the detection function in MCDS.exe", call. = FALSE) } adj.pres <- FALSE @@ -648,11 +648,16 @@ validate.MCDS.options <- function(model){ mod_paste <- paste(model) mod.vals <- try(eval(parse(text=mod_paste[2:length(mod_paste)]))) + # Check if the user is trying to fit a gamma model + if(mod.vals$key == "gamma"){ + return("MCDS.exe cannot fit a gamma detection function model.") + } + # Check if there are covariates if(mod.vals$formula != ~1){ # if so only HN and HR permitted if(!mod.vals$key %in% c("hn", "hr")){ - return("MCDS can only fit covariates with the half-normal and hazard rate key functions.") + return("MCDS.exe can only fit covariates with the half-normal and hazard rate key functions.") } }