Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design matrix for interaction term missing factor when no intercept is used #114

Open
filthysocks opened this issue Jan 27, 2025 · 1 comment

Comments

@filthysocks
Copy link

Hello,

I'm using a model without intercept with an interaction term between two groups and i think i'm missing an interaction term.

Here a simple example that reproduces the problem:

from formulae import design_matrices
import pandas as pd

user = ["1","1","2","2","3","3"]
group = ["a","b"]*3
score = [1]*len(user)

data = pd.DataFrame({"user":user, "group":group, "score":score})

dm = design_matrices("score ~ 0 + user*group",data)
dm.common

output:

CommonEffectsMatrix with shape (6, 6)
Terms:  
  user  
    kind: categoric
    levels: ['1', '2', '3']
    columns: 0:3
  group  
    kind: categoric
    levels: ['b']
    column: 3
  user:group  
    kind: interaction
    levels: ['2, b', '3, b']
    columns: 4:6

Where is the interaction term '1, b'?
Is this a bug or am i not getting something here?

Version: 0.5.4

@tomicapretto
Copy link
Collaborator

Hi @filthysocks

That is the expected behavior. The operation a*b is equivalent to a + b + a:b which means "main effects plus interactions".
Since you're suppressing the intercept with the explicit 0 the first main effect a does not have any restriction and thus spans the intercept. Then, b gets a restriction because the intercept is already spanned (otherwise there will be redundancies), and the same happens with the interaction term a:b.

An equivalent model, in terms of what gets into the model is 0 + a:b.

A good explanation can be found in Patsy documentation here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants