-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdstanplots.R
311 lines (258 loc) · 8.59 KB
/
cmdstanplots.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
# installing cmdstanr to use covariance functions
#devtools::install_github("stan-dev/cmdstanr")
#devtools::install_github("cran/StanHeaders")
#install.packages("rstan")
library(cmdstanr)
library(rstan)
library(brms)
#install.packages("brms")
# requires working version of cmdstan
# install_cmdstan()
# verify that version can be found
cmdstan_version()
# executing example model
file <- file.path(cmdstan_path(), "examples", "bernoulli", "bernoulli.stan")
mod <- cmdstan_model(file)
# R object
mod$print()
mod$exe_file()
data_list <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
fit <- mod$sample(
data = data_list,
seed = 123,
num_chains = 2,
num_cores = 2
)
fit$summary()
fit$cmdstan_diagnose()
fit$cmdstan_summary()
# creating stanfit object
stanfit <- rstan::read_stan_csv(fit$output_files())
print(stanfit)
library(mgcv)
set.seed(1)
dat <- gamSim(1, n = 500)
m_matern32<- gam(y ~ s(x1, x2, bs = "gp", m = 3), data = dat)
summary(m_matern32)
stancode <- brms::make_stancode(y ~ gp(x1, x2, k = 30, c = 5/4), data = dat)
standata <- brms::make_standata(y ~ gp(x1, x2, k = 30, c = 5/4), data = dat)
modeldata <- list()
for (i in 1:length(standata)) {
modeldata[[i]] <- standata[[i]]
}
names(modeldata) <- names(standata)
setwd("/home/rstudio/geostatsBA/modeldump")
file <- paste0(getwd(), "/test.stan")
setwd("/home/rstudio/geostatsBA/modeldump")
mod <- cmdstan_model(file)
b_matern32 <- mod$sample(
data = modeldata,
seed = 123,
num_chains = 4,
num_cores = 4,
num_samples = 400,
num_warmup = 400,
refresh = 1
)
# 300 data points 4 chains
# 400 samples 400 warmup finished with mean chain execution time of
# 9434.5 seconds = 2.6 hours
# # 30 of 1600 transitions ended with a divergence on default 0.8 adapt delta
saveRDS(b_matern32, file = "toydata_matern32.RDS")
b_matern32 <- readRDS("toydata_matern32.RDS")
draws_matern32 <- m_matern32$draws()
draws <- b_matern32$draws()
dim(draws)[2]
chains <- list()
for (i in 1:dim(draws)[2]) {
chains[[i]] <- draws[, i, ]
}
library(purrr)
chains <- purrr::map(chains, as.data.frame)
for (i in 1:length(chains)) {
chains[[i]] <- chains[[i]][, -grep(pattern="zgp_1",colnames(chains[[i]]))]
chains[[i]] <- chains[[i]][, -grep(pattern="mu_generated",colnames(chains[[i]]))]
}
chains <- purrr::map(chains, subset, select = 2:5)
purrr::map(chains, head, n = 5)
dta <- do.call(cbind, chains)
tmp <- list()
for (i in 1:(length(colnames(dta))/length(chains))) {
tmp[[i]] <- dta[,seq(i, ncol(dta), 4)]
}
mlt <- purrr::map(tmp, reshape2::melt)
# function to generate lineplot from such data
df <- mlt[[1]]
plt_chains <- function(df, nwalks = 4, variable = "Intercept") {
df$rown <- rep(seq(from = 1, to = (nrow(df)/nwalks)), times = 4)
get_labels <- function(n) {
labels <- as.character(1:n)
labels
}
plt <- ggplot(df, aes(x = rown, y = value, group = variable, col = variable)) +
geom_line() +
scale_color_manual(values = c("#313695", "#74add1", "#a50026", "#f46d43"),
labels = get_labels(nwalks)) +
theme_bw() +
xlab("Iteration") +
ylab(variable) +
guides(color=guide_legend(title="Chain"))
plt
}
# Intercept, Sigma GP, l-scale, sigma
plt_chains(mlt[[1]], variable = "Intercept")
plt_chains(mlt[[2]], variable = "Sigma GP")
plt_chains(mlt[[3]], variable = "l-scale")
plt_chains(mlt[[4]], variable = "Sigma")
test <- tmp[[1]]
colnames(test) <- c("Intercept", "Intercept", "Intercept", "Intercept")
# getting all relevant values into a matrix
mat <- matrix(nrow = 1600, ncol = 4)
colnames(mat) <- c("Intercept", "SigmaGP", "l-scale", "Sigma")
for (i in 1:length(tmp)) {
mat[, i] <- reshape2::melt(tmp[[i]])$value
}
# plotting this matrix
bayesplot::mcmc_areas(mat, pars = c("Intercept", "SigmaGP"), prob = 0.9)
bayesplot::mcmc_areas(mat, pars = c("l-scale", "Sigma"), prob = 0.9)
plotchains <- chains
for (i in 1:length(plotchains)) {colnames(plotchains[[i]]) <- c("Intercept", "SigmaGP", "l-scale", "Sigma")}
plotchains <- purrr::map(plotchains, as.matrix.data.frame)
overlay_plt <- bayesplot::mcmc_dens_overlay(plotchains, pars = c("Sigma"))
overlay_plt +
scale_color_manual(values = c("#313695", "#74add1", "#a50026", "#f46d43"))
# df.new = dta[,seq(1, ncol(dta), 4)]
get_mlt <- function(draws) {
c1 <- draws[, 1, ]
c2 <- draws[, 2, ]
c1 <- as.data.frame(c1)
c2 <- as.data.frame(c2)
c1 <- c1[,-grep(pattern="1.zgp_1",colnames(c1))]
c2 <- c2[,-grep(pattern="2.zgp_1",colnames(c2))]
c1 <- c1[, 2:5]
c2 <- c2[, 2:5]
colnames(c1) <- c("Intercept", "SGP", "LScale", "Sigma")
colnames(c2) <- c("Intercept", "SGP", "LScale", "Sigma")
twochainz <- rbind(c1, c2)
mlt <- reshape2::melt(twochainz)
return(mlt)
}
mlt <- get_mlt(draws_matern32)
ggplot(mlt, aes(x = value)) +
geom_density() +
facet_wrap(~variable, scales = "free", ncol = 1)
b_matern32$cmdstan_diagnose()
setwd("/home/rstudio/geostatsBA")
file <- paste0(getwd(), "/cmdstanr_matern_52.stan")
mod <- cmdstan_model(file)
############## matern 3 2 for predictive maps
set.seed(1)
dat <- gamSim(1, n = 100)
m_matern32<- gam(y ~ s(x1, x2, bs = "gp", m = 3), data = dat)
summary(m_matern32)
plot(m_matern32)
# stancode <- brms::make_stancode(y ~ gp(x1, x2), data = dat)
standata <- brms::make_standata(y ~ gp(x1, x2), data = dat)
modeldata <- list()
for (i in 1:length(standata)) {
modeldata[[i]] <- standata[[i]]
}
names(modeldata) <- names(standata)
b_matern_pred <- mod$sample(
data = modeldata,
seed = 123,
num_chains = 2,
num_cores = 2,
num_samples = 200,
num_warmup = 200,
refresh = 1
)
draws_matern52 <- b_matern52$draws()
mlt_matern_52 <- get_mlt(draws_matern52)
ggplot(mlt_matern_52, aes(x = value)) +
geom_density() +
facet_wrap(~variable, scales = "free", ncol = 1)
# modeling the gaussian process smooth here
rm(list = ls())
evidence <- readRDS("Daten/evidence.csv")
library(brms)
# taking a sample of 1000 points
set.seed(1)
evd <- evidence[sample(nrow(evidence), 1000),]
b_smooth <- brm(site ~ s(lon, lat),
data = evd,
family = bernoulli,
chains = 4,
cores = 4,
iter = 1000,
control = list(adapt_delta = 0.8,
max_treedepth = 13))
# 4 seconds for 1000 steps
summary(b_smooth)
plot(b_smooth)
plt_cs_500 <- conditional_smooths(b_smooth)
plt_cs_500
b_smooth_fullevidence <- brm(site ~ s(lon, lat),
data = evidence,
family = bernoulli,
chains = 4,
cores = 4,
iter = 1000,
control = list(adapt_delta = 0.8,
max_treedepth = 13))
# about 270 seconds for 1000 steps
# chain 2 clocking in at 5 minutes for 100 iterations
# 50% 12 minutes in
# chain 2: 20.5 minutes total
# chain 3: 1358 seconds
# chain 4: 1377 seconds
# chain 1: 1473 seconds
plt_cs_12222 <- conditional_smooths(b_smooth_fullevidence)
plt_cs_12222
evd_500 <- evd[1:500, ]
b_gp_500 <- brm(site ~ gp(lon, lat),
data = evd_500,
family = bernoulli,
chains = 4,
cores = 4,
iter = 1000,
control = list(adapt_delta = 0.8,
max_treedepth = 13))
saveRDS(b_gp_500, "b_gp_500.RDS")
plt_gp_500 <- conditional_effects(b_gp_500)
# starting worker pid=16506 on localhost:11911 at 20:07:26.750
# 1000 transitions using 10 leapfrog steps per transition would take 2871.49 seconds.
#
# Chain 1: Elapsed Time: 6477.24 seconds (Warm-up)
# Chain 1: 2909.06 seconds (Sampling)
# Chain 1: 9386.3 seconds (Total)
# Chain 1:
# Chain 4: Iteration: 1000 / 1000 [100%] (Sampling)
# Chain 4:
# Chain 4: Elapsed Time: 6499.09 seconds (Warm-up)
# Chain 4: 2940.27 seconds (Sampling)
# Chain 4: 9439.36 seconds (Total)
# Chain 4:
# Chain 3: Iteration: 1000 / 1000 [100%] (Sampling)
# Chain 3:
# Chain 3: Elapsed Time: 7034.45 seconds (Warm-up)
# Chain 3: 2708.78 seconds (Sampling)
# Chain 3: 9743.24 seconds (Total)
#
evidence <- readRDS("Daten/evidence.csv")
set.seed(1)
evd <- evidence[sample(nrow(evidence), 1000), ]
evd$site <- as.factor(evd$site)
library(caret)
library(mgcv)
b <- train(site ~ lon + lat + dem + temp + rain + distance_water +
frostdays + sunhours + tpi + slope,
family = binomial,
data = evd,
method = "gam",
trControl = trainControl(method = "cv", number = 100),
tuneGrid = data.frame(method = "GCV.Cp", select = TRUE)
)
# r + s + r:s
print(b)
summary(b$finalModel)