forked from MatsuuraKentaro/RStanBook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-model12-13.R
33 lines (28 loc) · 987 Bytes
/
run-model12-13.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
library(rstan)
T <- 96
d <- as.matrix(read.csv('input/data-2Dmesh.txt', header = FALSE))
I <- nrow(d)
J <- ncol(d)
rownames(d) <- 1:I
colnames(d) <- 1:J
d_melt <- reshape2::melt(d)
colnames(d_melt) <- c('i', 'j', 'Y')
loess_res <- loess(Y ~ i + j, data = d_melt, span = 0.1)
smoothed <- matrix(loess_res$fitted, nrow = I, ncol = J)
TID <- read.csv('input/data-2Dmesh-design.txt', header = FALSE)
data <- list(I = I, J = J, Y = d, T = T, TID = TID)
stanmodel <- stan_model(file = 'model/model12-13.stan')
fit <- sampling(
stanmodel, data = data, iter = 5000, thin = 5, seed = 1234,
init = function() {
list(r = smoothed, s_r = 1, s_Y = 1, s_beta = 1, beta = rnorm(T, 0, 0.1))
}
)
save.image('output/result-model12-13.RData')
stanmodel_b <- stan_model(file = 'model/model12-13b.stan')
fit_b <- sampling(
stanmodel_b, data = data, iter = 5000, thin = 5, seed = 1234,
init = function() {
list(r = smoothed, s_r = 1, s_Y = 1, s_beta = 1, beta = rnorm(T, 0, 0.1))
}
)