FixedEffects / FixedEffectModels.jl

Fast Estimation of Linear Models with IV and High Dimensional Categorical Variables
Other
227 stars 46 forks source link

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

Closed IljaK91 closed 2 years ago

IljaK91 commented 2 years ago

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

Plus, I thought that the second and third regression should yield the same, but indeed the standard errors are very different. Or did I misunderstand something?

matthieugomez commented 2 years ago

For the first issue, you should open an issue on StatsModels on why term(:a) * term(:b) does not work even though term(:a) & term(:b) does — I am just following the methods defined in that package.

For the second issue, this is to be expected: it's super confusing but fe(:c) * fe(:d) actually means fe(:c) + fe(:d) + fe(:c) & fe(:d).

kleinschmidt commented 2 years ago

FWIW there's no reason why you can't implement whatever run-time operators you want even if they're not defined in StatsModels per se!

matthieugomez commented 2 years ago

@kleinschmidt I'm a lazy man ;)

matthieugomez commented 2 years ago

@IljaK91 to conclude: instead of writing fe(:c) * fe(:d), write fe(:c) + fe(:d) + fe(:c) & fe(:d), which does the same thing.