lrberge / fixest

Fixed-effects estimations
https://lrberge.github.io/fixest/
362 stars 59 forks source link

`etable()`: `extralines` errors with only one model #399

Open etiennebacher opened 1 year ago

etiennebacher commented 1 year ago

Argument extralines in etable() doesn't seem to work when there's only one model:

library(fixest)

packageVersion("fixest")
#> [1] '0.11.1'

# From examples of `etable()`

est_all  = feols(Ozone ~ Solar.R + Temp + Wind, data = airquality)
#> NOTE: 42 observations removed because of NA values (LHS: 37, RHS: 7).
est_sub1 = feols(Ozone ~ Solar.R + Temp + Wind, data = airquality,
                 subset = ~ Month %in% 5:6)
#> NOTE: 28 observations removed because of NA values (LHS: 26, RHS: 4).
est_sub2 = feols(Ozone ~ Solar.R + Temp + Wind, data = airquality,
                 subset = ~ Month %in% 7:8)
#> NOTE: 13 observations removed because of NA values (LHS: 10, RHS: 3).

# 3 models: fine
etable(est_all, est_sub1, est_sub2,
       extralines = list("Sub-sample" = c("All", "May-June", "Jul.-Aug.")))
#>                            est_all         est_sub1          est_sub2
#> Dependent Var.:              Ozone            Ozone             Ozone
#>                                                                      
#> Constant          -64.34** (23.05)   -48.52 (30.42)    -80.06 (57.59)
#> Solar.R           0.0598* (0.0232)  0.0249 (0.0319)  0.0993* (0.0414)
#> Temp             1.652*** (0.2535) 1.184** (0.4123)  2.037** (0.6508)
#> Wind            -3.334*** (0.6544)  -1.107 (0.9236) -5.830*** (1.124)
#> Sub-sample                     All         May-June         Jul.-Aug.
#> _______________ __________________ ________________ _________________
#> S.E. type                      IID              IID               IID
#> Observations                   111               33                49
#> R2                         0.60589          0.35006           0.66607
#> Adj. R2                    0.59484          0.28282           0.64381
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# 2 models: fine
etable(est_all, est_sub1,
       extralines = list("Sub-sample" = c("All", "May-June")))
#>                            est_all         est_sub1
#> Dependent Var.:              Ozone            Ozone
#>                                                    
#> Constant          -64.34** (23.05)   -48.52 (30.42)
#> Solar.R           0.0598* (0.0232)  0.0249 (0.0319)
#> Temp             1.652*** (0.2535) 1.184** (0.4123)
#> Wind            -3.334*** (0.6544)  -1.107 (0.9236)
#> Sub-sample                     All         May-June
#> _______________ __________________ ________________
#> S.E. type                      IID              IID
#> Observations                   111               33
#> R2                         0.60589          0.35006
#> Adj. R2                    0.59484          0.28282
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

# 1 model: error
etable(est_all, 
       extralines = list("Sub-sample" = c("All")))
#> Warning in expand_list_vector(el): NAs introduced by coercion
#> Error in rep(x_names[j], x[j]): invalid 'times' argument
MTueting commented 1 month ago

Doubling down on this. It would be great if this could be implemented.

MTueting commented 1 month ago

As a hotfix, registering an extralines function that simply returns the text works.

rm(list=ls())

# We register a function including our text.
my_fun2 = function(z){"Return this text in column."}
extralines_register("hotfix", my_fun2, "")

# An estimation
data(iris)
est = feols(Petal.Length ~ Sepal.Length | Species, iris)

# We can change the alias on the fly:
etable(est, extralines = list("_This text" = ~ hotfix))
                                        est
Dependent Var.:                Petal.Length

Sepal.Length               0.6321* (0.1232)
Fixed-Effects:             ----------------
Species                                 Yes
_______________            ________________
S.E.: Clustered                 by: Species
Observations                            150
R2                                  0.97489
Within R2                           0.57178
This text       Return this text in column.
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1