R Under development (unstable) (2024-02-14 r85901 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=5,warn=1,lfe.eps=1e-5) > set.seed(42) > x <- rnorm(400) > x2 <- rnorm(length(x)) > > id <- factor(sample(10,length(x),replace=TRUE)) > firm <- factor(sample(3,length(x),replace=TRUE,prob=c(2,1,1))) > id.eff <- rnorm(nlevels(id)) > firm.eff <- rnorm(nlevels(firm)) > > ## left hand side > u <- rnorm(length(x)) > x3 <- 0.2*x + 0.3*x2 + rnorm(length(x)) > x4 <- 0.1*x - 0.2*x2 + rnorm(length(x)) > Q <- 0.3*x3 + 0.4*x4 + x + 0.2*x2 + id.eff[id] + 0.15*u + rnorm(length(x),sd=0.2) > R <- 0.3*x3 + 0.33*x4 + 0.2*x + 0.5*x2 + 0.7*id.eff[id] - 0.11*u + rnorm(length(x),sd=0.2) > y <- x + 0.5*x2 + id.eff[id] + firm.eff[firm] + Q + R + u > > ## estimate and print result > est <- felm(y ~ x+x2 | id+firm |(Q|R~x3+x4)) > summary(est,robust=TRUE) Call: felm(formula = y ~ x + x2 | id + firm | (Q | R ~ x3 + x4)) Residuals: Min 1Q Median 3Q Max -3.398 -0.847 0.011 0.815 4.416 Coefficients: Estimate Robust s.e t value Pr(>|t|) x 1.4677 0.8115 1.81 0.071 . x2 0.0857 0.4406 0.19 0.846 `Q(fit)` 0.1913 1.0945 0.17 0.861 `R(fit)` 2.0963 1.2848 1.63 0.104 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.27 on 384 degrees of freedom Multiple R-squared(full model): 0.903 Adjusted R-squared: 0.899 Multiple R-squared(proj model): 0.811 Adjusted R-squared: 0.803 F-statistic(full model, *iid*): 244 on 15 and 384 DF, p-value: <2e-16 F-statistic(proj model): 436 on 4 and 384 DF, p-value: <2e-16 F-statistic(endog. vars): 130 on 2 and 384 DF, p-value: <2e-16 > summary(felm(y ~ x+x2 | id+firm |(Q|R~x3+x4), kclass='liml')) Call: felm(formula = y ~ x + x2 | id + firm | (Q | R ~ x3 + x4), kclass = "liml") Residuals: Min 1Q Median 3Q Max -3.398 -0.847 0.011 0.815 4.416 Coefficients: Estimate Std. Error t value Pr(>|t|) x 1.4677 0.8535 1.72 0.086 . x2 0.0857 0.4703 0.18 0.856 Q 0.1913 1.1687 0.16 0.870 R 2.0963 1.3755 1.52 0.128 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.27 on 384 degrees of freedom Multiple R-squared(full model): 0.903 Adjusted R-squared: 0.899 Multiple R-squared(proj model): 0.811 Adjusted R-squared: 0.803 F-statistic(full model): 238 on 15 and 384 DF, p-value: <2e-16 F-statistic(proj model): 437 on 4 and 384 DF, p-value: <2e-16 > update(est, . ~ x) x `Q(fit)` `R(fit)` 1.5834 0.0307 2.2853 > # try it from within a function > fr <- data.frame(y,x,id,firm,Q,R,x3,x4) > fun <- function() { + Y <- y + S <- Q + clu <- factor(sample(10,length(x), replace=TRUE)) + felm(Y ~ x+x2 | id + firm |(Q|R ~ x3+x4), cluster=clu) + fr <- data.frame(y,x,x2,id,firm,Q,R,x3,x4,clu) + # test whether it finds names in the wrong place. + `S(fit)` <- as.name('a') + R <- as.name('b') + felm(y ~ x+x2 | id + firm |(S|R ~ x3+x4)|clu, data=fr) + } > est <- fun() Warning in felm(Y ~ x + x2 | id + firm | (Q | R ~ x3 + x4), cluster = clu) : Argument(s) clustervar are deprecated and will be removed, use multipart formula instead > rm(x2) > summary(est, robust=TRUE) Call: felm(formula = y ~ x + x2 | id + firm | (S | R ~ x3 + x4) | clu, data = fr) Residuals: Min 1Q Median 3Q Max -3.398 -0.847 0.011 0.815 4.416 Coefficients: Estimate Cluster s.e. t value Pr(>|t|) x 1.4677 0.8656 1.70 0.091 . x2 0.0857 0.5022 0.17 0.865 `S(fit)` 0.1913 1.1942 0.16 0.873 `R(fit)` 2.0963 1.4199 1.48 0.141 --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 1.27 on 384 degrees of freedom Multiple R-squared(full model): 0.903 Adjusted R-squared: 0.899 Multiple R-squared(proj model): 0.811 Adjusted R-squared: 0.803 F-statistic(full model, *iid*): 244 on 15 and 384 DF, p-value: <2e-16 F-statistic(proj model): 507 on 4 and 9 DF, p-value: 1.39e-10 F-statistic(endog. vars): 175 on 2 and 9 DF, p-value: 6.23e-08 > for(lh in est$stage1$lhs) print(summary(est$stage1, lhs=lh)) Summary for outcome S Call: NULL Residuals: Min 1Q Median 3Q Max -0.6979 -0.1744 0.0002 0.1829 0.7426 Coefficients: Estimate Cluster s.e. t value Pr(>|t|) x 0.97216 0.01249 77.8 <2e-16 *** x2 0.22848 0.01483 15.4 <2e-16 *** x3 0.29656 0.00942 31.5 <2e-16 *** x4 0.42598 0.01252 34.0 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.269 on 384 degrees of freedom Multiple R-squared(full model): 0.971 Adjusted R-squared: 0.97 Multiple R-squared(proj model): 0.951 Adjusted R-squared: 0.949 F-statistic(full model, *iid*): 870 on 15 and 384 DF, p-value: <2e-16 F-statistic(proj model): 8.37e+03 on 4 and 9 DF, p-value: 4.7e-16 F-statistic(excl instr.): 710 on 2 and 9 DF, p-value: 1.25e-10 Summary for outcome R Call: NULL Residuals: Min 1Q Median 3Q Max -0.6051 -0.1380 -0.0178 0.1519 0.6989 Coefficients: Estimate Cluster s.e. t value Pr(>|t|) x 0.2049 0.0114 18.0 <2e-16 *** x2 0.5115 0.0111 46.1 <2e-16 *** x3 0.2905 0.0116 25.0 <2e-16 *** x4 0.3308 0.0120 27.6 <2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 0.231 on 384 degrees of freedom Multiple R-squared(full model): 0.95 Adjusted R-squared: 0.948 Multiple R-squared(proj model): 0.913 Adjusted R-squared: 0.91 F-statistic(full model, *iid*): 485 on 15 and 384 DF, p-value: <2e-16 F-statistic(proj model): 1.53e+03 on 4 and 9 DF, p-value: 9.68e-13 F-statistic(excl instr.): 605 on 2 and 9 DF, p-value: 2.55e-10 > print(condfstat(est,NULL,quantiles=c(0.1,0.5,0.9))) S R iid F 6.8028 6.8024 robust F 7.9129 7.9919 cluster F 14.6613 13.8477 attr(,"df1") [1] 1 attr(,"quantiles") 10% 50% 90% S -1.89448 0.39829 1.2657 R 0.80861 1.79392 4.2885 attr(,"quantiles")attr(,"q") [1] 0.1 0.5 0.9 attr(,"quantiles")attr(,"samples") [1] 100 > > proc.time() user system elapsed 1.25 0.18 1.37