diff --git a/DESCRIPTION b/DESCRIPTION index 9709ad72..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.9001 +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 231f16b3..7c632173 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,8 @@ 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/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..628783af 100644 --- a/R/mcds_tools.R +++ b/R/mcds_tools.R @@ -254,7 +254,7 @@ create.command.file <- function(dsmodel=call(), data, }else if(mod.vals$key == "unif"){ cat("UNIFORM", file=command.file.name, append=TRUE) }else{ - cat("NEXPON", file=command.file.name, append=TRUE) + stop("Unrecognised key function for the detection function in MCDS.exe", call. = FALSE) } adj.pres <- FALSE @@ -639,3 +639,29 @@ 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 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.exe can only fit covariates with the half-normal and hazard rate key functions.") + } + } + + # If no issue return NULL + return(NULL) +} +