Skip to content

Commit 2fb895e

Browse files
author
David Lawrence Miller
committed
monotonicity now "on" by default for adjustment models
extra documentation and fixed tests for the above bug fix in check.bins -- missing $distance not caught in tests
1 parent 8fc4211 commit 2fb895e

4 files changed

Lines changed: 39 additions & 22 deletions

File tree

R/create.bins.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ create.bins <- function(data,cutpoints){
1616
cp <- cutpoints
1717

1818
# remove distances outside bins
19-
in.cp.ind <- data>=cp[1] & data<=cp[length(cp)]
19+
in.cp.ind <- data$distance>=cp[1] & data$distance<=cp[length(cp)]
20+
if(!all(in.cp.ind)){
21+
warning("Some distances were outside bins and have been removed.")
22+
}
2023
data <- data[in.cp.ind,]
2124

2225
# pull out the distances (removing the NAs for now)
@@ -33,6 +36,12 @@ create.bins <- function(data,cutpoints){
3336
distbegin[ind] <- cp[i]
3437
distend[ind] <- cp[i+1]
3538
}
39+
# last cutpoint, include those observations AT the truncation point
40+
ind <- which(d>=cp[i] & d<=cp[i+1])
41+
42+
distbegin[ind] <- cp[i]
43+
distend[ind] <- cp[i+1]
44+
3645

3746
# handle NA distances, that we need to preserve
3847
distbegin.na <- rep(NA,length(data$distance))

R/ds.R

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535
#' \code{distend} then these will be used as bins if \code{cutpoints}
3636
#' is not specified. If both are specified, \code{cutpoints} has
3737
#' precedence.
38-
#' @param monotonicity should the detection function be constrained for
39-
#' monotonicity weakly ("weak"), strictly ("strict") or not at all
40-
#' ("none" or \code{FALSE}). See Montonicity, below. (Default
41-
#' \code{FALSE}).
38+
#' @param monotonicity should the detection function be constrained for monotonicity weakly (\code{"weak"}), strictly (\code{"strict"}) or not at all (\code{"none"} or \code{FALSE}). See Montonicity, below. (Default \code{"strict"}).
4239
#' @param dht.group should density abundance estimates consider all groups to be
4340
#' size 1 (abundance of groups) \code{dht.group=TRUE} or should the
4441
#' abundance of individuals (group size is taken into account),
@@ -147,7 +144,7 @@
147144
#' library(Distance)
148145
#' data(book.tee.data)
149146
#' tee.data<-book.tee.data$book.tee.dataframe[book.tee.data$book.tee.dataframe$observer==1,]
150-
#' ds.model<-ds(tee.data,4,monotonicity="strict")
147+
#' ds.model<-ds(tee.data,4)
151148
#' summary(ds.model)
152149
#' plot(ds.model)
153150
#'
@@ -158,17 +155,20 @@
158155
#' samples<-book.tee.data$book.tee.samples
159156
#' obs<-book.tee.data$book.tee.obs
160157
#'
161-
#' ds.dht.model<-ds(tee.data,4,region.table=region,monotonicity="strict",
158+
#' ds.dht.model<-ds(tee.data,4,region.table=region,
162159
#' sample.table=samples,obs.table=obs)
163160
#' summary(ds.dht.model)
164161
#'
165162
#' # specify order 2 cosine adjustments
166163
#' ds.model.cos2<-ds(tee.data,4,adjustment="cos",order=2)
167164
#' summary(ds.model.cos2)
168165
#'
169-
#' # specify order 2 and 3 cosine adjustments - LOTS of non-monotonicity!
170-
#' ds.model.cos24<-ds(tee.data,4,adjustment="cos",order=c(2,3))
171-
#' summary(ds.model.cos24)
166+
#' # specify order 2 and 3 cosine adjustments, turning monotonicity
167+
#' # constraints off
168+
#' ds.model.cos24<-ds(tee.data,4,adjustment="cos",order=c(2,3),
169+
#' monotonicity=FALSE)
170+
#' # check for non-monotonicity -- actually no problems
171+
#' check.mono(ds.model.cos24$ddf,plot=TRUE,n.pts=100)
172172
#'
173173
#' # truncate the largest 10% of the data and fit only a hazard-rate
174174
#' # detection function
@@ -185,7 +185,7 @@ ds<-function(data, truncation=ifelse(is.null(cutpoints),
185185
formula=~1, key=c("hn","hr","unif"),
186186
adjustment=c("cos","herm","poly"),
187187
order=NULL, scale=c("width","scale"),
188-
cutpoints=NULL, monotonicity=FALSE, dht.group=FALSE,
188+
cutpoints=NULL, monotonicity="strict", dht.group=FALSE,
189189
region.table=NULL, sample.table=NULL, obs.table=NULL,
190190
convert.units=1, method="nlminb", quiet=FALSE, debug.level=0,
191191
initial.values=NULL){
@@ -488,6 +488,9 @@ ds<-function(data, truncation=ifelse(is.null(cutpoints),
488488
" with ", adj.name,"(",
489489
paste(order[1:i],collapse=","),
490490
") adjustments", sep="")
491+
}else{
492+
# if we have only the key function, turn off monotonicity
493+
meta.data$mono <- meta.data$mono.strict <- FALSE
491494
}
492495

493496
model.formula<-paste(model.formula,")",sep="")

man/ds.Rd

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ds(data, truncation = ifelse(is.null(cutpoints), ifelse(is.null(data$distend),
66
max(data$distance), max(data$distend)), max(cutpoints)),
77
transect = c("line", "point"), formula = ~1, key = c("hn", "hr",
88
"unif"), adjustment = c("cos", "herm", "poly"), order = NULL,
9-
scale = c("width", "scale"), cutpoints = NULL, monotonicity = FALSE,
9+
scale = c("width", "scale"), cutpoints = NULL, monotonicity = "strict",
1010
dht.group = FALSE, region.table = NULL, sample.table = NULL,
1111
obs.table = NULL, convert.units = 1, method = "nlminb", quiet = FALSE,
1212
debug.level = 0, initial.values = NULL)
@@ -75,9 +75,10 @@ ds(data, truncation = ifelse(is.null(cutpoints), ifelse(is.null(data$distend),
7575
\code{cutpoints} has precedence.}
7676

7777
\item{monotonicity}{should the detection function be
78-
constrained for monotonicity weakly ("weak"), strictly
79-
("strict") or not at all ("none" or \code{FALSE}). See
80-
Montonicity, below. (Default \code{FALSE}).}
78+
constrained for monotonicity weakly (\code{"weak"}),
79+
strictly (\code{"strict"}) or not at all (\code{"none"}
80+
or \code{FALSE}). See Montonicity, below. (Default
81+
\code{"strict"}).}
8182

8283
\item{dht.group}{should density abundance estimates
8384
consider all groups to be size 1 (abundance of groups)
@@ -261,7 +262,7 @@ using \code{ds()}.
261262
library(Distance)
262263
data(book.tee.data)
263264
tee.data<-book.tee.data$book.tee.dataframe[book.tee.data$book.tee.dataframe$observer==1,]
264-
ds.model<-ds(tee.data,4,monotonicity="strict")
265+
ds.model<-ds(tee.data,4)
265266
summary(ds.model)
266267
plot(ds.model)
267268
@@ -272,17 +273,20 @@ region<-book.tee.data$book.tee.region
272273
samples<-book.tee.data$book.tee.samples
273274
obs<-book.tee.data$book.tee.obs
274275
275-
ds.dht.model<-ds(tee.data,4,region.table=region,monotonicity="strict",
276+
ds.dht.model<-ds(tee.data,4,region.table=region,
276277
sample.table=samples,obs.table=obs)
277278
summary(ds.dht.model)
278279
279280
# specify order 2 cosine adjustments
280281
ds.model.cos2<-ds(tee.data,4,adjustment="cos",order=2)
281282
summary(ds.model.cos2)
282283
283-
# specify order 2 and 3 cosine adjustments - LOTS of non-monotonicity!
284-
ds.model.cos24<-ds(tee.data,4,adjustment="cos",order=c(2,3))
285-
summary(ds.model.cos24)
284+
# specify order 2 and 3 cosine adjustments, turning monotonicity
285+
# constraints off
286+
ds.model.cos24<-ds(tee.data,4,adjustment="cos",order=c(2,3),
287+
monotonicity=FALSE)
288+
# check for non-monotonicity -- actually no problems
289+
check.mono(ds.model.cos24$ddf,plot=TRUE,n.pts=100)
286290
287291
# truncate the largest 10\% of the data and fit only a hazard-rate
288292
# detection function

tests/testthat/test_ds.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test_that("Simple models work",{
4545

4646
# specify order 2 cosine adjustments
4747
ds.model.cos2<-ds(egdata,4,adjustment="cos",order=2, region.table=region,
48-
sample.table=samples,obs.table=obs)
48+
sample.table=samples,obs.table=obs,monotonicity=FALSE)
4949
# pars and lnl
5050
#result <- ddf(dsmodel=~mcds(key="hn", formula=~1, adj.series="cos",
5151
# adj.order=2), data=egdata, method="ds",
@@ -58,7 +58,8 @@ test_that("Simple models work",{
5858

5959
# specify order 2 and 4 cosine adjustments
6060
ds.model.cos24<-ds(egdata,4,adjustment="cos",order=c(2,4),
61-
region.table=region, sample.table=samples, obs.table=obs)
61+
region.table=region, sample.table=samples, obs.table=obs,
62+
monotonicity=FALSE)
6263
tp <- c(0.92121582, -0.03712634, -0.03495348)
6364
names(tp) <- c("X.Intercept.","V2","V3")
6465
expect_equal(ds.model.cos24$ddf$par, tp)

0 commit comments

Comments
 (0)