*! version 1.0.0 31 Oct 2023
help qcm

Title

qcm -- implementation of quantile control method (QCM) via Random Forest

Syntax

qcm depvar [indepvars]
,
trunit(#)
trperiod(#)
[options]

Model
counit(numlist:numlist) control units to be used as the donor pool

preperiod(numlist:numlist) pretreatment periods before the intervention occurred

postperiod(numlist:numlist) posttreatment periods when and after the intervention occurred

Optimization
ntree(int) number of trees to grow

mtry(int) number of predictors randomly sampled as candidates at each split

maxdepth(int) maximum depth of trees

minlsize(int) minimum number of observations required at each terminal node

minssize(int) minimum number of observations required to split an internal node

seed(int) seed used by the random number generator

fill(fil_method) method used to fill in missing values

Placebo Test
placebo([{unit|unit(numlist)} period(numlist) cutoff(#_c)]) in-space placebo test using fake treatment units and/or in-time placebo test using a fake treatment time

Reporting
cilevel(#_c) set confidence level; default is cilevel(95)

cistyle([1|2|3]) style of the confidence interval shown in figures; default is cistyle(1)

qtype(qdef) quantile definition to be transmitted to mm_quantile(); default is qtype(10)

frame(framename) create a Stata frame storing dataset with generated variables in the wide form

show(#_s) set the maximum number of bars to show in the bar charts in descending order

noimportance Do not show the variable importance. The default is to show the variable importance.

nofigure Do not display figures. The default is to display all figures.

savegraph([prefix], [asis replace]) Save all produced graphs to the current path.

xtset panelvar timevar must be used to declare a (strongly balanced) panel dataset before qcm is implemented; See xtset.

depvar and indepvars must be numeric variables, abbreviations are not allowed.

Description

qcm implements quantile control method (QCM) via random forest (Chen, Xiao and Yao, 2023), which provides confidence intervals for treatment effects in panel data with a single treated unit using quantile random forest (Meinshausen, 2006).
As a nonparametric ensemble learning, QCM is suitable for high-dimensional data, and robust to heteroskedasticity, autocorrelation and model misspecification.
Simulations in Chen, Xiao and Yao (2023) show that for 95% nominal confidence level, the empirical coverage rate can reach above 90% if the number of pretreatment periods is 30 or larger.
qcm also supports placebo tests using fake treatment units and/or fake treatment time as alternative methods for inference and robustness check.

Note that qcm is written entirely in Stata and Mata codes, without referring to any external environments such as Python or Java. Additionally, qcm requires moremata to be installed, which is available on SSC.

Preprocessing of data: A key assumption of QCM is that the data are stationary. For nonstationary data, it is strongly recommended to transform them into stationary data before applying QCM. For example, if the outcome variable is GDP with an exponential trend, it should be first transformed into the growth rate of GDP.

Required Settings

qcm automatically reshapes the panel dataset from the long form to the wide form before implementation, such that depvar of the treated unit is transformed to the response, while depvar of the control units are transformed to predictors. If indepvars are specified, indepvars of all units are transformed to predictors.

trunit(#) the unit number of the treated unit (i.e., the unit affected by intervention) as given in the panel variable specified in xtset panelvar. Note that only a single unit number can be specified.

trperoid(#) the time period when the intervention occurred. The time period refers to the time variable specified in xtset timevar, and must be an integer (see examples below). Note that only a single time period can be specified.

Options

Model

counit(numlist:numlist) a list of unit numbers for the control units as numlist given in the panel variable specified in xtset panelvar. The list of control units specified constitute what is known as the "donor pool". If no counit is specified, the donor pool defaults to all available units other than the treated unit.

preperiod(numlist:numlist) a list of pretreatment periods as numlist given in the time variable specified in xtset timevar.
If no preperiod is specified, preperiod defaults to the entire pre-intervention period,
which ranges from the earliest time period available in the time variable to the period immediately prior to the intervention.

postperiod(numlist:numlist) a list of posttreatment periods (when and after the intervention occurred) as numlist given in the time variable specified in xtset timevar.
If no postperiod is specified, postperiod defaults to the entire post-intervention period, which ranges from the time period when the intervention occurred to the last time period available in the time variable.

Optimization

ntree(int) specifies the number of trees to grow, which defaults to 500.

mtry(int) specifies the number of predictors randomly selected as candidates at each split, which defaults to (number of predictors)/3.

maxdepth(int) specifies the maximum depth of trees. If not specified, the maximum depth is unlimited, and nodes are split until all terminal nodes contain less than minssize observations.

minlsize(int) specifies the minimum number of observations required at each terminal node, which defaluts to 5.

minssize(int) specifies the minimum number of observations required to split an internal node, which defaluts to 10.

seed(int) specifies the seed for reproducible results, which defaluts to 1.

fill(fil_method) specifies the method to fill in missing values. If fill(mean) is specified, missing values are filled in by sample means for each unit. If fill(linear) is specified, then missing values are filled in linear interpolation for each unit. Note that these two methods for filling in missing values are rough, and only provided for convenience. If no fill(fil_method) is specified, then missing values are left unchanged.

Placebo Test

placebo([{unit|unit(numlist)} period(numlist) cutoff(#_c)]) specifies the types of placebo tests to be performed; otherwise, no placebo test will be implemented.

{unit|unit(numlist)} specifies in-space placebo test using fake treatment units in the donor pool,
where unit uses all fake treatment units and unit(numlist) uses a list of fake treatment units specified by numlist.
These two options iteratively reassign the treatment to control units where no intervention actually occurred,
and calculate the p-values of the treatment effects. Note that only one of unit and unit(numlist) can be specified.

period(numlist:numlist) specifies placebo tests using fake treatment times. This option assigns the treatment to time periods before the intervention, when no treatment actually ocurred.

cutoff(#_c) specifies a cutoff threshold that discards fake treatment units with pretreatment MSPE #_c times larger than that of the treated unit, where #_c must be a real number greater than or equal to 1.
This option only applies when unit or unit(numlist) is specified. If this option is not specified, then no fake treatment units are discarded.

Reporting

cilevel(#_c) specifies #_c as the confidence level expressed as a percentage for calculating confidence intervals, where #_c must be an integer within the range [1, 99].
The default is cilevel(95).

cistyle([1|2|3])} specifies the style for confidence intervals, where three distinct styles are available as denoted by cistyle(1), cistyle(2), and cistyle(3) respectively. The default is cistyle(1).

qtype(qdef) specifies qdef as the quantile definition to be transmitted to Mata command mm_quantile() of moremata,
where qdef must be an integer from 0 to 11.
The default is qtype(10), which uses the trimmed Harrell-Davis quantile estimator to compute sample quantiles (Akinshin, 2022), with the width parameter wd set as T0^(-1/2) and T0 being the number of pretreatment periods as suggested by Akinshin(2022). The trimmed Harrell-Davis quantile estimator is arguably the best approach currently available in the literature, which strikes a balance between efficiency and robustness. For details, see mm_quantile().

frame(framename) creates a Stata frame storing generated variables in the wide form. The frame named framename is replaced if existed, or created if not.

show(#_s) specifies the number of bars to show in all bar charts, where #_s correponds to bars with the largest #_s values. If this option is not specified, the default is to show all bars.

noimportance Do not show the variable importance. The default is to show the variable importance.

nofigure Do not display figures. The default is to display all figures.

savegraph([prefix], [asis replace]) automatically and iteratively calls the graph save to save all produced graphs to the current path,
where prefix specifies the prefix added to _graphname to form a file name, that is, the graph named graphname is stored as prefix_graphname.gph.
asis and replace are options passed to graph save; for details, see [G-2] graph save.
Note that this option only applies when nofigure is not specified.

Examples 1: estimating the economic impact of the 2004q1 economic integration of Hong Kong with mainland China (Hsiao et al.,2012)

. use growth, clear

. xtset region time

* Show the unit number of Hong Kong and the treatment time

. label list

. di tq(2004q1)

* Implement quantile control method

. qcm gdp, trunit(9) trperiod(176)

* Same as above, but uses a different style of confidence intervals

. qcm gdp, trunit(9) trperiod(176) cistyle(2)

* Same as above, but uses yet another different style of confidence intervals

. qcm gdp, trunit(9) trperiod(176) cistyle(3)

* Implement in-space placebo test with pretreatment MSPE 2 times smaller than or equal to that of the treated unit, and create a Stata frame "growth" storing generated variables in the wide form

. qcm gdp, trunit(9) trperiod(176) placebo(unit cut(2)) frame(growth)

* Implement in-time placebo test with the fake treatment time 2002q1, and save all produced graphs using prefix "growth" to the current path

. di tq(2002q1)

. qcm gdp, trunit(9) trperiod(176) placebo(period(168)) savegraph(growth, replace)

Examples 2: estimating the effect of carbon taxes on CO2 emissions in Sweden (Andersson, 2019)

. use carbontaxgr, clear

. xtset country year

* Show the unit number of Sweden

. label list

* Implement quantile control method, and only show up to 25 bars in bar plots

. qcm CO2 GDP gas vehicles urban pop, tru(13) trp(1990) show(25)

* Implement in-space placebo test with pretreatment MSPE 2 times smaller than or equal to that of the treated unit, and create a Stata frame "carbon" storing generated variables in the wide form

. qcm CO2 GDP gas vehicles urban pop, tru(13) trp(1990) show(25) placebo(unit cut(2)) frame(carbon)

* Implement in-time placebo test with the fake treatment time 1985, and save all produced graphs using prefix "carbon" to the current path

. qcm CO2 GDP gas vehicles urban pop, tru(13) trp(1990) show(25) placebo(period(1985)) savegraph(carbon, replace)

Stored results

qcm stores the following in e():

Scalars

e(G) number of units

e(T) number of observations with the dataset in the wide form

e(T0) number of observations in pretreatment periods with the dataset in the wide form

e(T1) number of observations in posttreatment periods with the dataset in the wide form

e(K) number of predictors with the dataset in the wide form

e(mae) mean absolute error of the optimal model fitted in pretreatment periods

e(mse) mean squared error of the optimal model fitted in pretreatment periods

e(rmse) root mean squared error of the optimal model fitted in pretreatment periods

e(r2) R-squared of the optimal model fitted in pretreatment periods

e(att) average treatment effect

Macros

e(panelvar) name of panel variable

e(timevar) name of time variable

e(varlist) names of dependent variable and independent variables

e(depvar) name of the dependent variable

e(indepvars) names of independent variables (covariates)

e(response) name of response

e(predictor) name of predictors

e(unit_all) all units

e(unit_tr) treated unit

e(unit_ctrl) control units

e(time_all) all periods

e(time_tr) treatment period

e(time_pre) pretreatment periods

e(time_post) posttreatment periods

e(ntree) number of trees to grow

e(mtry) number of predictors randomly selected as candidates at each split

e(maxdepth) maximum depth of trees

e(minlsize) minimum number of observations required at each terminal node

e(minssize) minimum number of observations required to split an internal node

e(seed) seed for reproducible results

e(frame) name of frame storing dataset with generated variables in the wide form

Matrices

e(pred) coefficient vector of the optimal model fitted in pretreatment periods

e(eff) variance-covariance matrix of the estimators of the optimal model fitted in pretreatment periods

e(imp) matrix containing variable importance

e(imp_U) matrix containing variable importance aggregated by units

e(imp_V) matrix containing variable importance aggregated by variables

e(pval) matrix containg treatment effects and the p-values of treatment effects in posttreatment periods

Reference

Andersson, J. J. 2019. Carbon taxes and CO2 emissions: Sweden as a case study. American Economic Journal: Economic Policy 11(4): 1-30.

Akinshin, A. 2022. Trimmed Harrell-Davis quantile estimator based on the highest density interval of the given width. Communications in Statistics - Simulation and Computation, DOI: 10.1080/03610918.2022.2050396

Chen, Q., Xiao Z. and Yao Q. 2023, Quantile Control via Random Forest, Journal of Econometrics, revise and resubmit.

Hsiao, C., Steve Ching, H., & Ki Wan, S. 2012.
A panel data approach for program evaluation: measuring the benefits of political and economic integration of Hong Kong with mainland China.
Journal of Applied Econometrics 27(5): 705-740.

Meinshausen, N. 2006. Quantile regression forests. Journal of Machine Learning Research 7(2006): 983-999.

Author

Guanpeng Yan, Shandong University of Finance and Economics, CN

guanpengyan@yeah.net

Qiang Chen (corresponding author), Shandong University, CN

econometrics-stata.com

qiang2chen2@126.com