Blazing fast and highly versatile! The fixest package for fixed effects regressions in R

  Рет қаралды 6,466

Econometrics, Causality, and Coding with Dr. HK

Econometrics, Causality, and Coding with Dr. HK

Күн бұрын

See the project page here: lrberge.github... and install fixest using install.packages('fixest')
NOTE: As of fixest 0.9.0 (released after this video), the syntax for i() with interactions has changed. You now want the factor first, THEN the continuous variable. The line should be
m9 = feols(crmrte ~ polpc + density + taxpc + wcon + i(factor_avgsen, wcon, ref = 8) | county + year, data = crime4)
Code from the video (adjusted to remove all greater-than or less-than signs for KZbin description rules):
library(wooldridge)
data(crime4)
library(fixest)
m1 = feols(crmrte ~ polpc + density + taxpc | county + year, data = crime4)
etable(m1)
m2 = feols(crmrte ~ polpc + density + taxpc | county + year, data = crime4, se = 'hetero')
m3 = feols(crmrte ~ density + taxpc | county + year | polpc ~ prbarr, data = crime4)
m4 = feglm( ~ polpc + density + taxpc | county + year, data = crime4)
crime4$lowcrime = floor(crime4$crmrte*100) %in% 0:2
m5 = feglm(lowcrime ~ polpc + density + taxpc | county + year, data = crime4, cluster = c('county', 'year'))
m6 = feols(lowcrime ~ density + d(taxpc) | county + year | polpc ~ prbarr, data = crime4, panel.id = c('county', 'year'))
esttable(m6)
m7 = feols(crmrte ~ density + taxpc | county + year | polpc ~ prbarr, data = crime4, fsplit = 'urban')
esttable(m7)
crime4$factor_avgsen = round(crime4$avgsen)
m8 = feols(crmrte ~ polpc + density + taxpc + i(factor_avgsen, ref = 5) | county + year, data = crime4)
m9 = feols(crmrte ~ polpc + density + taxpc + wcon + i(wcon, factor_avgsen, ref = 8) | county + year, data = crime4)
esttable(m9)

Пікірлер: 66
@fabiominatto4650
@fabiominatto4650 3 жыл бұрын
Excellent video and package!! Thanks!
@impieman10
@impieman10 3 жыл бұрын
Great video! Love your content! Certainly the best youtuber around when it comes to fixed effects using R.
@leoli2363
@leoli2363 3 жыл бұрын
Thanks for your clear clarification of the package.
@raulguarini2342
@raulguarini2342 2 жыл бұрын
Thanks for the video! Super helpful and practical!
@pengsun2733
@pengsun2733 3 жыл бұрын
thank you for this video🤗 i guess i might use it in DID analysis too😄
@deanedabadia2259
@deanedabadia2259 6 ай бұрын
Nice video! Tks!
@remogurtner6907
@remogurtner6907 Ай бұрын
Thank you very much for theses explanations. Do you have any insights on the sunab package by any chance? I'm currently struggling to understand how to incorporate fixed effects and if any are actually needed when using sunab() within fixest? example: feols(outcome ~ sunab(cohort, period) | firm + period, data) are the fixed effects necessary or not? Thank you in advance!
@NickHuntingtonKlein
@NickHuntingtonKlein Ай бұрын
@@remogurtner6907 I haven't used it much, but I believe sunab already adds the proper fixed effects for anything included. So your period fixed effects aren't necessary. The firm ones might be though. I'd check the example code in help(sunab)
@remogurtner6907
@remogurtner6907 Ай бұрын
@@NickHuntingtonKlein thank you for the quick reply!
@pengsun2733
@pengsun2733 3 жыл бұрын
there we go. my previous comment posted when i watched for like 3 minutes. five minutes later i found the nice argument of splitting groups 😂
@praveenkumar-kn7qf
@praveenkumar-kn7qf 3 жыл бұрын
this was awesome
@BH-fq4qn
@BH-fq4qn Жыл бұрын
Thanks for the video super helpful. Do you know if you can run first difference fixed effects estimation on fixest? I've been looking online and can't find anything but seems like a functionality that should be included
@NickHuntingtonKlein
@NickHuntingtonKlein Жыл бұрын
Yep, use d() while setting panel.id. See lrberge.github.io/fixest/reference/l.html
@kegs666
@kegs666 11 ай бұрын
2:37 ... looks like you can get the fixed effects coefficients by using fixest::fixef(model), right?
@NickHuntingtonKlein
@NickHuntingtonKlein 11 ай бұрын
Yes I believe so
@kegs666
@kegs666 11 ай бұрын
@@NickHuntingtonKlein
@wilsonokoth1798
@wilsonokoth1798 6 ай бұрын
What about an interaction term? for instance can this be used in a regression with an interacton term (success
@NickHuntingtonKlein
@NickHuntingtonKlein 6 ай бұрын
Yep! It accepts all the regular R regression formula stuff, including interaction terms. You can turn any lm() formula into feols() and it will still work. feols also has the i() function which gives you some more flexibility with interaction terms where one of the things being interacted is a factor.
@samkim6933
@samkim6933 3 жыл бұрын
Thank you for the great video Nick, this was very helpful for my research. I just have a quick question. Is it possible to report "Driscoll-Kraay" standard error in fixest package? I have read the R documentation, but it seems there is no option to report DK standard error.
@NickHuntingtonKlein
@NickHuntingtonKlein 3 жыл бұрын
Not as far as I know, I'm afraid. There is a vcovSCC function in the plm package, but I'm not sure if it works with feols output. You may need to use plm instead.
@samkim6933
@samkim6933 3 жыл бұрын
@@NickHuntingtonKlein Thank you so much for your prompt reply!! I really appreciate it.
@iliasaretos
@iliasaretos 3 жыл бұрын
Excellent video, very useful!!! One question: How can I use an interaction term as a fixed effect variable? Thanks in advance!!!
@NickHuntingtonKlein
@NickHuntingtonKlein 3 жыл бұрын
I think it should just work, no? Or at least if you create the interaction as its own variable ahead of time. Or if you want it in the model proper, check out the fixest i() function
@iliasaretos
@iliasaretos 3 жыл бұрын
@@NickHuntingtonKlein I should try it, for sure!!! One more question, are these methods equivalent to x1^x2?
@NickHuntingtonKlein
@NickHuntingtonKlein 3 жыл бұрын
@@iliasaretos I haven't used x1^x2 a lot but I think so
@iliasaretos
@iliasaretos 3 жыл бұрын
@@NickHuntingtonKlein great. Thanks a lot for your help!!!!!
@LetsDoItFromScratch
@LetsDoItFromScratch 2 жыл бұрын
Nice video! Quick question. The library "effects" allows to plot effects-plot for quadratic variablies within a "normal" regression models "lm". For example: plot(effect("rooms", testreg)) where the variable "rooms" is quadratic within an "lm" regression-model. This works usually fine. However, doing a two-ways-fixed-feols regression with the help of the fixest package: I cannot plot those graphs anymore. Then plot(effect("variable_x", reg_fixest)) gives me this error message: "Error in mod.matrix %*% scoef : non-conformable arguments" - Is this because the matrix behind has not the right dimensions anymore?
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
Try the marginaleffects package, which supports fixest output
@lanaschludi5466
@lanaschludi5466 3 жыл бұрын
As always, thank you for your helpful video! I used the fixest-package to run a logistic regression with fixed effects. The notes say that a certain number of observations have been removed because of only 0 (or 0/1) outcomes. What does this mean? I'm not quite sure how to explain or interpret the removal of the observations.. Thank you in advance for your feedback!
@NickHuntingtonKlein
@NickHuntingtonKlein 3 жыл бұрын
In general, a logit model cannot run if there is no variation in the dependent variable. If everything is 0,say, then your coefficients all want to be negative infinity and it doesn't work. Perfect prediction. With fixed effects included, this extends within individual. FE isolates within variation. If there IS no within variation for an individual, the model would be able to perfectly predict them by setting their FE to positive or negative infinity. It can't, so those get dropped.
@lanaschludi5466
@lanaschludi5466 3 жыл бұрын
@@NickHuntingtonKlein Thank you so much for the explanation, I highly appreciate that you're always taking the time to answer my questions! Such a great help for my thesis!
@samkim6933
@samkim6933 3 жыл бұрын
@@NickHuntingtonKlein Hi Nick, may I ask which command in fixest package I should use to run fixed-effect logistic regression? Sorry to bother you.
@NickHuntingtonKlein
@NickHuntingtonKlein 3 жыл бұрын
@@samkim6933 feglm
@paolagarcia6106
@paolagarcia6106 3 жыл бұрын
Thanks for helping me to understand how I can use Package. However, I am trying to use an instrumental variable approach with a binary dependent variable. And I want to replace a continuous variable due to endogeneity with my instruments, but when I try to run feglm function the package gives me this error "The argument 'fml' cannot contain more than two parts separated by a pipe ('|'). The syntax is: DEP VAR ~ EXPL VARS | FIXED EFFECTS". can you please help me to understand if for iv approach using feols is correct or I need to process all manually with ivreg function? because feglm do not accept the iv structure. I also want to know if this package has the possibility to compute marginal effects such as alpaca, I was searching on internet but I don´t find anything about it.
@NickHuntingtonKlein
@NickHuntingtonKlein 3 жыл бұрын
It should support IV, check the feglm docs. Did you make sure to put the IV part last? As for marginal effects I'm actually not sure. Maybe see if the margins package works with feglm
@paolagarcia6106
@paolagarcia6106 3 жыл бұрын
@@NickHuntingtonKlein thanks for answering me. Yes I'm pretty sure. But it seems that for glm this parte is not available... About marginal effect i only can jave it using alpaca... In the mean time im trying other package.
@larszintel6801
@larszintel6801 2 жыл бұрын
Hi Nick, thanks a lot for your videos on panel data in R, they've been super helpful! I do have one question though. I wanted to report my 2SLS-FE-regression results from the fixest package through stargazer but unfortunately it is not supported by stargazer. So I tried to run the exact same regression in plm, but I got different results. Can you maybe tell me why the estimates differ?
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
Stargazer is pretty outdated and doesn't support a lot of stuff. I'd recommend modelsummary instead. As to why the two don't match I'm not sure but I'd guess it's some degrees or freedom adjustment. I haven't really used plm for IV so I don't know what it does. Might be worth trying to fiddle with the options and reading the docs to see if you can get them to match, or asking on stackexchange or the fixest github.
@larszintel6801
@larszintel6801 2 жыл бұрын
Thanks a lot for the answer! I also tried to report my output with modelsummary. It went really fine actually, but I can't seem to find anything about whether "add_rows" can be combined with "row_spec"? Because I added a few rows, but cannot alter their look with row_spec. Do you happen to know if that's possible?
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
@@larszintel6801 row_spec is a kableextra function isn't it? So it only works on kables. Try setting modelsummary to output a kable and it should work
@larszintel6801
@larszintel6801 2 жыл бұрын
@@NickHuntingtonKlein Yes I tried that, too. And it works fine on each row except fot the ones added manually by "add_rows" (in combination with "tibble"), which remain unchanged by the "row_spec" command. And unfortunately those are the ones I actually need to change :/
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
@@larszintel6801 Seems to work fine to me: library(modelsummary); library(kableExtra) m % row_spec(12, bold = TRUE)
@mauriices
@mauriices 2 жыл бұрын
Hey Nick, I am running the feglm regression with a dummy-variable as Dep Var and I get the following error: The left hand side is not numeric. The Dummies are factor. Can you help me please? Do have to recode to numeric? But Why?
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
What kind of glm are you running? The default for feglm is poisson, which requires a numeric dependent variable. If you're trying to run a logit or something, use the family argument of feglm, see the help file for options.
@anggabagusbismoko7305
@anggabagusbismoko7305 Жыл бұрын
Hi, Nick. Just came across this great video. Regarding this question, is it possible to do ordered-logit using the fixest package? LHF requires the dependent variable to be numeric, but I assume that the ordered logit model works with factors as the dependent variable. I have 4 levels of dependent variable and would like to keep that instead of replacing it with binary 0 and 1. Thanks.
@raginijain1583
@raginijain1583 2 жыл бұрын
Hi Nick, I've been using this package and feols which I find very useful, and based on comparing the results to results obtained via lm, it seems like the model is including an intercept, but I can't seem to figure out how to recover the intercept.
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
If you don't have any fixed effects in the model, it will report the intercept as normal. With fixed effects included, the intercept usually isn't too useful (it gives the mean of Y for the reference group with all predictors at 0, which is arbitrary since the reference group is usually arbitrary) so it omits it. But if your model is named m you can do fixef(m) and it will report all the fixed effects. Pick one of those to be your reference group, and that's what the intercept would be. If you have a non-arbitrary reference group, I recommend using the i(group, ref = 'my reference group') function in your regression formula rather than including the group as a set of fixed effects.
@raginijain1583
@raginijain1583 2 жыл бұрын
Thanks so much! Do you know if there's a way to report the coefficients for the reference group as well when fixed effects are included? Using i(group, ref=`my reference group') or using a factor variable with a pre-defined reference level both give me everything relative to the the reference level, although there are some situations where I would like to be able to recover each coefficient separately rather than relative to the reference group with fixed effects included as well. I get the output I want simply removing the pipe and including my fixed effects as standard predictors so I could do it this way, but I'm interested in using the sumFE feature while also recovering the coefficient for the reference group
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
@@raginijain1583 There's two ways to think about this: (1) either you're in a model with no intercept, in which case there is no reference group, and fixef(m) or sumFE will give you all the fixed effects, including the one for whatever *would* have been your reference group in a model with an intercept, or (2) you're in a model that does have an intercept, in which case the coefficient on the reference group is by definition 0, so you can do whatever to get the rest of the FEs and then fill in 0 for your reference group.
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
Notably, in the second case (with an intercept), the FE cofficients you get are all relative to that intercept.
@NickHuntingtonKlein
@NickHuntingtonKlein 2 жыл бұрын
@@raginijain1583 Yes, if you include them as fixed effects rather than in i(), then the fixef() function will give you all the fixed effects (and there is no reference group in this approach, so nothing is left out). sumFE doesn't really produce coefficients; it produces the sum of the relevant FEs for each observation. If you only have one set of fixed effects, then sumFE will give you, in effect, the coefficient on each FE, lined up with the observations that that FE is relevant for (and again, no reference group to be left out).
@wilsonokoth1798
@wilsonokoth1798 6 ай бұрын
Is there a way to export the table to a word document?
@NickHuntingtonKlein
@NickHuntingtonKlein 6 ай бұрын
You can send the etable to knitr::kable() which allows export to Word
@Dave-east45
@Dave-east45 4 ай бұрын
@@NickHuntingtonKlein please can you make a video, or explain how to export output to word, preferrable in journal article format. Thanks in advance
@NickHuntingtonKlein
@NickHuntingtonKlein 4 ай бұрын
@@Dave-east45 send the results to the modelsummary command in the modelsummary package, which supports fixest output and supports export to word.
Econometrics - Within Variation and Fixed Effects
20:06
Econometrics, Causality, and Coding with Dr. HK
Рет қаралды 15 М.
Panel Data (Fixed Effects, Random Effects) - R for Economists Moderate 9
13:06
Econometrics, Causality, and Coding with Dr. HK
Рет қаралды 46 М.
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 12 МЛН
Which One Is The Best - From Small To Giant #katebrush #shorts
00:17
R Ladies Baltimore | Intro to Causal Inference in R using fixest
1:02:06
R-Ladies Baltimore
Рет қаралды 629
Diff-in-Diff in R, the Minimum Legal Drinking Age (MLDA) example
37:15
Fancy Linear Regression Formulas (Squares, Interactions, etc.) - R for Economists Moderate 1
15:13
Econometrics, Causality, and Coding with Dr. HK
Рет қаралды 4,5 М.
Panel Data and Fixed Effects in R
13:05
SebastianWaiEcon
Рет қаралды 70 М.
Econometrics - Difference in Differences
16:29
Econometrics, Causality, and Coding with Dr. HK
Рет қаралды 13 М.
What Language Should You Use for Econometrics?
20:51
Econometrics, Causality, and Coding with Dr. HK
Рет қаралды 4,3 М.
Robust or Clustered Errors and Post-Regression Statistics - R for Economists Moderate 2
9:15
Econometrics, Causality, and Coding with Dr. HK
Рет қаралды 13 М.
Econometrics - Marginal Effects for Probit and Logit (and Marginal Effects in R)
13:47
Econometrics, Causality, and Coding with Dr. HK
Рет қаралды 42 М.