R Under development (unstable) (2024-02-26 r85990 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(42) > options(digits=3, warn=1,lfe.threads=1) > x <- rnorm(1000) > x1 <- rnorm(1000) > x2 <- rnorm(1000) > z1 <- rnorm(1000) > z2 <- rnorm(1000) > Q <- rnorm(1000) > f1 <- sample(8,length(x), repl=T) > f2 <- factor(sample(8,length(x), repl=T)) > clu <- factor(sample(30,length(x), replace=T)) > cluerr <- rnorm(nlevels(clu))[clu] > err <- abs(x)*rnorm(length(x)) + cluerr > y <- x +rnorm(nlevels(clu),sd=0.3)[clu] + log(f1) + err > f1 <- factor(f1) > dat <- data.frame(y, x, f1=f1, f2, cluster=clu) > > # deprecated stuff > summary(felm(y ~x + f1+f2, dat, clustervar='clu')) Warning in felm(y ~ x + f1 + f2, dat, clustervar = "clu") : Argument(s) clustervar are deprecated and will be removed, use multipart formula instead Call: felm(formula = y ~ x + f1 + f2, data = dat, clustervar = "clu") Residuals: Min 1Q Median 3Q Max -5.834 -1.150 -0.026 1.183 6.311 Coefficients: Estimate Cluster s.e. t value Pr(>|t|) (Intercept) -0.4610 0.3126 -1.47 0.14057 x 0.9198 0.0704 13.06 < 2e-16 *** f12 0.5837 0.1674 3.49 0.00051 *** f13 0.9914 0.2345 4.23 2.6e-05 *** f14 1.3504 0.1756 7.69 3.5e-14 *** f15 1.2982 0.1692 7.67 4.1e-14 *** f16 1.7813 0.2045 8.71 < 2e-16 *** f17 1.9645 0.1492 13.17 < 2e-16 *** f18 2.0515 0.1933 10.62 < 2e-16 *** f22 0.1926 0.2160 0.89 0.37279 f23 -0.0330 0.2465 -0.13 0.89365 f24 0.0556 0.2165 0.26 0.79721 f25 0.0413 0.2028 0.20 0.83856 f26 -0.0217 0.1980 -0.11 0.91283 f27 0.1857 0.2218 0.84 0.40277 f28 0.1583 0.1977 0.80 0.42362 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.71 on 984 degrees of freedom Multiple R-squared(full model): 0.307 Adjusted R-squared: 0.297 Multiple R-squared(proj model): 0.307 Adjusted R-squared: 0.297 F-statistic(full model, *iid*):29.1 on 15 and 984 DF, p-value: <2e-16 F-statistic(proj model): 52.8 on 15 and 29 DF, p-value: <2e-16 > summary(felm(y ~ x + G(f1)+G(f2), dat, clustervar=clu)) Warning in felm(y ~ x + G(f1) + G(f2), dat, clustervar = clu) : Argument(s) clustervar are deprecated and will be removed, use multipart formula instead Warning in ..oldfelm(formula = y ~ x + G(f1) + G(f2), data = dat, clustervar = clu) : Argument(s) clustervar are deprecated and will be removed, use multipart formula instead Warning in oldparseformula(form, data) : The G() syntax is deprecated, please use multipart formulas instead Call: ..oldfelm(formula = y ~ x + G(f1) + G(f2), data = dat, clustervar = clu) Residuals: Min 1Q Median 3Q Max -5.834 -1.150 -0.026 1.183 6.311 Coefficients: Estimate Cluster s.e. t value Pr(>|t|) x 0.9198 0.0704 13.1 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.7 on 985 degrees of freedom Multiple R-squared(full model): 0.307 Adjusted R-squared: 0.297 F-statistic(full model, *iid*):31.2 on 14 and 985 DF, p-value: <2e-16 F-statistic(proj model): 171 on 1 and 29 DF, p-value: 1.12e-13 > summary(felm(y ~ x | f1+f2, dat, clustervar=clu)) Warning in felm(y ~ x | f1 + f2, dat, clustervar = clu) : Argument(s) clustervar are deprecated and will be removed, use multipart formula instead Call: felm(formula = y ~ x | f1 + f2, data = dat, clustervar = clu) Residuals: Min 1Q Median 3Q Max -5.834 -1.150 -0.026 1.183 6.311 Coefficients: Estimate Cluster s.e. t value Pr(>|t|) x 0.9198 0.0704 13.1 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.71 on 984 degrees of freedom Multiple R-squared(full model): 0.307 Adjusted R-squared: 0.297 Multiple R-squared(proj model): 0.225 Adjusted R-squared: 0.213 F-statistic(full model, *iid*):29.1 on 15 and 984 DF, p-value: <2e-16 F-statistic(proj model): 171 on 1 and 29 DF, p-value: 1.12e-13 > > #anomalies. No variables, etc. > summary(est <- felm(y ~ 1 | f1|0|cluster, dat)) Call: felm(formula = y ~ 1 | f1 | 0 | cluster, data = dat) Residuals: Min 1Q Median 3Q Max -6.706 -1.223 0.012 1.168 9.396 Coefficients: (No coefficients) > fevcov(est) f1 f1 0.401 attr(,"bias") f1 f1 0.0184 > summary(felm(y ~ 0 | f1|0|cluster, dat)) Call: felm(formula = y ~ 0 | f1 | 0 | cluster, data = dat) Residuals: Min 1Q Median 3Q Max -6.706 -1.223 0.012 1.168 9.396 Coefficients: (No coefficients) > summary(felm(y ~ 0 | 0|0|cluster, dat)) Call: felm(formula = y ~ 0 | 0 | 0 | cluster, data = dat) Residuals: Min 1Q Median 3Q Max -5.825 -0.488 0.833 2.099 10.277 Coefficients: (No coefficients) > > summary(felm(y ~ x + x2|f1+factor(f2),dat)) Call: felm(formula = y ~ x + x2 | f1 + factor(f2), data = dat) Residuals: Min 1Q Median 3Q Max -5.830 -1.143 -0.025 1.184 6.307 Coefficients: Estimate Std. Error t value Pr(>|t|) x 0.9201 0.0545 16.88 <2e-16 *** x2 0.0116 0.0529 0.22 0.83 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.71 on 983 degrees of freedom Multiple R-squared(full model): 0.307 Adjusted R-squared: 0.296 Multiple R-squared(proj model): 0.225 Adjusted R-squared: 0.212 F-statistic(full model):27.2 on 16 and 983 DF, p-value: <2e-16 F-statistic(proj model): 143 on 2 and 983 DF, p-value: <2e-16 > summary(felm(y ~ x + x2+f1|factor(f2),dat)) Call: felm(formula = y ~ x + x2 + f1 | factor(f2), data = dat) Residuals: Min 1Q Median 3Q Max -5.830 -1.143 -0.025 1.184 6.307 Coefficients: Estimate Std. Error t value Pr(>|t|) x 0.9201 0.0545 16.88 < 2e-16 *** x2 0.0116 0.0529 0.22 0.8269 f12 0.5837 0.2136 2.73 0.0064 ** f13 0.9923 0.2164 4.59 5.1e-06 *** f14 1.3518 0.2123 6.37 2.9e-10 *** f15 1.2983 0.2088 6.22 7.4e-10 *** f16 1.7815 0.2203 8.09 1.8e-15 *** f17 1.9639 0.2196 8.94 < 2e-16 *** f18 2.0519 0.2154 9.53 < 2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.71 on 983 degrees of freedom Multiple R-squared(full model): 0.307 Adjusted R-squared: 0.296 Multiple R-squared(proj model): 0.302 Adjusted R-squared: 0.29 F-statistic(full model):27.2 on 16 and 983 DF, p-value: <2e-16 F-statistic(proj model): 47.2 on 9 and 983 DF, p-value: <2e-16 > summary(felm(y ~ x + x2+f1+factor(f2),dat)) Call: felm(formula = y ~ x + x2 + f1 + factor(f2), data = dat) Residuals: Min 1Q Median 3Q Max -5.830 -1.143 -0.025 1.184 6.307 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.4598 0.2060 -2.23 0.0258 * x 0.9201 0.0545 16.88 < 2e-16 *** x2 0.0116 0.0529 0.22 0.8269 f12 0.5837 0.2136 2.73 0.0064 ** f13 0.9923 0.2164 4.59 5.1e-06 *** f14 1.3518 0.2123 6.37 2.9e-10 *** f15 1.2983 0.2088 6.22 7.4e-10 *** f16 1.7815 0.2203 8.09 1.8e-15 *** f17 1.9639 0.2196 8.94 < 2e-16 *** f18 2.0519 0.2154 9.53 < 2e-16 *** factor(f2)2 0.1884 0.2158 0.87 0.3829 factor(f2)3 -0.0327 0.2211 -0.15 0.8825 factor(f2)4 0.0554 0.2238 0.25 0.8045 factor(f2)5 0.0394 0.2137 0.18 0.8538 factor(f2)6 -0.0225 0.2214 -0.10 0.9191 factor(f2)7 0.1831 0.2163 0.85 0.3974 factor(f2)8 0.1561 0.2210 0.71 0.4802 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.71 on 983 degrees of freedom Multiple R-squared(full model): 0.307 Adjusted R-squared: 0.296 Multiple R-squared(proj model): 0.307 Adjusted R-squared: 0.296 F-statistic(full model):27.2 on 16 and 983 DF, p-value: <2e-16 F-statistic(proj model): 27.2 on 16 and 983 DF, p-value: <2e-16 > summary(lm(y ~ x + x2 + f1 + factor(f2),dat)) Call: lm(formula = y ~ x + x2 + f1 + factor(f2), data = dat) Residuals: Min 1Q Median 3Q Max -5.830 -1.143 -0.025 1.184 6.307 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.4598 0.2060 -2.23 0.0258 * x 0.9201 0.0545 16.88 < 2e-16 *** x2 0.0116 0.0529 0.22 0.8269 f12 0.5837 0.2136 2.73 0.0064 ** f13 0.9923 0.2164 4.59 5.1e-06 *** f14 1.3518 0.2123 6.37 2.9e-10 *** f15 1.2983 0.2088 6.22 7.4e-10 *** f16 1.7815 0.2203 8.09 1.8e-15 *** f17 1.9639 0.2196 8.94 < 2e-16 *** f18 2.0519 0.2154 9.53 < 2e-16 *** factor(f2)2 0.1884 0.2158 0.87 0.3829 factor(f2)3 -0.0327 0.2211 -0.15 0.8825 factor(f2)4 0.0554 0.2238 0.25 0.8045 factor(f2)5 0.0394 0.2137 0.18 0.8538 factor(f2)6 -0.0225 0.2214 -0.10 0.9191 factor(f2)7 0.1831 0.2163 0.85 0.3974 factor(f2)8 0.1561 0.2210 0.71 0.4802 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.71 on 983 degrees of freedom Multiple R-squared: 0.307, Adjusted R-squared: 0.296 F-statistic: 27.2 on 16 and 983 DF, p-value: <2e-16 > summary(felm(y ~ x + x2 + f1 |0|0|0|factor(f2))) Call: felm(formula = y ~ x + x2 + f1 | 0 | 0 | 0 | factor(f2)) Residuals: Min 1Q Median 3Q Max -5.830 -1.143 -0.025 1.184 6.307 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.4598 0.2060 -2.23 0.0258 * x 0.9201 0.0545 16.88 < 2e-16 *** x2 0.0116 0.0529 0.22 0.8269 f12 0.5837 0.2136 2.73 0.0064 ** f13 0.9923 0.2164 4.59 5.1e-06 *** f14 1.3518 0.2123 6.37 2.9e-10 *** f15 1.2983 0.2088 6.22 7.4e-10 *** f16 1.7815 0.2203 8.09 1.8e-15 *** f17 1.9639 0.2196 8.94 < 2e-16 *** f18 2.0519 0.2154 9.53 < 2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.71 on 983 degrees of freedom Multiple R-squared(full model): 0.307 Adjusted R-squared: 0.296 Multiple R-squared(proj model): 0.311 Adjusted R-squared: 0.299 F-statistic(full model):27.2 on 16 and 983 DF, p-value: <2e-16 F-statistic(proj model): 47.2 on 9 and 983 DF, p-value: <2e-16 7 variable(s) were projected out > summary(felm(y ~ x + f1+factor(f2) |0|0|0|x2)) Call: felm(formula = y ~ x + f1 + factor(f2) | 0 | 0 | 0 | x2) Residuals: Min 1Q Median 3Q Max -5.830 -1.143 -0.025 1.184 6.307 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.4598 0.2060 -2.23 0.0258 * x 0.9201 0.0545 16.88 < 2e-16 *** f12 0.5837 0.2136 2.73 0.0064 ** f13 0.9923 0.2164 4.59 5.1e-06 *** f14 1.3518 0.2123 6.37 2.9e-10 *** f15 1.2983 0.2088 6.22 7.4e-10 *** f16 1.7815 0.2203 8.09 1.8e-15 *** f17 1.9639 0.2196 8.94 < 2e-16 *** f18 2.0519 0.2154 9.53 < 2e-16 *** factor(f2)2 0.1884 0.2158 0.87 0.3829 factor(f2)3 -0.0327 0.2211 -0.15 0.8825 factor(f2)4 0.0554 0.2238 0.25 0.8045 factor(f2)5 0.0394 0.2137 0.18 0.8538 factor(f2)6 -0.0225 0.2214 -0.10 0.9191 factor(f2)7 0.1831 0.2163 0.85 0.3974 factor(f2)8 0.1561 0.2210 0.71 0.4802 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.71 on 983 degrees of freedom Multiple R-squared(full model): 0.307 Adjusted R-squared: 0.296 Multiple R-squared(proj model): 0.307 Adjusted R-squared: 0.296 F-statistic(full model):27.2 on 16 and 983 DF, p-value: <2e-16 F-statistic(proj model): 29.1 on 15 and 983 DF, p-value: <2e-16 1 variable(s) were projected out > > # IV > est <- felm(y ~ x | 0 | (x1 | x2 ~ z1 + z2)) > for(lh in est$stage1$lhs) print(summary(est$stage1, lhs=lh)) Summary for outcome x1 Call: NULL Residuals: Min 1Q Median 3Q Max -2.825 -0.660 0.003 0.643 3.528 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.00361 0.03119 -0.12 0.908 x 0.00895 0.03111 0.29 0.774 z1 0.05904 0.03153 1.87 0.061 . z2 0.01201 0.03065 0.39 0.695 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.986 on 996 degrees of freedom Multiple R-squared(full model): 0.00381 Adjusted R-squared: 0.000807 Multiple R-squared(proj model): 0.00381 Adjusted R-squared: 0.000807 F-statistic(full model):1.27 on 3 and 996 DF, p-value: 0.284 F-statistic(proj model): 1.27 on 3 and 996 DF, p-value: 0.284 F-statistic(excl instr.):1.85 on 2 and 996 DF, p-value: 0.157 Summary for outcome x2 Call: NULL Residuals: Min 1Q Median 3Q Max -3.160 -0.649 0.011 0.665 3.551 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.00456 0.03261 -0.14 0.89 x -0.02166 0.03252 -0.67 0.51 z1 -0.02110 0.03296 -0.64 0.52 z2 -0.02108 0.03204 -0.66 0.51 Residual standard error: 1.03 on 996 degrees of freedom Multiple R-squared(full model): 0.00133 Adjusted R-squared: -0.00168 Multiple R-squared(proj model): 0.00133 Adjusted R-squared: -0.00168 F-statistic(full model):0.442 on 3 and 996 DF, p-value: 0.723 F-statistic(proj model): 0.442 on 3 and 996 DF, p-value: 0.723 F-statistic(excl instr.):0.434 on 2 and 996 DF, p-value: 0.648 > summary(est) Call: felm(formula = y ~ x | 0 | (x1 | x2 ~ z1 + z2)) Residuals: Min 1Q Median 3Q Max -7.209 -1.624 -0.084 1.642 7.998 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.8521 0.0816 10.45 < 2e-16 *** x 0.9339 0.1129 8.27 4.2e-16 *** `x1(fit)` 0.1249 2.3324 0.05 0.96 `x2(fit)` 1.5652 4.6101 0.34 0.73 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 2.42 on 996 degrees of freedom Multiple R-squared(full model): -0.414 Adjusted R-squared: -0.419 Multiple R-squared(proj model): -0.414 Adjusted R-squared: -0.419 F-statistic(full model):46.4 on 3 and 996 DF, p-value: <2e-16 F-statistic(proj model): 46.4 on 3 and 996 DF, p-value: <2e-16 F-statistic(endog. vars):0.147 on 2 and 996 DF, p-value: 0.864 > condfstat(est,type=NULL) x1 x2 iid F 0.282 0.226 robust F 0.267 0.224 attr(,"df1") [1] 1 > summary(est2 <- felm(y ~1 | 0 | (x1 | x2 ~ z1 + z2) | 0 | x)) Call: felm(formula = y ~ 1 | 0 | (x1 | x2 ~ z1 + z2) | 0 | x) Residuals: Min 1Q Median 3Q Max -7.209 -1.624 -0.084 1.642 7.998 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.8521 0.0816 10.45 <2e-16 *** `x1(fit)` 0.1249 2.3324 0.05 0.96 `x2(fit)` 1.5652 4.6101 0.34 0.73 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 2.42 on 996 degrees of freedom Multiple R-squared(full model): -0.414 Adjusted R-squared: -0.419 Multiple R-squared(proj model): -0.762 Adjusted R-squared: -0.767 F-statistic(full model):46.4 on 3 and 996 DF, p-value: <2e-16 F-statistic(proj model): 0.147 on 2 and 996 DF, p-value: 0.864 F-statistic(endog. vars):0.147 on 2 and 996 DF, p-value: 0.864 1 variable(s) were projected out > condfstat(est2, type=NULL) x1 x2 iid F 0.282 0.226 robust F 0.267 0.224 attr(,"df1") [1] 1 > > est0 <- felm( y ~ 1|0|(Q~z1)) > condfstat(est0) Q iid F 0.148 attr(,"df1") [1] 2 > > # inplace test and NA > foo <- as.numeric(1:6) > fl <- list(factor(c('r','g','g','r','g','b')),factor(c('b','b','r','g','b','g'))) > a <- demeanlist(unnamed(foo), fl) > round(foo,6) [1] 0.0 -1.5 0.0 0.0 1.5 0.0 > foo <- rnorm(6) > foo[3] <- NaN > demeanlist(foo,fl) [1] NaN NaN NaN NaN NaN NaN > round(demeanlist(foo,fl,na.rm=TRUE),3) [1] 0.000 0.257 0.000 -0.257 0.000 attr(,"na.rm") [1] 3 > foo <- list(vec=runif(6),mat=matrix(runif(18),6)) > foo$vec[4] = NaN > foo$mat[3,2] = NaN > lapply(demeanlist(foo,fl),round,3) $vec [1] NaN NaN NaN NaN NaN NaN $mat [,1] [,2] [,3] [1,] 0.000 NaN 0.000 [2,] -0.066 NaN 0.116 [3,] 0.000 NaN 0.000 [4,] 0.000 NaN 0.000 [5,] 0.066 NaN -0.116 [6,] 0.000 NaN 0.000 > a <- demeanlist(foo,fl,na.rm=TRUE) > attributes(a) $names [1] "vec" "mat" $na.rm [1] 3 4 > lapply(a,round,3) $vec [1] 0.000 -0.201 0.201 0.000 $mat [,1] [,2] [,3] [1,] 0.000 0.000 0.000 [2,] -0.066 0.321 0.116 [3,] 0.066 -0.321 -0.116 [4,] 0.000 0.000 0.000 > > # autoload plm: > if(require('plm', quietly=TRUE)) { + data("EmplUK", package = "plm") + Em <- pdata.frame(EmplUK) + detach('package:plm', unload=TRUE) + print(felm(emp~output+capital + lag(wage,1)|firm, data=Em)) + } Attaching package: 'plm' The following object is masked from 'package:lfe': sargan Loading required namespace: plm output capital lag(wage, 1) 0.0613 0.9570 -0.1073 > > > > proc.time() user system elapsed 2.10 0.17 2.28