-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.SMR.multisession.Dcov.Generalized.R
More file actions
132 lines (123 loc) · 5.41 KB
/
init.SMR.multisession.Dcov.Generalized.R
File metadata and controls
132 lines (123 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
e2dist <- function (x, y){
i <- sort(rep(1:nrow(y), nrow(x)))
dvec <- sqrt((x[, 1] - y[i, 1])^2 + (x[, 2] - y[i, 2])^2)
matrix(dvec, nrow = nrow(x), ncol = nrow(y), byrow = F)
}
init.SMR.multisession.Dcov.Generalized <- function(data,inits=NA,M=NA){
N.session <- length(data)
n.marked <- sapply(data,function(x){x$n.marked})
if(length(M)!=N.session)stop("Must supply an M for each session.")
init.session <- vector("list",N.session)
for(g in 1:N.session){
inits.use <- inits #p0, lam0, and sigma inits vary by session
inits.use$p0 <- inits.use$p0[g]
inits.use$lam0 <- inits.use$lam0[g]
inits.use$sigma <- inits.use$sigma[g]
init.session[[g]] <- init.SMR.Dcov.Generalized(data[[g]],inits.use,M=M[g])
}
anyTelemetry <- FALSE
locs.use <- vector("list",N.session)
tlocs.sess.max <- rep(NA,N.session)
for(g in 1:N.session){
if(all(is.na(init.session[[g]]$locs))){
locs.use <- NA
}else{
if(n.marked[g]>1){
tlocs.sess.max[g] <- max(rowSums(!is.na(init.session[[g]]$locs[1:n.marked[g],,1])))
locs.use[[g]] <- init.session[[g]]$locs[1:n.marked[g],1:tlocs.sess.max[g],1:2]
}else{
tlocs.sess.max[g] <- sum(!is.na(init.session[[g]]$locs[1:n.marked[g],,1]))
locs.use[[g]] <- array(init.session[[g]]$locs[1,1:tlocs.sess.max[g],1:2],dim=dim(init.session[[g]]$locs))
}
anyTelemetry <- TRUE
}
}
if(anyTelemetry){
n.tel.inds <- unlist(lapply(locs.use,nrow))
n.locs.ind <- matrix(NA,N.session,max(n.tel.inds))
tel.inds <- matrix(NA,N.session,max(n.tel.inds))
}else{
tel.inds <- n.locs.ind <- n.tel.inds <- NA
}
J.mark <- unlist(lapply(data,function(x){nrow(x$X.mark)}))
J.sight <- unlist(lapply(data,function(x){nrow(x$X.sight)}))
n.marked <- unlist(lapply(data,function(x){x$n.marked}))
maxM <- max(M)
s <- array(NA,dim=c(N.session,maxM,2))
z <- matrix(NA,N.session,maxM)
K1D.mark <- matrix(NA,N.session,max(J.mark))
K1D.sight <- matrix(NA,N.session,max(J.sight))
y.mark <- array(NA,dim=c(N.session,maxM,max(J.mark)))
y.mID <- array(NA,dim=c(N.session,maxM,max(J.sight)))
y.mnoID <- matrix(NA,N.session,max(J.sight))
y.um <- matrix(NA,N.session,max(J.sight))
y.unk <- matrix(NA,N.session,max(J.sight))
n.cells <- unlist(lapply(data,function(x){x$n.cells}))
n.cells.x <- unlist(lapply(data,function(x){x$n.cells.x}))
n.cells.y <- unlist(lapply(data,function(x){x$n.cells.y}))
n.cells.max <- max(n.cells)
n.cells.x.max <- max(n.cells.x)
n.cells.y.max <- max(n.cells.y)
res <- unlist(lapply(data,function(x){x$res}))
cellArea <- res^2
xlim <- ylim <- matrix(NA,N.session,2)
x.vals <- matrix(NA,N.session,n.cells.x.max)
y.vals <- matrix(NA,N.session,n.cells.y.max)
dSS <- array(NA,dim=c(N.session,n.cells.max,2))
InSS <- array(0,dim=c(N.session,n.cells.max))
D.cov <- array(NA,dim=c(N.session,n.cells.max))
cells <- array(0,dim=c(N.session,n.cells.x.max,n.cells.y.max))
for(g in 1:N.session){
s[g,1:M[g],] <- init.session[[g]]$s
z[g,1:M[g]] <- init.session[[g]]$z
K1D.mark[g,1:J.mark[g]] <- init.session[[g]]$K1D.mark
K1D.sight[g,1:J.sight[g]] <- init.session[[g]]$K1D.sight
y.mark[g,1:M[g],1:J.mark[g]] <- init.session[[g]]$y.mark
y.mID[g,1:n.marked[g],1:J.sight[g]] <- init.session[[g]]$y.mID
y.mnoID[g,1:J.sight[g]] <- init.session[[g]]$y.mnoID
y.um[g,1:J.sight[g]] <- init.session[[g]]$y.um
y.unk[g,1:J.sight[g]] <- init.session[[g]]$y.unk
xlim[g,] <- data[[g]]$xlim
ylim[g,] <- data[[g]]$ylim
x.vals[g,1:n.cells.x[g]] <- data[[g]]$x.vals
y.vals[g,1:n.cells.y[g]] <- data[[g]]$y.vals
dSS[g,1:n.cells[g],] <- data[[g]]$dSS
InSS[g,1:n.cells[g]] <- data[[g]]$InSS
D.cov[g,1:n.cells[g]] <- data[[g]]$D.cov
cells[g,1:n.cells.x[g],1:n.cells.y[g]] <- data[[g]]$cells
if(anyTelemetry){
tel.inds[g,1:n.tel.inds[g]] <- init.session[[g]]$tel.inds
n.locs.ind[g,1:n.tel.inds[g]] <- init.session[[g]]$n.locs.ind
}
}
if(anyTelemetry){
#reformat locs.use actually
locs.use2 <- array(NA,dim=c(N.session,max(n.tel.inds,na.rm=TRUE),max(n.locs.ind,na.rm=TRUE),2))
for(g in 1:N.session){
locs.use2[g,1:nrow(locs.use[[g]]),1:ncol(locs.use[[g]]),] <- locs.use[[g]]
}
#remove unused telemetry dimensions if not all marked individuals telemetered
rem.idx <- which(colSums(is.na(n.locs.ind))==N.session)
if(length(rem.idx)>0){
n.locs.ind <- n.locs.ind[,-rem.idx]
tel.inds <- tel.inds[,-rem.idx]
}
}else{
locs.use2 <- NA
}
#put X in ragged array
X.mark.new <- array(NA,dim=c(N.session,max(J.mark),2))
X.sight.new <- array(NA,dim=c(N.session,max(J.sight),2))
for(g in 1:N.session){
X.mark.new[g,1:J.mark[g],] <- data[[g]]$X.mark
X.sight.new[g,1:J.sight[g],] <- data[[g]]$X.sight
}
dummy.data <- matrix(0,N.session,maxM) #dummy data not used, doesn't really matter what the values are
return(list(y.mark=y.mark,y.mID=y.mID,y.mnoID=y.mnoID,y.um=y.um,y.unk=y.unk,
s.init=s,z.init=z,K1D.mark=K1D.mark,K1D.sight=K1D.sight,J.mark=J.mark,J.sight=J.sight,
X.mark=X.mark.new,X.sight=X.sight.new,n.marked=n.marked,
xlim=xlim,ylim=ylim,locs=locs.use2,tel.inds=tel.inds,n.locs.ind=n.locs.ind,n.tel.inds=n.tel.inds,
res=res,cellArea=cellArea,x.vals=x.vals,xlim=xlim,ylim=ylim,
y.vals=y.vals,dSS=dSS,InSS=InSS,cells=cells,n.cells=n.cells,n.cells.x=n.cells.x,
n.cells.y=n.cells.y,D.cov=D.cov,dummy.data=dummy.data))
}