Multiple levels in hierarchical linear regression using PYMC3

Viewed 1562

I am trying to set up a hierarchical linear regression model using PYMC3. In my particular case, I want to see whether postal codes provide a meaningful structure for other features. Suppose I use the following mock data:

import pandas as pd
import numpy as np
import pymc3 as pm

data = pd.DataFrame({"postalcode": np.floor(np.random.uniform(low=10, high=99, size=1000)),
                 "x": np.random.normal(size=1000),
                 "y": np.random.normal(size=1000)})
data["postalcode"] = data["postalcode"].astype(int)

I generate postal codes from 10 to 99, as well as a normally distributed feature x and a target value y. Now I set up my indices for postal code level 1 and level 2:

def create_pc_index(level):
    pc = data["postalcode"].astype(str).str[0:level]
    unique_pc = pc.unique()
    pc_dict = dict(zip(unique_pc, range(0, len(unique_pc))))
    return pc_dict, pc.apply(lambda x: pc_dict[x]).values

pc1_dict, pc1_index = create_pc_index(1)
pc2_dict, pc2_index = create_pc_index(2) 

Using the first digit of the postal code as hierarchical attribute works fine:

number_of_samples = 1000

x = data["x"]
y = data["y"]

with pm.Model() as model:
    sigma = pm.HalfCauchy('sigma', beta=10, testval=0.5, shape=1)
    mu_i = pm.Normal("mu_i", 5, sd=25, shape=1)
    intercept = pm.Normal('Intercept', mu_i, sd=1, shape=len(pc1_dict))

    mu_s = pm.Normal("mu_x", 0, sd=3, shape=1)
    x_coeffs = pm.Normal("x", mu_s, 1, shape=len(pc1_dict))

    mean = intercept[pc1_index] + x_coeffs[pc1_index] * x

    likelihood_mean = pm.Deterministic("mean", mean)
    likelihood = pm.Normal('y', mu=likelihood_mean, sd=sigma, observed=y)

    trace = pm.sample(number_of_samples)
    burned_trace = trace[number_of_samples/2:]

However, if I want to add a second level to my hierarchy (in this case only on the intercept, ignoring x for the moment), I run into shape problems

with pm.Model() as model:
    sigma = pm.HalfCauchy('sigma', beta=10, testval=0.5, shape=1)
    mu_i_level_1 = pm.Normal("mu_i", 0, sd=25, shape=1)
    mu_i_level_2 = pm.Normal("mu_i_level_2", mu_i_level_1, sd=1, shape=len(pc1_dict))
    intercept = pm.Normal('Intercept', mu_i_level_2[pc1_index], sd=1, shape=len(pc2_dict))

    mu_s = pm.Normal("mu_x", 0, sd=3, shape=1)
    x_coeffs = pm.Normal("x", mu_s, 1, shape=len(pc1_dict))

    mean = intercept[pc2_index] + x_coeffs[pc1_index] * x

    likelihood_mean = pm.Deterministic("mean", mean)
    likelihood = pm.Normal('y', mu=likelihood_mean, sd=sigma, observed=y)

    trace = pm.sample(number_of_samples)
    burned_trace = trace[number_of_samples/2:]

The error message is:

operands could not be broadcast together with shapes (89,) (1000,) 

How do I model multiple levels in my regression correctly? Is this just an issue with the correct shape size or is there a more fundamental error on my part?

Thanks in advance!

1 Answers

I don't think intercept can have a shape of len(pc2_dict) but a mu of len(pc1_dict). The contradiction is here:

intercept = pm.Normal('Intercept', mu_i_level_2[pc1_index], sd=1, shape=len(pc2_dict))
Related