Hello everyone.
I have been studying pymc version 5 for a few weeks now. My goal is to build a simple linear regression model and make out-of-sample predictions.
Reading the documentation and other community posts, I have written this short and simplified code.
with pm.Model(coords=coords) as model:
y = pm.MutableData('targets', df['y'].values.squeeze())
contributions = []
Z = pm.MutableData('control_data', df_controls.values)
control_betas = pm.Normal('control_beta', sigma = 2, dims=['controls'])
for w in range(n_controls):
z = Z[:,w]*control_betas[w]
contributions.append(z)
mu = pm.Deterministic("contributions", tt.stack(contributions).T, dims=['all_vars'])
sigma = pm.HalfNormal('sigma', sigma=1)
y_hat = pm.Normal("y_hat", mu=mu.sum(axis=-1), sigma=sigma, observed=target, shape=Z.shape[0])
with model:
data = pm.sample(idata_kwargs={'dims':{'contributions':[None, 'all_vars']}})
z_test = df_x_test.values
with model:
pm.set_data({"control_data":z_test})
idata.extend(pm.sample_posterior_predictive(idata))
y_hat_predicted = idata.posterior_predictive['y_hat'].mean(dim=["chain", "draw"])
The code works correctly, I get the new out-of-sample y, but I would also like to have predictions of the contributions of the individual variables, not just the y.
Is this possible?
I tried looking in the idata object but without success.
Can someone kindly point out where I am going wrong in the code or what code I need to insert to also get these predictions?
Thanks in advance to anyone who can help.