DistanceDevelopment / MCDS_mrds_compare

1 stars 0 forks source link

key functions used in `test.models` function #1

Closed erex closed 1 year ago

erex commented 1 year ago

@LHMarshall Looking at your report from this morning, I noticed that the log-likelihoods with 0 adjustments were identical regardless of the key function used. Also we noticed this morning that P_a for uniform without adjustments should be 1, but they were not.

Looking at your test.models function, I did not see the argument key provided for any of the calls to ds (whether it be the creation of fit_R, fit_MCDS or fit_both). The result is that the default argument for key is used in all calls; the default being hn.

I checked this by debugging test.models for the capercaille data set. Stopping after the first model is fitted (which should be the uniform without adjustments), I asked to see the summary of the fitted model:

Browse[2]> summary(fit_R)

Summary for distance analysis 
Number of observations :  112 
Distance range         :  0  -  80 

Model       : Half-normal key function 
AIC         :  957.9051 
Optimisation:  mrds (nlminb) 

Sure enough, the half normal key was fitted, even though it should have been the uniform. Because of this, P_a is not equal to 1, because it is not the uniform being fitted.

lenthomas commented 1 year ago

I created a branch, had a go at fixing it, and created a pull request -- good practice for me! @LHMarshall over to you to pull if you want!

lenthomas commented 1 year ago

I note, BTW, that the cue counting example produces a strange result for the first line of output:

data("CueCountingExample")
model.compare <- test.models(CueCountingExample, 
                             truncation = max(CueCountingExample$distance), 
                             transect = "point")
model.compare

gives

    key  adj nadj      lnl_R lnl_MCDS     optimizer  p_R p_MCDS Nhat_R Nhat_MCDS
1  unif  cos    0 -18.110662 1.916333      MCDS.exe 1.00   0.24  40.00    167.61
2  unif  cos    1   1.933002 2.109789 mrds (nlminb) 0.31   0.30 128.30    132.27
3  unif  cos    2   2.377847 2.132096      MCDS.exe 0.25   0.28 160.47    144.34
4  unif  cos    3   2.569998 2.512145 mrds (nlminb) 0.32   0.25 124.64    161.83
5    hn  cos    0   1.916333 1.916333      MCDS.exe 0.24   0.24 167.61    167.61
6    hn  cos    1   2.109706 2.109789 mrds (nlminb) 0.30   0.30 132.40    132.27
7    hn  cos    2   2.132097 2.132096      MCDS.exe 0.28   0.28 144.42    144.34
8    hn herm    0   1.916333 1.916333      MCDS.exe 0.24   0.24 167.61    167.61
9    hn herm    1   2.016928 1.925329      MCDS.exe 0.28   0.24 145.05    165.77
10   hn herm    2   2.016971 3.876089 mrds (nlminb) 0.28   0.28 145.04    142.45
11   hr poly    0   1.381404 1.916333      MCDS.exe 0.28   0.24 142.27    167.61
12   hr poly    1   1.701666 1.930192      MCDS.exe 0.27   0.24 145.93    164.58
13   hr poly    2   2.130225 3.772227      MCDS.exe 0.18   0.32 224.87    126.38

where, on the first line, the lnl_R seems very different from the lnl_MCDS and quite implausible! I note also that Nhat_R on the first line is just returning the number of observations, and not trying to account for the area surveyed compared with the size of the study area (so far as I can see).

erex commented 1 year ago

The addition of a key argument in the first call to ds in test.models in this commit is helpful, but recognise there are two more calls to ds that also need to have the key argument added.

lenthomas commented 1 year ago

My total bad - guess that's why no-one pays me to code. Fixed the other two and re-issued pull request (and results seem better now).

cue counting example again: Screenshot 2023-05-04 000931