[英]How to plot parameters timeseries in rstan
我已經安裝了一個名為“fit”的rstan
,類型如下:
Inference for Stan model: MySecondModel.
1 chains, each with iter=2000; warmup=1000; thin=1;
post-warmup draws per chain=1000, total post-warmup draws=1000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
step_size 0.01 0.00 0.00 0.01 0.01 0.01 0.01 0.01 493 1.00
theta_init 0.18 0.00 0.02 0.14 0.17 0.18 0.19 0.22 1167 1.00
theta_steps_unscaled[1] -0.02 0.02 0.91 -1.81 -0.60 0.00 0.58 1.73 1453 1.00
theta_steps_unscaled[2] -0.02 0.03 0.94 -1.95 -0.65 0.00 0.57 1.81 1382 1.00
theta_steps_unscaled[3] 0.14 0.03 0.95 -1.73 -0.51 0.15 0.80 2.01 1224 1.00
theta_steps_unscaled[4] -0.34 0.02 0.91 -2.05 -0.95 -0.36 0.26 1.44 1499 1.00
theta_steps_unscaled[5] -0.32 0.02 0.88 -2.02 -0.94 -0.31 0.31 1.32 2079 1.00
theta_steps_unscaled[6] -0.99 0.02 0.86 -2.69 -1.56 -0.98 -0.41 0.66 1759 1.00
theta_steps_unscaled[7] 0.23 0.03 0.94 -1.59 -0.40 0.24 0.88 2.06 1411 1.00
theta_steps_unscaled[8] -0.34 0.02 0.89 -2.13 -0.94 -0.38 0.24 1.34 1774 1.00
theta_steps_unscaled[9] -0.78 0.02 0.89 -2.49 -1.39 -0.75 -0.17 0.86 1509 1.00
theta_steps_unscaled[10] -0.66 0.02 0.87 -2.34 -1.28 -0.71 -0.05 1.05 1928 1.00
我想訪問整個向量“theta_steps_unscaled”,首先是最終調試鏈,最后是 plot,例如它的均值/模式作為時間序列。 我一直在尋找類似regex_pars的東西,但似乎沒有“stan”對象。
我也試過這個來分析鏈條:
traceplot(fit, "theta_steps_unscaled[6]")
mcmc.list(x) 中的錯誤:Arguments 必須是 mcmc 對象
最后將其轉換為 mcmc:
traceplot(as.mcmc(fit, "theta_steps_unscaled[6]"))
雖然這引發了:
xy.coords 中的錯誤(x,y,xlabel,ylabel,log = log,recycle = TRUE):'list' object 不能被強制輸入'double'
請注意我的最后一個目標是 plot 整個向量theta_steps_unscaled[1:some_index]
,獲得類似 . 即具有可信區間、眾數/均值和其他貝葉斯參數。
tidybayes
是一種以方便的格式提取樣本的便捷方法。 (它也有方便的繪圖功能;這個小插圖給出了一個概述。)我會使用tidybayes
以你想要的方式提取你的樣本和ggplot2
到 plot 它們。
首先,提取采樣值:
library(tidyverse)
theme_set(theme_bw())
library(tidybayes)
samples.df = fit %>%
spread_draws(theta_steps_unscaled[t]) %>%
ungroup()
接下來,plot 它們與 x 軸上的步驟和 y 軸上的可信區間:
samples.df %>%
group_by(t) %>%
summarise(median = median(theta_steps_unscaled),
upper.95 = quantile(theta_steps_unscaled, 0.975),
lower.95 = quantile(theta_steps_unscaled, 0.025)) %>%
ggplot(aes(x = t, y = median)) +
geom_line(size = 1, color = "blue") +
geom_ribbon(aes(ymin = lower.95, ymax = upper.95),
fill = "blue", alpha = 0.1) +
scale_x_continuous("step", breaks = 1:10) +
scale_y_continuous(expression(theta))
plot 看起來像這樣:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.