Including sex in multiple-session models

questions concerning anlysis/theory using program DENSITY and R package secr. Focus on spatially-explicit analysis.

Including sex in multiple-session models

Postby Tom.B » Wed Jul 27, 2011 11:39 am

Hi,

I have been happily using Density and the secr R package to build and compare models, despite my very limited experience with R. However, I have fallen down when it comes to including sex in the models. I have good reason to suspect that sex will effect at least sigma, and possibly g0 as well, so would like to include it.

I understand that I need to specify the sexes as groups rather than as covariates when using full-likelihood models, but despite reading through all the supporting material and several similar posts on this forum I have been unable to successfully create a group and add it to any models, despite much trying! I suspect this may be more down to my poor R knowledge than a specific secr question, but I would be extremely grateful if someone could talk me through it.

My data file looks like this (minus header)...

Session ID Occasion Trap Sex
7 035.088.784 1 B1B 1
7 035.838.796 2 B2A 1
7 035.838.796 3 H3A 1
7 035.838.796 4 C4B 1
7 035.838.796 5 G3A 1
7 035.838.852 2 B1B 1

Many thanks,
Tom
Tom.B
 
Posts: 19
Joined: Fri Jan 22, 2010 12:01 pm

Re: Including sex in multiple-session models

Postby murray.efford » Wed Jul 27, 2011 12:09 pm

Tom
You're not the only person to stumble over this! There are several ways to include sex - one would be to separate the sexes as different 'sessions' - but as you ask about using groups...

First, your input file looks fine, although I prefer to use letters to distinguish sexes. You should be able to read it with read.capthist e.g.

myCH <- read.capthist('mycapt.txt','mytrap.txt', covnames = 'sex')

To demonstrate model fitting I'll simulate some data (conveniently, sim.popn automatically generates a random 'sex' covariate for each individual)

library(secr)
tempgrid <- make.grid()
temppop <- sim.popn(core = tempgrid, D=5)
tempCH <- sim.capthist(tempgrid, popn = temppop)
## If you check covariates(tempCH) you'll see 'sex' is there as an individual covariate.
## We will use it to define groups, rather than as an individual covariate per se, because,
## as you know, that would require conditional likelihood
fit0 <- secr.fit(tempCH, model = list(g0 ~ 1), groups = 'sex')
fit1 <- secr.fit(tempCH, model = list(g0 ~ g), groups = 'sex')

AIC(fit0, fit1)

predict(fit0)
predict(fit1)

Note that for model comparison one should specify the same 'groups' structure even if you are not including groups (g) in the model.
Hope that works for you.

Murray
murray.efford
 
Posts: 686
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: Including sex in multiple-session models

Postby Tom.B » Wed Jul 27, 2011 6:11 pm

Murray,

Thanks very much for the prompt reply. That all seems to make perfect sense, up until I attempt to run a model, whereby the following happens -

> library(secr)
> setwd(system.file("VB", package = "secr"))
> vbCH <- read.capthist("VBsex.txt", "10x10x2grid.txt", fmt = "trapID", covnames = "sex")
No errors found :-)
> secrtest <- secr.fit(vbCH, model = g0~1, groups = "sex", detectfn = 2, trace = TRUE, buffer = 100)
Checking data
Preparing detection design matrices
Error in names(grouping) <- groups :
'names' attribute [1] must be the same length as the vector [0]
>

This happens whenever I have 'groups = "sex"' specified, regardless of whatever the model is.

Any thoughts?

EDIT - i tried changing the codes for sex from '1' and '2' to 'm' and 'f', and R now crashes rather than giving an error code (much as described by another user in the thread titled 'groups' lower down the page).

EDIT 2 - i tried using a different set of capture histories and it works fine... must be something in that particular data file it doesn't like. Will try and hunt it down.

EDIT 3 - Now working! Not sure what I did, recoded a few things in Excel and saved it back to .txt and hey presto it worked, must have been something screwy with the way it had been saved before. Hopefully no more edits...
Tom.B
 
Posts: 19
Joined: Fri Jan 22, 2010 12:01 pm

Re: Including sex in multiple-session models

Postby murray.efford » Thu Jul 28, 2011 9:25 am

Tom
I'm glad you could sort this out - I guessed it might be a data problem. Before fitting a model it helps to review the individual covariates (in your case covariates(vbCH)) to confirm there is a dataframe with a column named as you expect ('sex') and for which there are no missing values (i.e. one per individual). If you discover what went wrong, let me know and I'll add a more specific error message.
Murray
murray.efford
 
Posts: 686
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand


Return to analysis help

Who is online

Users browsing this forum: No registered users and 8 guests