Shape broadcast error (in PyMC) for model with
See original GitHub issueHello all,
Thanks again for this awesome package! I am having some trouble getting Bambi to build a model with a fairly complex set of terms (a spline term interaction with a categorical predictor, plus a three-way interaction between the same categorical predictor and two continuous, plus a group-specific effect). However the problem does not seem caused by Bambi but in PyMC - formulae
will build the design matrices (but not convert to a pandas DataFrame) but PyMC throws a ValueError
when trying to broadcast operands together. I will head to the PyMC repo if needed, but thought I would ask here first!
Here is a minrepex that mimics the data structure and the exact specification I am using, giving the same error, though the actual dataset is much bigger:
test_data = {'session': np.repeat([f'pid_{x}' for x in np.arange(0, 10)], 6),
'state': np.tile(['lonely', 'depressed', 'hopeful', 'stressed', 'positive', 'isolated'], 10),
'level': np.repeat(np.random.normal(0, 1, size=5), 12),
'trait': np.repeat(np.random.normal(0, 1, size=10), 6),
'time': np.repeat(np.arange(0, 5), 12),
'rating': np.random.randint(1, 6, size=60)}
test_data = (pd.DataFrame(test_data)
.assign(state = lambda x: x['state'].astype('category'),
session = lambda x: x['session'].astype('category'))
)
tester_mod = bmb.Model('rating ~ bs(time, degree=2, df=3) * state + state * level * trait + (1|session)', data=test_data)
tester_mod.build()
I get a ValueError
from PyMC - operands could not be broadcast together with shapes (5,) (15,)
. I can’t think why this is happening, but the offending term seems to be the bs(time, degree=2, df=3) * state
term - dropping the interaction leads to the model being built.
Is this related to the coords
Bambi/formulae give categorical predictors? I coded my own model in PyMC to work around this using patsy
, and I noted that the intercept term (and thus one of the state
level predictions) has basically no variability after estimation, and I am guessing Bambi does something clever behind the scenes to negate this! Any help hugely appreciated!
Issue Analytics
- State:
- Created a year ago
- Comments:8 (3 by maintainers)
@alexjonesphd I’m sorry about that issue. And yes, please open an issue in formulae to see if we can solve it.
@alexjonesphd this was a problem with how formulae was handling levels of interactions involving numerical terms with more than one column (as is in the case of a bs() call). If you install
formulae
from the development version, it should work now.Do
It’s working on my side now