3

I am using this helpful package https://github.com/FixedEffects/FixedEffectModels.jl in Julia which helps to run Fixed Effects models.

I have one problem though, i am not sure how to extract the average marginal effects or predicted values of an interaction variable following the use of that package. For instance, the two lines below show how to extract the average marginal effects in Stata.

xtreg chronic_illness age country_birth social_class#macro_unemployment, fe
margins crisis, dydx(social_class)

Here is how to extract them in R: How to run the predicted probabilities (or average marginal effects) for individuals fixed effects in panel data using R?

Is there by any chance a similar version of it in Julia?

Here is the model that i am running in Julia:

m= reg(df1, @formula(chronic_illness ~ status+ age + social_class*crisis + fe(id) + fe(year) + fe(country)), contrasts=contr,save=true)
 

Chronic illness is a binary variable (0= no chronic illness), crisis is a binary variable (0= no financial crisis). The idea is to see how much different social classes scored on chronic illness when there is no crisis compared with when there is one. The model here just shows me the interaction effect, but i am interested in the baseline values.

Here is the output:

                                         Fixed Effect Model                                           
========================================================================================================
Number of obs:                              1468882  Degrees of freedom:                          459252
R2:                                           0.703  R2 Adjusted:                                  0.567
F-Stat:                                     62.8378  p-value:                                      0.000
R2 within:                                    0.001  Iterations:                                      18
========================================================================================================
cillness                           |    Estimate  Std.Error     t value Pr(>|t|)   Lower 95%   Upper 95%
-------------------------------------------------------------------------------------------------------
status: Unemployed                 |   0.0145335 0.00157535     9.22556    0.000   0.0114459   0.0176212
status: missing                    | -0.00702545  0.0136504    -0.51467    0.607  -0.0337797   0.0197288
age                                |  0.00178437    2.79058 0.000639427    0.999    -5.46766     5.47123
class: Lower-middle class          |  0.00458331    250.251  1.83149e-5    1.000    -490.478     490.487
class: Working class               |   0.0286466    163.324 0.000175398    1.000    -320.081     320.138
crisis                             | -0.00600744 0.00156138    -3.84753    0.000 -0.00906768 -0.00294719
class: Lower-middle class & crisis | -0.00189866 0.00192896   -0.984289    0.325 -0.00567936  0.00188205
class: Working class & crisis      | -0.00332881 0.00170221    -1.95558    0.051  -0.0066651  7.46994e-6
Jack
  • 813
  • 4
  • 17
  • can you show a MWE or the `@formula` you are trying to estimate? I'm not aware of any particular package or functionality in FixedEffects.jl but I'm not a very experienced user of those. Isn't this _just_ a question of contructing a `newdata` dataframe and multiplying it with the coef vector? Like you'd chose some level for the other X's and predict for ranges of `social_class` and `marco_unemployment`? sorry I may not be understanding properly. – Florian Oswald Jul 15 '22 at 09:06
  • Thank you for the answer, I included some more explanation about the model – Jack Jul 15 '22 at 09:36

1 Answers1

1

I'm not aware of an exact match for emmeans in Julia, but you might be interested in the Effects.jl package. From the docs:

Regression models are useful but they can be tricky to interpret. Variable centering and contrast coding can obscure the meaning of main effects. Interaction terms, especially higher order ones, only increase the difficulty of interpretation. Here, we introduce Effects.jl which translates the fitted model, including estimated uncertainty, back into data space. Using Effects.jl, it is possible to generate effects plots that enable rapid visualization and interpretation of regression models.

As an aside, it appears that you have a binary response in your model so you probably should not fit a lienar model but a model appropriate for your data such that you can interpret coefficients as changes in probabilities/log odds, e.g. a logit or probit model. (Note that these aren't supported in FixedEffectsModels though, you'd have to fall back onto GLM.jl if your data isn't too big or GLFixedEffectModels).

Nils Gudat
  • 13,222
  • 3
  • 39
  • 60
  • Thanks Nils for the response, i have another outcome that is continuous, it was just an example. I have actually contacted the author of Effects.jl some time ago regarding the same question and told me that his package does not support the FixedEffects.jl, but only the MixedModels.jl. And that he doesn't think he'll do one for the FixedEffects.jl as it is time intensive. I was just wondering whether someone else knows how to do it in Julia... – Jack Jul 15 '22 at 10:07
  • With 1.5 million observations you might be fine just doing things in GLM? Like Florian above I also don't really understand your question - if you just want the predicted outcome for a specific combinations of levels of your variables you can just to `predict(m, x)` where `x` is a DataFrame with the combination of characteristics you want. – Nils Gudat Jul 15 '22 at 10:22
  • `df = Dict(:class => ["Working class","Upper-middle class"],:crisis => [0:1; 0:1]) predict(m,df)`. I am trying this code but then i am getting the following error: 'type NamedTuple has no field status Stacktrace: [1] #164 @ ./none:0 [inlined] [2] iterate(::Base.Generator{UnitRange{Int64}, Base.var"#164#167"{(:status, :age, :class, :binary), NamedTuple{(:binary, :class), Tuple{Vector{Int64}, Vector{String}}}}}) @ Base ./generator.jl:47' – Jack Jul 15 '22 at 10:29
  • You're building a Dictionary not a DataFrame? Also you will need to supply all covariates if you want to make a prediction. – Nils Gudat Jul 15 '22 at 10:32
  • Could you please show me an example of how to do it, so I can reproduce it in my code? I am not very skilled in Julia so not sure how to do these steps – Jack Jul 15 '22 at 10:34
  • 1
    You need something like `pred_df = DataFrame(status = X, age = X, social_class = X, crisis = X, id = X, year = X, country = X)` where for X you set your desired values. Look at `Iterators.product` if you want to create all combinations of different levels of your variables (e.g. `DataFrame(Iterators.product(["Working class", "Middle class"], 0:1))`) – Nils Gudat Jul 15 '22 at 10:42