R Under development (unstable) (2024-02-23 r85978 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=2,digits=3,warn=1) > set.seed(655318) > x <- rnorm(500) > x2 <- rnorm(length(x)) > > ## create individual and firm > id <- factor(sample(400,length(x),replace=TRUE)) > firm <- factor(sample(300,length(x),replace=TRUE,prob=c(2,rep(1,299)))) > > ## effects > id.eff <- rnorm(nlevels(id)) > firm.eff <- rnorm(nlevels(firm)) > > ## left hand side > y <- x + 0.25*x2 + id.eff[id] + firm.eff[firm] + rnorm(length(x)) > > # make a data frame > fr <- data.frame(y,x,x2,id,firm) > ## estimate and print result > print(est <- felm(y ~ x+x2|id+firm, data=fr)) x x2 0.677 0.444 > > ## extract the group fixed effects > tail(getfe(est)) effect obs comp fe idx firm.294 -2.057 1 6 firm 294 firm.295 -1.347 4 1 firm 295 firm.296 -0.227 1 51 firm 296 firm.297 -2.447 2 4 firm 297 firm.298 1.220 1 23 firm 298 firm.300 -4.167 3 1 firm 300 > > head(round(model.matrix(est),7)) x x2 1 0.000 0.000 2 0.000 0.000 3 0.693 0.437 4 0.000 0.000 5 0.000 0.000 6 0.000 0.000 > head(model.matrix(est,centred=FALSE)) x x2 1 -1.251 1.0210 2 0.795 -1.1390 3 1.546 0.6954 4 -1.099 1.1567 5 -0.359 -0.5846 6 0.368 -0.0823 > head(round(model.matrix(felm(y ~ x+x2|id+firm, data=fr,keepCX=TRUE)),7)) x x2 [1,] 0.000 0.000 [2,] 0.000 0.000 [3,] 0.693 0.437 [4,] 0.000 0.000 [5,] 0.000 0.000 [6,] 0.000 0.000 > head(model.matrix(felm(y ~ x+x2|id+firm, data=fr,keepX=TRUE), centred=FALSE)) x x2 [1,] -1.251 1.0210 [2,] 0.795 -1.1390 [3,] 1.546 0.6954 [4,] -1.099 1.1567 [5,] -0.359 -0.5846 [6,] 0.368 -0.0823 > > proc.time() user system elapsed 1.18 0.14 1.26