Currently, I am doing modeling and parameter estimation using stan, but since the Trae plot is crushed and difficult to see, there was a way to make it a little easier to see, so I will share it. (Maybe it's natural for those who know ...) Also, please let me know if there is a better way.
OS | Windows10 |
Python | 3.7.4 |
PyStan | 2.19.1 |
stan_model_ver1.py
import arviz
import pandas as pd
data = pd.read_csv("~~.csv")
dat = {"Defined in dictionary"}
model = StanModel(file="~~.stan")
fit = model.sampling(data=dat, n_jobs=-1, seed=999, iter=1000,chains=1)
arviz.plot_trace(fit)
Something like this is drawn with.
However, if there are many parameters, it is hard to see ...
Therefore,
stan_model_ver2.py
import arviz
import pandas as pd
data = pd.read_csv("~~.csv")
dat = {"Defined in dictionary"}
model = StanModel(file="~~.stan")
fit = model.sampling(data=dat, n_jobs=-1, seed=999, iter=1000,chains=1)
'''====Change below ==='''
fit_df = fit.to_dataframe()
index = fit_df["draw"]
lenght = len(fit_df.keys())-7
for i in range(lenght):
ob = fit_df[fit_df.keys()[i+3]]
plt.subplots(figsize=(15, 7))
plt.title(f"{fit_df.keys()[i+3]}")
plt.subplot(1, 2, 1)
sns.distplot(ob)
plt.subplot(1, 2, 2)
plt.plot(index, ob)
plt.savefig(f"figure/stan_figure/{fit_df.keys()[i+3]}.png ")
plt.show()
will do.
It takes a little time to create a data frame and visualize each parameter one by one,
In this way, it becomes easier to see each stage.
If the number of parameters is large, even if you try to check the convergence state of each parameter by print (fit.stansummary ())
, it may not be displayed. (Especially considering the time series model)
In such a case
omake.py
summary_df = fit.stansummary()
file = open('summary_stan.txt', 'w')
string = summary_df
file.write(string)
If you save it as a text file like this, you can check the status of the parameters you want to see.
Recommended Posts