Skip to content

Commit

Permalink
summarize_ds_models now will only compare models that are allowed by …
Browse files Browse the repository at this point in the history
…AIC (all binning and truncation must be the same). Thanks to Carolin Tröger and Eric Rextad for highlighting this issue.
  • Loading branch information
David Lawrence Miller committed Apr 28, 2017
1 parent c346b2f commit 52eb627
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 37 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Depends:
mrds (>= 2.1.15)
Suggests:
testthat
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Distance version 0.9.7
* summarize_ds_models now will only compare models that are allowed by AIC (all binning and truncation must be the same). Thanks to Carolin Tröger and Eric Rextad for highlighting this issue.
* If there are numerical issues that cause NAs in the Hessian, ds() will not try to run dht() to estimate abundance (as it will fail), instead throws a message and returns only the detection function. Thanks to Steve Ahlswede for bringing this to our attention.

Distance version 0.9.6
Expand Down
55 changes: 51 additions & 4 deletions R/summarize_ds_models.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Make a table of summary statistics for detection function models
#'
#' Provide a summary table of useful information about fitted detection functions. This can be useful when paired with \code{knitr}s \code{kable} function.
#' Provide a summary table of useful information about fitted detection functions. This can be useful when paired with \code{knitr}s \code{kable} function. By default models are sorted by AIC and will therefore not allow models with different truncations and distance binning.
#'
#' Note that the column names are in LaTeX format, so if you plan to manipulate the resulting \code{data.frame} in R, you may wish to rename the columns for ease of access.
#'
Expand All @@ -14,10 +14,42 @@ summarize_ds_models <- function(..., sort="AIC", output="latex", delta_only=TRUE

# get the models
models <- list(...)

# get the model names
model_names <- setdiff(as.character(match.call(expand.dots=TRUE)),
as.character(match.call(expand.dots=FALSE)))


## checking
# can't compare models with different truncations
r_truncs <- unlist(lapply(models, function(x) x$ddf$meta.data$width))
l_truncs <- unlist(lapply(models, function(x) x$ddf$meta.data$left))
if(!all(abs(c(r_truncs-mean(r_truncs),
l_truncs-mean(l_truncs))) < 1e-8)){
stop("All truncations must be the same for AIC comparison.")
}
# check all binned
binned <- unlist(lapply(models, function(x) x$ddf$meta.data$binned))
if((any(binned) & !all(binned)) | (any(!binned) & !all(!binned))){
stop("Can't compare binned and unbinned distances")
}
# check all binning is the same
if(all(binned)){
breaks <- lapply(models, function(x) x$ddf$meta.data$breaks)
# if the breaks aren't the same length it's easy
len_breaks <- unlist(lapply(breaks, length))
if(!all(abs(len_breaks-mean(len_breaks)) < 1e-8)){
stop("Distance binning must be the same for all models.")
}
# if not??? (WARNING: Byzantine process :( )
for(i in seq_along(breaks[[1]])){
this_set <- unlist(lapply(breaks, "[[", i))
if(!all(abs(this_set-mean(this_set)) < 1e-8)){
stop("Distance binning must be the same for all models.")
}
}
}

# this function extracts the model data for a single model (row)
extract_model_data <- function(model){
summ <- summary(model)
Expand All @@ -27,9 +59,15 @@ summarize_ds_models <- function(..., sort="AIC", output="latex", delta_only=TRUE
if(is.null(formula)) formula <- NA

desc <- gsub(" key function","",model.description(model$ddf))
# only get CvM if not binned
if(model$ddf$meta.data$binned){
gof <- NA
}else{
gof <- ddf.gof(model$ddf, qq=FALSE)$dsgof$CvM$p
}
ret <- c(desc,
formula,
ddf.gof(model$ddf, qq=FALSE)$dsgof$CvM$p,
gof,
summ$ds$average.p,
summ$ds$average.p.se,
model$ddf$criterion
Expand All @@ -54,20 +92,29 @@ summarize_ds_models <- function(..., sort="AIC", output="latex", delta_only=TRUE
# making sure the correct columns are numeric
res[,4:7] <- apply(res[,4:7], 2, as.numeric)

# what test did we do?
if(all(binned)){
gof_name <- "Chi^2 p-value"
gof_latexname <- "$\\chi^2$ $p$-value"
}else{
gof_name <- "C-vM $p$-value"
gof_latexname <- "C-vM p-value"
}

# giving the columns names
if(output == "latex"){
colnames(res) <- c("Model",
"Key function",
"Formula",
"C-vM $p$-value",
gof_latexname,
"$\\hat{P_a}$",
"se($\\hat{P_a}$)",
"AIC")
}else if(output=="plain"){
colnames(res) <- c("Model",
"Key function",
"Formula",
"C-vM p-value",
gof_name,
"Average detectability",
"se(Average detectability)",
"AIC")
Expand Down
5 changes: 2 additions & 3 deletions man/AIC.dsmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions man/Distance-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/amakihi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/checkdata.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/create.bins.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/ds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions man/ds.gof.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/flatfile.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/gof_ds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/logLik.dsmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/minke.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/plot.dsmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/print.dsmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions man/print.summary.dsmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/summarize_ds_models.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion man/summary.dsmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 52eb627

Please sign in to comment.