Valerio Leone Sciabolazza
2020-Jul-09 10:09 UTC
[R] Fixed effects regression constant (intercept) using lfe::felm
Dear list users,
When calculating a panel data regression with multiple fixed effects
using the function felm() from the lfe package, no constant term (i.e.
intercept) is generated in the summary results.
In an old post on stackoverflow [1], someone suggested that it is
possible to retrieve the value of the intercept by using the function
lfe::getfe, setting the field "ef" equal to "zm2".
However, I get different results when comparing the value of the
intercept calculated by the function lm, with that obtained with the
function lfe::getfe.
Here's a reproducible example:
library(lfe)
# set seed for reproducible example
set.seed(123)
## generate independent variables
x <- rnorm(4000)
x2 <- rnorm(length(x))
## create individual and firm ids
id <- factor(sample(500,length(x),replace=TRUE))
firm <- factor(sample(300,length(x),replace=TRUE))
## generate dependent variable
id.eff <- rlnorm(nlevels(id))
firm.eff <- rexp(nlevels(firm))
y <- x + 0.25*x2 + id.eff[id] + firm.eff[firm] + rnorm(length(x))
## estimate results
est <- felm(y ~ x + x2 | id + firm)
est2 <- lm(y ~ x + x2 + id + firm)
## Compare estimates
coef(est)
# x x2
# 1.017696 0.246784
coef(est2)[2:3]
# x x2
# 1.017696 0.246784
# estimate fixed effects
fe_est <- getfe(est, ef = "zm2")
## Compare intercept
fe_est[grepl("icpt", rownames(fe_est)), ]
# effect obs comp
# icpt.1 2.583704 4000 1
coef(est2)[1]
# (Intercept)
# 7.312307
I am wondering if anyone can explain this difference.
Also, I would like to know if it is possible to obtain a reliable
approximation of the value of the intercept estimated by lm using
lfe::getfe.
Best,
Valerio
P.s.Observe that 3 weeks ago I have posted this question on
stackoverflow [2] but no one answered.
[1]
https://stackoverflow.com/questions/49351201/fixed-effects-regression-constant-intercept-using-lfe-felm-in-r
[2]
https://stackoverflow.com/questions/62497705/estimate-of-constant-term-using-r-felmlfe-package-and-differences-with-stats
Valerio Leone Sciabolazza, Ph.D.
Department of Business and Economics
University of Naples, Parthenope.
Bert Gunter
2020-Jul-09 14:15 UTC
[R] Fixed effects regression constant (intercept) using lfe::felm
While you may get lucky here, your experience on SO indicates that you may do better by contacting the package maintainer (?maintainer) and asking him/her to refer you to appropriate references, as this sounds like a statistical methodology query. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Jul 9, 2020 at 3:09 AM Valerio Leone Sciabolazza < sciabolazza at gmail.com> wrote:> Dear list users, > When calculating a panel data regression with multiple fixed effects > using the function felm() from the lfe package, no constant term (i.e. > intercept) is generated in the summary results. > > In an old post on stackoverflow [1], someone suggested that it is > possible to retrieve the value of the intercept by using the function > lfe::getfe, setting the field "ef" equal to "zm2". > > However, I get different results when comparing the value of the > intercept calculated by the function lm, with that obtained with the > function lfe::getfe. > > Here's a reproducible example: > > library(lfe) > > # set seed for reproducible example > set.seed(123) > > ## generate independent variables > x <- rnorm(4000) > x2 <- rnorm(length(x)) > > ## create individual and firm ids > id <- factor(sample(500,length(x),replace=TRUE)) > firm <- factor(sample(300,length(x),replace=TRUE)) > > ## generate dependent variable > id.eff <- rlnorm(nlevels(id)) > firm.eff <- rexp(nlevels(firm)) > y <- x + 0.25*x2 + id.eff[id] + firm.eff[firm] + rnorm(length(x)) > > ## estimate results > est <- felm(y ~ x + x2 | id + firm) > est2 <- lm(y ~ x + x2 + id + firm) > > ## Compare estimates > coef(est) > # x x2 > # 1.017696 0.246784 > coef(est2)[2:3] > # x x2 > # 1.017696 0.246784 > > # estimate fixed effects > fe_est <- getfe(est, ef = "zm2") > > ## Compare intercept > fe_est[grepl("icpt", rownames(fe_est)), ] > # effect obs comp > # icpt.1 2.583704 4000 1 > coef(est2)[1] > # (Intercept) > # 7.312307 > > I am wondering if anyone can explain this difference. > Also, I would like to know if it is possible to obtain a reliable > approximation of the value of the intercept estimated by lm using > lfe::getfe. > > Best, > Valerio > > P.s.Observe that 3 weeks ago I have posted this question on > stackoverflow [2] but no one answered. > > [1] > https://stackoverflow.com/questions/49351201/fixed-effects-regression-constant-intercept-using-lfe-felm-in-r > [2] > https://stackoverflow.com/questions/62497705/estimate-of-constant-term-using-r-felmlfe-package-and-differences-with-stats > > Valerio Leone Sciabolazza, Ph.D. > Department of Business and Economics > University of Naples, Parthenope. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Rasmus Liland
2020-Jul-09 16:44 UTC
[R] Fixed effects regression constant (intercept) using lfe::felm
On 2020-07-09 07:15 -0700, Bert Gunter wrote:> On Thu, Jul 9, 2020 at 3:09 AM Valerio Leone Sciabolazza <sciabolazza at gmail.com> wrote: > > > > When calculating a panel data > > regression with multiple fixed > > effects using the function felm() > > from the lfe package, no constant > > term (i.e. intercept) is generated > > in the summary results. > > > > Also, I would like to know if it is > > possible to obtain a reliable > > approximation of the value of the > > intercept estimated by lm using > > lfe::getfe. > > you may do better by contacting the > package maintainer (?maintainer) and > asking him/her to refer you to > appropriate references, as this sounds > like a statistical methodology query.Valerio, these are such interesting questions. Rememeber to post a follow-up of your findings here and on SO if you are able to solve this. V r -- _________________________________________ / Difficulty \ | | | Practice no-action; Attend to | | do-nothing; Taste the flavorless, | | Magnify the small, Multiply the few, | | Return love for hate. Deal with the | | difficult while it is yet easy; Deal | | with the great while it is yet small; | | The difficult develops naturally from | | the easy, And the great from the small; | | So the sage, by dealing with the small, | | Achieves the great. Who finds it easy | | to promise finds it hard to be trusted; | | Who takes things lightly finds things | | difficult; The sage recognizes | | difficulty, and so has none. | | | \ -- Lao Tse, "Tao Te Ching" / ----------------------------------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200709/4bab4da5/attachment.sig>