-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.r
executable file
·124 lines (122 loc) · 2.85 KB
/
functions.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
#source c++ VE to iVE conversion method
sourceCpp("./cumVEtoInsFast.cpp")
#make a simple plot
plotEstimates <- function(vaccine_efficacy_data, vaccine_efficacy_central = NULL){
p <- ggplot(
)+geom_errorbar(
data=vaccine_efficacy_data,
aes(
x=follow_up_end,
ymin=1-rr_high,
ymax=1-rr_low
)
)+geom_point(
data=vaccine_efficacy_data,
aes(
x=follow_up_end,
y=1-rr,
#Standard error RR dominated by number of cases
size=(cases_vaccine + cases_placebo)
)
)+coord_cartesian(
ylim=c(0, 1),
xlim=c(0.2, 36)
)+facet_grid(
vaccine_efficacy~mortality_factor
)+labs(
x="Months since completed vaccine schedule",
y="Vaccine efficacy (prop)"
)+scale_size_continuous(
range = c(0.5,2.5)
)+guides(
size="none"
)+geom_hline(
yintercept=0
)
if(!is.null(vaccine_efficacy_central)){
p <- p +geom_ribbon(
data=vaccine_efficacy_central,
aes(
x=time,
ymin=low,
ymax=high
),
alpha=0.2
)+geom_line(
data=vaccine_efficacy_central,
aes(
x=time,
y=median
)
)
}
print(p)
}
#calculate empirical p-values and credible intervals from 2 matrixes (data_a and data_b) with the posterior samples that are to be compared
#cols argument can be used to specify the index of the column(s) to be compared
empiricalDifference <- function(
data_a,
data_b,
cols = NULL
){
if(is.null(cols)){
cols <- c(1:ncol(data_a))
}
data_a <- data_a[, cols]
data_b <- data_b[, cols]
data_difference <- matrix(
0,
ncol=ncol(data_a),
nrow=nrow(data_a)*nrow(data_b)
)
for(r in 1:nrow(data_a)){
data_difference[c(1:nrow(data_a))+nrow(data_a)*(r-1), ] <- - sweep(data_b, 2, data_a[r,], "-")
}
data_difference_low <- sapply(
c(1:ncol(data_difference)),
function(x){
quantile(
data_difference[, x],
probs=c(0.025)
)
}
)
data_difference_median <- sapply(
c(1:ncol(data_difference)),
function(x){
quantile(
data_difference[, x],
probs=c(0.5)
)
}
)
data_difference_high <- sapply(
c(1:ncol(data_difference)),
function(x){
quantile(
data_difference[, x],
probs=c(0.975)
)
}
)
data_pval <- matrix(
0,
ncol=ncol(data_a),
nrow=nrow(data_b)
)
#count number of times data_a is higher than data_b
for(r in 1:nrow(data_a)){
data_pval[r, ] <- colSums(
sweep(data_b, 2, data_a[r,], "<")
)
}
data_pval <- 1 - (colSums(data_pval) / (nrow(data_a)*nrow(data_b)))
return(
list(
"pvalue" = data_pval,
"diff_low" = data_difference_low,
"diff_high" = data_difference_high,
"diff_median" = data_difference_median
)
)
}