# Including predictors in Dirichlet-multinomial models

**URL:** https://discourse.pymc.io/t/including-predictors-in-dirichlet-multinomial-models/10622
**Category:** v5
**Tags:** modeling
**Created:** [October 18, 2022, 11:32pm UTC](https://discourse.pymc.io/t/including-predictors-in-dirichlet-multinomial-models/10622 "2022-10-18T23:32:50Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![mr\_penguin](https://avatars.discourse-cdn.com/v4/letter/m/5daacb/32.png) [@mr\_penguin](https://discourse.pymc.io/u/mr_penguin)
#### Post date: [October 18, 2022, 11:32pm UTC](https://discourse.pymc.io/t/including-predictors-in-dirichlet-multinomial-models/10622/1 "2022-10-18T23:32:50Z")

</div>

I am trying to build a DM model based on the [example](https://docs.pymc.io/en/v3/pymc-examples/examples/mixture_models/dirichlet_mixture_of_multinomials.html) in the documentation, but with some predictor variables, and I get different errors each time I adjust my code.

I have some data adjusted from the example:

```auto
import pymc as pm
import pandas as pd
import numpy as np
import scipy as sp
import scipy.stats
import arviz

rng = np.random.default_rng(0)

# Sample data structure
true_conc = 6.0
true_frac = np.array([0.5, 0.25, 0.25])
k = len(true_frac) # Number of different tree species
n = 10 # Number of forests
total_count = 50 # Number of trees in each forest
true_p = sp.stats.dirichlet(true_conc * true_frac).rvs(size=n)
observed_counts = np.vstack([sp.stats.multinomial(n=total_count, p=p_i).rvs() for p_i in true_p])
x = np.random.normal(0,1,n)

print(observed_counts)

```

I then run the code on the example, and get three frac posteriors and one conc parameter:

```auto
# Version 1 - intercept only
with pm.Model() as model_dm_1:
    frac = pm.Dirichlet("frac", a=np.ones(k))
    conc = pm.Lognormal("conc", mu=1, sigma=1)
    counts = pm.DirichletMultinomial("counts", n=total_count, a=frac * conc, shape=(n, k), observed=observed_counts)
    # Sample
    trace_dm_1 = pm.sample(chains = 4, return_inferencedata = True)

pm.model_to_graphviz(model_dm_1)
arviz.plot_posterior(trace_dm_1)

```

When I try to add in a predictor to the fraction, I get an error on sample. When I run model\_to\_graphviz, the dimensions do not match up (again varying each time), so I assume that it is failing on some matrix multiplication, but I can’t figure out why.

```auto
# Version 2 - some predictors
with pm.Model() as model_dm_2:
    b0 = pm.Normal("b0", shape = k)
    b1 = pm.Normal("b1", shape = k)
    y = b0 + b1 * [x, x, x]
    
    frac = pm.Dirichlet("frac", a=pm.math.exp(y))
    conc = pm.Lognormal("conc", mu=1, sigma=1)
    counts = pm.DirichletMultinomial("counts", n=total_count, a=frac * conc, shape=(n, k), observed=observed_counts)
    
    # Sample
    trace_dm_2 = pm.sample(chains = 4, return_inferencedata = True)

```

Gives me `Incompatible Elemwise input shapes [(3, 10), (10, 3)]`  
Another change (removing `[x, x, x]` and just putting `x` gives me `SpecifyShape: dim 0 of input has shape 3, expected 10.`  
Another change to replace frac gave me `Incompatible Elemwise input shapes [(10, 3), (1, 10)]`

The y = b0 + b1 \* x part works on a Poisson regression and on a linear regression, so where am I going wrong on Dirichlet regression?

---

<div class="post-metadata">

### Author: ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)
#### Post date: [October 21, 2022, 6:26am UTC](https://discourse.pymc.io/t/including-predictors-in-dirichlet-multinomial-models/10622/2 "2022-10-21T06:26:52Z")

</div>

I haven’t checked your model carefully, but multivariate distributions are very specific about the other of their dimensions. This guide might be of help: [Distribution Dimensionality — PyMC 0+untagged.345.g2bd0611.dirty documentation](https://www.pymc.io/projects/docs/en/stable/learn/core_notebooks/dimensionality.html)
