# Hierarchical Modeling Index Error

**URL:** <https://discourse.pymc.io/t/hierarchical-modeling-index-error/2531>\
**Category:** Questions\
**Created:** [January 15, 2019, 4:07pm UTC](https://discourse.pymc.io/t/hierarchical-modeling-index-error/2531 "2019-01-15T16:07:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jordan.howell2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jordan.howell2/32/1742_2.png) [@jordan.howell2](https://discourse.pymc.io/u/jordan.howell2)\
**Post date:** [January 15, 2019, 4:07pm UTC](https://discourse.pymc.io/t/hierarchical-modeling-index-error/2531/1 "2019-01-15T16:07:40Z")

</div>

Hello. I’m trying to learn hierarchical modeling with a simple regeression model based on the Walmart sales data set found on Kaggle. The deptartments are 99 different departments represented by an integer. I tried following along with the tutorial found at [https://docs.pymc.io/notebooks/GLM-hierarchical.html](https://docs.pymc.io/notebooks/GLM-hierarchical.html).

I keep getting an out of bounds error though. Models and error message below. Thank you.

> dept\_numbers = X\_train[‘Dept’].unique()  
> dept\_idx = X\_train[‘Dept’].values  
> n\_dept = len(X\_train[‘Dept’].unique())
> 
> with pm.Model() as sales\_model:
> 
> ```
> #define the priors
> mu_a = pm.Normal('mu_a', mu = 0, sd = 100)
> sigma_a = pm.HalfCauchy('sigma_a', 5)
> mu_b = pm.Normal('mu_b', mu = 0, sd = 100)
> sigma_b = pm.HalfCauchy('sigma_b', 5)
> 
> alpha = pm.Normal('intercept', mu= mu_a, sd = sigma_a, shape = n_dept)
> beta_1 = pm.Normal('dept', mu = mu_b, sd = sigma_b, shape = n_dept)
> beta_2 = pm.Normal('IsHoliday_T', mu = 0, sd = 1, shape = X_train['IsHoliday_True'].shape)
> #beta_3 = pm.Normal('Week', mu=0, sd = 10)
> #beta_4 = pm.Normal('Fuel_Prices', mu=0, sd = 10)
> #beta_5 = pm.Normal('Temperature', mu=0, sd = 10)
> #beta_6 = pm.Normal('Markdown1', mu=0, sd = 10)
> #beta_7 = pm.Normal('Markdown2', mu=0, sd = 10)
> #beta_8 = pm.Normal('Markdown4', mu=0, sd = 10)
> #beta_9 = pm.Normal('Markdown5', mu=0, sd = 10)
> #beta_10 = pm.Normal('CPI', mu=0, sd = 10)
> #beta_11 = pm.Normal('Unemployment', mu=0, sd = 10)
> 
> s = pm.Normal('sd', mu = 0, sd = 50)
> 
> #define the likelihood
> mu = alpha[dept_idx] + beta_1[dept_idx]*X_train['Dept'].values + 
> beta_2*X_train['IsHoliday_True'].values
> 
> y = pm.StudentT('sales', nu=len(Y_train)-1, mu = mu, observed = Y_train, shape = 
> Y_train.shape)
> 
> trace = pm.sample(draws=5000 ,init='advi' ,progressbar=True)
> print(sales_model.check_test_point())
> 
> ```

This is the errror:

> * * *
> 
> IndexError Traceback (most recent call last)  
> in ()  
> 26  
> 27 #define the likelihood  
> —\> 28 mu = alpha[dept\_idx] + beta\_1[dept\_idx]_X\_train[‘Dept’].values + beta\_2_X\_train[‘IsHoliday\_True’].values  
> 29  
> 30 y = pm.StudentT(‘sales’, nu=len(Y\_train)-1, mu = mu, observed = Y\_train, shape = Y\_train.shape)
> 
> ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\theano\tensor\var.py in **getitem** (self, args)  
> 568 TensorVariable, TensorConstant,  
> 569 theano.tensor.sharedvar.TensorSharedVariable))):  
> → 570 return self.take(args[axis], axis)  
> 571 else:  
> 572 return theano.tensor.subtensor.advanced\_subtensor(self, \*args)
> 
> ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\theano\tensor\var.py in take(self, indices, axis, mode)  
> 612  
> 613 def take(self, indices, axis=None, mode=‘raise’):  
> → 614 return theano.tensor.subtensor.take(self, indices, axis, mode)  
> 615  
> 616 # COPYING
> 
> ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\theano\tensor\subtensor.py in take(a, indices, axis, mode)  
> 2429 return advanced\_subtensor1(a.flatten(), indices)  
> 2430 elif axis == 0:  
> → 2431 return advanced\_subtensor1(a, indices)  
> 2432 else:  
> 2433 if axis \< 0:
> 
> ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\theano\gof\op.py in **call** (self, \*inputs, \*\*kwargs)  
> 672 thunk.outputs = [storage\_map[v] for v in node.outputs]  
> 673  
> → 674 required = thunk()  
> 675 assert not required # We provided all inputs  
> 676
> 
> ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\theano\gof\op.py in rval()  
> 860  
> 861 def rval():  
> → 862 thunk()  
> 863 for o in node.outputs:  
> 864 compute\_map[o][0] = True
> 
> ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\theano\gof\cc.py in **call** (self)  
> 1733 print(self.error\_storage, file=sys.stderr)  
> 1734 raise  
> → 1735 reraise(exc\_type, exc\_value, exc\_trace)  
> 1736  
> 1737
> 
> ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\six.py in reraise(tp, value, tb)  
> 691 if value. **traceback** is not tb:  
> 692 raise value.with\_traceback(tb)  
> → 693 raise value  
> 694 finally:  
> 695 value = None
> 
> IndexError: index 79 is out of bounds for size 77

---

<div class="post-metadata">

**Author:** ![jordan.howell2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jordan.howell2/32/1742_2.png) [@jordan.howell2](https://discourse.pymc.io/u/jordan.howell2)\
**Post date:** [January 15, 2019, 6:30pm UTC](https://discourse.pymc.io/t/hierarchical-modeling-index-error/2531/2 "2019-01-15T18:30:21Z")

</div>

Nevermind. I figured it out. I added cat.codes for each department, then made those the idx vector.
