-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_convergence.R
182 lines (152 loc) · 5.49 KB
/
model_convergence.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
#:--------------------------------------------------------
#
# In this file we plot the model parameter traceplots.
#
#:--------------------------------------------------------
# load libraries
library(tidyverse)
library(ggplot2)
library(patchwork)
# load parameters
type_1_model_posteriors <- readRDS("model_development/type_1_model_posteriors_all_genes.rds")
type_2_model_posteriors <- readRDS("model_development/type_2_model_posteriors_all_genes.rds")
# Calculate r hat values
## Turn list of chains into one dataset
type_1_posteriors <- type_1_model_posteriors$samples$chain1 %>%
rbind(
type_1_model_posteriors$samples$chain2,
type_1_model_posteriors$samples$chain3,
type_1_model_posteriors$samples$chain4
) %>%
# turn to data.frame
as.data.frame()
## Calculate r hat
apply(type_1_posteriors, 2, rstan::Rhat)
# R_hat < 1.002
# beta[1] beta[2] beta[3] beta[4] beta[5] beta0
# 1.000011 1.000006 1.000105 1.000266 1.000028 1.000249
# beta_spline[1] beta_spline[2] beta_spline[3] beta_t[1] beta_t[2] beta_t[3]
# 1.001476 1.000656 1.000144 1.002055 1.000752 1.000013
# beta_t[4] beta_t0 gamma0 gamma1 pMp_Cn_or_Ap
# 1.000081 1.000609 1.000011 1.000009 1.000003
# Calculate r hat values
## Turn list of chains into one dataset
type_2_posteriors <- type_2_model_posteriors$samples$chain1 %>%
rbind(
type_2_model_posteriors$samples$chain2,
type_2_model_posteriors$samples$chain3,
type_2_model_posteriors$samples$chain4
) %>%
# turn to data.frame
as.data.frame()
## Calculate r hat
apply(type_2_posteriors %>% select(where(is.numeric)), 2, rstan::Rhat)
# R_hat < 1.0005
# beta[1] beta[2] beta[3] beta[4] beta[5] beta[6] beta[7] beta0 gamma0 gamma1
# 1.000462 1.000122 1.000184 1.000011 1.000223 1.000040 1.000209 1.000118 1.000014 1.000025
# labels for parameters
type_1_labels <- list(
'beta0'="Intercept",
'beta[1]'="At least one parent affected with diabetes",
'beta[2]'="Age at recruitment",
'beta[3]'="HbA1c",
'beta[4]'="At at diagnosis",
'beta[5]'="Sex",
'gamma0'="Gamma 0",
'gamma1'="Gamma 1",
'pMp_Cn_or_Ap'="P(M) | C- OR A+"
)
parameter_type_1_labeller <- function(variable,value){
return(type_1_labels[value])
}
# Trace plots
plot_type_1_model <- type_1_posteriors %>%
# create variables needed for plotting
mutate(
`Chain:` = rep(paste("Chain ", 1:length(type_1_model_posteriors$samples)), each = nrow(type_1_model_posteriors$samples$chain1)),
`Chain:` = factor(`Chain:`)
) %>%
# gather values into two columns (don't gather chain, used for colour)
gather(key = "key", value = "value", -`Chain:`) %>%
# group by chain
group_by(`Chain:`, key) %>%
# count the iterations
mutate(
Iterations = row_number()
) %>%
# remove grouping
ungroup() %>%
# only keep relevant parameters
filter(key %in% c("beta0", "beta[1]", "beta[2]", "beta[3]", "beta[4]", "beta[5]", "pMp_Cn_or_Ap", "gamma0", "gamma1")) %>%
# order parameters
mutate(key = factor(key, levels = c("beta0", "beta[1]", "beta[2]", "beta[3]", "beta[4]", "beta[5]", "pMp_Cn_or_Ap", "gamma0", "gamma1"))) %>%
# start plotting
ggplot(aes(x = Iterations, y = value, colour = `Chain:`)) +
geom_path(alpha = 0.6) +
scale_x_continuous(labels = scales::comma) +
facet_wrap(~key, scales = "free", ncol = 1, labeller=parameter_type_1_labeller) +
guides(colour = guide_legend(nrow = 1)) +
theme_bw() +
theme(
legend.position = "none",
axis.title.y = element_blank()
)
pdf("figures/type_1_model_trace_plots.pdf", width = 6, height = 11)
plot_type_1_model
dev.off()
# labels for parameters
type_2_labels <- list(
'beta0'="Intercept",
'beta[4]'="At least one parent affected with diabetes",
'beta[5]'="Age at recruitment",
'beta[3]'="HbA1c",
'beta[1]'="At at diagnosis",
'beta[7]'="Sex",
'beta[6]'="Currently treated with insulin or tablets",
'beta[2]'="BMI",
'gamma0'="Gamma 0",
'gamma1'="Gamma 1"
)
parameter_type_2_labeller <- function(variable,value){
return(type_2_labels[value])
}
plot_type_2_model <- type_2_posteriors %>%
# create variables needed for plotting
mutate(
`Chain:` = rep(paste("Chain ", 1:length(type_2_model_posteriors$samples)), each = nrow(type_2_model_posteriors$samples$chain1)),
`Chain:` = factor(`Chain:`)
) %>%
# gather values into two columns (don't gather chain, used for colour)
gather(key = "key", value = "value", -`Chain:`) %>%
# group by chain
group_by(`Chain:`, key) %>%
# count the iterations
mutate(
Iterations = row_number()
) %>%
# remove grouping
ungroup() %>%
# order parameters
mutate(key = factor(key, levels = c("beta0", "beta[4]", "beta[5]", "beta[3]", "beta[1]", "beta[7]", "beta[6]", "beta[2]", "gamma0", "gamma1"))) %>%
# start plotting
ggplot(aes(x = Iterations, y = value, colour = `Chain:`)) +
geom_path(alpha = 0.6) +
scale_y_continuous(labels = scales::comma) +
facet_wrap(~key, scales = "free", ncol = 1, labeller=parameter_type_2_labeller) +
guides(colour = guide_legend(nrow = 1)) +
theme_bw() +
theme(
legend.position = "none",
axis.title.y = element_blank()
)
pdf("figures/type_2_model_trace_plots.pdf", width = 6, height = 12)
plot_type_2_model
dev.off()
pdf("figures/models_trace_plots.pdf", width = 10, height = 12)
patchwork::wrap_plots(
plot_type_1_model,
plot_type_2_model,
ncol = 2, nrow = 1
) +
patchwork::plot_annotation(tag_levels = list(c("A", "B")))
dev.off()