question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unintuitive: order matters in specifying mixed effects?

See original GitHub issue

Hi - I’ve recently been using bambi 0.6.3 with pymc3 3.11.2 and python 3.8. I spent a while struggling to specify mixed effects - interactions like I’d do in patsy (* or :) worked fine, but | gave me a slope+intercept per observation and OOMed on real data. After a fair bit of unsuccessful effort - I thought bambi thought my continuous variable was a category or that my category was continuous - realized I need to do feature|group, not group|feature. It’s very possible this is standard practice in other bayesian linear model packages, but I find this very unintuitive and suspect others might feel similarly? In case this is a bug, here’s code to reproduce on a toy dataset:

# debug on toy data
from sklearn.datasets import make_regression


xs, ys = make_regression(
    n_samples=500,
    n_features=5,
    n_informative=5,
    n_targets=1,
    bias=0.0,
    effective_rank=None,
    tail_strength=0.5,
    noise=5e1,
    shuffle=True,
    coef=False,
    random_state=42,
)

feat_names = {i: f"f{i}" for i in range(5)}
xys = pd.concat(
    [pd.DataFrame(xs).rename(columns=feat_names), pd.Series(ys).rename("y")], axis=1
)
# add hierarchy
xys["group_i"] = xys.index // 50
xys["group"] = "g_" + xys.group_i.astype("str")

# add group intercept
xys["y"] = xys.y + 30 * xys.group_i
# add group slope
xys["y"] = xys.y + 1e1 * xys.f0 * xys.group_i

# scale y to unit normal
xys["y"] = (xys.y - xys.y.mean()) / xys.y.std()

# expected behavior: mixed effects
debug_model = bmb.Model(
    "y ~ f0 + f1 + f2 + f3 + f4 + (f0|group_i)",
    data=xys,
)
debug_model
debug_model.build()
debug_model.graph()

# unintuitive behavior: one group per observation!
debug_model = bmb.Model(
    "y ~ f0 + f1 + f2 + f3 + f4 + (group_i|f0)",
    data=xys,
)
debug_model
debug_model.build()
debug_model.graph()

expected: image

unexpected: image

Appreciate your help and am a fan of the package 😃 rapid progress since I last used it this ~spring. Looking forward to splines!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
No-Streamcommented, Nov 29, 2021

Yes, that would help - just trying to make it so the next person doesn’t end up very confused why they have one group per observation. Feel free to close.

Read more comments on GitHub >

github_iconTop Results From Across the Web

lme4: Linear Mixed-Effects Models using 'Eigen' and S4
Fit a generalized linear mixed-effects model (GLMM). Both fixed effects and random effects are specified via the model formula. Usage glmer( ...
Read more >
Estimating power in (generalized) linear mixed models
In all scenarios, we will estimate power in order to detect differences in the fixed effects (i.e. regression coefficients) of the model ...
Read more >
Mixed-effects modeling with crossed random effects for ... - OSF
This paper provides an introduction to mixed-effects models for the analysis of repeated measurement data with sub- jects and items as crossed random ......
Read more >
Specifying a Random Intercept or Random Slope Model in ...
It's the covariance structure of the random effects. In the models above, both mixed and genlinmixed, I'm using variance components, which is telling...
Read more >
Linear mixed-effects modeling approach to FMRI group analysis
Here we present a linear mixed-effects modeling (LME) methodology that extends the conventional group analysis approach to analyze many ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found