-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis_MEE_updated.r
125 lines (101 loc) · 3.5 KB
/
Analysis_MEE_updated.r
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
#To run this example code, set the working directory below and place the example dataset in that directory
#Also, be sure to have installed the 'rjags' library from CRAN
setwd("C:\\Users\\conve\\Documents\\Projects - Old\\Hierarchical Capture-Recapture Projects\\Andy Royle HSCR")
load("Example_Data_MEE.RData")
peromyscus.data <- peromyscus.data
group.mem <- group.mem
S.st <- S.st
################################################################################
#BUGS CODE
cat("
model {
#PRIORS
#abundance model
for(i in 1:M){
group.mem[i] ~ dcat(gprobs[])
z[i] ~ dbern(psi)
}
psi ~ dunif(0,1)
for(i in 1:n.sites){
b.site[i] ~ dnorm(int.lam,tau.site)
}
int.lam <- 0
sigma.site ~ dunif(0,10)
tau.site<-1/(sigma.site*sigma.site)
for(i in 1:2){
b.season[i] <- b.seas[i]
b.seas[i] ~ dunif(-10,10)
}
b.season[3] <- -1*(b.season[1]+b.season[2])
b.fire ~ dunif(-10,10)
b.thin ~ dunif(-10,10)
#observation model
for(i in 1:M){
cent[i,1] ~ dunif(Xl,Xu)
cent[i,2] ~ dunif(Yl,Yu)
}
for(s in 1:24){
bgroup.p[s] ~ dnorm(int.p,tau.p)
}
int.p ~ dunif(-10,10)
sigma.p ~ dunif(0,10)
tau.p <- 1/(sigma.p*sigma.p)
bcap.p ~ dunif(-10,10)
bcap.am ~ dunif(-10,10)
for(i in 1:24){
b.dist[i] ~ dnorm(int.dist,tau.dist)
}
int.dist ~ dunif(-10,10)
sigma.dist ~ dunif(0,10)
tau.dist <- 1/(sigma.dist*sigma.dist)
#LIKELIHOOD
#abundance model
for(j in 1:n.groups){
log(lam[j]) <- b.site[site[j]] + b.season[season[j]] + b.thin*thin[j] + b.fire*fire[j]
gprobs[j] <- lam[j]/sum(lam[1:n.groups])
}
#observation model
for(i in 1:M){
for(j in 1:n.traps){
#distance from capture to the center of the home range
d[i,j] <- pow(pow(cent[i,1]-trap.locs[j,1],2) + pow(cent[i,2]-trap.locs[j,2],2),.5)
}
# assumes traplocs are the SAME for all groups
for(k in 1:last.cap[i]){
for(j in 1:n.traps){
lp[i,k,j] <- (exp(bgroup.p[group.mem[i]] + am.effect[k]*bcap.am + bcap.p*reencounter[i,k] + b.dist[group.mem[i]]*d[i,j])*traps.avail[j,group.mem[i]])*z[i]
cp[i,k,j] <- lp[i,k,j]/(1+sum(lp[i,k,]))
}
cp[i,k,n.traps+1] <- 1-sum(cp[i,k,1:n.traps]) # last cell = not captured
Ycat[i,k] ~ dcat(cp[i,k,])
}
}
#DERIVED PARAMETERS
#The G.N are the total population sizes by group
N.tot <- sum(z[1:M])
for(i in 1:M){
group.out[i] <- group.mem[i]*z[i]
#This will allow us to count the number of guys in each replicate
for(j in 1:n.groups){
g.N[j,i] <- step(0.01*(j-group.out[i])-0.02*(j-group.out[i])*(j-group.out[i])+0.001)
}
}
for(j in 1:n.groups){
G.N[j] <- sum(g.N[j,])
}
}
",file="replicated_scr_model.txt")
zst<-rep(1,length(group.mem))
gst <- group.mem
gst[is.na(group.mem)]<- sample(1:24,sum(is.na(group.mem)),replace=TRUE)
gst[!is.na(group.mem)]<-NA
inits <- function(){list (z=zst,group.mem=gst,psi=runif(1),b.site=runif(8),sigma.site=runif(1),b.seas=runif(2),b.thin=runif(1),b.fire=runif(1),cent=S.st,bgroup.p=runif(24),int.p=runif(1),sigma.p=runif(1),bcap.p=runif(1),bcap.am=runif(1),b.dist=runif(24),int.dist=runif(1),sigma.dist=runif(1))}
# Bundle data
jags.data <- peromyscus.data
parameters <- c("psi","sigma.site","b.season","b.fire","b.thin","int.p","sigma.p","bcap.p","bcap.am","int.dist","sigma.dist","N.tot","G.N")
library('jagsUI')
ni <- 50
nt <- 1
nb <- 20
nc <- 3
out.1 <- jags(jags.data, inits, parameters, "replicated_scr_model.txt", n.chains = nc, n.burnin = nb, n.thin = nt, n.iter = ni,factories = "base::Finite sampler FALSE")