-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sobol_analysis_intervention_event_individual.R
505 lines (438 loc) · 21.3 KB
/
Sobol_analysis_intervention_event_individual.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# This script performs sobol sensitivity analysis of simple TB models
# uses the sensobol package to generate the matrices (using sobol sequences)
# uses the sensitivity package to calculate the indices (using the jansen method)
# Looks at the sensitivity of impact of a hypothetical mass screening to:
# model choice, natural history parameters, input data, intervention parameters
# in this version all inputs are considered seperatley
# Set the working directory
setwd("~/GitHub/TB_structure_analysis")
# USER DEFINED INPUTS ##########################################################
# Set the number of samples
N_sobol <- 5000
# Set the data set to use for baseline incidence. Currently one of: global, PHI
inc_source <- "PHI"
# and the distribution to assume. Currently one of: unif, lnorm
inc_dist <- "lnorm"
# set the time to run the intervention for
tend <- 10
times=seq(0,tend)
################################################################################
# load libraries
require(deSolve)
require(ggplot2)
require(reshape2)
require(stats)
library(FME)
library(rriskDistributions)
library(epiR)
library(sensitivity)
library(sensobol)
library(dplyr)
# define palette for plotting
# colorBlind <- c("#000000", "#E69F00", "#56B4E9", "#009E73",
# "#F0E442", "#0072B2", "#D55E00", "#CC79A7","#999999")
colorBlind <- c("#88CCEE", "#CC6677", "#DDCC77", "#117733", "#332288", "#AA4499",
"#44AA99", "#999933", "#882255", "#661100", "#6699CC", "#888888")
# load the TB models
source("Model_1_intervention_event.R")
source("Model_2_intervention_event.R")
# load the event function for the intervention
source("intervention_event.R")
# fit distributions to parameters
source("Par_dist_gen.R")
# define inputs
params <- c("Progression model", "Reinfection model",
"RR of re-infection","TB mortality","Self-cure",
"Slow progression","Transition to remote infection",
"Proportion fast","Fast progression (1)","Fast progression (2)",
"Baseline incidence","Treatment success","Case detection ratio",
"ACF sensitivity","Treatment uptake",
"TST completion","TST sensitivity","PT uptake","PT completion","PT efficacy")
kpars <- length(params)
k2pars <- kpars+2
# Create Sobol matrices - default is to use Sobol low discrepancy sequences (U[0,1])
# Use sensobol package to do this
mat <- sobol_matrices(N = N_sobol,params = params)
# Then convert to the correct distributions
# natural history parameters
mat[,"RR of re-infection"] <- qbeta(mat[,"RR of re-infection"],fit_q[1],fit_q[2])
mat[,"TB mortality"] <- qlnorm(mat[,"TB mortality"],fit_m[1],fit_m[2])
mat[,"Self-cure"] <- qlnorm(mat[,"Self-cure"],fit_w[1],fit_w[2])
mat[,"Slow progression"] <- qlnorm(mat[,"Slow progression"],fit_v[1],fit_v[2])
mat[,"Transition to remote infection"] <- qlnorm(mat[,"Transition to remote infection"],fit_k[1],fit_k[2])
mat[,"Proportion fast"] <- qbeta(mat[,"Proportion fast"],fit_g[1],fit_g[2])
mat[,"Fast progression (1)"] <- qlnorm(mat[,"Fast progression (1)"],fit_e[1,1],fit_e[2,1])
mat[,"Fast progression (2)"] <- qlnorm(mat[,"Fast progression (2)"],fit_e[1,2],fit_e[2,2])
# model structures
mm <- rep(0,dim(mat)[1])
zz <- mm
for (ii in 1:dim(mat)[1]){
if (mat[ii,"Progression model"]>=0.5) mm[ii] <- 1
if (mat[ii,"Progression model"]<0.5) mm[ii] <- 2
if (mat[ii,"Reinfection model"]>=0.5) zz[ii] <- 1
if (mat[ii,"Reinfection model"]<0.5) zz[ii] <- 2
}
# inputs
assign("fit_inc",get(paste("inc_",inc_source,"_",inc_dist,sep="")))
if (inc_dist=="unif")
mat[,"Baseline incidence"] <- qunif(mat[,"Baseline incidence"],fit_inc[1],fit_inc[2])/100000
if (inc_dist=="lnorm")
mat[,"Baseline incidence"] <- qlnorm(mat[,"Baseline incidence"],fit_inc[1],fit_inc[2])/100000
mat[,"Case detection ratio"] <- qunif(mat[,"Case detection ratio"],fit_CDR[1],fit_CDR[2])
mat[,"Treatment success"] <- qunif(mat[,"Treatment success"],fit_tau[1],fit_tau[2])
# Intervention parameters
mat[,"ACF sensitivity"] <- qbeta(mat[,"ACF sensitivity"],fit_ACF_sens[1],fit_ACF_sens[2])
mat[,"Treatment uptake"] <- qbeta(mat[,"Treatment uptake"],fit_treat_uptake[1],fit_treat_uptake[2])
mat[,"TST completion"] <- qbeta(mat[,"TST completion"],fit_TST_comp[1],fit_TST_comp[2])
mat[,"TST sensitivity"] <- qbeta(mat[,"TST sensitivity"],fit_TST_sens[1],fit_TST_sens[2])
mat[,"PT uptake"] <- qbeta(mat[,"PT uptake"],fit_PT_start[1],fit_PT_start[2])
mat[,"PT completion"] <- qbeta(mat[,"PT completion"],fit_PT_comp[1],fit_PT_comp[2])
mat[,"PT efficacy"] <- 1-qbeta(mat[,"PT efficacy"],fit_PT_eff[1],fit_PT_eff[2])
# split out the A and B matrices for the sensitivity package
x1 <- mat[1:N_sobol,]
x2 <- mat[(N_sobol+1):(2*N_sobol),]
# generate the input matrix for the Sobol analysis - sets model as NULL as we run the model separately below
sobol_calc <- soboljansen(model = NULL, x1, x2, nboot = 1000)
x <- sobol_calc$X
# array to store model outputs
y <- mat.or.vec(dim(x)[1],6)
beta_out <- rep(0,dim(x)[1])
# set the intervention coverage
coverage_ACF <- 0.7
coverage_PT <- 0.7
# Run the model for each row of x, return % reduction in TB incidence and mortality (per 100k) after 1 and 10 yrs
source("Run_model.R")
# calculate sobol indices - this passes the model outputs back to the sobol function to calculate the indices
# we do this for each output (incidence, mortality) at 1 and 10 years
xtemp <- sobol_calc
tell(xtemp,inc_drop_1,return.var=c("S.boot","T.boot"))
x_sens_inc_1 <- xtemp
xtemp <- sobol_calc
tell(xtemp,inc_drop_10,return.var=c("S.boot","T.boot"))
x_sens_inc_10 <- xtemp
xtemp <- sobol_calc
tell(xtemp,mort_drop_1,return.var=c("S.boot","T.boot"))
x_sens_mort_1 <- xtemp
xtemp <- sobol_calc
tell(xtemp,mort_drop_10,return.var=c("S.boot","T.boot"))
x_sens_mort_10 <- xtemp
# calculate indices for a dummy parameter and bootstrap
# use approach in https://click.endnote.com/viewer?doi=10.1016%2Fj.envsoft.2017.02.001&token=WzI5NzA3MzcsIjEwLjEwMTYvai5lbnZzb2Z0LjIwMTcuMDIuMDAxIl0.qaCejmzKjLqhkWIJ3UhiKwdr7qU
# uses model outputs that correspond to input matrices x1 and x2
N_boot <- 1000
Sd_boot <- mat.or.vec(N_boot,4)
Td_boot <- Sd_boot
#combine the 4 outputs of interest so we can do this in a loop
outputs <- cbind(inc_drop_1,inc_drop_10,mort_drop_1,mort_drop_10)
for(i in 1:N_boot){
boot_i <- sample(seq(1,N_sobol),N_sobol,replace=TRUE)
for (o in 1:4){
#calculate f02 - eqn 7
f02 <- (1/N_sobol)*sum(outputs[boot_i,o]*outputs[(boot_i+N_sobol),o])
#calculate total variance - eqn 6
Vtot <- (1/((2*N_sobol)-1))*sum(outputs[boot_i,o]^2+outputs[(boot_i+N_sobol),o]^2)-f02
#calculate partial variance Vd - eqn 12
Vd <- (1/(N_sobol-1))*sum(outputs[boot_i,o]*outputs[(boot_i+N_sobol),o])-f02
#calculate partial variance V~d - eqn 13
Vnotd <- (1/(N_sobol-1))*sum(outputs[(boot_i+N_sobol),o]*outputs[(boot_i+N_sobol),o])-f02
#calculate Sd - eqn 3
Sd_boot[i,o] <- Vd/Vtot
#calculate Td - eqn 4
Td_boot[i,o] <- 1-(Vnotd/Vtot)
}
}
Sd <- cbind(apply(Sd_boot, 2, quantile, probs = c(0.5), na.rm = TRUE),
apply(Sd_boot, 2, quantile, probs = c(0.025), na.rm = TRUE),
apply(Sd_boot, 2, quantile, probs = c(0.975), na.rm = TRUE))
Td <- cbind(apply(Td_boot, 2, quantile, probs = c(0.5), na.rm = TRUE),
apply(Td_boot, 2, quantile, probs = c(0.025), na.rm = TRUE),
apply(Td_boot, 2, quantile, probs = c(0.975), na.rm = TRUE))
# combine these so we can add to the plot
dummy_ind <- cbind(rbind(Sd,Td),
rep(c(rep("Incidence",2),rep("Mortality",2)),2),
c(rep("Si",4),rep("Ti",4)),
rep(c("1yr","10yr"),4))
colnames(dummy_ind) <- c("original","min","max","variable","index","time")
dummy_ind <- as.data.frame(dummy_ind)
# combine results for plotting
df1 <- data.frame(cbind(x_sens_inc_1$S[,c(1,4,5)],params,"Incidence","1yr","Si",0))
df2 <- data.frame(cbind(x_sens_inc_10$S[,c(1,4,5)],params,"Incidence","10yr","Si",0))
df3 <- data.frame(cbind(x_sens_mort_1$S[,c(1,4,5)],params,"Mortality","1yr","Si",0))
df4 <- data.frame(cbind(x_sens_mort_10$S[,c(1,4,5)],params,"Mortality","10yr","Si",0))
df5 <- data.frame(cbind(x_sens_inc_1$T[,c(1,4,5)],params,"Incidence","1yr","Ti",0))
df6 <- data.frame(cbind(x_sens_inc_10$T[,c(1,4,5)],params,"Incidence","10yr","Ti",0))
df7 <- data.frame(cbind(x_sens_mort_1$T[,c(1,4,5)],params,"Mortality","1yr","Ti",0))
df8 <- data.frame(cbind(x_sens_mort_10$T[,c(1,4,5)],params,"Mortality","10yr","Ti",0))
colnames(df1) <- c("original","min","max","parameter","variable","time","index","sig")
names(df8) <- names(df7) <- names(df6) <- names(df5) <- names(df4) <- names(df3) <- names(df2) <- names(df1)
ind_to_plot <- rbind(df1,df2,df3,df4,df5,df6,df7,df8)
# set negative values to 0
tempa <- ind_to_plot
tempa[tempa$original<0,"original"] <- 0
tempa[tempa$min<0,"min"] <- 0
tempa[tempa$max<0,"max"] <- 0
# sum up main effects (Si) - less than one indicates interactions
aggregate(cbind(original=tempa$original,
min=tempa$min,
max=tempa$max), by=list(time=tempa$time,
variable=tempa$variable,
index=tempa$index),FUN=sum)
# Compare input indices to the dummy values
# If they overlap then conclude that input is not important
# For each input and each output, check if the lower value of the Si is less than the upper value of the dummy Si
for (ii in c("Incidence","Mortality")){
for (jj in c("1yr","10yr")){
for (kk in c("Si","Ti")){
temp_ind <- ind_to_plot[ind_to_plot$variable==ii&
ind_to_plot$time==jj&
ind_to_plot$index==kk,]
temp_dummy <-dummy_ind[dummy_ind$variable==ii&
dummy_ind$time==jj&
dummy_ind$index==kk,]
ind_to_plot[ind_to_plot$variable==ii&
ind_to_plot$time==jj&
ind_to_plot$index==kk,"sig"] <- as.numeric(as.character(temp_ind$min)) > as.numeric(as.character(temp_dummy$max))
}
}
}
ind_to_plot$par_order <- factor(ind_to_plot$parameter,
levels = c("Progression model", "Reinfection model",
"RR of re-infection","TB mortality","Self-cure",
"Slow progression","Transition to remote infection",
"Proportion fast","Fast progression (1)","Fast progression (2)",
"Baseline incidence","Treatment success","Case detection ratio",
"ACF sensitivity","Treatment uptake",
"TST completion","TST sensitivity","PT uptake","PT completion","PT efficacy"))
ind_to_plot$time_order <- factor(ind_to_plot$time,
levels=c("1yr","10yr"))
ind_plot <- ggplot(ind_to_plot,aes(par_order,original))+
geom_col(aes(fill=index,group=index,alpha=as.factor(sig)),colour="black",position=position_dodge())+
geom_errorbar(aes(ymin=min, ymax=max,group=index), width=.2,position=position_dodge(0.9))+
facet_grid(time_order~variable)+
scale_x_discrete(guide = guide_axis(angle = 90))+
ylab("Value of index")+
xlab("")+
scale_fill_manual(values=colorBlind,name="")+
#scale_color_manual(values=colorBlindGrey8)+
theme_bw()+
theme(legend.position="bottom")+
theme(strip.background =element_rect(fill="white"))+
guides(alpha = "none")+
scale_y_continuous(expand = c(0, 0))+
scale_alpha_manual(values=c(0.2,0.8))+
geom_vline(xintercept=c(2.5,10.5,13.5,15.5),linetype="dashed")
# Sum up values across the groups used in group analysis to compare
nat_params <- c("RR of re-infection","TB mortality","Self-cure",
"Slow progression","Transition to remote infection",
"Proportion fast","Fast progression (1)","Fast progression (2)") # natural history parameters
ACF_params <- c("ACF sensitivity","Treatment uptake")
PT_params <- c("TST completion","TST sensitivity","PT uptake",
"PT completion","PT efficacy")
in_params <- c( "Baseline incidence","Treatment success","Case detection ratio")
mod_params <- c("Progression model","Reinfection model")
temp <- ind_to_plot[ind_to_plot$parameter%in%nat_params,]
sum_nat_indices <- aggregate(temp$original, by=list(Time=temp$time,outputs=temp$variable,index=temp$index), FUN=sum)
temp <- ind_to_plot[ind_to_plot$parameter%in%ACF_params,]
sum_ACF_indices <- aggregate(temp$original, by=list(Time=temp$time,outputs=temp$variable,index=temp$index), FUN=sum)
temp <- ind_to_plot[ind_to_plot$parameter%in%PT_params,]
sum_PT_indices <- aggregate(temp$original, by=list(Time=temp$time,outputs=temp$variable,index=temp$index), FUN=sum)
temp <- ind_to_plot[ind_to_plot$parameter%in%in_params,]
sum_in_indices <- aggregate(temp$original, by=list(Time=temp$time,outputs=temp$variable,index=temp$index), FUN=sum)
temp <- ind_to_plot[ind_to_plot$parameter%in%mod_params,]
sum_mod_indices <- aggregate(temp$original, by=list(Time=temp$time,outputs=temp$variable,index=temp$index), FUN=sum)
# Check convergence of sobol indices by recalculating for sub-samples of the original matrix
# Due to sampling scheme, need to select appropriate elements for sub-sample (i.e. can't just pick randomly)
sub_step <- 100
sub_seq <- seq(sub_step,N_sobol,sub_step) # sequence of sub-sample sizes to use
sub_S_inc_1 <- array(0,c(length(sub_seq),kpars,3))
sub_S_inc_10 <- sub_S_inc_1
sub_S_mort_1 <- sub_S_inc_1
sub_S_mort_10 <- sub_S_inc_1
sub_T_inc_1 <- sub_S_inc_1
sub_T_inc_10 <- sub_S_inc_1
sub_T_mort_1 <- sub_S_inc_1
sub_T_mort_10 <- sub_S_inc_1
for(i in 1:length(sub_seq)){
N_sub <- sub_seq[i] # set size of sub-sample
x1sub <- x1[1:N_sub,] # get x1
x2sub <- x2[1:N_sub,] # get x2
xsub <- soboljansen(model = NULL, x1sub, x2sub, nboot = 1000) # generate x
# get the rows of the model output to use for the sub-sample
tt <- split(seq(1:dim(sobol_calc$X)[1]),ceiling(seq_along(seq(1:dim(sobol_calc$X)[1]))/N_sobol))
tt <- matrix(unlist(tt),ncol=k2pars)
ttt <- as.vector(tt[1:N_sub,])
# re-calculate indices
Ysub <- inc_drop_1[ttt]
xtemp <- xsub
tell(xtemp,Ysub)
sub_S_inc_1[i,,1] <- xtemp$S[,"original"]
sub_T_inc_1[i,,1] <- xtemp$T[,"original"]
sub_S_inc_1[i,,2] <- xtemp$S[,"min. c.i."]
sub_T_inc_1[i,,2] <- xtemp$T[,"min. c.i."]
sub_S_inc_1[i,,3] <- xtemp$S[,"max. c.i."]
sub_T_inc_1[i,,3] <- xtemp$T[,"max. c.i."]
Ysub <- inc_drop_10[ttt]
xtemp <- xsub
tell(xtemp,Ysub)
sub_S_inc_10[i,,1] <- xtemp$S[,"original"]
sub_T_inc_10[i,,1] <- xtemp$T[,"original"]
sub_S_inc_10[i,,2] <- xtemp$S[,"min. c.i."]
sub_T_inc_10[i,,2] <- xtemp$T[,"min. c.i."]
sub_S_inc_10[i,,3] <- xtemp$S[,"max. c.i."]
sub_T_inc_10[i,,3] <- xtemp$T[,"max. c.i."]
Ysub <- mort_drop_1[ttt]
xtemp <- xsub
tell(xtemp,Ysub)
sub_S_mort_1[i,,1] <- xtemp$S[,"original"]
sub_T_mort_1[i,,1] <- xtemp$T[,"original"]
sub_S_mort_1[i,,2] <- xtemp$S[,"min. c.i."]
sub_T_mort_1[i,,2] <- xtemp$T[,"min. c.i."]
sub_S_mort_1[i,,3] <- xtemp$S[,"max. c.i."]
sub_T_mort_1[i,,3] <- xtemp$T[,"max. c.i."]
Ysub <- mort_drop_10[ttt]
xtemp <- xsub
tell(xtemp,Ysub)
sub_S_mort_10[i,,1] <- xtemp$S[,"original"]
sub_T_mort_10[i,,1] <- xtemp$T[,"original"]
sub_S_mort_10[i,,2] <- xtemp$S[,"min. c.i."]
sub_T_mort_10[i,,2] <- xtemp$T[,"min. c.i."]
sub_S_mort_10[i,,3] <- xtemp$S[,"max. c.i."]
sub_T_mort_10[i,,3] <- xtemp$T[,"max. c.i."]
}
# rearrange sub-sample data to plot
# incidence
tempS <- cbind(sub_seq,sub_S_inc_1[,,1])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS1 <- melt(tempS,id.vars=c("N"))
tempS <- cbind(sub_seq,sub_S_inc_1[,,2])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS2 <- melt(tempS,id.vars=c("N"))
tempS <- cbind(sub_seq,sub_S_inc_1[,,3])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS3 <- melt(tempS,id.vars=c("N"))
tempSI1 <- cbind(tempS1,tempS2[,3],tempS3[,3],"Incidence","1yr","Si")
colnames(tempSI1) <- c("N","parameter","original","min","max","variable","time","index")
tempS <- cbind(sub_seq,sub_S_inc_10[,,1])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS1 <- melt(tempS,id.vars=c("N"))
tempS <- cbind(sub_seq,sub_S_inc_10[,,2])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS2 <- melt(tempS,id.vars=c("N"))
tempS <- cbind(sub_seq,sub_S_inc_10[,,3])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS3 <- melt(tempS,id.vars=c("N"))
tempSI10 <- cbind(tempS1,tempS2[,3],tempS3[,3],"Incidence","10yr","Si")
colnames(tempSI10) <- c("N","parameter","original","min","max","variable","time","index")
tempT <- cbind(sub_seq,sub_T_inc_1[,,1])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT1 <- melt(tempT,id.vars=c("N"))
tempT <- cbind(sub_seq,sub_T_inc_1[,,2])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT2 <- melt(tempT,id.vars=c("N"))
tempT <- cbind(sub_seq,sub_T_inc_1[,,3])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT3 <- melt(tempT,id.vars=c("N"))
tempTI1 <- cbind(tempT1,tempT2[,3],tempT3[,3],"Incidence","1yr","Ti")
colnames(tempTI1) <- c("N","parameter","original","min","max","variable","time","index")
tempT <- cbind(sub_seq,sub_T_inc_10[,,1])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT1 <- melt(tempT,id.vars=c("N"))
tempT <- cbind(sub_seq,sub_T_inc_10[,,2])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT2 <- melt(tempT,id.vars=c("N"))
tempT <- cbind(sub_seq,sub_T_inc_10[,,3])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT3 <- melt(tempT,id.vars=c("N"))
tempTI10 <- cbind(tempT1,tempT2[,3],tempT3[,3],"Incidence","10yr","Ti")
colnames(tempTI10) <- c("N","parameter","original","min","max","variable","time","index")
tempI <- rbind(tempSI1,tempSI10,tempTI1,tempTI10)
# Mortality
tempS <- cbind(sub_seq,sub_S_mort_1[,,1])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS1 <- melt(tempS,id.vars=c("N"))
tempS <- cbind(sub_seq,sub_S_mort_1[,,2])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS2 <- melt(tempS,id.vars=c("N"))
tempS <- cbind(sub_seq,sub_S_mort_1[,,3])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS3 <- melt(tempS,id.vars=c("N"))
tempSM1 <- cbind(tempS1,tempS2[,3],tempS3[,3],"Mortality","1yr","Si")
colnames(tempSM1) <- c("N","parameter","original","min","max","variable","time","index")
tempS <- cbind(sub_seq,sub_S_mort_10[,,1])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS1 <- melt(tempS,id.vars=c("N"))
tempS <- cbind(sub_seq,sub_S_mort_10[,,2])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS2 <- melt(tempS,id.vars=c("N"))
tempS <- cbind(sub_seq,sub_S_mort_10[,,3])
colnames(tempS) <- c("N",params)
tempS <- as.data.frame(tempS)
tempS3 <- melt(tempS,id.vars=c("N"))
tempSM10 <- cbind(tempS1,tempS2[,3],tempS3[,3],"Mortality","10yr","Si")
colnames(tempSM10) <- c("N","parameter","original","min","max","variable","time","index")
tempT <- cbind(sub_seq,sub_T_mort_1[,,1])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT1 <- melt(tempT,id.vars=c("N"))
tempT <- cbind(sub_seq,sub_T_mort_1[,,2])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT2 <- melt(tempT,id.vars=c("N"))
tempT <- cbind(sub_seq,sub_T_mort_1[,,3])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT3 <- melt(tempT,id.vars=c("N"))
tempTM1 <- cbind(tempT1,tempT2[,3],tempT3[,3],"Mortality","1yr","Ti")
colnames(tempTM1) <- c("N","parameter","original","min","max","variable","time","index")
tempT <- cbind(sub_seq,sub_T_mort_10[,,1])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT1 <- melt(tempT,id.vars=c("N"))
tempT <- cbind(sub_seq,sub_T_mort_10[,,2])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT2 <- melt(tempT,id.vars=c("N"))
tempT <- cbind(sub_seq,sub_T_mort_10[,,3])
colnames(tempT) <- c("N",params)
tempT <- as.data.frame(tempT)
tempT3 <- melt(tempT,id.vars=c("N"))
tempTM10 <- cbind(tempT1,tempT2[,3],tempT3[,3],"Mortality","10yr","Ti")
colnames(tempTM10) <- c("N","parameter","original","min","max","variable","time","index")
tempM <- rbind(tempSM1,tempSM10,tempTM1,tempTM10)
temp <- rbind(tempI,tempM)
# And select those that are significantly different from zero to plot
signif <- ind_to_plot[ind_to_plot$sig==1,c("parameter","variable","time","index")]
tthk <- semi_join(temp,signif)
ttyk <- semi_join(ind_to_plot,signif)
# Plot parameters with significant Si in colour, all others in grey
tthk$time_order <- factor(tthk$time,
levels=c("1yr","10yr"))
temp$time_order <- factor(temp$time,
levels=c("1yr","10yr"))
conv_plot <- ggplot(temp[temp$index=="Si",],aes(N/1000,original))+
geom_line(colour="grey",aes(group=parameter))+
geom_line(data=tthk[tthk$index=="Si",],aes(colour=parameter))+
facet_grid(time_order~variable,scale="free")+
ylab("Value of index")+xlab("N (thousands)")+
scale_color_manual(values=colorBlind,name="")+
theme_bw()+
theme(legend.position="bottom")+
theme(strip.background =element_rect(fill="white"))+
scale_x_continuous(limits=c(0,10))