-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfigure 4 experiment.R
280 lines (274 loc) · 12.4 KB
/
figure 4 experiment.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
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
############# my crouse figure 4 experiment
# change each value individually by 10%
#change p values
life_pi1 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "1", annual_survivorship * 1.1, annual_survivorship * 1)) # p for survival probility, i for increase, 1 for the stage that its p is being altered
life_pd1 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "1", annual_survivorship * 0.9, annual_survivorship * 1)) # d for decrease
life_pi2 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "2", annual_survivorship * 1.1, annual_survivorship * 1))
life_pd2 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "2", annual_survivorship * 0.9, annual_survivorship * 1))
life_pi3 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "3", annual_survivorship * 1.1, annual_survivorship * 1))
life_pd3 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "3", annual_survivorship * 0.9, annual_survivorship * 1))
life_pi4 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "4", annual_survivorship * 1.1, annual_survivorship * 1))
life_pd4 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "4", annual_survivorship * 0.9, annual_survivorship * 1))
life_pi5 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "5", annual_survivorship * 1.1, annual_survivorship * 1))
life_pd5 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "5", annual_survivorship * 0.9, annual_survivorship * 1))
life_pi6 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "6", annual_survivorship * 1.1, annual_survivorship * 1))
life_pd6 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "6", annual_survivorship * 0.9, annual_survivorship * 1))
life_pi7 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "7", annual_survivorship * 1.1, annual_survivorship * 1))
life_pd7 <- mutate(lifetable, annual_survivorship = ifelse(stage_number == "7", annual_survivorship * 0.9, annual_survivorship * 1))
#ds increasing and decreasing by 5%
life_di1 <- mutate(lifetable, stage_duration = ifelse(stage_number == "1", annual_survivorship * 1.1, annual_survivorship * 1))
life_dd1 <- mutate(lifetable, stage_duration = ifelse(stage_number == "1", annual_survivorship * 0.9, annual_survivorship * 1))
life_di2 <- mutate(lifetable, stage_duration = ifelse(stage_number == "2", annual_survivorship * 1.1, annual_survivorship * 1))
life_dd2 <- mutate(lifetable, stage_duration = ifelse(stage_number == "2", annual_survivorship * 0.9, annual_survivorship * 1))
life_di3 <- mutate(lifetable, stage_duration = ifelse(stage_number == "3", annual_survivorship * 1.1, annual_survivorship * 1))
life_dd3 <- mutate(lifetable, stage_duration = ifelse(stage_number == "3", annual_survivorship * 0.9, annual_survivorship * 1))
life_di4 <- mutate(lifetable, stage_duration = ifelse(stage_number == "4", annual_survivorship * 1.1, annual_survivorship * 1))
life_dd4 <- mutate(lifetable, stage_duration = ifelse(stage_number == "4", annual_survivorship * 0.9, annual_survivorship * 1))
life_di5 <- mutate(lifetable, stage_duration = ifelse(stage_number == "5", annual_survivorship * 1.1, annual_survivorship * 1))
life_dd5 <- mutate(lifetable, stage_duration = ifelse(stage_number == "5", annual_survivorship * 0.9, annual_survivorship * 1))
life_di6 <- mutate(lifetable, stage_duration = ifelse(stage_number == "6", annual_survivorship * 1.1, annual_survivorship * 1))
life_dd6 <- mutate(lifetable, stage_duration = ifelse(stage_number == "6", annual_survivorship * 0.9, annual_survivorship * 1))
life_di7 <- mutate(lifetable, stage_duration = ifelse(stage_number == "7", annual_survivorship * 1.1, annual_survivorship * 1))
life_dd7 <- mutate(lifetable, stage_duration = ifelse(stage_number == "7", annual_survivorship * 0.9, annual_survivorship * 1))
#make new matrices
mat_pi1 <- myFunc(life_pi1)
mat_pd1 <- myFunc(life_pd1)
mat_pi2 <- myFunc(life_pi2)
mat_pd2 <- myFunc(life_pd2)
mat_pi3 <- myFunc(life_pi3)
mat_pd3 <- myFunc(life_pd3)
mat_pi4 <- myFunc(life_pi4)
mat_pd4 <- myFunc(life_pd4)
mat_pi5 <- myFunc(life_pi5)
mat_pd5 <- myFunc(life_pd5)
mat_pi6<- myFunc(life_pi6)
mat_pd6 <- myFunc(life_pd6)
mat_pi7 <- myFunc(life_pi7)
mat_pd7 <- myFunc(life_pd7)
mat_di1 <- myFunc(life_di1)
mat_dd1 <- myFunc(life_dd1)
mat_di2 <- myFunc(life_di2)
mat_dd2 <- myFunc(life_dd2)
mat_di3 <- myFunc(life_di3)
mat_dd3 <- myFunc(life_dd3)
mat_di4 <- myFunc(life_di4)
mat_dd4 <- myFunc(life_dd4)
mat_di5 <- myFunc(life_di5)
mat_dd5 <- myFunc(life_dd5)
mat_di6 <- myFunc(life_di6)
mat_dd6 <- myFunc(life_dd6)
mat_di7 <- myFunc(life_di7)
mat_dd7 <- myFunc(life_dd7)
#sensitivity of projection matrices
#eigen analysis
eigs_pi1 <- eigen(mat_pi1)
eigs_di1 <- eigen(mat_di1)
eigs_pi2 <- eigen(mat_pi2)
eigs_di2 <- eigen(mat_di2)
eigs_pi3 <- eigen(mat_pi3)
eigs_di3 <- eigen(mat_di3)
eigs_pi4 <- eigen(mat_pi4)
eigs_di4 <- eigen(mat_di4)
eigs_pi5 <- eigen(mat_pi5)
eigs_di5 <- eigen(mat_di5)
eigs_pi6 <- eigen(mat_pi6)
eigs_di6 <- eigen(mat_di6)
eigs_pi7 <- eigen(mat_pi7)
eigs_di7 <- eigen(mat_di7)
eigs_pd1 <- eigen(mat_pd1)
eigs_dd1 <- eigen(mat_dd1)
eigs_pd2 <- eigen(mat_pd2)
eigs_dd2 <- eigen(mat_dd2)
eigs_pd3 <- eigen(mat_pd3)
eigs_dd3 <- eigen(mat_dd3)
eigs_pd4 <- eigen(mat_pd4)
eigs_dd4 <- eigen(mat_dd4)
eigs_pd5 <- eigen(mat_pd5)
eigs_dd5 <- eigen(mat_dd5)
eigs_pd6 <- eigen(mat_pd6)
eigs_dd6 <- eigen(mat_dd6)
eigs_pd7 <- eigen(mat_pd7)
eigs_dd7 <- eigen(mat_dd7)
#finding first eigenvalue
#1
dom.pos.pi1 <- which.max(eigs_pi1[["values"]])
Lpi1 <- Re(eigs_pi1[["values"]][dom.pos.pi1])
dom.pos.di1 <- which.max(eigs_di1[["values"]])
Ldi1 <- Re(eigs_di1[["values"]][dom.pos.di1])
dom.pos.pd1 <- which.max(eigs_pd1[["values"]])
Lpd1 <- Re(eigs_pd1[["values"]][dom.pos.pd1])
dom.pos.dd1 <- which.max(eigs_dd1[["values"]])
Ldd1 <- Re(eigs_dd1[["values"]][dom.pos.dd1])
#2
dom.pos.pi2 <- which.max(eigs_pi2[["values"]])
Lpi2 <- Re(eigs_pi2[["values"]][dom.pos.pi2])
dom.pos.di2 <- which.max(eigs_di2[["values"]])
Ldi2 <- Re(eigs_di2[["values"]][dom.pos.di2])
dom.pos.pd2 <- which.max(eigs_pd2[["values"]])
Lpd2 <- Re(eigs_pd2[["values"]][dom.pos.pd2])
dom.pos.dd2 <- which.max(eigs_dd2[["values"]])
Ldd2 <- Re(eigs_dd2[["values"]][dom.pos.dd2])
#3
dom.pos.pi3 <- which.max(eigs_pi3[["values"]])
Lpi3 <- Re(eigs_pi3[["values"]][dom.pos.pi3])
dom.pos.di3 <- which.max(eigs_di3[["values"]])
Ldi3 <- Re(eigs_di3[["values"]][dom.pos.di3])
dom.pos.pd3 <- which.max(eigs_pd3[["values"]])
Lpd3 <- Re(eigs_pd3[["values"]][dom.pos.pd3])
dom.pos.dd3 <- which.max(eigs_dd3[["values"]])
Ldd3 <- Re(eigs_dd3[["values"]][dom.pos.dd3])
#4
dom.pos.pi4 <- which.max(eigs_pi4[["values"]])
Lpi4 <- Re(eigs_pi4[["values"]][dom.pos.pi4])
dom.pos.di4 <- which.max(eigs_di4[["values"]])
Ldi4 <- Re(eigs_di4[["values"]][dom.pos.di4])
dom.pos.pd4 <- which.max(eigs_pd4[["values"]])
Lpd4 <- Re(eigs_pd4[["values"]][dom.pos.pd4])
dom.pos.dd4 <- which.max(eigs_dd4[["values"]])
Ldd4 <- Re(eigs_dd4[["values"]][dom.pos.dd4])
#5
dom.pos.pi5 <- which.max(eigs_pi5[["values"]])
Lpi5 <- Re(eigs_pi5[["values"]][dom.pos.pi5])
dom.pos.di5 <- which.max(eigs_di5[["values"]])
Lpd5 <- Re(eigs_di5[["values"]][dom.pos.di5])
dom.pos.pd5 <- which.max(eigs_pd5[["values"]])
Lpd5 <- Re(eigs_pd5[["values"]][dom.pos.pd5])
dom.pos.dd5 <- which.max(eigs_dd5[["values"]])
Ldd5 <- Re(eigs_dd5[["values"]][dom.pos.dd5])
#6
dom.pos.pi6 <- which.max(eigs_pi6[["values"]])
Lpi6 <- Re(eigs_pi6[["values"]][dom.pos.pi6])
dom.pos.di6 <- which.max(eigs_di6[["values"]])
Lpi6 <- Re(eigs_di6[["values"]][dom.pos.di6])
dom.pos.pd6 <- which.max(eigs_pd6[["values"]])
Lpd6 <- Re(eigs_pd6[["values"]][dom.pos.pd6])
dom.pos.dd6 <- which.max(eigs_dd6[["values"]])
Ldd6 <- Re(eigs_dd6[["values"]][dom.pos.dd6])
#7
dom.pos.pi7 <- which.max(eigs_pi7[["values"]])
Lpi7 <- Re(eigs_pi7[["values"]][dom.pos.pi7])
dom.pos.di7 <- which.max(eigs_di7[["values"]])
Lpi7 <- Re(eigs_di7[["values"]][dom.pos.di7])
dom.pos.pd7 <- which.max(eigs_pd7[["values"]])
Lpd7 <- Re(eigs_pd7[["values"]][dom.pos.pd7])
dom.pos.dd7 <- which.max(eigs_dd7[["values"]])
Ldd7 <- Re(eigs_dd7[["values"]][dom.pos.dd7])
wd1 <- Re(eigs_pd1[["vectors"]][, dom.pos]) #just 5% decrese in p for now
wd2 <- Re(eigs_pd2[["vectors"]][, dom.pos])
wd3 <- Re(eigs_pd3[["vectors"]][, dom.pos])
wd4 <- Re(eigs_pd4[["vectors"]][, dom.pos])
wd5 <- Re(eigs_pd5[["vectors"]][, dom.pos])
wd6 <- Re(eigs_pd6[["vectors"]][, dom.pos])
wd7 <- Re(eigs_pd7[["vectors"]][, dom.pos])
wdd1 <- Re(eigs_dd1[["vectors"]][, dom.pos]) #just 5% decrese in p for now
wdd2 <- Re(eigs_dd2[["vectors"]][, dom.pos])
wdd3 <- Re(eigs_dd3[["vectors"]][, dom.pos])
wdd4 <- Re(eigs_dd4[["vectors"]][, dom.pos])
wdd5 <- Re(eigs_dd5[["vectors"]][, dom.pos])
wdd6 <- Re(eigs_dd6[["vectors"]][, dom.pos])
wdd7 <- Re(eigs_dd7[["vectors"]][, dom.pos])
#calculating reproductive value
t <- 20 #random for now
#ps
Md1 <- eigen(t(mat_pd1))
Md2 <- eigen(t(mat_pd2))
Md3 <- eigen(t(mat_pd3))
Md4 <- eigen(t(mat_pd4))
Md5 <- eigen(t(mat_pd5))
Md6 <- eigen(t(mat_pd6))
Md7 <- eigen(t(mat_pd7))
#ds
Mdd1 <- eigen(t(mat_dd1))
Mdd2 <- eigen(t(mat_dd2))
Mdd3 <- eigen(t(mat_dd3))
Mdd4 <- eigen(t(mat_dd4))
Mdd5 <- eigen(t(mat_dd5))
Mdd6 <- eigen(t(mat_dd6))
Mdd7 <- eigen(t(mat_dd7))
#ps
vd1 <- Re(Md1$vectors[,which.max(Re(Md1$values))])
vd2 <- Re(Md2$vectors[,which.max(Re(Md2$values))])
vd3 <- Re(Md3$vectors[,which.max(Re(Md3$values))])
vd4 <- Re(Md4$vectors[,which.max(Re(Md4$values))])
vd5 <- Re(Md5$vectors[,which.max(Re(Md5$values))])
vd6 <- Re(Md6$vectors[,which.max(Re(Md6$values))])
vd7 <- Re(Md7$vectors[,which.max(Re(Md7$values))])
#ds
vdd1 <- Re(Mdd1$vectors[,which.max(Re(Mdd1$values))])
vdd2 <- Re(Mdd2$vectors[,which.max(Re(Mdd2$values))])
vdd3 <- Re(Mdd3$vectors[,which.max(Re(Mdd3$values))])
vdd4 <- Re(Mdd4$vectors[,which.max(Re(Mdd4$values))])
vdd5 <- Re(Mdd5$vectors[,which.max(Re(Mdd5$values))])
vdd6 <- Re(Mdd6$vectors[,which.max(Re(Mdd6$values))])
vdd7 <- Re(Mdd7$vectors[,which.max(Re(Mdd7$values))])
#ps
vw.sd1 <- vd1 %*% t (wd1)
Sd1 <- vw.sd1/as.numeric(vd1 %*% wd1)
vw.sd2 <- vd2 %*% t (wd2)
Sd2 <- vw.sd2/as.numeric(vd2 %*% wd2)
vw.sd3 <- vd3 %*% t (wd3)
Sd3 <- vw.sd3/as.numeric(vd3 %*% wd3)
vw.sd4 <- vd4 %*% t (wd4)
Sd4 <- vw.sd4/as.numeric(vd4 %*% wd4)
vw.sd5 <- vd5 %*% t (wd5)
Sd5 <- vw.sd5/as.numeric(vd5 %*% wd5)
vw.sd6 <- vd6 %*% t (wd6)
Sd6 <- vw.sd6/as.numeric(vd6 %*% wd6)
vw.sd7 <- vd7 %*% t (wd7)
Sd7 <- vw.sd7/as.numeric(vd7 %*% wd7)
#ds
vw.sdd1 <- vdd1 %*% t (wdd1)
Sdd1 <- vw.sdd1/as.numeric(vdd1 %*% wdd1)
vw.sdd2 <- vdd2 %*% t (wdd2)
Sdd2 <- vw.sdd2/as.numeric(vdd2 %*% wdd2)
vw.sdd3 <- vdd3 %*% t (wdd3)
Sdd3 <- vw.sdd3/as.numeric(vdd3 %*% wdd3)
vw.sdd4 <- vdd4 %*% t (wdd4)
Sdd4 <- vw.sd4/as.numeric(vdd4 %*% wdd4)
vw.sdd5 <- vdd5 %*% t (wdd5)
Sdd5 <- vw.sdd5/as.numeric(vdd5 %*% wdd5)
vw.sdd6 <- vdd6 %*% t (wdd6)
Sdd6 <- vw.sd6/as.numeric(vdd6 %*% wdd6)
vw.sdd7 <- vdd7 %*% t (wdd7)
Sdd7 <- vw.sdd7/as.numeric(vdd7 %*% wdd7)
#elasticity of projection matrices
elasd1 <- (mat_pd1/Lpd1) * Sd1
elasticity1 <- round(elas, 3)
elasd2 <- (mat_pd2/Lpd1) * Sd2
elasticity2 <- round(elas, 3)
elasd3 <- (mat_pd3/Lpd1) * Sd3
elasticity3 <- round(elas, 3)
elasd4 <- (mat_pd4/Lpd1) * Sd4
elasticity4 <- round(elas, 3)
elasd5 <- (mat_pd5/Lpd1) * Sd5
elasticity5 <- round(elas, 3)
elasd6 <- (mat_pd6/Lpd1) * Sd6
elasticity6 <- round(elas, 3)
elasd7 <- (mat_pd7/Lpd1) * Sd7
elasticity7 <- round(elas, 3)
#elasticity of projection matrices for changes in d
elasdd1 <- (mat_dd1/Ldd1) * Sdd1
elasticityd1 <- round(elas, 3)
elasdd2 <- (mat_dd2/Ldd1) * Sdd2
elasticityd2 <- round(elas, 3)
elasdd3 <- (mat_dd3/Ldd1) * Sdd3
elasticityd3 <- round(elas, 3)
elasdd4 <- (mat_dd4/Ldd1) * Sdd4
elasticityd4 <- round(elas, 3)
elasdd5 <- (mat_dd5/Ldd1) * Sdd5
elasticityd5 <- round(elas, 3)
elasdd6 <- (mat_dd6/Ldd1) * Sdd6
elasticityd6 <- round(elas, 3)
elasdd7 <- (mat_dd7/Ldd1) * Sdd7
elasticityd7 <- round(elas, 3)
### figure 4 - plot the proportional sensitivity to changes in survival probability, p, and stage duration, d.
stage <- c(1:7)
P <- c(elasticity1[1,1], elasticity2[2,2], elasticity3[3,3], elasticity4[4,4], elasticity5[5,5], elasticity6[6,6], elasticity7[7,7])
sensi <- data.frame(stage, P)
fig.4a <- ggplot(sensi, aes(x = stage, y = P)) + geom_line() + geom_point(size = 4) + labs(x = "Stage", y = "Elasticity")
fig.4a
stage <- c(1:7)
P <- c(elasticityd1[1,1], elasticityd2[2,2], elasticityd3[3,3], elasticityd4[4,4], elasticityd5[5,5], elasticityd6[6,6], elasticityd7[7,7])
sensi <- data.frame(stage, P)
fig.4b <- ggplot(sensi, aes(x = stage, y = P)) + geom_line() + geom_point(size = 4) + labs(x = "Stage", y = "Elasticity")
fig.4b