Skip to content

Commit df55f9d

Browse files
authored
Merge pull request #146 from DistanceDevelopment/mcds-dot-exe
`MCDS.exe` support for `Distance`
2 parents 59d0b2c + b55cacc commit df55f9d

14 files changed

Lines changed: 72 additions & 22 deletions

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ Description: A simple way of fitting detection functions to distance sampling
1111
Horvitz-Thompson-like estimator) if survey area information is provided. See
1212
Miller et al. (2019) <doi:10.18637/jss.v089.i01> for more information on
1313
methods and <https://examples.distancesampling.org/> for example analyses.
14-
Version: 1.0.7
14+
Version: 1.0.8
1515
URL: https://github.com/DistanceDevelopment/Distance/
1616
BugReports: https://github.com/DistanceDevelopment/Distance/issues
1717
Language: en-GB
1818
Depends:
1919
R (>= 3.5.0),
20-
mrds (>= 2.2.1)
20+
mrds (>= 2.2.9)
2121
Imports:
2222
dplyr,
2323
methods,
@@ -35,4 +35,4 @@ Suggests:
3535
readxl
3636
Encoding: UTF-8
3737
Roxygen: list(markdown = TRUE)
38-
RoxygenNote: 7.2.1
38+
RoxygenNote: 7.2.3

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ importFrom(dplyr,inner_join)
4848
importFrom(dplyr,left_join)
4949
importFrom(dplyr,mutate)
5050
importFrom(dplyr,mutate_if)
51+
importFrom(dplyr,reframe)
5152
importFrom(dplyr,select)
52-
importFrom(dplyr,summarize)
5353
importFrom(dplyr,summarize_all)
5454
importFrom(dplyr,ungroup)
5555
importFrom(dplyr,vars)

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Distance 1.0.8
2+
----------------------
3+
4+
* Support for using MCDS.exe from Distance for Windows to run analyses. You can now download MCDS.exe using mrds::download_MCDS_dot_exe run analyses using this engine, rather (or in tandem with) the usual R optimizers provided in mrds. ds will pick the best (by likelihood) and return that. See ?ds and ?"mcds-dot-exe" for more details.
5+
16
Distance 1.0.7
27
----------------------
38

R/dht2.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#' @importFrom stats qt na.omit predict terms var qnorm
5555
#' @importFrom dplyr group_by group_by_at mutate ungroup select distinct
5656
#' mutate_if if_else summarize_all "%>%" filter_at inner_join anti_join
57-
#' bind_rows left_join arrange vars summarize
57+
#' bind_rows left_join arrange vars
5858
#' @importFrom mrds DeltaMethod
5959
#' @section Data:
6060
#' The data format allows for complex stratification schemes to be set-up. Three

R/ds.R

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,21 @@
100100
#' used.
101101
#' @param max_adjustments maximum number of adjustments to try (default 5) only
102102
#' used when `order=NULL`.
103-
#' @param er_method encounter rate variance calculation: default = 2 gives the method of Innes et al, using expected counts in the encounter rate. Setting to 1 gives observed counts (which matches Distance for Windows) and 0 uses binomial variance (only useful in the rare situation where study area = surveyed area). See [`dht.se`][mrds::dht.se] for more details.
104-
#' @param dht_se should uncertainty be calculated when using `dht`? Safe to leave as `TRUE`, used in `bootdht`.
103+
#' @param er_method encounter rate variance calculation: default = 2 gives the
104+
#' method of Innes et al, using expected counts in the encounter rate. Setting
105+
#' to 1 gives observed counts (which matches Distance for Windows) and 0 uses
106+
#' binomial variance (only useful in the rare situation where study area =
107+
#' surveyed area). See [`dht.se`][mrds::dht.se] for more details.
108+
#' @param dht_se should uncertainty be calculated when using `dht`? Safe to
109+
#' leave as `TRUE`, used in `bootdht`.
110+
#' @param optimizer By default this is set to 'both'. In this case
111+
#' the R optimizer will be used and if present the MCDS optimizer will also
112+
#' be used. The result with the best likelihood value will be selected. To
113+
#' run only a specified optimizer set this value to either 'R' or 'MCDS'.
114+
#' See [`mcds_dot_exe`][mrds::mcds_dot_exe] for setup instructions.
115+
#' @param winebin If you are trying to use our MCDS.exe optimizer on a
116+
#' non-windows system then you may need to specify the winebin. Please
117+
#' see [`mcds_dot_exe`][mrds::mcds_dot_exe] for more details.
105118
#' @param dht.group deprecated, see same argument with underscore, above.
106119
#' @param region.table deprecated, see same argument with underscore, above.
107120
#' @param sample.table deprecated, see same argument with underscore, above.
@@ -314,6 +327,8 @@ ds <- function(data, truncation=ifelse(is.null(cutpoints),
314327
convert_units=1, er_var=ifelse(transect=="line", "R2", "P3"),
315328
method="nlminb", quiet=FALSE, debug_level=0,
316329
initial_values=NULL, max_adjustments=5, er_method=2, dht_se=TRUE,
330+
optimizer = "both",
331+
winebin = NULL,
317332
# deprecated below here:
318333
dht.group,
319334
region.table,
@@ -496,7 +511,8 @@ ds <- function(data, truncation=ifelse(is.null(cutpoints),
496511
}
497512

498513
# set up the control options
499-
control <- list(optimx.method=method, showit=debug_level)
514+
control <- list(optimx.method=method, showit=debug_level,
515+
optimizer = optimizer, winebin = winebin)
500516

501517
# if initial values were supplied, pass them on
502518
if(!is.null(initial_values) & !aic.search){
@@ -685,7 +701,7 @@ ds <- function(data, truncation=ifelse(is.null(cutpoints),
685701
} # end for() over adjustments
686702

687703
if(is.null(model)){
688-
stop("No models could be fitted.")
704+
stop("No models could be fitted.", call. = FALSE)
689705
}
690706

691707
# check that hazard models have a reasonable scale parameter
@@ -714,6 +730,10 @@ ds <- function(data, truncation=ifelse(is.null(cutpoints),
714730
if(any(is.na(model$hessian))){
715731
message("Some variance-covariance matrix elements were NA, possible numerical problems; only estimating detection function.\n")
716732
dht.res <- NULL
733+
# If hessian is NULL - with the exception of the unif with no adj
734+
}else if(is.null(model$hessian) && !(model$ds$aux$ddfobj$type == "unif" && is.null(model$ds$aux$ddfobj$adjustment))){
735+
message("No hessian, possible numerical problems; only estimating detection function.\n")
736+
dht.res <- NULL
717737
}else{
718738
dht.res <- dht(model, region_table, sample_table,
719739
options=dht_options, se=dht_se)
@@ -733,6 +753,10 @@ ds <- function(data, truncation=ifelse(is.null(cutpoints),
733753
if(any(is.na(model$hessian))){
734754
message("Some variance-covariance matrix elements were NA, possible numerical problems; only estimating detection function.\n")
735755
dht.res <- NULL
756+
# If hessian is NULL - with the exception of the unif with no adj
757+
}else if(is.null(model$hessian) && !(model$ds$aux$ddfobj$type == "unif" && is.null(model$ds$aux$ddfobj$adjustment))){
758+
message("No hessian, possible numerical problems; only estimating detection function.\n")
759+
dht.res <- NULL
736760
}else{
737761
dht.res <- dht(model, region_table, sample_table, obs_table,
738762
options=dht_options, se=dht_se)

R/dummy_ddf.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ dummy_ddf <- function(data, width, left=0, transect="line"){
5757
#' @param object model object
5858
#' @param newdata how many 1s should we return?
5959
#' @param compute unused, compatibility with [`mrds::predict`][mrds::predict]
60-
#' @param int.range unused, compatability with [`mrds::predict`][mrds::predict]
60+
#' @param int.range unused, compatibility with [`mrds::predict`][mrds::predict]
6161
#' @param esw should the strip width be returned?
6262
#' @param \dots for S3 consistency
6363
#' @author David L Miller

R/make_activity_fn.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#' passed to `bootdht`. It is recommended that you try out the function before
1010
#' passing it to [`bootdht`]. See examples for a template for use.
1111
#'
12-
#' @inheritParams activity::fitact
1312
#' @param \dots parameters specified by activity::fitact
1413
#' @param detector_daily_duration by default we assume that detectors were able to detect animals for 24 hours, if they were only able to do this for some proportion of the day (say daylight hours), then adjust this argument accordingly
1514
#' @return a function which generates a single bootstrap estimate of

R/print.summary.dsmodel.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ print.summary.dsmodel <- function (x,...){
4242
cat("Number of observations : ", x$n, "\n")
4343
cat("Distance range : ", x$left, " - ", x$width, "\n")
4444

45-
cat("\nModel :", model.description(model), "\n")
45+
cat("\nModel :", model.description(model), "\n")
4646
# Remind the user that monotonicity constraints were enforced
4747
if(x$mono & x$mono.strict){
4848
cat("\nStrict monotonicity constraints were enforced.\n")
4949
}else if(x$mono){
5050
cat("\nMonotonicity constraints were enforced.\n")
5151
}
52-
cat("AIC :", x$aic, "\n")
52+
cat("AIC : ", x$aic, "\n")
53+
cat("Optimisation: ", x$optimise, "\n")
5354

5455
# parameter summaries
5556
cat("\nDetection function parameters\n")

R/varNhat.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Calculate the variance contribution of the detection function
22
# to abundance estimates
3+
#' @importFrom dplyr reframe
34
varNhat <- function(data, model){
45

56
# format the data
@@ -18,7 +19,7 @@ varNhat <- function(data, model){
1819
grp_dat <- data %>%
1920
select("Covered_area", "Area", "Sample.Label", !!strat_vars) %>%
2021
distinct() %>%
21-
summarize(Covered_area = sum(.data$Covered_area),
22+
reframe(Covered_area = sum(.data$Covered_area),
2223
Area = .data$Area) %>%
2324
distinct()
2425

@@ -38,7 +39,7 @@ varNhat <- function(data, model){
3839

3940
res <- data %>%
4041
mutate(Nc = (.data$size/.data$p)/.data$rate) %>%
41-
summarize(N = (.data$Area/.data$Covered_area) *
42+
reframe(N = (.data$Area/.data$Covered_area) *
4243
sum(.data$Nc, na.rm=TRUE)) %>%
4344
distinct()
4445

man/ds.Rd

Lines changed: 19 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)