DistanceDevelopment / Distance

Simple distance sampling analysis
GNU General Public License v3.0
9 stars 8 forks source link

Difficulties fitting uniform detection function with no adjustments #130

Closed lenthomas closed 1 year ago

lenthomas commented 2 years ago

This may be related to #129.

I tried to see if I could recreate the problem in #129 with the book.tee data. I tried

library(Distance)
data(book.tee.data)
tee.data <- subset(book.tee.data$book.tee.dataframe, observer==1)
ds.model <- ds(tee.data, 4,  key = "unif", nadj = 0)
summary(ds.model)

but the line ds.model <- ds(tee.data, 4, key = "unif", nadj = 0) reported Fitting uniform key function with cosine(1,0) adjustments which made me suspicious of what was being fitted, and the line summary(ds.model) gave me Error in t(partial) %*% vcov : non-conformable arguments. Using nadj = 0 works for hn key function.

I saw in the help under the adjustment argument that A value of NULL indicates that no adjustments are to be fitted. so tried

ds.model <- ds(tee.data, 4,  key = "unif", adj = NULL)

This seems to get it to fit the right function, but then there is an error message:

Fitting uniform key function
Key only model: not constraining for monotonicity.
Error in abs(x) : non-numeric argument to mathematical function

All models failed to fit!

Error in ds(tee.data, 4, key = "unif", adj = NULL) : 
  No models could be fitted.

(apologies if these are two separate issues).

lenthomas commented 2 years ago

Forgot to say: Distance_1.0.6, mrds_2.2.7.

LHMarshall commented 2 years ago

Hmm when I fit to uniform data and ask for no adjustments it doesn't display a summary and has definately fitted something other than a uniform with no adjustments.

library(Distance)
set.seed(747) #windows
ddata <- data.frame(object = 1:100,
                    distance = runif(100,0,1500))
> model <- ds(data = ddata,
+             key = "unif",
+             nadj = 0,
+             truncation = "10%")
Fitting uniform key function with cosine(1,0) adjustments
AIC= 335.973
No survey area information supplied, only estimating detection function.

> 
> summary(model)
>
> plot(model)

image

LHMarshall commented 2 years ago

Now working for first problematic model

> data(book.tee.data)
> tee.data <- subset(book.tee.data$book.tee.dataframe, observer==1)
> ds.model <- ds(tee.data, 4,  key = "unif", nadj = 0)
Fitting uniform key function
AIC= 343.801
No survey area information supplied, only estimating detection function.

> summary(ds.model)

Summary for distance analysis 
Number of observations :  124 
Distance range         :  0  -  4 

Model : Uniform key function 
AIC   : 343.801 

Detection function parameters
Scale coefficient(s):  
NULL

                    Estimate
Average p                  1
N in covered region      124
> plot(ds.model)

image

LHMarshall commented 2 years ago

The following model

ds.model <- ds(tee.data, 4,  key = "unif", adj = NULL)

is failing because mono and mono.strict are set to TRUE in the call to ddf when using this call to ds.

According to the comment these lines of code should turn off mono and mono.strict when it is a key only model these cases aren't caught (same for hn and hr only models).

# turn-off monotonicity if we have a key only model
    if(i==0){
      mono.save <- meta.data$mono
      mono.strict.save <- meta.data$mono.strict
      meta.data$mono <- FALSE
      meta.data$mono.strict <- FALSE
    } 

Seems like the check should be for i = 1 then the following model fits:

ds.model <- ds(tee.data, 4,  key = "unif", adj = NULL)

However, uniform with automatic selection of adjustments fails

> ds.model <- ds(tee.data, 4,  key = "unif")
Starting AIC adjustment term selection.
Fitting uniform key function
Key only model: not constraining for monotonicity.
Error in abs(x) : non-numeric argument to mathematical function

All models failed to fit!

Error in ds(tee.data, 4, key = "unif") : No models could be fitted.

AH ha!

check should be for i = 0 but i should be set to 0 when adj = NULL (ds line 528)

Hmm one of the tests in Distance package is failing for wren data:

✖ | 1      15 | wrens [2.1s]                           
───────────────────────────────────────────────────────
Failure (test_wrens.R:20:3): wren 5 minute counts works
attr(w1_nhat, "density")$UCI not equal to 2.1077.
1/1 mismatches
[1] 2.08 - 2.11 == -0.0285

Reverting back to master branch versions of both packages - the UCI is fine but the LCI is off a bit

✖ | 1      15 | wrens [2.1s]                           
───────────────────────────────────────────────────────
Failure (test_wrens.R:20:3): wren 5 minute counts works
attr(w1_nhat, "density")$LCI not equal to 0.79504.
1/1 mismatches
[1] 0.785 - 0.795 == -0.0105
───────────────────────────────────────────────────────

Change in failure is down to the update to mrds (update to Distance doesn't change anything)