Reduce- Multi-Session caphist

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

Reduce- Multi-Session caphist

Postby ctlamb » Tue Dec 01, 2015 1:40 pm

Hello,

I know how to reduce a single-session caphist :

Code: Select all
bear08_reduce<-reduce(bear2008,newocc=list(1:2, 3:4))


But, I can't quite figure out how to reduce a multi-session caphist where the structure of the reduce is session-specific.

For example, I can reduce a large multi-session caphist in this way, but all sessions get reduced in the same way;

Code: Select all
test<-lapply(bearCH, function(x) reduce(x,newocc=list(1:2, 3:4)))



I've been trying head this route, but can't seem to figure it out:

Code: Select all
test<-mapply(reduce, bearCH_F,
       reduce=lapply(bearCH_F, function(x) reduce(x,newocc=list(1:2, 3:4))),
       MoreArgs = list(Session = 1:5)
       )


I have 9 years of data, 2006-2014

I want to reduce 2006-2010 in this fashion:
Code: Select all
bear_reduce<-reduce(bear,newocc=list(1:2, 3:4))


2011-2012,2014 I don't want to do anything to,

and I want to do this to 2013:
Code: Select all
bear_reduce<-reduce(bear,newocc=list(1:2, 3, 4 ))


thoughts?
ctlamb
 
Posts: 56
Joined: Mon Nov 04, 2013 9:44 pm

Re: Reduce- Multi-Session caphist

Postby murray.efford » Tue Dec 01, 2015 1:43 pm

Without looking at the detail = I think you are on the right track with mapply, but perhaps need SIMPLIFY = FALSE.
Murray
murray.efford
 
Posts: 686
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: Reduce- Multi-Session caphist

Postby ctlamb » Tue Dec 01, 2015 1:52 pm

Thanks, Murray

When I do so, I get the following.

Code: Select all
> test<-mapply(reduce, bearCH_F,
+        reduce=lapply(bearCH_F, function(x) reduce(x,newocc=list(1:2, 3:4))),
+        MoreArgs = list(session = 1:5),
+        SIMPLIFY = FALSE
+        )

Session 2008
Conflicting number of occasions in usage matrix
capthist : 3 occasions
usage(traps(capthist)) : 4 occasions
Session 2009
Conflicting number of occasions in usage matrix
capthist : 3 occasions
usage(traps(capthist)) : 4 occasions
Session 2010
Conflicting number of occasions in usage matrix
capthist : 3 occasions
usage(traps(capthist)) : 4 occasions


> class(test)<-class(bearCH)
> summary(test)


Error in counts[8, ] <- apply(usage(traps), 2, function(x) sum(x > 0)) :
  number of items to replace is not a multiple of replacement length



And not sure how to implement the second argument for 2013 where I reduce only sessions 1:2.
ctlamb
 
Posts: 56
Joined: Mon Nov 04, 2013 9:44 pm

Re: Reduce- Multi-Session caphist

Postby murray.efford » Tue Dec 01, 2015 2:06 pm

You want to provide a list of values for newoccasions, one per input session. Without checking details, how about something like this (note need to restore the class of the resulting list):
Code: Select all
newocc <- list(c(1:2, 3:4), NULL, c(1:2,3,4))[c(1,1,1,1,1,2,3,2)]
newCH <- mapply(reduce, bearCH_F, newoccasions = newocc, SIMPLIFY = FALSE)
class(newCH) <- class(bearCH_F)
murray.efford
 
Posts: 686
Joined: Mon Sep 29, 2008 7:11 pm
Location: Dunedin, New Zealand

Re: Reduce- Multi-Session caphist

Postby ctlamb » Tue Dec 01, 2015 2:29 pm

Ah I can see we are very close!!

The issue with the:
Code: Select all
Session 2008
Conflicting number of occasions in usage matrix
capthist : 3 occasions
usage(traps(capthist)) : 4 occasions
Session 2009
Conflicting number of occasions in usage matrix
capthist : 3 occasions
usage(traps(capthist)) : 4 occasions
Session 2010
Conflicting number of occasions in usage matrix
capthist : 3 occasions
usage(traps(capthist)) : 4 occasions


stemmed from the fact that a bunch of females had no captures in session 1 when I subsetted the data, so secr didn't seem to be able to combine. If I apply the reduce to the male + female dataset first, then subset by sex I think I'll be better off.

Now, the problem with the code below seems to be that c(1:2, 3:4) and c(1,2, 3:4) produce the same string in a list, 1,2,3,4.

Code: Select all
> newocc <- list(c(1:2, 3:4), NULL, c(1:2,3,4))[c(1,1,1,1,1,2,2,3,2)]
> newocc
[[1]]
[1] 1 2 3 4

[[2]]
[1] 1 2 3 4

[[3]]
[1] 1 2 3 4

[[4]]
[1] 1 2 3 4

[[5]]
[1] 1 2 3 4

[[6]]
NULL

[[7]]
NULL

[[8]]
[1] 1 2 3 4

[[9]]
NULL
ctlamb
 
Posts: 56
Joined: Mon Nov 04, 2013 9:44 pm

Re: Reduce- Multi-Session caphist

Postby ctlamb » Tue Dec 01, 2015 2:37 pm

I got it, this is how to reduce a multi-session caphist

Code: Select all
newocc <- list(
              list(1:2,3:4),
              NULL,
              list(1:2,3,4)
              )[c(1,1,1,1,1,2,2,3,2)]


newCH <- mapply(reduce, bearCH, newoccasions = newocc, SIMPLIFY = FALSE)
class(newCH) <- class(bearCH)
ctlamb
 
Posts: 56
Joined: Mon Nov 04, 2013 9:44 pm


Return to analysis help

Who is online

Users browsing this forum: No registered users and 11 guests