# Extract model coefficients with FixedEffectModels.jl package

**URL:** <https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174>\
**Category:** Machine Learning\
**Tags:** package, regression\
**Created:** [October 28, 2020, 1:07pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174 "2020-10-28T13:07:46Z")\
**Posts on this page:** 15\
**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 28, 2020, 1:07pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/1 "2020-10-28T13:07:46Z")

</div>

Hi everyone!  
In my regression model (`Y ~ A:B`), a numeric variable (`A`) interacts with a categorical variable (`B`). Since the categorical variable has a lot of unique levels, fitting the model using `GLM.jl` package consumes a lot of RAM. I used the `FixedEffectModels.jl` package and it looks much better! However, I have difficulties in extracting model coefficients such as **name of the effects, estimates, stderr, p\_value, t\_value** , etc. and also with **residuals** and **predict**. I read the documentation [here](https://juliastats.org/StatsModels.jl/stable/formula/#Modeling-tabular-data-1) and [here](https://github.com/FixedEffects/FixedEffectModels.jl), but I didn’t see the functions I needed 😕  
When building the model, I selected the option `save = true`, which saves the residuals and fixed effects estimates. When I extract `fe(model)`, I get a table like this:

```julia
julia> fe(model)
444378×1 DataFrame
│ Row │ fe_B&A │
│ │ Float64 │
├────────┼────────────────────────┤
│ 1 │ -1.47252 │
│ 2 │ -1.47252 │
⋮
│ 444376 │ -0.610356 │
│ 444377 │ -0.610356 │
│ 444378 │ -0.610356 │

```

How can I also get **names of the effects to match them with the fixed effects estimates**? (because, I see only row numbers)  
Also, how can I get other coefficients (stderr, p\_value, t\_value, etc)?

Thanks a lot for your help!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 28, 2020, 1:17pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/2 "2020-10-28T13:17:14Z")

</div>

All of the functions here should be implemented: [Abstraction for Statistical Models · StatsBase.jl](https://juliastats.org/StatsBase.jl/stable/statmodels/)

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [October 28, 2020, 2:10pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/3 "2020-10-28T14:10:49Z")

</div>

I also wasn’t able to find the FE names.  
I don’t think it’s currently possible.

@matthieu?

Ps: When you have High dimensional FE, the FEs become hard to interpret anyway, users rarely try.

---

<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, 2:11pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/4 "2020-10-28T14:11:51Z")

</div>

Thanks for the advice!  
I have already tried most of these functions, but did not get the acceptable results.  
For example:

1. `coefnames()`

```julia
julia> coefnames(model)
1-element Array{String,1}:
 "(Intercept)"

```

but I need names of fixed effects too (not Intercept only).

1. `coef()`

```julia
 julia> coef(model)
1-element Array{Float64,1}:
 2.821248194859784

```

does not return coefficients for fixed effects.

1. `stderror()`

```julia
julia> stderror(model)
1-element Array{Float64,1}:
 0.027292973948148374

```

returns result for intercept only (fixed effects needed as well).

1. `residuals()`

```julia
julia> residuals(model)
444378-element Array{Union{Missing, Float64},1}:
 -0.4949565332014614
 -0.38919178650364045
 -0.4186431448593252
 -0.2590869635253943
 -0.17627291925168315
  ⋮
 -0.34583852183630376
 -1.5652955707355511
 -1.1542833700855635
 -2.1144655963560104

```

how can I match values ​​with coefficient names?

1. `predict()`

```julia
julia> predict(fit)
ERROR: predict is not defined for FixedEffectModel.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] predict(::FixedEffectModel) at /home/antonina_kliuieva/.julia/packages/StatsBase/EA8Mh/src/statmodels.jl:368
 [3] top-level scope at REPL[19]:1

```

have error ☹  
etc.

So, my question is still open…  
Sorry, I’m new to Julia, maybe I’m missing something 😕

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 28, 2020, 2:19pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/5 "2020-10-28T14:19:38Z")

</div>

No, you are not missing anything unfortunately. There is an issue [here](https://github.com/FixedEffects/FixedEffectModels.jl/issues/134) filed last week to make prediction easier with fixed effects.

With regards to the values of the fixed effects, I _think_ that the only solution is to

1. `hcat` the vector of intercepts with your data
2. Keep just the columns of interest, i.e. `B` and `A`
3. Call `unique` on the data frame
4. Work with the results of that to match combinations of `:B` and `:A` to intercepts

It would be nice if obtaining the value of the intercepts were as easy as in `reghdfe` in Stata or `fixest` in R. Hopefully we can build more UX polish into the package in the future.

---

<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, 3:03pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/6 "2020-10-28T15:03:29Z")

</div>

Ummm, the package created to evaluate high-dimensional fixed effect variables doesn’t provide an easy way to extract estimates for these fixed effects?  
Maybe you know - the order of `fe` definition in `fe(model) DataFrame` is the same as the original data set? Сan I expect, for example, that estimate -1.47252 (first row from the dataframe below) corresponds to effect that is in the first row of the input data?  
**That is, can I join the `fe` dataframe with input data by Row?**

```julia
julia> fe(model)
444378×1 DataFrame
│ Row │ fe_B&A │
│ │ Float64 │
├────────┼────────────────────────┤
│ 1 │ -1.47252 │
│ 2 │ -1.47252 │
⋮
│ 444376 │ -0.610356 │
│ 444377 │ -0.610356 │
│ 444378 │ -0.610356 │

```

Thank you!

---

<div class="post-metadata">

**Author:** ![danicaratelli](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@danicaratelli](https://discourse.julialang.org/u/danicaratelli)\
**Post date:** [October 28, 2020, 3:05pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/7 "2020-10-28T15:05:22Z")

</div>

There should be a “save=true” option. Add that at the end of the regression function and then you should be able to do everything. Something like this:

```julia
reg_res = reg(df, @formula(X ~ Y + Z + fe(K) ),save=true);
residuals(reg_res)

```

---

<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, 3:13pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/8 "2020-10-28T15:13:13Z")

</div>

I have the `save=true` option enabled, but most functions don’t give the same result as GLM (please, see my reply to @pdeffebach above).

For example, I get an array like this:

```julia
julia> residuals(model)
444378-element Array{Union{Missing, Float64},1}:
 -0.4949565332014614
 -0.38919178650364045
 -0.4186431448593252
 -0.2590869635253943
 -0.17627291925168315
  ⋮
 -0.34583852183630376
 -1.5652955707355511
 -1.1542833700855635
 -2.1144655963560104

```

How can I match each value with the name of effect for which it is calculated?

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [October 28, 2020, 3:22pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/9 "2020-10-28T15:22:52Z")

</div>

> [@Antonina\_Klyuyeva](#):
>
> Ummm, the package created to evaluate high-dimensional fixed effect variables doesn’t provide an easy way to extract estimates for these fixed effects?

Yes.  
Stata xtreg reports estimates & stats for fe  
STATA reghdfe does not for “absorbed FE”

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 28, 2020, 3:32pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/10 "2020-10-28T15:32:58Z")

</div>

yes, in general economists don’t care about the values of the fixed effects. We just want to use “within-unit variation”, hence the lack of emphasis on analyzing these fixed effects.

> [@Antonina\_Klyuyeva](#):
>
> **That is, can I join the `fe` dataframe with input data by Row?**

yes, just use `hcat(df, fe(model))`. That will work.

---

<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, 3:43pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/11 "2020-10-28T15:43:19Z")

</div>

Thanks a lot @pdeffebach, I’ll try this.  
If I understand correctly, at the moment there is no way to obtain stderr, t\_value, p\_value for `A:fe(B)`?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 28, 2020, 3:52pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/12 "2020-10-28T15:52:48Z")

</div>

No. My understanding is that this is why it converges so fast. By not calculating (or even materializing) the FE, you can estimate things faster and with less memory.

---

<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, 5:03pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/13 "2020-10-28T17:03:19Z")

</div>

Ok, thank you!

---

<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, 5:06pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/14 "2020-10-28T17:06:08Z")

</div>

One more question (maybe someone knows) - will [this issue](https://github.com/FixedEffects/FixedEffectModels.jl/issues/134) (with `predict` function) be taken into account in the near future? I just would like to understand whether it is value to wait or is it better to focus on another package.  
Thanks!

---

<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 28, 2020, 6:26pm UTC](https://discourse.julialang.org/t/extract-model-coefficients-with-fixedeffectmodels-jl-package/49174/15 "2020-10-28T18:26:55Z")

</div>

You could extend my hack in the issue to take account of the interactions in your model 😃 (you might have noticed if you tried that why I put in the issue comment breaks down if there are interactions in the model)

Overall it might be helpful to understand a bit better what you are trying to achieve - as Peter says, fixed effects models are heavily used by economists (and the author of `FixedEffectsModels` is an economist as well) who mostly use them to get closer causal identification of marginal effects (by controlling for unobserved, time-invariant heterogeneity), and because of that don’t actually care about the fixed effects themselves. It appears that your use case is different, so a different approach might be warranted.
