Skip to contents

Subselection from a fixest_panel which has been created with the function panel. Also allows to create lag/lead variables with functions l/f if the fixest_panel is also a data.table::data.table.

Usage

# S3 method for fixest_panel
[(x, i, j, ...)

Arguments

x

A fixest_panel object, created with the function panel.

i

Row subselection. Allows data.table::data.table style selection (provided the data is also a data.table).

j

Variable selection. Allows data.table::data.table style selection/variable creation (provided the data is also a data.table).

...

Other arguments to be passed to [.data.frame or data.table::data.table (or whatever the class of the initial data).

Value

It returns a fixest_panel data base, with the attributes allowing to create lags/leads properly bookkeeped.

Details

If the original data was also a data.table, some calls to [.fixest_panel may dissolve the fixest_panel object and return a regular data.table. This is the case for subselections with additional arguments. If so, a note is displayed on the console.

See also

Alternatively, the function panel changes a data.frame into a panel from which the functions l and f (creating leads and lags) can be called. Otherwise you can set the panel 'live' during the estimation using the argument panel.id (see for example in the function feols).

Author

Laurent Berge

Examples


data(base_did)

# Creating a fixest_panel object
pdat = panel(base_did, ~id+period)

# Subselections of fixest_panel objects bookkeeps the leads/lags engine
pdat_small = pdat[!pdat$period %in% c(2, 4), ]
a = feols(y~l(x1, 0:1), pdat_small)
#> NOTE: 324 observations removed because of NA values (RHS: 324).

# we obtain the same results, had we created the lags "on the fly"
base_small = base_did[!base_did$period %in% c(2, 4), ]
b = feols(y~l(x1, 0:1), base_small, panel.id = ~id+period)
#> NOTE: 324 observations removed because of NA values (RHS: 324).
etable(a, b)
#>                                 a                 b
#> Dependent Var.:                 y                 y
#>                                                    
#> Constant        3.691*** (0.3085) 3.691*** (0.3085)
#> x1              1.013*** (0.0707) 1.013*** (0.0707)
#> l(x1,1)          -0.0063 (0.0779)  -0.0063 (0.0779)
#> _______________ _________________ _________________
#> S.E.: Clustered            by: id            by: id
#> Observations                  540               540
#> R2                        0.26258           0.26258
#> Adj. R2                   0.25983           0.25983
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1


# Using data.table to create new lead/lag variables
if(require("data.table")){
  pdat_dt = panel(as.data.table(base_did), ~id+period)

  # Variable creation
  pdat_dt[, x_l1 := l(x1)]
  pdat_dt[, c("x_l1", "x_f1_2") := .(l(x1), f(x1)**2)]

  # Estimation on a subset of the data
  #  (the lead/lags work appropriately)
  feols(y~l(x1, 0:1), pdat_dt[!period %in% c(2, 4)])
}
#> NOTE: 324 observations removed because of NA values (RHS: 324).
#> OLS estimation, Dep. Var.: y
#> Observations: 540
#> Standard-errors: Clustered (id) 
#>              Estimate Std. Error   t value  Pr(>|t|)    
#> (Intercept)  3.691450   0.308452 11.967645 < 2.2e-16 ***
#> x1           1.012976   0.070704 14.327048 < 2.2e-16 ***
#> l(x1, 1)    -0.006268   0.077920 -0.080439   0.93604    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 5.02071   Adj. R2: 0.259834