Yes, I can. I’m using pystan, I have a ntb for that (just do not know how to share it here, so I will post the code).
The STAN code is the one I posted before, ra_noLA.
The python code goes as follow:
# define conditions and path
import os, sys
import pystan
import pickle
import numpy as np
import pandas as pd
import time
from datetime import timedelta
model_folder = "C:/Users/user/stan_files/"
data_path = 'C:/Users/user/data/'
#prepare model
model = 'ra_noLA.stan'
model_path = os.path.join(model_folder, model)
try:
with open(os.path.join(data_path, 'data_LA_exp1_testSTAN.txt'), "rb") as fp: # Unpickling
LA_data_prep_test = pickle.load(fp)
print('loading data from folder')
except IOError:
print("File not accessible")
# Time the process
start = time.time()
# Compile the model
start_compile = time.time()
stan_model_noLA = pystan.StanModel(file=model_path)
end_compile = time.time()
print('Duration of compilation: ', timedelta(seconds=end_compile - start_compile))
# run the model
start_model = time.time()
fit_noLA = stan_model_noLA.sampling(data=LA_data_prep_test,
chains=4, iter=2000, warmup=1000, thin=1, init='random', verbose=True,
control = {"adapt_delta":0.95, "stepsize":1, "max_treedepth":10}, n_jobs=-1)
end_model = time.time()
print('Duration of the model run is: ', timedelta(seconds=end_model - start_model))
# Create an ordered dictionary
summary_dict = fit_noLA.summary()
#print(summary_dict)
# Create a data frame out of the output
df_noLA = pd.DataFrame(summary_dict['summary'],
columns=summary_dict['summary_colnames'],
index=summary_dict['summary_rownames'])
end = time.time()
print('Duration of the entire process is: ', timedelta(seconds=end - start))
My data example is attached. data_LA_exp1_testSTAN.txt (7.4 KB)
Printing df_noLA.loc['y_pred[1,1]':'y_pred[1,256]', 'mean']
gives the values and type:
y_pred[1,1] 0.00025
y_pred[1,2] 0.00000
y_pred[1,3] 0.00050
y_pred[1,4] 0.00025
y_pred[1,5] 0.00075
…
y_pred[1,252] 0.81200
y_pred[1,253] 0.86200
y_pred[1,254] 0.90425
y_pred[1,255] 0.93550
y_pred[1,256] 0.94850
Name: mean, Length: 256, dtype: float64