简体   繁体   English

stan矢量参数的一个不错的pystan轨迹图

[英]a nice pystan trace plot for a stan vector parameter

I am doing a multiple regression in Stan. 我在Stan中进行多元回归。

I want a trace plot of the beta vector parameter for the regressors/design matrix. 我想要回归器/设计矩阵的beta矢量参数的迹线图。

When I do the following: 当我执行以下操作时:

fit = model.sampling(data=data, iter=2000, chains=4)
fig = fit.plot('beta')

I get a pretty horrid image: 我得到一个非常恐怖的形象:

矢量参数的可怕轨迹图

I was after something a little more user friendly. 我在追求一些用户友好的东西。 I have managed to hack the following which is closer to what I am after. 我设法破解了以下内容,使其更接近我的追求。

矢量参数的更好的示踪图

My hack plugs into the back of pystan as follows. 我的hack如下插入pystan的背面。

r = fit.extract() # r for results
from pystan.external.pymc import plots
param = 'beta'
beta = r[param] 
name = df.columns.values.tolist()
(rows, cols) = beta.shape
assert(len(df.columns) == cols)
values = {param+'['+str(k+1)+'] '+name[k]: 
    beta[:,k] for k in range(cols)}
fig = plots.traceplot(values, values.keys())
for a in fig.axes:
    # shorten the y-labels
    l = a.get_ylabel()
    if l == 'frequency': 
        a.set_ylabel('freq')
    if l=='sample value': 
        a.set_ylabel('val')
fig.set_size_inches(8, 12)
fig.tight_layout(pad=1)
fig.savefig(g_dir+param+'-trace.png', dpi=125)
plt.close()

My question - surely I have missed something - but is there an easier way to get the kind of output I am after from pystan for a vector parameter? 我的问题-当然我错过了一些事情-但是有没有更简单的方法可以从pystan获得我想要的矢量参数输出呢?

Discovered that the ArviZ module does this pretty well. 发现ArviZ模块可以很好地完成此任务。

ArviZ can be found here: https://arviz-devs.github.io/arviz/ 可以在这里找到ArviZ: https ://arviz-devs.github.io/arviz/

I also struggled with this and just found a way to extract the parameters for the traceplot (the betas, I already knew). 我也为此而苦苦挣扎,只是找到了一种提取traceplot参数的方法(我已经知道beta)。

When you do your fit, you can save it to a dataframe: 适合时,可以将其保存到数据框:

fit_df = fit.to_dataframe()

Now you have a new variable, your dataframe. 现在,您有了一个新变量,即数据框。 Yes, it took me a while to find that pystan had a straightforward way to save the fit to a dataframe. 是的,我花了一段时间才发现pystan有一种直接的方法来将拟合保存到数据框中。

With that at hand you can check your dataframe. 有了它,您就可以检查数据框。 You can see it's header by printing the keys: 您可以通过打印键来查看其标题:

fit_df.keys()

the output is something like this: 输出是这样的:

Index([u'chain', u'chain_idx', u'warmup', u'accept_stat__', u'energy__',
       u'n_leapfrog__', u'stepsize__', u'treedepth__', u'divergent__',
       u'beta[1,1]', ...
       u'eta05[892]', u'eta05[893]', u'eta05[894]', u'eta05[895]',
       u'eta05[896]', u'eta05[897]', u'eta05[898]', u'eta05[899]',
       u'eta05[900]', u'lp__'],
      dtype='object', length=9037)

Now, you have everything you need! 现在,您拥有所需的一切! The betas are in columns as well as the chain ids. Beta在列中以及链ID。 That's all you need to plot the betas and traceplot. 这就是绘制beta和traceplot所需要的。 Therefore, you can manipulate it in anyway you want and customize your figures as you wish. 因此,您可以根据需要任意操作它,并根据需要自定义图形。 I'll show you an example of how I did it: 我将向您展示一个示例:

chain_idx = fit_df['chain_idx']
beta11 = fit_df['beta[1,1]']
beta12 = fit_df['beta[1,2]']

plt.subplots(figsize=(15,3))
plt.subplot(1,4,1)
sns.kdeplot(beta11)
plt.subplot(1,4,2)
plt.plot(chain_idx, beta11)

plt.subplot(1,4,3)
sns.kdeplot(beta12)
plt.subplot(1,4,4)
plt.plot(chain_idx, beta12)

plt.tight_layout()
plt.show()

The image from the above plot! 上图的图片!

I hope it helps (if you still need it) ;) 希望对您有所帮助(如果您仍然需要);)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM