# 模型矩阵
modMat <- as.data.frame(model.matrix(glm1))
modMat$offset <- log(eba1977$pop)
names(modMat) <- c("intercept", "age55_59", "age60_64", "age65_69", "age70_74",
"age75plus", "cityHorsens", "cityKolding", "cityVejle", "offset")
dat <- as.list(modMat)
dat$y <- eba1977$cases
dat$N <- nrow(modMat)
dat$p <- ncol(modMat) - 1
## Load Stan file
fileName <- "./poisson.stan"
stan_code <- readChar(fileName, file.info(fileName)$size)
cat(stan_code)
## Run Stan
resStan <- stan(model_code = stan_code, data = dat,
chains = 3, iter = 3000, warmup = 500, thin = 10)
##
## TRANSLATING MODEL 'stan_code' FROM Stan CODE TO C++ CODE NOW.
## COMPILING THE C++ CODE FOR MODEL 'stan_code' NOW.
## In file included from file60814bc1cb78.cpp:8:
## In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/model/model_header.hpp:17:
## In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/agrad/rev.hpp:5:
## /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/agrad/rev/chainable.hpp:87:17: warning: 'static' function 'set_zero_all_adjoints' declared in header file should be declared 'static inline' [-Wunneeded-internal-declaration]
## static void set_zero_all_adjoints() {
## ^
## In file included from file60814bc1cb78.cpp:8:
## In file included from /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/model/model_header.hpp:21:
## /Library/Frameworks/R.framework/Versions/3.1/Resources/library/rstan/include//stansrc/stan/io/dump.hpp:26:14: warning: function 'product' is not needed and will not be emitted [-Wunneeded-internal-declaration]
## size_t product(std::vector<size_t> dims) {
## ^
##
##
## SAMPLING FOR MODEL 'stan_code' NOW (CHAIN 1).
##
## Iteration: 1 / 3000 [ 0%] (Warmup)
## Iteration: 300 / 3000 [ 10%] (Warmup)
## Iteration: 501 / 3000 [ 16%] (Sampling)
## Iteration: 800 / 3000 [ 26%] (Sampling)
## Iteration: 1100 / 3000 [ 36%] (Sampling)
## Iteration: 1400 / 3000 [ 46%] (Sampling)
## Iteration: 1700 / 3000 [ 56%] (Sampling)
## Iteration: 2000 / 3000 [ 66%] (Sampling)
## Iteration: 2300 / 3000 [ 76%] (Sampling)
## Iteration: 2600 / 3000 [ 86%] (Sampling)
## Iteration: 2900 / 3000 [ 96%] (Sampling)
## Iteration: 3000 / 3000 [100%] (Sampling)
## # Elapsed Time: 0.142295 seconds (Warm-up)
## # 0.543612 seconds (Sampling)
## # 0.685907 seconds (Total)
##
##
## SAMPLING FOR MODEL 'stan_code' NOW (CHAIN 2).
##
## Iteration: 1 / 3000 [ 0%] (Warmup)
## Iteration: 300 / 3000 [ 10%] (Warmup)
## Iteration: 501 / 3000 [ 16%] (Sampling)
## Iteration: 800 / 3000 [ 26%] (Sampling)
## Iteration: 1100 / 3000 [ 36%] (Sampling)
## Iteration: 1400 / 3000 [ 46%] (Sampling)
## Iteration: 1700 / 3000 [ 56%] (Sampling)
## Iteration: 2000 / 3000 [ 66%] (Sampling)
## Iteration: 2300 / 3000 [ 76%] (Sampling)
## Iteration: 2600 / 3000 [ 86%] (Sampling)
## Iteration: 2900 / 3000 [ 96%] (Sampling)
## Iteration: 3000 / 3000 [100%] (Sampling)
## # Elapsed Time: 0.13526 seconds (Warm-up)
## # 0.517139 seconds (Sampling)
## # 0.652399 seconds (Total)
##
##
## SAMPLING FOR MODEL 'stan_code' NOW (CHAIN 3).
##
## Iteration: 1 / 3000 [ 0%] (Warmup)
## Iteration: 300 / 3000 [ 10%] (Warmup)
## Iteration: 501 / 3000 [ 16%] (Sampling)
## Iteration: 800 / 3000 [ 26%] (Sampling)
## Iteration: 1100 / 3000 [ 36%] (Sampling)
## Iteration: 1400 / 3000 [ 46%] (Sampling)
## Iteration: 1700 / 3000 [ 56%] (Sampling)
## Iteration: 2000 / 3000 [ 66%] (Sampling)
## Iteration: 2300 / 3000 [ 76%] (Sampling)
## Iteration: 2600 / 3000 [ 86%] (Sampling)
## Iteration: 2900 / 3000 [ 96%] (Sampling)
## Iteration: 3000 / 3000 [100%] (Sampling)
## # Elapsed Time: 0.120931 seconds (Warm-up)
## # 0.509901 seconds (Sampling)
## # 0.630832 seconds (Total)
## 绘制路径图
traceplot(resStan, pars = c("beta"), inc_warmup = TRUE)