fixest
fixest copied to clipboard
Post-estimation linear hypothesis testing using sunab()
Hi lrberge,
I love the fixest package. Thank you so much for such a wonderful contribution to the research community.
I have a regression I am running:
model_01 <- feols(y~sunab(week_treated, week, no_agg=T):x1 + sunab(year_treated, year, no_agg=T):x2 | id + year, dataset)
sa_x1 <- aggregate(res_sa_ar, agg=c("ATT_add" = "week::[^-]:cohort::.:x1")) sa_x2 <- aggregate(res_sa_ar, agg=c("ATT_rem" = "week::[^-]:cohort::.:x2"))
I want to run a post-estimation linear hypothesis test on the sum of two coefficients in sa_X1 and sa_X2, but I am unsure of how to obtain the new variance-covariance matrix (which I believe is a 2x2) to make this happen.
If you have any ideas, they would be hugely helpful to me and my colleagues.
Thanks so much, and very much looking forward to your thoughts.
Best,
Dave
Here is a rough draft of a function that takes the current results of fixest
with the sunab
option and returns the coefficients and full-variance covariance matrix. (Primarily written by the fantastic Mauricio Bravo, and turned into a function by me). I have not debugged for edge cases but it appears to match the coefficients and SEs in the baseline example on the fixest website.
#' @description
#' This function takes a regression estimated using fixest with the sunab option
#' and extracts the aggregated event-study coefficients and their variance-covariance matrix
#' @param sunab_fixest The result of a fixest call using the sunab option
#' @returns A list containing beta (the event-study coefficients) and sigma (the variance-covariance matrix)
sunab_beta_vcv <-
function(sunab_fixest){
## The following code block extracts the weights on individual coefs used in
# the fixest aggregation ##
sunab_agg <- sunab_fixest$model_matrix_info$sunab$agg_period
sunab_names <- names(sunab_fixest$coefficients)
sunab_sel <- grepl(sunab_agg, sunab_names, perl=TRUE)
sunab_names <- sunab_names[sunab_sel]
if(!is.null(sunab_fixest$weights)){
sunab_wgt <- colSums(sunab_fixest$weights * sign(model.matrix(sunab_fixest)[, sunab_names, drop=FALSE]))
} else {
sunab_wgt <- colSums(sign(model.matrix(sunab_fixest)[, sunab_names, drop=FALSE]))
}
#Construct matrix sunab_trans such that sunab_trans %*% non-aggregated coefs = aggregated coefs,
sunab_cohorts <- as.numeric(gsub(paste0(".*", sunab_agg, ".*"), "\\2", sunab_names, perl=TRUE))
sunab_mat <- model.matrix(~ 0 + factor(sunab_cohorts))
sunab_trans <- solve(t(sunab_mat) %*% (sunab_wgt * sunab_mat)) %*% t(sunab_wgt * sunab_mat)
#Get the coefs and vcv
sunab_coefs <- sunab_trans %*% cbind(sunab_fixest$coefficients[sunab_sel])
sunab_vcov <- sunab_trans %*% sunab_fixest$cov.scaled[sunab_sel, sunab_sel] %*% t(sunab_trans)
return(list(beta = sunab_coefs,
sigma = sunab_vcov))
}
Here is an illustration using the example from the fixest website on sunab.
library(fixest)
#Run fixest with sunab
res_sunab = feols(y ~ x1 + sunab(year_treated, year) | id + year, base_stagg)
#Extract the beta and vcv
beta <- sunab_beta_vcv(res_sunab)$beta
sigma <- sunab_beta_vcv(res_sunab)$sigma
beta
## [,1]
## factor(sunab_cohorts)-9 0.35176562
## factor(sunab_cohorts)-8 -0.07900914
## factor(sunab_cohorts)-7 0.10104016
## factor(sunab_cohorts)-6 -0.05640239
## factor(sunab_cohorts)-5 -0.29530929
## factor(sunab_cohorts)-4 -0.30372133
## factor(sunab_cohorts)-3 -0.08837921
## factor(sunab_cohorts)-2 0.08466162
## factor(sunab_cohorts)0 -5.18820685
## factor(sunab_cohorts)1 -3.54002789
## factor(sunab_cohorts)2 -2.15548532
## factor(sunab_cohorts)3 -0.77495372
## factor(sunab_cohorts)4 1.06885656
## factor(sunab_cohorts)5 2.11789308
## factor(sunab_cohorts)6 4.63147404
## factor(sunab_cohorts)7 4.88978792
## factor(sunab_cohorts)8 6.24362605
sigma
## factor(sunab_cohorts)-9 factor(sunab_cohorts)-8
## factor(sunab_cohorts)-9 0.128933582 0.067939863
## factor(sunab_cohorts)-8 0.067939863 0.088488482
## factor(sunab_cohorts)-7 0.053810916 0.063358191
## factor(sunab_cohorts)-6 0.030521218 0.011069808
## factor(sunab_cohorts)-5 0.025000525 0.037388940
## factor(sunab_cohorts)-4 0.034091068 0.027993199
## factor(sunab_cohorts)-3 0.014102186 0.021641191
## factor(sunab_cohorts)-2 0.019426648 0.025768492
## factor(sunab_cohorts)0 -0.006254676 -0.002100608
## factor(sunab_cohorts)1 -0.005566029 0.002006195
## factor(sunab_cohorts)2 -0.008015280 -0.009445044
## factor(sunab_cohorts)3 -0.010047652 -0.012629826
## factor(sunab_cohorts)4 -0.009278072 -0.012409486
## factor(sunab_cohorts)5 -0.014048477 -0.018299919
## factor(sunab_cohorts)6 -0.018700604 -0.023396088
## factor(sunab_cohorts)7 -0.017098764 -0.017936276
## factor(sunab_cohorts)8 -0.015227495 -0.013512327
## factor(sunab_cohorts)-7 factor(sunab_cohorts)-6
## factor(sunab_cohorts)-9 0.053810916 0.030521218
## factor(sunab_cohorts)-8 0.063358191 0.011069808
## factor(sunab_cohorts)-7 0.134663047 0.033008871
## factor(sunab_cohorts)-6 0.033008871 0.095429159
## factor(sunab_cohorts)-5 0.042104548 0.036285164
## factor(sunab_cohorts)-4 0.025031300 0.030851253
## factor(sunab_cohorts)-3 0.016542264 0.018027324
## factor(sunab_cohorts)-2 0.014019342 0.018530576
## factor(sunab_cohorts)0 0.013788922 0.012006910
## factor(sunab_cohorts)1 0.007598789 -0.000417817
## factor(sunab_cohorts)2 -0.005172434 0.009624978
## factor(sunab_cohorts)3 -0.012394643 0.018398669
## factor(sunab_cohorts)4 -0.012807776 -0.010992537
## factor(sunab_cohorts)5 -0.018483044 -0.014121085
## factor(sunab_cohorts)6 -0.020057899 -0.015257499
## factor(sunab_cohorts)7 -0.012809114 -0.008736086
## factor(sunab_cohorts)8 -0.010698189 -0.007121364
## factor(sunab_cohorts)-5 factor(sunab_cohorts)-4
## factor(sunab_cohorts)-9 0.025000525 0.034091068
## factor(sunab_cohorts)-8 0.037388940 0.027993199
## factor(sunab_cohorts)-7 0.042104548 0.025031300
## factor(sunab_cohorts)-6 0.036285164 0.030851253
## factor(sunab_cohorts)-5 0.086409139 0.045731969
## factor(sunab_cohorts)-4 0.045731969 0.062057196
## factor(sunab_cohorts)-3 0.035745051 0.036278958
## factor(sunab_cohorts)-2 0.043217170 0.032155145
## factor(sunab_cohorts)0 0.021598704 0.018788237
## factor(sunab_cohorts)1 0.023419488 0.016584035
## factor(sunab_cohorts)2 0.020351912 0.020600776
## factor(sunab_cohorts)3 0.022940438 0.017597472
## factor(sunab_cohorts)4 0.000285400 0.003028443
## factor(sunab_cohorts)5 -0.011426423 -0.007108845
## factor(sunab_cohorts)6 -0.012950121 -0.010112556
## factor(sunab_cohorts)7 -0.006956971 -0.006181218
## factor(sunab_cohorts)8 -0.006827285 -0.005221381
## factor(sunab_cohorts)-3 factor(sunab_cohorts)-2
## factor(sunab_cohorts)-9 0.014102186 0.019426648
## factor(sunab_cohorts)-8 0.021641191 0.025768492
## factor(sunab_cohorts)-7 0.016542264 0.014019342
## factor(sunab_cohorts)-6 0.018027324 0.018530576
## factor(sunab_cohorts)-5 0.035745051 0.043217170
## factor(sunab_cohorts)-4 0.036278958 0.032155145
## factor(sunab_cohorts)-3 0.065429110 0.036136779
## factor(sunab_cohorts)-2 0.036136779 0.059671076
## factor(sunab_cohorts)0 0.028520161 0.033244607
## factor(sunab_cohorts)1 0.025963145 0.028372051
## factor(sunab_cohorts)2 0.027805039 0.030313285
## factor(sunab_cohorts)3 0.022468117 0.035636009
## factor(sunab_cohorts)4 0.024409588 0.030644295
## factor(sunab_cohorts)5 0.028446271 0.048323292
## factor(sunab_cohorts)6 0.018949415 0.042344620
## factor(sunab_cohorts)7 -0.005047820 -0.002192158
## factor(sunab_cohorts)8 -0.004307115 -0.002963959
## factor(sunab_cohorts)0 factor(sunab_cohorts)1
## factor(sunab_cohorts)-9 -0.0062546764 -0.005566029
## factor(sunab_cohorts)-8 -0.0021006083 0.002006195
## factor(sunab_cohorts)-7 0.0137889217 0.007598789
## factor(sunab_cohorts)-6 0.0120069098 -0.000417817
## factor(sunab_cohorts)-5 0.0215987040 0.023419488
## factor(sunab_cohorts)-4 0.0187882371 0.016584035
## factor(sunab_cohorts)-3 0.0285201610 0.025963145
## factor(sunab_cohorts)-2 0.0332446067 0.028372051
## factor(sunab_cohorts)0 0.0488710693 0.027997468
## factor(sunab_cohorts)1 0.0279974678 0.045293056
## factor(sunab_cohorts)2 0.0256699250 0.029111042
## factor(sunab_cohorts)3 0.0217966968 0.021259107
## factor(sunab_cohorts)4 0.0389804530 0.025768712
## factor(sunab_cohorts)5 0.0493358241 0.053764339
## factor(sunab_cohorts)6 0.0491677558 0.046185422
## factor(sunab_cohorts)7 0.0003513777 0.024067581
## factor(sunab_cohorts)8 0.0057193123 0.014891992
## factor(sunab_cohorts)2 factor(sunab_cohorts)3
## factor(sunab_cohorts)-9 -0.008015280 -0.01004765
## factor(sunab_cohorts)-8 -0.009445044 -0.01262983
## factor(sunab_cohorts)-7 -0.005172434 -0.01239464
## factor(sunab_cohorts)-6 0.009624978 0.01839867
## factor(sunab_cohorts)-5 0.020351912 0.02294044
## factor(sunab_cohorts)-4 0.020600776 0.01759747
## factor(sunab_cohorts)-3 0.027805039 0.02246812
## factor(sunab_cohorts)-2 0.030313285 0.03563601
## factor(sunab_cohorts)0 0.025669925 0.02179670
## factor(sunab_cohorts)1 0.029111042 0.02125911
## factor(sunab_cohorts)2 0.050976324 0.03663242
## factor(sunab_cohorts)3 0.036632423 0.07679952
## factor(sunab_cohorts)4 0.038369055 0.03346085
## factor(sunab_cohorts)5 0.054561128 0.05635604
## factor(sunab_cohorts)6 0.055593892 0.02938926
## factor(sunab_cohorts)7 0.025137794 0.04064336
## factor(sunab_cohorts)8 0.010833514 -0.01024950
## factor(sunab_cohorts)4 factor(sunab_cohorts)5
## factor(sunab_cohorts)-9 -0.009278072 -0.014048477
## factor(sunab_cohorts)-8 -0.012409486 -0.018299919
## factor(sunab_cohorts)-7 -0.012807776 -0.018483044
## factor(sunab_cohorts)-6 -0.010992537 -0.014121085
## factor(sunab_cohorts)-5 0.000285400 -0.011426423
## factor(sunab_cohorts)-4 0.003028443 -0.007108845
## factor(sunab_cohorts)-3 0.024409588 0.028446271
## factor(sunab_cohorts)-2 0.030644295 0.048323292
## factor(sunab_cohorts)0 0.038980453 0.049335824
## factor(sunab_cohorts)1 0.025768712 0.053764339
## factor(sunab_cohorts)2 0.038369055 0.054561128
## factor(sunab_cohorts)3 0.033460849 0.056356044
## factor(sunab_cohorts)4 0.096318498 0.111903119
## factor(sunab_cohorts)5 0.111903119 0.200814656
## factor(sunab_cohorts)6 0.094984783 0.151760690
## factor(sunab_cohorts)7 0.024052121 0.049822497
## factor(sunab_cohorts)8 0.004525991 0.010891136
## factor(sunab_cohorts)6 factor(sunab_cohorts)7
## factor(sunab_cohorts)-9 -0.01870060 -0.0170987639
## factor(sunab_cohorts)-8 -0.02339609 -0.0179362761
## factor(sunab_cohorts)-7 -0.02005790 -0.0128091141
## factor(sunab_cohorts)-6 -0.01525750 -0.0087360859
## factor(sunab_cohorts)-5 -0.01295012 -0.0069569714
## factor(sunab_cohorts)-4 -0.01011256 -0.0061812184
## factor(sunab_cohorts)-3 0.01894941 -0.0050478203
## factor(sunab_cohorts)-2 0.04234462 -0.0021921580
## factor(sunab_cohorts)0 0.04916776 0.0003513777
## factor(sunab_cohorts)1 0.04618542 0.0240675812
## factor(sunab_cohorts)2 0.05559389 0.0251377943
## factor(sunab_cohorts)3 0.02938926 0.0406433584
## factor(sunab_cohorts)4 0.09498478 0.0240521214
## factor(sunab_cohorts)5 0.15176069 0.0498224973
## factor(sunab_cohorts)6 0.21200833 0.0160907684
## factor(sunab_cohorts)7 0.01609077 0.1151916147
## factor(sunab_cohorts)8 0.03872778 0.0111496928
## factor(sunab_cohorts)8
## factor(sunab_cohorts)-9 -0.015227495
## factor(sunab_cohorts)-8 -0.013512327
## factor(sunab_cohorts)-7 -0.010698189
## factor(sunab_cohorts)-6 -0.007121364
## factor(sunab_cohorts)-5 -0.006827285
## factor(sunab_cohorts)-4 -0.005221381
## factor(sunab_cohorts)-3 -0.004307115
## factor(sunab_cohorts)-2 -0.002963959
## factor(sunab_cohorts)0 0.005719312
## factor(sunab_cohorts)1 0.014891992
## factor(sunab_cohorts)2 0.010833514
## factor(sunab_cohorts)3 -0.010249504
## factor(sunab_cohorts)4 0.004525991
## factor(sunab_cohorts)5 0.010891136
## factor(sunab_cohorts)6 0.038727781
## factor(sunab_cohorts)7 0.011149693
## factor(sunab_cohorts)8 0.088540290
We can check that the coefficients and implied SEs match those reported by etable.
cbind( beta,
sqrt(diag(sigma)))
## [,1] [,2]
## factor(sunab_cohorts)-9 0.35176562 0.3590732
## factor(sunab_cohorts)-8 -0.07900914 0.2974701
## factor(sunab_cohorts)-7 0.10104016 0.3669646
## factor(sunab_cohorts)-6 -0.05640239 0.3089161
## factor(sunab_cohorts)-5 -0.29530929 0.2939543
## factor(sunab_cohorts)-4 -0.30372133 0.2491128
## factor(sunab_cohorts)-3 -0.08837921 0.2557911
## factor(sunab_cohorts)-2 0.08466162 0.2442766
## factor(sunab_cohorts)0 -5.18820685 0.2210680
## factor(sunab_cohorts)1 -3.54002789 0.2128217
## factor(sunab_cohorts)2 -2.15548532 0.2257794
## factor(sunab_cohorts)3 -0.77495372 0.2771273
## factor(sunab_cohorts)4 1.06885656 0.3103522
## factor(sunab_cohorts)5 2.11789308 0.4481235
## factor(sunab_cohorts)6 4.63147404 0.4604436
## factor(sunab_cohorts)7 4.88978792 0.3393989
## factor(sunab_cohorts)8 6.24362605 0.2975572
etable(res_sunab)
## res_sunab
## Dependent Var.: y
##
## x1 0.9947*** (0.0184)
## year = -9 0.3518 (0.3591)
## year = -8 -0.0790 (0.2975)
## year = -7 0.1010 (0.3670)
## year = -6 -0.0564 (0.3089)
## year = -5 -0.2953 (0.2940)
## year = -4 -0.3037 (0.2491)
## year = -3 -0.0884 (0.2558)
## year = -2 0.0847 (0.2443)
## year = 0 -5.188*** (0.2211)
## year = 1 -3.540*** (0.2128)
## year = 2 -2.155*** (0.2258)
## year = 3 -0.7750** (0.2771)
## year = 4 1.069*** (0.3104)
## year = 5 2.118*** (0.4481)
## year = 6 4.631*** (0.4604)
## year = 7 4.890*** (0.3394)
## year = 8 6.244*** (0.2976)
## Fixed-Effects: ------------------
## id Yes
## year Yes
## _______________ __________________
## S.E.: Clustered by: id
## Observations 950
## R2 0.90982
## Within R2 0.87641
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1