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 > set.seed(127) > options(lfe.threads=1,warn=1,digits=5) > x <- rnorm(2000,mean=2000) > x2 <- rnorm(length(x)) > x3 <- 1.2*x + 0.9*x2 > x4 <- 0.8*x2 + 0.3*x3 > ## create individual and firm > id <- factor(sample(12,length(x),replace=TRUE)) > firm <- factor(sample(7,length(x),replace=TRUE)) > > # these are constant on the levels > x5 <- rnorm(nlevels(id))[id] > x6 <- rnorm(nlevels(firm))[firm] > ## effects > id.eff <- rnorm(nlevels(id)) > firm.eff <- rnorm(nlevels(firm)) > > ## left hand side > y <- x + 0.25*x2 + 0.5*x3 + 0.17*x4 + 0.8*x5 -0.3*x6 + id.eff[id] + firm.eff[firm] + rnorm(length(x)) > > ## estimate > est <- felm(y ~ x+x2 + x3 + x4 + x5 + x6 | id + firm) Warning in chol.default(mat, pivot = TRUE, tol = tol) : the matrix is either rank-deficient or not positive definite > ## extract the group fixed effects > alpha <- getfe(est) > summary(est) Warning in chol.default(mat, pivot = TRUE, tol = tol) : the matrix is either rank-deficient or not positive definite Call: felm(formula = y ~ x + x2 + x3 + x4 + x5 + x6 | id + firm) Residuals: Min 1Q Median 3Q Max -3.219 -0.676 0.005 0.660 3.028 Coefficients: Estimate Std. Error t value Pr(>|t|) x NaN NA NaN NaN x2 -0.4175 0.0281 -14.8 <2e-16 *** x3 1.4086 0.0188 75.1 <2e-16 *** x4 NaN NA NaN NaN x5 NaN NA NaN NaN x6 NaN NA NaN NaN --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.987 on 1980 degrees of freedom Multiple R-squared(full model): 0.873 Adjusted R-squared: 0.872 Multiple R-squared(proj model): 0.786 Adjusted R-squared: 0.784 F-statistic(full model): 717 on 19 and 1980 DF, p-value: <2e-16 F-statistic(proj model): 1.21e+03 on 6 and 1980 DF, p-value: <2e-16 > #alpha > #summary(lm(y ~ x + x2 + x3 + x4 + x5 + x6 + id + firm)) # remove from cran > # merge back > ideff <- alpha[paste('id',id,sep='.'),'effect'] > firmeff <- alpha[paste('firm',firm,sep='.'),'effect'] > > ## verify that id and firm coefficients are 1 > co <- coef(lm(y ~ x + x2 + x3 + x4 + x5 + x6 + ideff + firmeff-1))[7:8] > cat('accuracy:', formatC(co, digits=8, format="f"),'\n') accuracy: 1.00000000 1.00000000 > > > > proc.time() user system elapsed 0.89 0.09 0.98