Page 1 of 1

subset.capthist

PostPosted: Sun May 17, 2015 3:32 pm
by Bryan Hamilton
This question is probably a matter of taste. I have a somewhat complex capture history dataset, 11 years, six species and 11 sampling grids. I want to get density data on several of these species by year. Year is a capture session but I haven't been able to implement models with multiple years so I've been running models year by year.

I would prefer to subset from a master "capthist" file rather than create separate files for each species-year combination but I'm having trouble implementing the 'subset.capthist' example for covariates.

Generally, would folks recommend subsetting based on covariates from capthist file or creating separate files for the groupings of interest?

Thank you.

Re: subset.capthist

PostPosted: Sun May 17, 2015 4:41 pm
by murray.efford
This works for me, and seems much cleaner than using separate files -
Code: Select all
for (i in 1:5) {
CHi <- subset(ovenCH, session = i)
print(summary(CHi))
}

You may have mistakenly included '.capthist' - that is not needed because subset.capthist is a method for the generic function 'subset'.
Murray

Re: subset.capthist

PostPosted: Sun May 17, 2015 7:26 pm
by Bryan Hamilton
Thank you. The covariate subsetting worked for me after I restarted R. That's halfway there.

I'm still a little lost with functions and loops. In the code below what it the function (x) referring to?

Code: Select all
ovenCH.males <- mapply(subset, ovenCH,
                       subset = lapply(ovenCH, function(x) covariates(x)$Sex == "M"),
                       MoreArgs = list(occasions = 1:5))


With the code you provided, there are 5 sessions in the ovenCH, we're making a new capthist (CHi), by subsetting ovenCH? If I want to select session 2, would I replace the 'session = i' with 'session = 2'?

Code: Select all
for (i in 1:5) {
  CHi <- subset(ovenCH, session = i)
  print(summary(CHi))
}

Re: subset.capthist

PostPosted: Sun May 17, 2015 7:37 pm
by murray.efford
I'm relieved subset works!

That example you cite is an unfortunate example of R at its most convoluted. The bit you are concerned about is trying to find for each component of ovenCH a logical 'subset' condition based on the covariate 'sex' for that particular component. It gets this with lapply. lapply applies a function to each component of a list (in this case, ovenCH). Here we make up a function 'on the fly' and, for lack of imagination, call its argument 'x'; the function gets the covariate information we need from a (single-session) capthist object temporarily called 'x'.

With the code you provided, there are 5 sessions in the ovenCH, we're making a new capthist (CHi), by subsetting ovenCH? If I want to select session 2, would I replace the 'session = i' with 'session = 2'?

Yes.
Code: Select all
CH2 <- subset(ovenCH, session = 2)

But with lots of sessions it's worth learning about loops and perhaps even lapply.
Murray

Re: subset.capthist

PostPosted: Mon May 18, 2015 2:12 pm
by Bryan Hamilton
That worked really well. Thank you. I'm finally at a point where I've got confidence in the results. Now its just a matter of powering through the models. They take a long time to run, even for a capture session with 27 individuals!

In your experience, do the results of the different models differ much? I'm mostly interested in density and there is very little difference between say a null model and a time dependent model. That is reassuring but its going to take a couple weeks to get through all the candidate models for each species. I guess I'm wondering if I can take a shortcut with model selection.

I need to spend some more time on functions and lapply. Thanks to your help, I'm making good progress with 'secr'. Besides the subsetting, I figured out 'make.mask', 'closedN' and 'region.N' yesterday. I'll probably make a post comparing some of the different estimators. Its been interesting to compare the different results. I'm curious how correlated they are and if the differnet methods would result in different inferences.

Random question: What does "not run" refer to in the secr documentation? It shows up in quite a few of the examples.

Code: Select all
## End(Not run)
## Not run:


Thank you again.

Re: subset.capthist

PostPosted: Mon May 18, 2015 5:38 pm
by murray.efford
Bryan
Good to hear of progress...

It is never feasible or advisable to run all SECR models (and it's hard even to list them), but they're there when you need them. As you've noticed, many produce essentially the same answer (I've never found ~t interesting). The effects that matter are probably learned responses (b or bk) and unmodelled heterogeneity (maybe h2, but this may not work well with small datasets, and may not be needed). Model selection is the main worry alright, and you're largely on your own.

"Not run" brackets example code that the developer has blocked R from running when the package is checked (in secr this is usually because it would take too long).
Murray