Page 1 of 1

Reduce- Multi-Session caphist

PostPosted: Tue Dec 01, 2015 1:40 pm
by ctlamb
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?

Re: Reduce- Multi-Session caphist

PostPosted: Tue Dec 01, 2015 1:43 pm
by murray.efford
Without looking at the detail = I think you are on the right track with mapply, but perhaps need SIMPLIFY = FALSE.
Murray

Re: Reduce- Multi-Session caphist

PostPosted: Tue Dec 01, 2015 1:52 pm
by ctlamb
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.

Re: Reduce- Multi-Session caphist

PostPosted: Tue Dec 01, 2015 2:06 pm
by murray.efford
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)

Re: Reduce- Multi-Session caphist

PostPosted: Tue Dec 01, 2015 2:29 pm
by ctlamb
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

Re: Reduce- Multi-Session caphist

PostPosted: Tue Dec 01, 2015 2:37 pm
by ctlamb
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)