StatsModels.jl icon indicating copy to clipboard operation
StatsModels.jl copied to clipboard

Missing method for fe(:a) * fe(:d) is missing when using term()

Open IljaK91 opened this issue 3 years ago • 2 comments

Hi everyone, I was sent over from FixedEffectModels.jl as the problem seems to stem from StatsModels.jl

Here is the link to the thread: https://github.com/FixedEffects/FixedEffectModels.jl/issues/206

Here is the original issue:

The following throws an error when using *

df = DataFrame(a=rand(10), b=rand(10), c=[1, 1, 1, 0, 0, 0, 0, 0, 0, 0], d=[0,0,0,0,0, 1, 1, 1, 1, 1 ])

reg(df, term(:a) ~ term(:b) + fe(:c) * fe(:d)) # This throws an error
reg(df, term(:a) ~ term(:b) + fe(:c) & fe(:d)) # This works
reg(df, term(:a) ~ term(:b) + fe(:c) + fe(:d) + fe(:c) & fe(:d)) # This works

error message:

ERROR: MethodError: no method matching *(::FixedEffectModels.FixedEffectTerm, ::FixedEffectModels.FixedEffectTerm)
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at C:\Users\kantorov\.julia\juliaup\julia-1.7.3+0~x64\share\julia\base\operators.jl:655
  *(::Union{MathOptInterface.ScalarAffineFunction{T}, MathOptInterface.ScalarQuadraticFunction{T}, MathOptInterface.VectorAffineFunction{T}, MathOptInterface.VectorQuadraticFunction{T}}, ::T) where T at C:\Users\kantorov\.julia\packages\MathOptInterface\AiEiQ\src\Utilities\functions.jl:3270
  *(::ChainRulesCore.AbstractThunk, ::Any) at C:\Users\kantorov\.julia\packages\ChainRulesCore\16PWJ\src\tangent_arithmetic.jl:125
  ...
Stacktrace:
 [1] top-level scope
   @ c:\Users\kantorov\Desktop\mwe.jl:13

IljaK91 avatar Jul 19 '22 10:07 IljaK91

Among other things, this is fixed by #183 IIRC.

kleinschmidt avatar Jul 19 '22 16:07 kleinschmidt

As for why, it's because the formula syntax (distributed/associative rules for + and &, * expansion) are handled inside the @formula macro currently, and not at runtime. I decided at some point that it was better to have one source of truth for how these expansions worked or else you're in danger of getting different results from superficially similar formulae (at run-time vs. @formula). #183 changes that one source of truth to be the run-time operators themselves, so everything is handled by the methods like *(::AbstractTerm, ::AbstractTerm)

kleinschmidt avatar Jul 19 '22 17:07 kleinschmidt