-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruth.R
345 lines (286 loc) · 14.1 KB
/
truth.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
# generate some parameters
library(tidyverse)
library(gridExtra)
library(bayesplot)
library(doMC) # for parallel loops
registerDoMC(4) # use 4 threads
source("~/Dropbox/master/algo/subor.R")
knt <- seq(0, 10, 2)
simdata <- read_csv("~/Dropbox/master/algo/data/simdata.csv")
plotdata <- read_csv("~/Dropbox/master/algo/data/simdata-curve.csv")
plotdata_pop <- read_csv("~/Dropbox/master/algo/data/simdata-popcurve.csv")
plotdata_ridge <- read_csv("~/Dropbox/master/algo/data/simdata-ridge.csv")
plotdata_lmm <- read_csv("~/Dropbox/master/algo/data/simdata-lmm.csv")
## compara gen-data from either ridge and LMM
ridge_curve <- ggplot() +
geom_line(aes(plot_x, plot_y, col = sub), plotdata_ridge) +
geom_line(aes(plot_x, plot_y), plotdata_pop) +
labs(x = 'x', y = 'y') +
ylim(0, 35) +
theme_bw() + theme(legend.position="none")
lmm_curve <- ggplot() +
geom_line(aes(plot_x, plot_y, col = sub), plotdata_lmm) +
geom_line(aes(plot_x, plot_y), plotdata_pop) +
labs(x = 'x', y = 'y') +
ylim(0, 35) +
theme_bw() + theme(legend.position="none")
ridge_vs_lmm <- grid.arrange(ridge_curve, lmm_curve, ncol = 2)
## ggsave("~/Dropbox/master/thesis/images/ridge-vs-lmm.pdf", ridge_vs_lmm,
## width = 10, height = 6)
## generate the truth (dotted)
true_curve <- list(sub = geom_line(aes(plot_x, plot_y, col = sub), plotdata, lty = 2,
alpha = 0.9),
pop = geom_line(aes(plot_x, plot_y), plotdata_pop, lty = 2),
theme_bw(),
theme(legend.position="none"),
labs(x = 'x', y = 'y'))
## design matrices (B-spline LMM model)
rm(list = setdiff(ls(), c("simdata", "knt", "true_curve")))
source("~/Dropbox/master/algo/subor.R")
deg <- 1
K <- length(knt) - 2 # inner knots
n_bsf <- K + deg + 1 # number of b-spline basis functions
D <- get_diff_mat(n_bsf, deg + 1) # difference matrix
type <- "bs" # "bs", "bs-ridge" or "tpf"
## design matrices, Gmat and Hmat
des_info <- get_design_bs(simdata$x, K, deg) # bs
Gmat <- cbind(-1/sqrt(n_bsf), poly(1:n_bsf, deg = deg, raw = FALSE)) # bs unraw
Hmat <- crossprod(D, solve(tcrossprod(D))) # bs
Bmat <- des_info$design %*% cbind(Gmat, Hmat) # bs
Kmat <- cbind(matrix(0, K, deg + 1), diag(K)) # bs
rm(list = c("K", "n_bsf", "D"))
source("~/Dropbox/master/algo/main-ridge.R")
set.seed(100, kind = "L'Ecuyer-CMRG")
fm1_ls <- foreach(i = 1:4) %dopar% {
init <- list(pop = get_pls(simdata$y, Bmat, Kmat) + rnorm(NCOL(Bmat), sd = 100))
fm <- bayes_ridge_sub(simdata$y, simdata$sub, Bmat, Kmat, deg + 1, 1000, 2000,
init = init)
fm$basis <- list(type = 'bs_hier', knots = des_info$knots, degree = deg, # bs
trans_mat = cbind(Gmat, Hmat))
fm$data <- simdata %>% mutate(grp_sub = sub, grp_pop = NA, sub = NULL)
fm
}
RNGkind("Mersenne-Twister")
source("~/Dropbox/master/algo/subor.R")
g1ls <- list()
for (fm in fm1_ls) {
g1ls <- c(g1ls, plot_spline(fm, shade = TRUE))
}
g1curve_all <- ggplot() + g1ls[grep("pop|sub|data", names(g1ls))] + theme_bw() +
theme(legend.position="none")
## ggsave("~/Dropbox/master/thesis/images/truth-gibbs-all.pdf", g1curve_all,
## width = 5, height = 6)
source("~/Dropbox/master/algo/diagnostic.R")
## visualise the truth curve and add it to g1curve
fm1 <- do.call(combine_fm, fm1_ls)
g1curve_true <- ggplot() + true_curve + plot_spline(fm1, shade = FALSE)
## regression curve
## ggsave("~/Dropbox/master/thesis/images/truth-gibbs-true.pdf", g1curve_true,
## width = 5, height = 6)
## diagnostic
library(xtable)
source("~/Dropbox/master/algo/diagnostic.R")
flat1 <- do.call(flatten_chains, fm1_ls)
long1 <- summary_matrix_flats(flat1)
short1 <- long1 %>% filter(Rhat > 1.01 | n_eff < 500)
short1 <- long1 %>% filter(grepl("theta\\[1\\]|delta\\[1,1\\]", Parameter))
## print(xtable(short1), include.rownames=FALSE, tabular.environment = "tabular")
## diagnostic plots
g1combo_theta1 <- mcmc_combo(flat1, pars = "theta[1]", c("dens_overlay", "trace"))
g1combo_delta11 <- mcmc_combo(flat1, pars = "delta[1,1]", c("dens_overlay", "trace"))
g1combo_sum11 <- as.tibble(flat1[, , "theta[1]"] + flat1[, , "delta[1,1]"]) %>%
gather(Chain, "theta[1] + delta[1,1]") %>%
separate(Chain, c(NA, "Chain"), " ") %>%
mcmc_combo(c("dens_overlay", "trace"))
## ggsave("~/Dropbox/master/thesis/images/truth-gibbs-combo-theta1.pdf", g1combo_theta1,
## width = 10, height = 3)
## ggsave("~/Dropbox/master/thesis/images/truth-gibbs-combo-delta11.pdf", g1combo_delta11,
## width = 10, height = 3)
## ggsave("~/Dropbox/master/thesis/images/truth-gibbs-combo-sum11.pdf", g1combo_sum11,
## width = 10, height = 3)
## VERSION 2 OF GIBBS
source("~/Dropbox/master/algo/main-ridge.R")
set.seed(101, kind = "L'Ecuyer-CMRG")
fm1v2_ls <- foreach(i = 1:4) %dopar% {
init <- list(pop = get_pls(simdata$y, Bmat, Kmat) + rnorm(NCOL(Bmat), sd = 100))
fm <- bayes_ridge_sub_v2(simdata$y, simdata$sub, Bmat, Kmat, deg + 1, 1000, 2000,
init = init)
if (type == "tpf") {
fm$basis <- list(type = 'tpf', knots = des_info$knots, degree = deg) # tpf
} else if (type == "bs") {
fm$basis <- list(type = 'bs_hier', knots = des_info$knots, degree = deg, # bs
trans_mat = cbind(Gmat, Hmat))
} else if (type == "bs-ridge") {
fm$basis <- list(type = 'bs', knots = des_info$knots, degree = deg) # bs-ridge
}
fm$data <- simdata %>% mutate(grp_sub = sub, grp_pop = NA, sub = NULL)
fm
}
RNGkind("Mersenne-Twister")
source("~/Dropbox/master/algo/subor.R")
g1v2ls <- list()
for (fm in fm1v2_ls) {
g1v2ls <- c(g1v2ls, plot_spline(fm, shade = TRUE, silent = TRUE))
}
g1v2curve_all <- ggplot() + g1v2ls[grep("pop|sub|data", names(g1v2ls))] + theme_bw() +
theme(legend.position="none")
## ggsave("~/Dropbox/master/thesis/images/truth-gibbsv2-all.pdf", g1v2curve_all,
## width = 5, height = 6)
source("~/Dropbox/master/algo/diagnostic.R")
## visualise the truth curve and add it to g1v2curve
fm1v2 <- do.call(combine_fm, fm1v2_ls)
g1v2curve_true <- ggplot() + true_curve + plot_spline(fm1v2, shade = FALSE, silent = TRUE)
## regression curve
## ggsave("~/Dropbox/master/thesis/images/truth-gibbsv2-true.pdf", g1v2curve_true,
## width = 5, height = 6)
## diagnostic
source("~/Dropbox/master/algo/diagnostic.R")
flat1v2 <- do.call(flatten_chains, fm1v2_ls)
long1v2 <- summary_matrix_flats(flat1v2)
## print(xtable(long1v2), include.rownames=FALSE, tabular.environment = "longtable")
short1v2 <- long1v2 %>% filter(Rhat > 1.01 | n_eff < 500)
short1v2 <- long1v2 %>% filter(grepl("theta\\[1\\]|delta\\[1,1\\]", Parameter))
## print(xtable(short1v2), include.rownames=FALSE, tabular.environment = "tabular")
## diagnostic plots
g1v2combo_theta1 <- mcmc_combo(flat1v2, pars = "theta[1]", c("dens_overlay", "trace"))
g1v2combo_delta11 <- mcmc_combo(flat1v2, pars = "delta[1,1]", c("dens_overlay", "trace"))
g1v2combo_sum11 <- as.tibble(flat1v2[, , "theta[1]"] + flat1v2[, , "delta[1,1]"]) %>%
gather(Chain, "theta[1] + delta[1,1]") %>%
separate(Chain, c(NA, "Chain"), " ") %>%
mcmc_combo(c("dens_overlay", "trace"))
## ggsave("~/Dropbox/master/thesis/images/truth-gibbsv2-combo-theta1.pdf",
## g1v2combo_theta1, width = 10, height = 3)
## ggsave("~/Dropbox/master/thesis/images/truth-gibbsv2-combo-delta11.pdf",
## g1v2combo_delta11, width = 10, height = 3)
## ggsave("~/Dropbox/master/thesis/images/truth-gibbsv2-combo-sum11.pdf",
## g1v2combo_sum11, width = 10, height = 3)
## TRY DIFFERENT MODELS, change the model parameters at the beginning and rerun v2
## bs-ridge
## ggsave("~/Dropbox/master/thesis/images/truth-bsridge-combo-theta1.pdf",
## g1v2combo_theta1, width = 10, height = 3.5)
## ggsave("~/Dropbox/master/thesis/images/truth-bsridge-combo-delta11.pdf",
## g1v2combo_delta11, width = 10, height = 3.5)
## ggsave("~/Dropbox/master/thesis/images/truth-bsridge-combo-sum11.pdf",
## g1v2combo_sum11, width = 10, height = 3.5)
## tpf
## ggsave("~/Dropbox/master/thesis/images/truth-tpf-combo-theta1.pdf",
## g1v2combo_theta1, width = 10, height = 3.5)
## ggsave("~/Dropbox/master/thesis/images/truth-tpf-combo-delta11.pdf",
## g1v2combo_delta11, width = 10, height = 3.5)
## ggsave("~/Dropbox/master/thesis/images/truth-tpf-combo-sum11.pdf",
## g1v2combo_sum11, width = 10, height = 3.5)
## WITH MONOTONICITY CONSTRAINT
## WITH MONOTONICITY CONSTRAINT
## TRY B-SPLINE (LINEAR AND QUADRATIC) AND TPF (QUADRATIC)
## TRY B-SPLINE (LINEAR AND QUADRATIC) AND TPF (QUADRATIC)
# generate some parameters
library(tidyverse)
library(gridExtra)
library(bayesplot)
library(doMC) # for parallel loops
registerDoMC(4) # use 4 threads
source("~/Dropbox/master/algo/subor.R")
knt <- seq(0, 10, 2)
simdata <- read_csv("~/Dropbox/master/algo/data/simdata.csv")
plotdata <- read_csv("~/Dropbox/master/algo/data/simdata-curve.csv")
plotdata_pop <- read_csv("~/Dropbox/master/algo/data/simdata-popcurve.csv")
## generate the truth (dotted)
true_curve <- list(sub = geom_line(aes(plot_x, plot_y, col = sub), plotdata, lty = 2,
alpha = 0.9),
pop = geom_line(aes(plot_x, plot_y), plotdata_pop, lty = 2),
theme_bw(),
theme(legend.position="none"),
labs(x = 'x', y = 'y'))
## design matrices (tpf of B-spline LMM model)
rm(list = setdiff(ls(), c("simdata", "knt", "true_curve")))
source("~/Dropbox/master/algo/subor.R")
deg <- 1
K <- length(knt) - 2 # inner knots
n_bsf <- K + deg + 1 # number of b-spline basis functions
D <- get_diff_mat(n_bsf, deg + 1) # difference matrix
type <- "bs" # "bs" or "tpf"
## design matrices, Gmat and Hmat
if (type == "tpf") {
des_info <- get_design_tpf(simdata$x, K, deg) # tpf
Bmat <- des_info$design # tpf
Kmat <- cbind(matrix(0, K, deg + 1), diag(K)) # tpf
Amat <- get_constmat_tpf(des_info$knots, "increasing", deg)
lower <- rep(0, NROW(Amat))
} else if (type == "bs") {
des_info <- get_design_bs(simdata$x, K, deg) # bs
Gmat <- cbind(-1/sqrt(n_bsf), poly(1:n_bsf, deg = deg, raw = FALSE)) # bs unraw
Hmat <- crossprod(D, solve(tcrossprod(D))) # bs
Bmat <- des_info$design %*% cbind(Gmat, Hmat) # bs
Kmat <- cbind(matrix(0, K, deg + 1), diag(K)) # bs
Amat <- get_constmat_bs(NCOL(Bmat), "increasing") %*% cbind(Gmat, Hmat)
lower <- rep(0, NROW(Amat))
}
rm(list = c("K", "n_bsf", "D"))
library(nlme)
pop <- rep(1, length(simdata$sub))
Xmat <- unname(Bmat[ , 1:(deg + 1)])
Zmat <- unname(Bmat[, -(1:(deg + 1))])
fit <- lme(y ~ Xmat - 1, simdata, list(pop = pdIdent(~Zmat - 1),
sub = pdBlocked(list(pdSymm(~Xmat - 1),
pdIdent(~Zmat - 1)))))
prec <- lapply(as.matrix(fit$modelStruct$reStruct), function(x) x * fit$sigma^2)
prec$pop <- 1 / diag(prec$pop)[[1]]
prec$sub1 <- unname(solve(prec$sub[1:(deg + 1), 1:(deg + 1)]))
prec$sub2 <- 1 / diag(prec$sub)[[deg + 2]]
prec$sub <- NULL
source("~/Dropbox/master/algo/main-ridge.R")
set.seed(103, kind = "L'Ecuyer-CMRG")
fm1cv2_ls <- foreach(i = 1:4) %dopar% {
init <- list(pop = c(tnorm::rmvtnorm(1, mean = get_pls(simdata$y, Bmat, Kmat),
initial = c(1, 1, rep(0, deg + 3)),
F = Amat, g = -1 * lower)))
fm <- bayes_ridge_cons_sub_v2(simdata$y, simdata$sub, Bmat, Kmat, deg + 1,
Amat, 1000, 2000, init, prec = prec)
if (type == "tpf") {
fm$basis <- list(type = 'tpf', knots = des_info$knots, degree = deg) # tpf
} else if (type == "bs") {
fm$basis <- list(type = 'bs_hier', knots = des_info$knots, degree = deg, # bs
trans_mat = cbind(Gmat, Hmat))
}
fm$data <- simdata %>% mutate(grp_sub = sub, grp_pop = NA, sub = NULL)
fm
}
RNGkind("Mersenne-Twister")
source("~/Dropbox/master/algo/subor.R")
g1cv2ls <- list()
for (fm in fm1cv2_ls) {
g1cv2ls <- c(g1cv2ls, plot_spline(fm, shade = TRUE, silent = TRUE))
}
g1cv2curve_all <- ggplot() + g1cv2ls[grep("pop|sub|data", names(g1cv2ls))] + theme_bw() +
theme(legend.position="none")
g1cv2curve_all
## ggsave("~/Dropbox/master/thesis/images/truth-gibbscv2-all-bslin.pdf",
## g1cv2curve_all, width = 5, height = 6)
## ggsave("~/Dropbox/master/thesis/images/truth-gibbscv2-all-bsquad.pdf",
## g1cv2curve_all, width = 5, height = 6)
## ggsave("~/Dropbox/master/thesis/images/truth-gibbscv2-all-tpfquad.pdf",
## g1cv2curve_all, width = 5, height = 6)
source("~/Dropbox/master/algo/diagnostic.R")
## visualise the truth curve and add it to g1cv2curve
fm1cv2 <- do.call(combine_fm, fm1cv2_ls)
g1cv2curve_true <- ggplot() + true_curve +
plot_spline(fm1cv2, shade = FALSE, silent = TRUE)
g1cv2curve_true
## regression curve
## ggsave("~/Dropbox/master/thesis/images/truth-gibbscv2-true-bslin.pdf",
## g1cv2curve_true, width = 5, height = 6)
## diagnostic
source("~/Dropbox/master/algo/diagnostic.R")
flat1cv2 <- do.call(flatten_chains, fm1cv2_ls)
long1cv2 <- summary_matrix_flats(flat1cv2)
print(xtable(long1cv2), include.rownames=FALSE, tabular.environment = "longtable")
short1cv2 <- long1cv2 %>% filter(Rhat > 1.01 | n_eff < 500)
short1cv2 <- long1cv2 %>% filter(grepl("theta\\[1\\]|delta\\[1,1\\]", Parameter))
## print(xtable(short1cv2), include.rownames=FALSE, tabular.environment = "tabular")
## diagnostic plots
g1cv2combo_theta1 <- mcmc_combo(flat1cv2, pars = "theta[1]", c("dens_overlay", "trace"))
g1cv2combo_delta11 <- mcmc_combo(flat1cv2, pars = "delta[1,1]", c("dens_overlay", "trace"))
source("~/Dropbox/master/algo/diagnostic.R")
flat1cv2 <- do.call(flatten_chains, list(fm1cv2))[, , 1:43, drop = FALSE]
tail(summary_matrix_flats(flat1cv2), n = 10)
mcmc_combo(flat1cv2[, , 1:43], pars = "theta[1]", c("dens", "trace"))