# Regression implementation using FixedEffectModels.jl, InteractiveFixedEffectModels.jl, GLFixedEffectModels.jl packages

**URL:** <https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036>\
**Category:** Machine Learning\
**Tags:** package, regression\
**Created:** [October 26, 2020, 11:18am UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036 "2020-10-26T11:18:08Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antonina\_Klyuyeva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antonina_klyuyeva/32/18875_2.png) [@Antonina\_Klyuyeva](https://discourse.julialang.org/u/Antonina_Klyuyeva)\
**Post date:** [October 26, 2020, 11:18am UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/1 "2020-10-26T11:18:08Z")

</div>

Hi guys,  
I need to implement a regression like  
`Y ~ A : B : C`  
or  
`Y ~ A : B_C`

where main effects are missing, in interactions `A` - numeric variable, `B` and `C` - categorical variables (factors)) or in the second case - variable `B_C` - new categorical variable which is a combination of B and C. Intercept must be present.

Can someone help me, please, implement these formulas using following packages:

- `FixedEffectModels.jl`,
- `InteractiveFixedEffectModels.jl`,
- `GLFixedEffectModels.jl`?

I’ve tried different syntax and options, but I keep getting different errors, for example  
`ERROR: MethodError: no method matching getindex (:: DataFrame, :: Expr)` for `FixedEffectModels.jl` package.

Thank you so much!!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 26, 2020, 12:16pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/2 "2020-10-26T12:16:40Z")

</div>

It’s hard to see where your error is coming from without an MWE - are you actually using the colon `:` operator to constuct your formulas (which is where the `Expr` error might come from?

I think you might be looking for something like this:

```julia
using DataFrames, FixedEffectModels

data = DataFrame(Y = rand(100), A = rand(100), B = rand('a':'z', 100), C = rand('a':'z', 100))

data[!, :B_C] = data.B .* data.C

reg(data, @formula(Y ~ A&fe(B_C)))

```

I haven’t used `InteractiveFixedEffectModels` before, but the readme suggests that you can do something like

```julia
ife(B, C, 2)

```

instead of creating the combination of `B` and `C` manually.

---

<div class="post-metadata">

**Author:** ![Antonina\_Klyuyeva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antonina_klyuyeva/32/18875_2.png) [@Antonina\_Klyuyeva](https://discourse.julialang.org/u/Antonina_Klyuyeva)\
**Post date:** [October 26, 2020, 12:50pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/3 "2020-10-26T12:50:43Z")

</div>

Hi @nilshg, thanks a lot for the help!  
When I try your example, I have following error:

```julia
julia> data[!, :B_C] = data.B .* data.C
ERROR: MethodError: no method matching setindex!(::DataFrame, ::Array{String,1}, ::typeof(!), ::Symbol)
Closest candidates are:
  setindex!(::DataFrame, ::AbstractArray{T,1} where T, ::AbstractArray{Bool,1}, ::Union{Signed, Symbol, Unsigned}) at /home/antonina_kliuieva/.julia/packages/DataFrames/0Em9Q/src/dataframe/dataframe.jl:561
  setindex!(::DataFrame, ::AbstractArray{T,1} where T, ::AbstractArray{#s68,1} where #s68<:Real, ::Union{Signed, Symbol, Unsigned}) at /home/antonina_kliuieva/.julia/packages/DataFrames/0Em9Q/src/dataframe/dataframe.jl:567
  setindex!(::DataFrame, ::Any, ::Colon, ::Any) at /home/antonina_kliuieva/.julia/packages/DataFrames/0Em9Q/src/dataframe/dataframe.jl:692
  ...
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 26, 2020, 12:53pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/4 "2020-10-26T12:53:49Z")

</div>

Which version of DataFrames are you on? This shouldn’t error.

---

<div class="post-metadata">

**Author:** ![Antonina\_Klyuyeva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antonina_klyuyeva/32/18875_2.png) [@Antonina\_Klyuyeva](https://discourse.julialang.org/u/Antonina_Klyuyeva)\
**Post date:** [October 26, 2020, 12:58pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/5 "2020-10-26T12:58:38Z")

</div>

`DataFrames v0.18.4`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 26, 2020, 12:59pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/6 "2020-10-26T12:59:32Z")

</div>

Okay that’s quite out of date - current version is 0.21.8, so you might want to `]up`

---

<div class="post-metadata">

**Author:** ![Antonina\_Klyuyeva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antonina_klyuyeva/32/18875_2.png) [@Antonina\_Klyuyeva](https://discourse.julialang.org/u/Antonina_Klyuyeva)\
**Post date:** [October 26, 2020, 1:12pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/7 "2020-10-26T13:12:36Z")

</div>

> [@nilshg](#):
>
> 0.21.8

I tried to install a new version of the DataFrames, but it looks like there is some problems with dependencies:

```julia
(v1.1) pkg> add DataFrames@0.21.8
 Resolving package versions...
ERROR: Unsatisfiable requirements detected for package DataFrames [a93c6f00]:
 DataFrames [a93c6f00] log:
 ├─possible versions are: [0.11.7, 0.12.0, 0.13.0-0.13.1, 0.14.0-0.14.1, 0.15.0-0.15.2, 0.16.0, 0.17.0-0.17.1, 0.18.0-0.18.4, 0.19.0-0.19.4, 0.20.0-0.20.2, 0.21.0-0.21.8] or uninstalled
 ├─restricted to versions 0.21.8 by an explicit requirement, leaving only versions 0.21.8
 └─restricted by compatibility requirements with RCall [6f49c342] to versions: [0.19.0-0.19.4, 0.20.0-0.20.2] — no versions left
   └─RCall [6f49c342] log:
     ├─possible versions are: [0.12.0-0.12.1, 0.13.0-0.13.9] or uninstalled
     └─restricted to versions 0.13.6 by an explicit requirement, leaving only versions 0.13.6

```

Same error when I just use `] up`

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 26, 2020, 1:15pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/8 "2020-10-26T13:15:35Z")

</div>

RCall definitely supports DataFrames 0.21.8. Your problem seems to be that you are on Julia version 1.1, current release is 1.5.2, so all sorts of packages might be held back by that. Best to update Julia to 1.5.2 and then install DataFrames and the other packages you need in a fresh environment

---

<div class="post-metadata">

**Author:** ![Antonina\_Klyuyeva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antonina_klyuyeva/32/18875_2.png) [@Antonina\_Klyuyeva](https://discourse.julialang.org/u/Antonina_Klyuyeva)\
**Post date:** [October 26, 2020, 1:16pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/9 "2020-10-26T13:16:48Z")

</div>

Thanks a lot, I’ll try this!

---

<div class="post-metadata">

**Author:** ![Antonina\_Klyuyeva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antonina_klyuyeva/32/18875_2.png) [@Antonina\_Klyuyeva](https://discourse.julialang.org/u/Antonina_Klyuyeva)\
**Post date:** [October 26, 2020, 3:36pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/10 "2020-10-26T15:36:27Z")

</div>

@nilshg, thank you so much for the recommendations! 😊  
it looks like the problem was with outdated versions of Julia and packages. For now my code is working. Thanks a lot!!

Could you please also suggest me which functions should I use to extract the model coefficients (name of the effects, estimate, stderr, p\_value, etc.) and also residuals and predict? (for the FixedEffectModels.jl package)  
I’ll be very grateful for this, as I cannot find good documentation for this package:(

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 26, 2020, 3:39pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/11 "2020-10-26T15:39:43Z")

</div>

It’s mentioned in the Readme [here](https://github.com/FixedEffects/FixedEffectModels.jl/blob/a17477e3bbd5cadae8a55842e45e960bc24b615b/README.md#output).

I agree that the docs are a bit sparse, but that’s because it builds on functionality from other packages (e.g. the syntax for interaction effects and the `@formula` macro are from Statsmodels, so you might learn something from reading [these docs](https://juliastats.org/StatsModels.jl/stable/formula/#Modeling-tabular-data-1) as well.

---

<div class="post-metadata">

**Author:** ![Antonina\_Klyuyeva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antonina_klyuyeva/32/18875_2.png) [@Antonina\_Klyuyeva](https://discourse.julialang.org/u/Antonina_Klyuyeva)\
**Post date:** [October 26, 2020, 3:42pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/12 "2020-10-26T15:42:05Z")

</div>

Great, thanks a lot! 😊

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [October 27, 2020, 4:56pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/13 "2020-10-27T16:56:08Z")

</div>

In case you’re not absolutely committed to using FixedEffectsModels, this should be possible using “vanilla” GLM/statsmodels as well:

```julia
using DataFrames, GLM
data = DataFrame(a = rand(10), b = repeat('a':'b', inner=5), c = repeat('x':'y', outer=5))
data.y = 1 .+ data.a .+ (data.b .== 'b').*2 .+ (data.c .== 'y').*3 .+ randn(10)
lm(@formula(y ~ a & b & c), data)

```

should give you something like

```julia
StatsModels.TableRegressionModel{LinearModel{GLM.LmResp{Array{Float64,1}},GLM.DensePredChol{Float64,LinearAlgebra.Cholesky{Float64,Array{Float64,2}}}},Array{Float64,2}}

y ~ 1 + a & b & c

Coefficients:
─────────────────────────────────────────────────────────────────────────────
                    Coef. Std. Error t Pr(>|t|) Lower 95% Upper 95%
─────────────────────────────────────────────────────────────────────────────
(Intercept) 4.73999 0.64628 7.33 0.0007 3.07868 6.40131
a & b: a & c: x -4.92912 1.16859 -4.22 0.0083 -7.93306 -1.92518
a & b: b & c: x -4.72008 2.07218 -2.28 0.0717 -10.0468 0.606636
a & b: a & c: y -1.39266 3.13547 -0.44 0.6755 -9.45266 6.66733
a & b: b & c: y 2.27275 1.21276 1.87 0.1198 -0.844756 5.39026
─────────────────────────────────────────────────────────────────────────────

```

---

<div class="post-metadata">

**Author:** ![dave.f.kleinschmidt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dave.f.kleinschmidt/32/55_2.png) [@dave.f.kleinschmidt](https://discourse.julialang.org/u/dave.f.kleinschmidt)\
**Post date:** [October 27, 2020, 4:57pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/14 "2020-10-27T16:57:44Z")

</div>

Ah I see from your other post that you tried GLM already and ran into trouble with the number of levels in `b` and `c`, so ignore my suggestion 🙂

---

<div class="post-metadata">

**Author:** ![Antonina\_Klyuyeva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antonina_klyuyeva/32/18875_2.png) [@Antonina\_Klyuyeva](https://discourse.julialang.org/u/Antonina_Klyuyeva)\
**Post date:** [October 28, 2020, 1:11am UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/15 "2020-10-28T01:11:21Z")

</div>

@dave.f.kleinschmidt, thank you very much for your suggestion!  
You are right, my initial model is already written in GLM.jl 😉

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [November 1, 2020, 7:35pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/16 "2020-11-01T19:35:47Z")

</div>

> [@Antonina\_Klyuyeva](#):
>
> - `FixedEffectModels.jl` ,
> - `InteractiveFixedEffectModels.jl` ,
> - `GLFixedEffectModels.jl`

What’s the difference between these three packages?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 2, 2020, 8:32am UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/17 "2020-11-02T08:32:35Z")

</div>

The clue is in the name - `FixedEffectModels` is for standard FE/IV models, `InteractiveFixedEffectModels` is for FE models with interactions, and `GLFixedEffectModels` is for general linear fixed effect models, e.g. logit models.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [November 2, 2020, 11:39am UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/18 "2020-11-02T11:39:22Z")

</div>

> [@nilshg](#):
>
> `GLFixedEffectModels`

With GLM you can also fit models with categorical variables if you code them as dummy. And it also accepts interactions and logit models.  
Why do we need to use the packages named in this thread?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [November 2, 2020, 12:10pm UTC](https://discourse.julialang.org/t/regression-implementation-using-fixedeffectmodels-jl-interactivefixedeffectmodels-jl-glfixedeffectmodels-jl-packages/49036/19 "2020-11-02T12:10:05Z")

</div>

I think you’re alluding to the problem in your post - coding fixed effects as dummies quickly becomes computationally infeasible (as every level adds a column to your model matrix), so these packages implement fast and efficient estimators that allow you to estimate high dimensional fixed effects.
