R Under development (unstable) (2024-02-21 r85967 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(lfe) Loading required package: Matrix > options(lfe.threads=1,digits=3,warn=1) > set.seed(6553) > # single fixed effect, special case which we risk destroying when we optimize, so check it > x <- rnorm(2000) > x2 <- rnorm(length(x)) > x3 <- rnorm(length(x)) > ## create individual and firm > id <- factor(sample(1500,length(x),replace=TRUE)) > nlevels(id) [1] 1112 > ## effects > id.eff <- rnorm(nlevels(id)) > > ## left hand side > y <- x + 0.25*x2 + 0.5*x3 + id.eff[id] + rnorm(length(x)) > > ## estimate > est <- felm(y ~ x+x2 + x3 |id,keepX=TRUE) > > ## extract the group fixed effects > fe <- getfe(est, se=TRUE) > ## merge back > head(fe) effect obs comp fe idx se id.2 2.8342 1 1 id 2 0.984 id.3 -0.6856 4 1 id 3 0.494 id.4 -1.0945 1 1 id 4 0.984 id.5 -3.1362 1 1 id 5 0.985 id.6 0.0450 2 1 id 6 0.696 id.7 -0.0583 4 1 id 7 0.492 > ideff <- fe[paste('id',id,sep='.'),'effect'] > > ## verify that id and firm coefficients are 1 > options(scipen=8) > lm(y ~ x + x2 + x3 + ideff -1) Call: lm(formula = y ~ x + x2 + x3 + ideff - 1) Coefficients: x x2 x3 ideff 0.950 0.252 0.470 1.000 > > # no factor > felm(y ~ x + x2 + x3) (Intercept) x x2 x3 -0.0567 0.9648 0.2642 0.5333 > > # no covariate > est <- felm(y ~ 0|id,keepX=TRUE) > head(getfe(est, se=TRUE)) effect obs comp fe idx se id.2 2.373 1 1 id 2 1.501 id.3 -1.937 4 1 id 3 0.751 id.4 -0.675 1 1 id 4 1.501 id.5 -2.237 1 1 id 5 1.501 id.6 0.306 2 1 id 6 1.061 id.7 0.136 4 1 id 7 0.751 > > > proc.time() user system elapsed 0.92 0.10 1.01