FixedEffects / FixedEffectModels.jl

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

Error when using predict with coefficient interactions. #189

Closed Shresth-Garg closed 2 years ago

Shresth-Garg commented 2 years ago

I am trying to use the new predict method (on the master branch) in models with fixed effects.

The method works when there are there are no interactions in the formula. For example, in the attached data file, the method works on the following model.

model = reg(df_test, @formula(output ~ input + fe(farmer_id)), save = :all)
predict(model, df_test)

However, consider the following model, with interaction between crop and input.

model = reg(df_test, @formula(output ~ crop & input + fe(farmer_id)), save = :all)
predict(model, df_test)

The predict errors out with the following message:

ERROR: DimensionMismatch("second dimension of A, 8, does not match length of x, 9")
Stacktrace:
 [1] gemv!(y::Vector{Float64}, tA::Char, A::Matrix{Float64}, x::Vector{Float64}, α::Bool, β::Bool)
   @ LinearAlgebra C:\Users\shres\AppData\Local\Programs\Julia-1.7.1\share\julia\stdlib\v1.7\LinearAlgebra\src\matmul.jl:530
 [2] mul!
   @ C:\Users\shres\AppData\Local\Programs\Julia-1.7.1\share\julia\stdlib\v1.7\LinearAlgebra\src\matmul.jl:66 [inlined]
 [3] mul!
   @ C:\Users\shres\AppData\Local\Programs\Julia-1.7.1\share\julia\stdlib\v1.7\LinearAlgebra\src\matmul.jl:275 [inlined]
 [4] *(A::Matrix{Float64}, x::Vector{Float64})
   @ LinearAlgebra C:\Users\shres\AppData\Local\Programs\Julia-1.7.1\share\julia\stdlib\v1.7\LinearAlgebra\src\matmul.jl:47
 [5] predict(x::FixedEffectModel, df::DataFrame)
   @ FixedEffectModels C:\Users\shres\.julia\packages\FixedEffectModels\lKPVy\src\FixedEffectModel.jl:96
 [6] top-level scope
   @ REPL[89]:1

Is there a way to use the predict method in models with interactions?

df_test.csv

matthieugomez commented 2 years ago

@nilshg could you have a look?

nilshg commented 2 years ago

Sure, although it might take me a couple weeks as I'm pretty busy at the minute - I'll put it on the to do list but feel free to ping me if nothing has happened in a couple of weeks!

matthieugomez commented 2 years ago

Should be fixed now.