This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code
371 lines (202 loc) · 9.7 KB
/
Code
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
####################################################################################################################
#
# Testing models of survivorship among juvenile corals
# 2nd November 2020
# Stuart Sandin & Lauranne Sarribouette
#
# - Using identifications of juveniles from imagery
# - Data from Lauranne Sarribouette and Nicole Pedersen
#
####################################################################################################################
#--- Data and set up
#-------------------------------------------------------------------------------------------------------------------
require(stats4)
juvs = read.csv("Data.csv", sep = ";", header=TRUE)
# for comparison all corals (ADL) and all corals (AD)
#juvs = juvs[juvs$X2017 != "Lost",]
n = dim(juvs)[1]
t_surv = array(NA,c(n))
col_start = 4 # Column that is the starting year
max_surv = dim(juvs)[2] - col_start - 1 # The number '1' is for the metadata rows following the survival data in 'juvs'
for(i in 1:n) {
# The following adds how many time steps the individual survives
# - Assuming here that once an individual is no longer 'Alive', it can never be 'Alive' again
# - An example, if the individual is not around in the second year, t=0 meaning that it did not survive any time steps
t=0
while(juvs[i,col_start+1+t]=="Alive") {
t=t+1
# Accounting for the maximum number of survival steps in the data
if(t==max_surv) break
}
t_surv[i]=t
}
possible_surv = seq(0,max(t_surv))
#--- Creating arrays that will be used within the functions
prob_surv = array(NA,c(length(possible_surv)),dimnames=list(c(possible_surv)))
s_log = array(NA,c(length(possible_surv)))
#--- Functions to calculate likelihoods
#-------------------------------------------------------------------------------------------------------------------
#--- Function to calculate likelihood for constant survival (one-parameter model)
nll <- function(s) {
if(s>0.9999 || s<0.0001) return(99999999)
else {
surv = rep(s,4)
prob_surv[1] = 1 - surv[1]
prob_surv[2] = surv[1]*(1-surv[2])
prob_surv[3] = surv[1]*surv[2]*(1-surv[3])
prob_surv[4] = surv[1]*surv[2]*surv[3]*(1-surv[4])
prob_surv[5] = 1 - sum(prob_surv[1:4])
likelihood = 0
for(i in 0:max(t_surv)) {
# This will calculate the probability of survival for the number of time steps indicated, pulling values from 'prob_surv' look-up table
# - Note that the "+1" accounts for the distinction between the column identification (1:max_col) vs the number of steps survived (0:max_surv)
temp_prob = log(prob_surv[i+1])
likelihood = likelihood + (n_t_surv[i+1]*temp_prob)
}
return(-likelihood)
}
}
#--- Function to calculate likelihood for logistic survival (two-parameter model)
nll2 <- function(s0,b) {
# Creating the logistic survivorship values
for(k in 1:length(possible_surv)){
s_log[k] = s0 / (1 + exp(-b*k))
}
if(max(s_log)>0.9999 || min(s_log)<0.0001) return(99999999)
else {
prob_surv[1] = 1 - s_log[1]
prob_surv[2] = s_log[1]*(1-s_log[2])
prob_surv[3] = s_log[1]*s_log[2]*(1-s_log[3])
prob_surv[4] = s_log[1]*s_log[2]*s_log[3]*(1-s_log[4])
prob_surv[5] = 1 - sum(prob_surv[1:4])
likelihood = 0
for(i in 0:max(t_surv)) {
# This will calculate the probability of survival for the number of time steps indicated, pulling values from 'prob_surv' look-up table
# - Note that the "+1" accounts for the distinction between the column identification (1:max_col) vs the number of steps survived (0:max_surv)
temp_prob = log(prob_surv[i+1]) # Log to convert the product to sum and since log is a strictly increasing function, it would not impact the resulting value of the parameter
likelihood = likelihood + (n_t_surv[i+1]*temp_prob)
}
return(-likelihood) # Return the negative to maximize rather than minimimize
}
}
#--- Likelihood calculations
#-------------------------------------------------------------------------------------------------------------------
# Calculating the likelihoods, given input of 'n_t_surv'
# - 'n_t_surv' is the vector of the number of individuals having survived for each of 't' time steps
# - 'n_t_surv' can be summed for different groupings, allowing one to compare models for different groups
#--- One habitat
# Use this summary table of counts for statistics involving all corals
n_t_surv = array(NA,c(length(possible_surv)),dimnames = list(possible_surv))
for(q in 1:length(possible_surv)) {
n_t_surv[q] = length(t_surv[which(t_surv==(q-1))])
}
sum(n_t_surv)
# MODEL 1: constant (1 parameter)
fit.mle <- mle(nll, start=list(s=0.5), method="Nelder-Mead")
summary(fit.mle)
model1 <- logLik(fit.mle)
# MODEL 2: logistic (2 parameters)
fit.mle.log <- mle(nll2, start=list(s0=0.5,b=1.0), method="Nelder-Mead")
summary(fit.mle.log)
model2 <- logLik(fit.mle.log)
#--- Two habitats: Consolidated, Unconsolidated
# - Four possibilities : Consolidated (constant & logistic), Unconsolidated (constant & logistic)
# Use this summary table of counts for statistics involving only one habitat type
habitat = levels(juvs$Habitat_type)
n_habitat = length(habitat)
count_by_habitat = table(juvs$Habitat_type) # Nber of juv by genus
# Consolidated
for(q in 1:length(possible_surv)) {
n_t_surv[q] = length(t_surv[which(juvs$Habitat_type=="Consolidated" & t_surv==(q-1) )])
}
sum(n_t_surv)
fit.mle.cons <- mle(nll, start=list(s=0.5), method="Nelder-Mead")
summary(fit.mle.cons)
logLik(fit.mle.cons)
fit.mle.cons.log <- mle(nll2, start=list(s0=0.5,b=1.0), method="Nelder-Mead")
summary(fit.mle.cons.log)
logLik(fit.mle.cons.log)
# Unconsolidated
for(q in 1:length(possible_surv)) {
n_t_surv[q] = length(t_surv[which(juvs$Habitat_type=="Unconsolidated" & t_surv==(q-1) )])
}
sum(n_t_surv)
fit.mle.unc <- mle(nll, start=list(s=0.5), method="Nelder-Mead")
summary(fit.mle.unc)
logLik(fit.mle.unc)
fit.mle.unc.log <- mle(nll2, start=list(s0=0.5,b=1.0), method="Nelder-Mead")
summary(fit.mle.unc.log)
logLik(fit.mle.unc.log)
# MODEL 3: Unc + Cons (1+1= 2 parameters)
model3 <- logLik(fit.mle.unc) + logLik(fit.mle.cons)
# MODEL 4: Unc + Cons.log (1+2= 3 parameters)
model4 <- logLik(fit.mle.unc) + logLik(fit.mle.cons.log)
# MODEL 5: Unc.log + Cons (2+1= 3 parameters)
model5 <- logLik(fit.mle.unc.log) + logLik(fit.mle.cons)
# MODEL 6: Unc.log + Cons.log (2+2= 4 parameters)
model6 <- logLik(fit.mle.unc.log) + logLik(fit.mle.cons.log)
#--- Repeat for individual taxa
# - by decreasing abundance: Pocillopora, Stylophora, Pavona, Goniastrea, Hydnophora, Astrea, Porites,
# Acropora, Favites
# Use this summary table of counts for statistics involving only individual genera
genera = levels(juvs$Genus)
n_genera = length(genera)
count_by_genus = table(juvs$Genus)
temp_genus = "Goniastrea"
for(q in 1:length(possible_surv)) {
n_t_surv[q] = length(t_surv[which(juvs$Genus==temp_genus & t_surv==(q-1) )])
}
sum(n_t_surv)
# MODEL 1: constant (1 parameter)
fit.mle <- mle(nll, start=list(s=0.5), method="Nelder-Mead")
summary(fit.mle)
model1 <- logLik(fit.mle)
# MODEL 2: logistic (2 parameters)
fit.mle.log <- mle(nll2, start=list(s0=0.5,b=1.0), method="Nelder-Mead")
summary(fit.mle.log)
model2 <- logLik(fit.mle.log)
#--- Two habitats: Consolidated, Unconsolidated
# - Four possibilities : Consolidated (constant & logistic), Unconsolidated (constant & logistic)
# Use this summary table of counts for statistics involving only one habitat type
habitat = levels(juvs$Habitat_type)
n_habitat = length(habitat)
count_by_habitat = table(juvs$Habitat_type) # Nber of juv by genus
# Consolidated
for(q in 1:length(possible_surv)) {
n_t_surv[q] = length(t_surv[which(juvs$Genus==temp_genus & juvs$Habitat_type=="Consolidated" & t_surv==(q-1) )])
}
sum(n_t_surv)
fit.mle.cons <- mle(nll, start=list(s=0.5), method="Nelder-Mead")
summary(fit.mle.cons)
logLik(fit.mle.cons)
fit.mle.cons.log <- mle(nll2, start=list(s0=0.5,b=1.0), method="Nelder-Mead")
summary(fit.mle.cons.log)
logLik(fit.mle.cons.log)
# Unconsolidated
for(q in 1:length(possible_surv)) {
n_t_surv[q] = length(t_surv[which(juvs$Genus==temp_genus & juvs$Habitat_type=="Unconsolidated" & t_surv==(q-1) )])
}
sum(n_t_surv)
fit.mle.unc <- mle(nll, start=list(s=0.5), method="Nelder-Mead")
summary(fit.mle.unc)
logLik(fit.mle.unc)
fit.mle.unc.log <- mle(nll2, start=list(s0=0.5,b=1.0), method="Nelder-Mead")
summary(fit.mle.unc.log)
logLik(fit.mle.unc.log)
# MODEL 3: Unc + Cons (1+1= 2 parameters)
model3 <- logLik(fit.mle.unc) + logLik(fit.mle.cons)
# MODEL 4: Unc + Cons.log (1+2= 3 parameters)
model4 <- logLik(fit.mle.unc) + logLik(fit.mle.cons.log)
# MODEL 5: Unc.log + Cons (2+1= 3 parameters)
model5 <- logLik(fit.mle.unc.log) + logLik(fit.mle.cons)
# MODEL 6: Unc.log + Cons.log (2+2= 4 parameters)
model6 <- logLik(fit.mle.unc.log) + logLik(fit.mle.cons.log)
#--- Performing the Likelihood Ratio Test
#-------------------------------------------------------------------------------------------------------------------
# - The difference (times 2) between the likelihood values for two nested models is approximated by a Chi squared distribution
# (minus 2 times the difference of log-likelihood values of the two models)
# - The null Chi squared is the distribution with the number of degrees of freedom equal to the number of parameters different between the models
# - Comparing 2 models: one habitat constant model and one habitat logistic model
# Repeat for all juvenile corals and for individual taxa
teststat12<- -2*(as.numeric(model1)-as.numeric(logLik(model2))) # df=2-1=1
chi12 <- pchisq(teststat12,df=1,lower.tail=FALSE)