i updated code as
# Define the model
with pm.Model() as hierarchical_model:
#Define the data
Location = pm.Data('LocationDesc_encoded', training_data['LocationDesc_encoded'],mutable=True)
Obesity = pm.Data('obesity_Prevalence', training_data['obesity_Prevalence'],mutable=True)
Data_val_py = pm.Data('data_value_py', training_data['data_value_py'],mutable=True)
Avg_Temp = pm.Data('Avg Temp(°F)', training_data['Avg Temp(°F)'],mutable=True)
AQI = pm.Data('AQI', training_data['AQI'],mutable=True)
# Hyperpriors for group nodes
mu_a = pm.Normal('mu_a', mu=0, sigma=0.5)
sigma_a = pm.HalfCauchy('sigma_a', beta=1)
# Priors for individual intercepts
a = pm.Normal('a', mu=mu_a, sigma=sigma_a, shape=len(training_data['LocationDesc_encoded'].unique()))
# Hyperpriors for group slopes
mu_b = pm.Normal('mu_b', mu=0, sigma=1)
sigma_b = pm.HalfCauchy('sigma_b', beta=1)
# Priors for individual slopes
b = pm.Normal('b', mu=mu_b, sigma=sigma_b, shape=(len(training_data['LocationDesc_encoded'].unique()), 4))
# Model error
sigma = pm.HalfCauchy('sigma', beta=1)
# Expected value
mu = a[Location] + \
b[Location, 0] * Obesity + \
b[Location, 1] * Data_val_py + \
b[Location, 2] * Avg_Temp + \
b[Location, 3] * AQI
# Likelihood
Y_obs = pm.Normal('Y_obs', mu=mu, sigma=sigma, observed=training_data['Data_Value'])
with hierarchical_model:
idata = pm.sample(100, chains=2, target_accept=0.9,random_seed = rng)
with hierarchical_model:
pm.set_data({
'LocationDesc_encoded': test_data['LocationDesc_encoded'],
'obesity_Prevalence': test_data['obesity_Prevalence'],
'data_value_py': test_data['data_value_py'],
'Avg Temp(°F)': test_data['Avg Temp(°F)'],
'AQI': test_data['AQI']
})
posterior_predictive = pm.sample_posterior_predictive(idata, random_seed=rng)
Now it is giving shape mismatch error because training data is (392 x 8) and test data is of (97 x 8).
To resolve this I tried
with hierarchical_model:
pm.Model.set_dim('LocationDesc_encoded', len(set(test_data['LocationDesc_encoded'])))
but this is giving error as
TypeError: Model.set_dim() missing 1 required positional argument: 'new_length'
Please help to resolve issue!