(Lack of) Symmetry in Interaction Terms

Open
#198 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
julia
Domain
data

Research direction

Start with the @formula interaction construction shown in the issue and inspect how InteractionTerms are compared. Decide whether operand order should be canonical or preserved, including implications for customized schemas. Done means the chosen behavior is implemented and documented, with the examples covering both ab and ba.

Written by the indexing model from the issue text.

Description

Right now, interaction terms are constructed with their constituent bits in the order specified in the formula:


julia> ab = @formula(0 ~ a * b)
FormulaTerm
Response:
  0
Predictors:
  a(unknown)
  b(unknown)
  a(unknown) & b(unknown)

julia> ba = @formula(0 ~ b * a)
FormulaTerm
Response:
  0
Predictors:
  b(unknown)
  a(unknown)
  b(unknown) & a(unknown)

The equality of InteractionTerms is impacted by this behavior:

julia> ba.rhs[2] == ab.rhs[1] # the terms for b are equal
true

julia> ba.rhs[3] == ab.rhs[3] # the terms for 
false

I can't find anything in the docs about whether this is behavior is defined to be this way or whether it's just an implementation detail. For my own purposes, it would be swell if there were a canonical ordering to interaction terms (especially since there is an lexical ordering applied in other contexts, e.g. the order of levels of a categorical term), but I can see counter arguments for keeping the order as specified in the formula. No matter which way the decision falls, I think it should be documented (or that the ordering is an implementation detail and subject to change).

The other side of this coin is that a&b != b&a. For standard use of interaction terms in statistics, this symmetry holds because they're formed by the element-wise multiplication of the two columns, which is a commutative operation. It's probably very difficult to make this equivalence hold without having a canonical ordering somewhere, whether if it's in a stored private field or recomputed on the fly. Again, I can see some potential counter arguments to this, especially for more customized schema, but we should probably make an explicit documentation note because this goes against the usual interpretation and underlying math of interactions.

cc: @ararslan

Dominant language
Julia
Stars
252
Forks
36
Avg merge
3h 14m
Merged PRs (30d)
4

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaStats/StatsModels.jl

All issues in JuliaStats/StatsModels.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.