Matrix CMR in R

posts related to the RMark library, which may not be of general interest to users of 'classic' MARK

Matrix CMR in R

Postby bfol » Mon Jan 16, 2017 6:49 am

Hello everyone,

I have a dataset of CMR data (type Euring for the connoisseurs) : for each row, I have an encounter (a ring, a recapture or a recovery) with the identification of the individual, the date, the localisation and some variables of interest. The first ring begins in 1935. The last recovery is in 2016. I have 16022 individuals ringed.

I would like to create a supplementary variable corresponding to the encounter history (for example "00001001012000000000") to be able to use the dataset in Rmark or Esurge. So I have to have a unique matrix for my dataset with 82 colums (1935-2016) and 16022 rows.

I would like to know if someone have a package or a function to create this CMR matrix ?

All the best,
Ben
bfol
 
Posts: 7
Joined: Wed Jul 20, 2016 8:08 am

Re: Matrix CMR in R

Postby jlaake » Tue Jan 17, 2017 12:44 pm

I don't know of any generic code that does what you want. I have thought about writing something like that but it depends on the data/model structure. If you know R, then this is done fairly easily once you know a few tricks. If your data are in dataframe x and it includes both the initial release and recaptures then conceptually it is something like the following where ID is the animal ID and occasions are years (Year) and each is a factor variable with the same levels. If ID and Year are numeric in your data do the following first.

Code: Select all
allyears=sort(unique(x$Year))
allids=sort(unique(x$ID))
x$Year=factor(x$Year)
z$Year=factor(z$Year,levels=allyears)
x$ID=factor(x$ID)
z$ID=factor(z$ID,levels=allids)


Doing so will mean that when you use the table function you will get the same number of rows and columns in each table.


After doing that create the release/recapture table

Code: Select all
freq=table(x$ID,x$Year)
freq[freq>0]=1


Then if it was only recaptures you would simply do

Code: Select all
ch=apply(freq,1,paste,collapse="")


and ch will be the encounter history string.

If you also had dead encounters and they are in dataframe z then you can add them with

Code: Select all
dfreq=table(z$ID,z$Year)
dfreq[dfreq>0]=2


Note that I'm also assuming that you have at least one release or recapture per year. If you don't then you'll either need to set time interval values for the gap or specify range of years in level for factor. Anyhow, hopefully you can start to appreciate why writing a generic routine is not easy. But if you both Year and ID are factors with same levels as described above then using the following will construct what you want.

Code: Select all
chmat=matrix(0,nrow=nrow(freq),ncol=2*ncol(freq))
chmat[,seq(1,ncol(chmat))]=freq
chmat[,seq(2,ncol(chmat))]=dfreq
ch=apply(chmat,1,paste,collapse="")


Putting this altogether use:
Code: Select all
allyears=sort(unique(x$Year))
allids=sort(unique(x$ID))
x$Year=factor(x$Year)
z$Year=factor(z$Year,levels=allyears)
x$ID=factor(x$ID)
z$ID=factor(z$ID,levels=allids)
freq=table(x$ID,x$Year)
freq[freq>0]=1
dfreq=table(z$ID,z$Year)
dfreq[dfreq>0]=2
chmat=matrix(0,nrow=nrow(freq),ncol=2*ncol(freq))
chmat[,seq(1,ncol(chmat),2)]=freq
chmat[,seq(2,ncol(chmat),2)]=dfreq
ch=apply(chmat,1,paste,collapse="")


Here is an example

Code: Select all
x=data.frame(ID=c(1,1,2,3,3),Year=c(1990,1991,1990,1991,1992))
z=data.frame(ID=c(2),Year=1990)


With this result

> ch
[1] "101000" "120000" "001010"

Obviously I have used a simple example and there may be complications like non-annual occasions, unequal intervals etc. But hopefully this will help you get started.

You can do the same thing in Excel with pivot table which is what I did before I learned R. It is too easy to make an unintended error in Excel that you may not see. Granted it is also possible to make an error in the R code but it is usually obvious and regardless it is a lot easier to fix and repeat than all that pointing and clicking.

It is real easy to convince yourself that you are only doing this once and I can probably get it done faster in Excel than in R. I've been there done that. Don't do it. Recently I had a colleague send me a file in Excel and asked me to do some manipulations. It was a one time thing so I thought why not just do it in Excel and then he could relate to it. Six hours later I couldn't work out how to do it and I'm pretty good at Excel and used to program in VBA. I finally smartly (or not so since it took me 6 hours) gave up and did it in R with 10 lines of code in about 15 minutes. I now have a sticky on my portable to remind me. It says "Don't do data analysis in Excel!"

regards --jeff
jlaake
 
Posts: 1417
Joined: Fri May 12, 2006 12:50 pm
Location: Escondido, CA

Re: Matrix CMR in R

Postby bfol » Tue Jan 24, 2017 11:45 am

Thank you very much Jeff !
I found a similitar method as yours on this site : https://sites.google.com/site/cmrsoftwa ... -and-rmark

And I wrote a function to combine mark / recapture / recovery in a same history matrix.

Thank you very much again for your help ;)
bfol
 
Posts: 7
Joined: Wed Jul 20, 2016 8:08 am


Return to RMark

Who is online

Users browsing this forum: No registered users and 11 guests

cron