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=4) > set.seed(42) > N <- 1000 > K <- 40 > id <- factor(sample(K,N, replace=TRUE)) > firm <- factor(sample(K,N, replace=TRUE)) > ideff <- rnorm(nlevels(id)) > firmeff <- rnorm(nlevels(firm)) > x <- ideff[id] + rnorm(N) > y <- x + ideff[id] - firmeff[firm] + rnorm(N,sd=2) > w <- abs(2*x + ideff[id] + firmeff[firm]) + runif(N,0.2,0.4) > > # lm says: > summary(lm(y ~ x + id + firm, wei=w)) Call: lm(formula = y ~ x + id + firm, weights = w) Weighted Residuals: Min 1Q Median 3Q Max -13.25 -2.08 -0.04 2.09 12.46 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.8613 0.5201 3.58 0.00036 *** x 1.0300 0.0652 15.81 < 2e-16 *** id2 -2.6194 0.6439 -4.07 5.2e-05 *** id3 -0.6692 0.5403 -1.24 0.21582 id4 -1.1415 0.7869 -1.45 0.14721 id5 -1.2926 0.6286 -2.06 0.04001 * id6 -0.7207 0.6152 -1.17 0.24171 id7 -1.0589 0.6822 -1.55 0.12094 id8 -1.2310 0.5789 -2.13 0.03372 * id9 -3.4462 0.5759 -5.98 3.1e-09 *** id10 -3.0019 0.5100 -5.89 5.5e-09 *** id11 -0.5706 0.6209 -0.92 0.35836 id12 -2.2776 0.6920 -3.29 0.00104 ** id13 -2.1031 0.6615 -3.18 0.00153 ** id14 -2.6145 0.5455 -4.79 1.9e-06 *** id15 -2.3412 0.7968 -2.94 0.00338 ** id16 -1.8604 0.6498 -2.86 0.00429 ** id17 -1.5416 0.6291 -2.45 0.01446 * id18 -1.6685 0.5805 -2.87 0.00414 ** id19 -1.1447 0.5764 -1.99 0.04733 * id20 -2.1958 0.5652 -3.88 0.00011 *** id21 -0.9335 0.5952 -1.57 0.11714 id22 -1.9375 0.6889 -2.81 0.00502 ** id23 -2.0967 0.5487 -3.82 0.00014 *** id24 -1.7334 0.6376 -2.72 0.00668 ** id25 0.1212 0.5341 0.23 0.82053 id26 -0.9601 0.5221 -1.84 0.06623 . id27 0.9065 0.4012 2.26 0.02408 * id28 -3.3619 0.5305 -6.34 3.6e-10 *** id29 -1.6277 0.6513 -2.50 0.01262 * id30 -1.3184 0.5657 -2.33 0.02000 * id31 -0.3721 0.4793 -0.78 0.43772 id32 -0.7692 0.5913 -1.30 0.19367 id33 -1.9824 0.5584 -3.55 0.00040 *** id34 -1.9499 0.5507 -3.54 0.00042 *** id35 0.0516 0.4764 0.11 0.91377 id36 0.1067 0.4759 0.22 0.82266 id37 -1.5141 0.5486 -2.76 0.00590 ** id38 0.1908 0.4384 0.44 0.66353 id39 -1.6424 0.6727 -2.44 0.01482 * id40 -2.5424 0.6198 -4.10 4.5e-05 *** firm2 -0.5050 0.5541 -0.91 0.36227 firm3 0.2966 0.5515 0.54 0.59082 firm4 -0.7015 0.6011 -1.17 0.24347 firm5 0.2912 0.5572 0.52 0.60137 firm6 -0.8755 0.5805 -1.51 0.13182 firm7 0.8419 0.5328 1.58 0.11442 firm8 -0.7795 0.5421 -1.44 0.15077 firm9 -1.2168 0.5727 -2.12 0.03387 * firm10 -0.3212 0.5286 -0.61 0.54355 firm11 -1.2400 0.6150 -2.02 0.04407 * firm12 -2.0502 0.4703 -4.36 1.5e-05 *** firm13 -1.5565 0.5272 -2.95 0.00323 ** firm14 -2.1253 0.5368 -3.96 8.1e-05 *** firm15 0.2583 0.5686 0.45 0.64980 firm16 0.3063 0.6257 0.49 0.62451 firm17 -1.5812 0.6148 -2.57 0.01028 * firm18 -0.6810 0.5711 -1.19 0.23341 firm19 -3.6899 0.5144 -7.17 1.5e-12 *** firm20 2.7891 0.5887 4.74 2.5e-06 *** firm21 -0.1260 0.5829 -0.22 0.82886 firm22 -1.9048 0.5790 -3.29 0.00104 ** firm23 1.0813 0.5574 1.94 0.05273 . firm24 0.0336 0.6337 0.05 0.95775 firm25 -0.2585 0.5185 -0.50 0.61818 firm26 0.9889 0.5502 1.80 0.07261 . firm27 0.5730 0.5260 1.09 0.27626 firm28 -0.4654 0.4999 -0.93 0.35211 firm29 -1.5603 0.5732 -2.72 0.00661 ** firm30 0.9660 0.5754 1.68 0.09355 . firm31 -1.6254 0.5390 -3.02 0.00264 ** firm32 -1.9788 0.5383 -3.68 0.00025 *** firm33 0.8544 0.5666 1.51 0.13191 firm34 1.9429 0.5533 3.51 0.00047 *** firm35 -0.3309 0.5302 -0.62 0.53270 firm36 -0.7887 0.6232 -1.27 0.20595 firm37 -1.7940 0.5364 -3.34 0.00086 *** firm38 0.7225 0.5934 1.22 0.22373 firm39 -1.2620 0.5646 -2.24 0.02565 * firm40 -0.8222 0.6186 -1.33 0.18409 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 3.69 on 920 degrees of freedom Multiple R-squared: 0.703, Adjusted R-squared: 0.677 F-statistic: 27.6 on 79 and 920 DF, p-value: <2e-16 > # felm w/o projection says > summary(felm(y ~ x + id + firm, wei=w)) Call: felm(formula = y ~ x + id + firm, weights = w) Weighted Residuals: Min 1Q Median 3Q Max -13.25 -2.08 -0.04 2.09 12.46 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.8613 0.5201 3.58 0.00036 *** x 1.0300 0.0652 15.81 < 2e-16 *** id2 -2.6194 0.6439 -4.07 5.2e-05 *** id3 -0.6692 0.5403 -1.24 0.21582 id4 -1.1415 0.7869 -1.45 0.14721 id5 -1.2926 0.6286 -2.06 0.04001 * id6 -0.7207 0.6152 -1.17 0.24171 id7 -1.0589 0.6822 -1.55 0.12094 id8 -1.2310 0.5789 -2.13 0.03372 * id9 -3.4462 0.5759 -5.98 3.1e-09 *** id10 -3.0019 0.5100 -5.89 5.5e-09 *** id11 -0.5706 0.6209 -0.92 0.35836 id12 -2.2776 0.6920 -3.29 0.00104 ** id13 -2.1031 0.6615 -3.18 0.00153 ** id14 -2.6145 0.5455 -4.79 1.9e-06 *** id15 -2.3412 0.7968 -2.94 0.00338 ** id16 -1.8604 0.6498 -2.86 0.00429 ** id17 -1.5416 0.6291 -2.45 0.01446 * id18 -1.6685 0.5805 -2.87 0.00414 ** id19 -1.1447 0.5764 -1.99 0.04733 * id20 -2.1958 0.5652 -3.88 0.00011 *** id21 -0.9335 0.5952 -1.57 0.11714 id22 -1.9375 0.6889 -2.81 0.00502 ** id23 -2.0967 0.5487 -3.82 0.00014 *** id24 -1.7334 0.6376 -2.72 0.00668 ** id25 0.1212 0.5341 0.23 0.82053 id26 -0.9601 0.5221 -1.84 0.06623 . id27 0.9065 0.4012 2.26 0.02408 * id28 -3.3619 0.5305 -6.34 3.6e-10 *** id29 -1.6277 0.6513 -2.50 0.01262 * id30 -1.3184 0.5657 -2.33 0.02000 * id31 -0.3721 0.4793 -0.78 0.43772 id32 -0.7692 0.5913 -1.30 0.19367 id33 -1.9824 0.5584 -3.55 0.00040 *** id34 -1.9499 0.5507 -3.54 0.00042 *** id35 0.0516 0.4764 0.11 0.91377 id36 0.1067 0.4759 0.22 0.82266 id37 -1.5141 0.5486 -2.76 0.00590 ** id38 0.1908 0.4384 0.44 0.66353 id39 -1.6424 0.6727 -2.44 0.01482 * id40 -2.5424 0.6198 -4.10 4.5e-05 *** firm2 -0.5050 0.5541 -0.91 0.36227 firm3 0.2966 0.5515 0.54 0.59082 firm4 -0.7015 0.6011 -1.17 0.24347 firm5 0.2912 0.5572 0.52 0.60137 firm6 -0.8755 0.5805 -1.51 0.13182 firm7 0.8419 0.5328 1.58 0.11442 firm8 -0.7795 0.5421 -1.44 0.15077 firm9 -1.2168 0.5727 -2.12 0.03387 * firm10 -0.3212 0.5286 -0.61 0.54355 firm11 -1.2400 0.6150 -2.02 0.04407 * firm12 -2.0502 0.4703 -4.36 1.5e-05 *** firm13 -1.5565 0.5272 -2.95 0.00323 ** firm14 -2.1253 0.5368 -3.96 8.1e-05 *** firm15 0.2583 0.5686 0.45 0.64980 firm16 0.3063 0.6257 0.49 0.62451 firm17 -1.5812 0.6148 -2.57 0.01028 * firm18 -0.6810 0.5711 -1.19 0.23341 firm19 -3.6899 0.5144 -7.17 1.5e-12 *** firm20 2.7891 0.5887 4.74 2.5e-06 *** firm21 -0.1260 0.5829 -0.22 0.82886 firm22 -1.9048 0.5790 -3.29 0.00104 ** firm23 1.0813 0.5574 1.94 0.05273 . firm24 0.0336 0.6337 0.05 0.95775 firm25 -0.2585 0.5185 -0.50 0.61818 firm26 0.9889 0.5502 1.80 0.07261 . firm27 0.5730 0.5260 1.09 0.27626 firm28 -0.4654 0.4999 -0.93 0.35211 firm29 -1.5603 0.5732 -2.72 0.00661 ** firm30 0.9660 0.5754 1.68 0.09355 . firm31 -1.6254 0.5390 -3.02 0.00264 ** firm32 -1.9788 0.5383 -3.68 0.00025 *** firm33 0.8544 0.5666 1.51 0.13191 firm34 1.9429 0.5533 3.51 0.00047 *** firm35 -0.3309 0.5302 -0.62 0.53270 firm36 -0.7887 0.6232 -1.27 0.20595 firm37 -1.7940 0.5364 -3.34 0.00086 *** firm38 0.7225 0.5934 1.22 0.22373 firm39 -1.2620 0.5646 -2.24 0.02565 * firm40 -0.8222 0.6186 -1.33 0.18409 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 3.69 on 920 degrees of freedom Multiple R-squared(full model): 0.703 Adjusted R-squared: 0.677 Multiple R-squared(proj model): 0.703 Adjusted R-squared: 0.677 F-statistic(full model):27.6 on 79 and 920 DF, p-value: <2e-16 F-statistic(proj model): 27.6 on 79 and 920 DF, p-value: <2e-16 > # felm with projection says > est <- felm(y ~x | id + firm, wei=w, keepX=TRUE) > summary(est) Call: felm(formula = y ~ x | id + firm, weights = w, keepX = TRUE) Weighted Residuals: Min 1Q Median 3Q Max -13.25 -2.08 -0.04 2.09 12.46 Coefficients: Estimate Std. Error t value Pr(>|t|) x 1.0300 0.0652 15.8 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 3.69 on 920 degrees of freedom Multiple R-squared(full model): 0.703 Adjusted R-squared: 0.677 Multiple R-squared(proj model): 0.214 Adjusted R-squared: 0.146 F-statistic(full model):27.6 on 79 and 920 DF, p-value: <2e-16 F-statistic(proj model): 250 on 1 and 920 DF, p-value: <2e-16 > head(sandwich::estfun(est)) x [1,] 7.0468 [2,] 3.6852 [3,] -0.5583 [4,] 3.0615 [5,] 1.7435 [6,] 0.6457 > > # test getfe: > Kid <- nlevels(id) > sid <- 1:Kid > Kfirm <- nlevels(firm) > sfirm <- 1:Kfirm + Kid > ef <- function(x, addnames=FALSE) { + icpt <- x[1] + x[Kid+1] + x[sid[-1]] <- x[sid[-1]] - x[1] + x[1] <- icpt + x[sfirm] <- x[sfirm] - x[Kid+1] + if(addnames) names(x) <- c('(Intercept)',paste('id',sid[-1],sep=''),paste('firm',1:Kid,sep='')) + x[c(sid,sfirm[-1])] + } > head(getfe(est,ef=ef,se=TRUE)) effect se (Intercept) 1.8613 0.4374 id2 -2.6194 0.5791 id3 -0.6692 0.5303 id4 -1.1415 0.7585 id5 -1.2926 0.5886 id6 -0.7207 0.5077 > # test fevcov > print(fv <- fevcov(est)) id firm id 1.3691 -0.3109 firm -0.3109 1.4383 attr(,"bias") id firm id 0.18118 -0.01039 firm -0.01039 0.18009 > message('correlation:', round(cov2cor(fv)[1,2],4)) correlation:-0.2216 > v1 <- lfe:::wvar(ideff[id],w) > v2 <- lfe:::wvar(firmeff[firm],w) > cv <- lfe:::wcov(ideff[id], -firmeff[firm], w) > message( + 'idvar ',round(v1,4), + ' firmvar ',round(v2,4), + ' cov ',round(cv,4), + ' cor ',round(cv/sqrt(v1*v2),4) + ) idvar 1.3914 firmvar 1.4939 cov -0.2937 cor -0.2037 > bccorr(est) corr v1 v2 cov d1 d2 d12 -0.221372 1.357323 1.467321 -0.312411 0.192984 0.151036 -0.008897 > > varvars(est) [1] 0.2769 0.1014 > varvars(est,biascorrect=TRUE) [1] -0.1638 -0.5108 Warning messages: 1: In varvar(index, fe, est$X, effs[[index]], resvar, tol[index], biascorrect, : bias corrected varvars with weights not tested 2: In varvar(index, fe, est$X, effs[[index]], resvar, tol[index], biascorrect, : bias corrected varvars with weights not tested > > proc.time() user system elapsed 2.65 0.18 2.82