derived() function in openCR

150 views
Skip to first unread message

jlbr...@ucdavis.edu

unread,
Jul 26, 2018, 3:33:30 AM7/26/18
to secr
Hello, 

I think I am having an issue with calling derived() from the openCR package.  I had experienced a suite of issues using the function on different models, and after trying a bunch of different things to resolve the problems, and exploring the source codes, I think they all stemmed from an inability to properly call the right derived() function.  This is what happens when I try to specify the package for use of derived() on an openCR model: 

> openCR::derived(test)
Error: 'derived' is not an exported object from 'namespace:openCR'

> secr::derived(test)
Total number observed 191 
Parameters in model p, phi, f, superN 
Superpopulation size 297.5006 
Session-specific counts and estimates:

 session sex t  n  R  m  r  z time      p    phi         f superN         b lambda  gamma  kappa     N
       1   F 1 63 63  0 29  0    0 0.1852 0.9661 3.997e-15  297.5 7.605e-01 0.9661     NA     NA 226.3
       2   F 2 80 80 18 38 11   10 0.1852 0.9661 2.838e-01     NA 2.247e-14 1.2499 1.0000 0.7872 160.2
       3   F 3 70 70 33 17 16   11 0.1852 0.9661 9.992e-16     NA 2.395e-01 0.9661 0.7729 0.8939 226.0
       4   F 4 62 62 33  0  0   20 0.1852     NA        NA     NA 7.017e-15     NA 1.0000 0.7036 165.6

This is confusing to me, as the output using secr::derived() looks like it is appropriate for an openCR model, but with some bugs, described below.  I have tried several times to uninstall and reinstall openCR and Rcpp (I thought the issue might be with the makegkcpp function), and have explored the source codes, but have been unable to resolve the issue. I was able to replicate results for derived() from the openCR source codes for the JSSAsecrbCL model type, as derived() doesn't work at all for those model types, but the openCR.esa() function did not work properly either, so I got stalled when trying to derive D: 

>openCR.esa(fitsecrb[[1]])
Error in makegkcpp(as.integer(nrow(realparval0)), as.integer(k), as.integer(m),  : 
  Not a matrix.

Below is a description of some errors that occurred when I use derived() on openCR models:

It seems a little buggy when used on spatial openCR models (you need to call the estimates directly from the output list to see them, the last kappa doesn't get estimated though it does for non-spatial models, and it doesn't work with JSSAsecrbCL models - it produces the following error: 

> derived(fitsecrb[[1]])
Error in makegkcpp(as.integer(nrow(realparval0)), as.integer(k), as.integer(m),  : 
  Not a matrix.).  

It seems to have less obvious issues with non-spatial models, but upon closer examination of the results, there might be issues there too. I have fit a JSSAf model type using the following parameterization: 
p~sex, phi~sex, f~t+sex, N~t  

and also used RMark to fit a similar type of model:
p~sex, phi~sex, f~t+sex, f0~t (RMark RDPdfcClosed, which uses the Pradel formulation instead)

The outputs from openCR, both real and derived are below, followed by the results from RMark. I understand that both phi and f are scaled for the user-specified time intervals (for these results, 10, 1, and 9 for primary sessions).

But why is phi the same using the predict() and derived() functions, but f is not? It looks like f is somehow scaled incorrectly, as it is the same in the second primary session, when there is a time interval of 1 afterward, but much smaller for the others.  Could this be affecting N estimates, as the openCR derived estimates are very different from the derived estimates using RMark? 

Note, I have an uneven number of secondary sampling occasions in my second primary session for some of my transects, and did not incorporate this into my RMark analysis, but my capthist traps object does have an appropriate usage matrix.  This might be why there are small discrepancies in estimates of phi and f between the two packages.

openCR results:

> predict(test)$phi
    session sex  estimate SE.estimate       lcl       ucl
capnew13         1   F 0.9660548 0.009549452 0.9414601 0.9805301
capnew14         2   F 0.9660548 0.009549452 0.9414601 0.9805301
capnew14PF    3   F 0.9660548 0.009549452 0.9414601 0.9805301
capnew15         4   F        NA          NA        NA        NA

> predict(test)$f
           session sex   estimate SE.estimate        lcl        ucl
capnew13         1   F 0.04436629 0.012595210 0.02543339 0.07739305
capnew14         2   F 0.28383438 0.096723275 0.14554522 0.55351838
capnew14PF    3   F 0.02659445 0.009059893 0.01363997 0.05185237
capnew15         4   F         NA          NA         NA         NA

> derived(test)
Total number observed 191 
Parameters in model p, phi, f, superN 
Superpopulation size 297.5006 
Session-specific counts and estimates:

 session sex t  n  R  m  r  z time      p       phi           f                  superN     b               lambda   gamma  kappa       N
       1   F 1 63 63  0   29  0     0  0.1852   0.9661    3.997e-15    297.5        7.605e-01  0.9661    NA         NA           226.3
       2   F 2 80 80 18  38 11   10 0.1852   0.9661    2.838e-01     NA           2.247e-14  1.2499    1.0000    0.7872   160.2
       3   F 3 70 70 33  17 16   11 0.1852   0.9661    9.992e-16     NA           2.395e-01  0.9661    0.7729    0.8939    226.0
       4   F 4 62 62 33  0   0     20 0.1852     NA        NA              NA             7.017e-15   NA         1.0000    0.7036    165.6

RMark results: 

> Robustmodels3[[112]]$results$real
                         estimate        se        lcl        ucl fixed    note
Phi gF a0 t1     0.9647136    0.0094949  0.9405676  0.9792659              
Phi gM a0 t1     0.8698507   0.0285431  0.8030513  0.9163535              
f gF a0 t1         0.0432405       0.0138039  0.0229605  0.0799671              
f gF a10 t11     0.3360935     0.1175158  0.1527746  0.5869810              
f gF a11 t12     0.0252802     0.0094636  0.0120701  0.0521843              
f gM a0 t1       0.1336951     0.0342360  0.0795819  0.2159697              
f gM a10 t11     0.6335197    0.1490309  0.3294282  0.8588129              
f gM a11 t12     0.0813585    0.0297191  0.0390380  0.1618318              
p gF s1 t1       0.1928667     0.0145030  0.1660209  0.2228933              
p gM s1 t1       0.1275172    0.0151713  0.1006192  0.1603238              
f0 gF a0 s1 t0  29.7639490   6.6478939 19.3142140 45.8673940              
f0 gF a0 s11 t0 20.2068670  4.9865618 12.5468890 32.5433240              
f0 gF a0 s12 t0 48.8236310  9.6297038 33.2920040 71.6011840              
f0 gF a0 s21 t0 42.8767310  8.8691548 28.7069070 64.0408290    



> Robustmodels3[[112]]$results$derived
$`N Population Size`
       estimate      lcl       ucl
F 1 70.76395 60.31421  86.86739     total N for each period = ~ 122, 120, 166, 146 
F 2 73.20687 65.54689  85.54332
F 3 95.82363 80.29200 118.60118
F 4 85.87673 71.70691 107.04083
M 5 51.76395 41.31421  67.86739
M 6 47.20687 39.54689  59.54332
M 7 71.82363 56.29200  94.60118
M 8 61.87673 47.70691  83.04083



Thanks very much for taking the time to read through all of that, and I appreciate any guidance in advance.  


Best, 
Jennifer

Murray Efford

unread,
Jul 26, 2018, 8:36:26 AM7/26/18
to secr
Hi Jennifer
It may be days/weeks before I digest and respond, but note my comment to Clayton: in secr 3.1.1 (November last year) derived() became a generic with methods for secr and now openCR objects.
Murray

jlbr...@ucdavis.edu

unread,
Jul 26, 2018, 3:43:52 PM7/26/18
to secr
Hi Murray, 

Thanks for your response, that makes more sense to me now in terms of the derived() crossover between secr and openCR.  I do still have the other issues with derived(), but I am in no hurry to get a resolution.  

Best Regards, 
Jennifer

Matt Rogan

unread,
Oct 30, 2018, 9:55:23 AM10/30/18
to secr
Hi all, I want to follow up on Jennifer's post to see if anyone has new insights. I have been playing around with openCR models and I am trying to make sense of the outputs and derived parameters. I've fit various versions of the model, with different movementmodels, spatial vs. non-spatial, single session vs multi-session capthist, etc and in all cases the parameter estimates are fairly consistent but I don't quite understand the derived results. I am directly estimating phi and f but am also interested in the derived estimate of lambda:

In the following case I have a multi-session capthist object with five sessions and each session consisting of a single occasion. Intervals between sessions are specified as c(0.84, 0.75, 1.32, 1.01). For simplicity's sake, I have not included any covariates for parameters. 

mod <- openCR.fit(igr.ch, type = "JSSAsecrfCL", mask = igr.mask, detectfn = "HHN", distribution = "poisson",
                               movementmodel = "exponential", trace = 25)

Below are the derived estimates. As Jennifer found, phi is constant across sessions, but f varies. It seems like both should vary based on the interval between sessions, but the variation in f cannot be explained by differences in the interval. There also is a disconnect between the derived estimates of lambda and D. Between sessions 2 and 3 (for example), lambda is greater than 1 but D decreases, while the esa stays the same. I'm not sure what to make of these results. Any clarification or insights would be greatly appreciated. code and input objects attached.


mod.drvd <- derived(mod, Dscale = 1e4)

mod.drvd$estimates #rounded to three or four digits

#session  n  R  m    r   z  time    lambda0       phi                 f           sigma      move.a          b       lambda     gamma     kappa            D
# 1          48 48  0  30  0  0.00    0.066         0.588          0.354       2004.231 1138.928   0.532     0.942           NA           NA           0.0097
#2           45 45 28 26  2  0.84    0.066         0.588          0.444       2004.231 1138.928   0.165     1.032         0.624      0.596          0.0092
#3           39 39 26 22  2  1.59    0.066         0.588          0.138       2004.231 1138.928   0.161     0.726         0.570      0.562          0.0091
#4           32 32 22 15  2  2.91    0.066         0.588          0.244       2004.231 1138.928   0.064     0.832         0.810      0.270          0.0057
#5           23 23 17  0   0  3.92    0.066           NA               NA         2004.231       NA       0.078     NA             0.707       NA             0.0048
IGR_exmpl_Rcode.R
igr_inputs.rdata

Murray Efford

unread,
Oct 31, 2018, 3:22:54 AM10/31/18
to secr
I think this is due to a bug in the code for derived.openCR(). See attached pdf where this is explored for Matt's example.
I have fixed the bug, rebuilt my working version of the next release (1.2.1) and placed the Windows binary here and the source code here.

Jennifer - I'm sorry this took so long; I've not been working on openCR.

Also - Dscale now applies to the session-specific derived D. Other changes noted in News -
news(package = 'openCR') [after installing package]

Murray
MGE.pdf

Matt Rogan

unread,
Nov 1, 2018, 5:00:34 AM11/1/18
to secr
Thanks so much, Murray. And thank you also for pointing out that the mask retains the polygon.

Matt
Reply all
Reply to author
Forward
0 new messages