Package ArCo. November 5, 2017

Similar documents
Package ConvergenceClubs

Package cointreg. August 29, 2016

Package datasets.load

Package sure. September 19, 2017

Package spark. July 21, 2017

Package estprod. May 2, 2018

Package cattonum. R topics documented: May 2, Type Package Version Title Encode Categorical Features

Package CausalImpact

Package modmarg. R topics documented:

Package TVsMiss. April 5, 2018

Package PSTR. September 25, 2017

Package PCADSC. April 19, 2017

Package dyncorr. R topics documented: December 10, Title Dynamic Correlation Package Version Date

Package fastdummies. January 8, 2018

Package ECctmc. May 1, 2018

Package rpst. June 6, 2017

Package TANDEM. R topics documented: June 15, Type Package

Package lcc. November 16, 2018

Package repec. August 31, 2018

Package vinereg. August 10, 2018

Package DTRreg. August 30, 2017

Package pcr. November 20, 2017

Package MIICD. May 27, 2017

Package IATScore. January 10, 2018

Package milr. June 8, 2017

Package robets. March 6, Type Package

Package fcr. March 13, 2018

Package naivebayes. R topics documented: January 3, Type Package. Title High Performance Implementation of the Naive Bayes Algorithm

Package raker. October 10, 2017

Package CALIBERrfimpute

Package gtrendsr. October 19, 2017

Package glmnetutils. August 1, 2017

Package gggenes. R topics documented: November 7, Title Draw Gene Arrow Maps in 'ggplot2' Version 0.3.2

Package MOST. November 9, 2017

Package ClusterBootstrap

Package intccr. September 12, 2017

Package sigqc. June 13, 2018

Package quickreg. R topics documented:

Package texteffect. November 27, 2017

Package bisect. April 16, 2018

Package CorporaCoCo. R topics documented: November 23, 2017

Package gtrendsr. August 4, 2018

Package CuCubes. December 9, 2016

Package rereg. May 30, 2018

Package gppm. July 5, 2018

Package ggimage. R topics documented: November 1, Title Use Image in 'ggplot2' Version 0.0.7

Package vip. June 15, 2018

Package mfbvar. December 28, Type Package Title Mixed-Frequency Bayesian VAR Models Version Date

Package blandr. July 29, 2017

Package survclip. November 22, 2017

Package FWDselect. December 19, 2015

Package dbx. July 5, 2018

Package simr. April 30, 2018

Package addhaz. September 26, 2018

Package palmtree. January 16, 2018

Package crochet. January 8, 2018

Package interplot. R topics documented: June 30, 2018

Package PTE. October 10, 2017

Package reghelper. April 8, 2017

Package rmi. R topics documented: August 2, Title Mutual Information Estimators Version Author Isaac Michaud [cre, aut]

Package comparedf. February 11, 2019

Package farver. November 20, 2018

Package areaplot. October 18, 2017

Package ggimage. R topics documented: December 5, Title Use Image in 'ggplot2' Version 0.1.0

Package sfadv. June 19, 2017

Package DTRlearn. April 6, 2018

Package docxtools. July 6, 2018

Package caretensemble

Package OLScurve. August 29, 2016

Package svalues. July 15, 2018

Package quadprogxt. February 4, 2018

Package messaging. May 27, 2018

Package condir. R topics documented: February 15, 2017

Package MatchIt. April 18, 2017

Package pampe. R topics documented: November 7, 2015

Package BiDimRegression

Package slickr. March 6, 2018

Package frequencyconnectedness

Package BigVAR. R topics documented: April 3, Type Package

Package Mondrian. R topics documented: March 4, Type Package

Package sfc. August 29, 2016

Package nprotreg. October 14, 2018

Package kirby21.base

Package queuecomputer

Package svmpath. R topics documented: August 30, Title The SVM Path Algorithm Date Version Author Trevor Hastie

Package gains. September 12, 2017

Package crossword.r. January 19, 2018

Package weco. May 4, 2018

Package orthodr. R topics documented: March 26, Type Package

Package citools. October 20, 2018

Package ibm. August 29, 2016

Package projector. February 27, 2018

Package OptimaRegion

Package fbroc. August 29, 2016

Package autocogs. September 22, Title Automatic Cognostic Summaries Version 0.1.1

Package nonlinearicp

Package endogenous. October 29, 2016

Package geojsonsf. R topics documented: January 11, Type Package Title GeoJSON to Simple Feature Converter Version 1.3.

Package SEMrushR. November 3, 2018

Package pwrab. R topics documented: June 6, Type Package Title Power Analysis for AB Testing Version 0.1.0

Transcription:

Title Artificial Counterfactual Package Version 0.3-1 Date 2017-11-05 Package ArCo November 5, 2017 Maintainer Gabriel F. R. Vasconcelos <gabrielrvsc@yahoo.com.br> BugReports https://github.com/gabrielrvsc/arco/issues Set of functions to analyse and estimate Artificial Counterfactual models from Carvalho, Masini and Medeiros (2016) <DOI:10.2139/ssrn.2823687>. License MIT + file LICENSE Encoding UTF-8 LazyData TRUE RoxygenNote 6.0.1 Imports Matrix, glmnet, boot NeedsCompilation no Author Yuri R. Fonseca [aut], Ricardo Masini [aut], Marcelo C. Medeiros [aut], Gabriel F. R. Vasconcelos [aut, cre] Repository CRAN Date/Publication 2017-11-05 12:29:56 UTC R topics documented: data.q1............................................ 2 data.q2............................................ 2 estimate_t0......................................... 3 fitarco........................................... 5 inflationnfp......................................... 8 panel_to_arco_list..................................... 8 plot.fitarco......................................... 9 Index 11 1

2 data.q2 data.q1 A generated dataset used in the examples Format This data contains 100 observations of 20 variables generated using the dgp from Carvalho, Masini and Medeiros (2016). Each variable is one ArCo unit. The intervention took place on the first unit at t0=51 by adding a constant equal to 0.628, which is one standard deviation of the treated unit variable before the intervention. data(data.q1) A matrix with 100 rows and 20 variables. References Carvalho, C., Masini, R., Medeiros, M. (2016) "ArCo: An Artificial Counterfactual Approach For High-Dimensional Panel Time-Series Data.". data.q2 A dataset used in the examples Format This data is a list with two matrixes, each have 100 observations and 6 variables. It was generatedthe using the dgp from Carvalho, Masini and Medeiros (2016). In the ArCo context, each matrix is a variable and each variable in the matrixes is an unit. The intervention took place on the first unit at t0=51 by adding constants of 0.840 and 0.511 (one standar deviation before the intervention) on variables (matrixes) 1 and 2. data(data.q2) A list with 2 matrixes of 100 rows and 6 variables. References Carvalho, C., Masini, R., Medeiros, M. (2016) "ArCo: An Artificial Counterfactual Approach For High-Dimensional Panel Time-Series Data.".

estimate_t0 3 estimate_t0 Estimates the intervention time on a given treated unit Estimates the intervention time on a given treated unit based on any model supplied by the user. estimate_t0(data, fn = NULL, p.fn = NULL, start = 0.4, end = 0.9, treated.unit = 1, lag = 0, Xreg = NULL,...) Arguments data fn p.fn start end treated.unit lag Xreg Details A list of matrixes or data frames of length q. Each matrix is T X n and it contains observations of a single variable for all units and all periods of time. Even in the case of a single variable (q=1), the matrix must be inside a list. The function used to estimate the first stage model. This function must receive only two arguments in the following order: X (independent variables), y (dependent variable). If the model requires additional arguments they must be supplied inside the function fn. If not supplied the default is the lm function. The forecasting function used to estimate the counterfactual using the first stage model (normally a predict funtion). This function also must receive only two arguments in the following order: model (model estimated in the first stage), newdata (out of sample data to estimate the second stage). If the prediction requires additional arguments they must be supplied inside the function p.fn. Initial value of λ 0 to be tested. Final value of λ 0 to be tested. Single number indicating the unit where the intervention took place. Number of lags in the first stage model. Default is 0, i.e. only contemporaneous variables are used. Exogenous controls.... Additional arguments used in the function fn. This description may be useful to clarify the notation and understand how the arguments must be supplied to the functions. units: Each unit is indexed by a number between 1,..., n. They are for exemple: countries, states, municipalities, firms, etc. Variables: For each unit and for every time period t = 1,..., T we observe q i 1 variables. They are for example: GDP, inflation, sales, etc. Intervention: The intervention took place only in the treated unit at time t 0 = λ 0 T, where λ 0 is in (0,1).

4 estimate_t0 Value A list with the following items: t0 Estimated t0. delta.norm The norm of the delta corresponding to t0. call The matched call. See Also fitarco Examples ## === Example for q=1 === ## data(data.q1) # = First unit was treated on t=51 by adding # a constant equal to one standard deviation. data=list(data.q1) # = Even if q=1 the data must be in a list ## == Fitting the ArCo using linear regression == ## # = creating fn and p.fn function = # fn=function(x,y){ return(lm(y~x)) } p.fn=function(model,newdata){ b=coef(model) return(cbind(1,newdata)%*%b) } t0a=estimate_t0(data = data,fn = fn, p.fn = p.fn, treated.unit = 1 ) ## === Example for q=2 === ## # = First unit was treated on t=51 by adding constants of one standard deviation. # for the first and second variables data(data.q2) # data is already a list t0b=estimate_t0(data = data.q2,fn = fn, p.fn = p.fn, treated.unit = 1, start=0.4)

fitarco 5 fitarco Estimates the ArCo using the model selected by the user Estimates the Artificial Counterfactual unsing any model supplied by the user, calculates the most relevant statistics and allows for the counterfactual confidence intervals to be estimated by block bootstrap. The model must be supplied by the user through the arguments fn and p.fn. The first determines which function will be used to estimate the model and the second determines the forecasting function. For more details see the examples and the description on the arguments. fitarco(data, fn = NULL, p.fn = NULL, treated.unit, t0, lag = 0, Xreg = NULL, alpha = 0.05, boot.cf = FALSE, R = 100, l = 3, VCOV.type = c("iid", "var", "nw", "varhac"), VCOV.lag = 1, bandwidth.kernel = NULL, kernel.type = c("quadraticspectral", "Truncated", "Bartlett", "Parzen", "TukeyHanning"), VHAC.max.lag = 5, prewhitening.kernel = FALSE,...) Arguments data fn p.fn treated.unit t0 lag Xreg alpha boot.cf R A list of matrixes or data frames of length q. Each matrix is T X n and it contains observations of a single variable for all units and all periods of time. Even in the case of a single variable (q=1), the matrix must be inside a list. The function used to estimate the first stage model. This function must receive only two arguments in the following order: X (independent variables), y (dependent variable). If the model requires additional arguments they must be supplied inside the function fn. If not supplied the default is the lm function. The forecasting function used to estimate the counterfactual using the first stage model (normally a predict funtion). This function also must receive only two arguments in the following order: model (model estimated in the first stage), newdata (out of sample data to estimate the second stage). If the prediction requires additional arguments they must be supplied inside the function p.fn. Single number indicating the unit where the intervention took place. Single number indicating the intervention period. Number of lags in the first stage model. Default is 0, i.e. only contemporaneous variables are used. Exogenous controls. Significance level for the delta confidence bands. Should bootstrap confidence intervals for the counterfactual be calculated (default=false). Number of bootstrap replications in case boot.cf=true.

6 fitarco l Block length for the block bootstrap. VCOV.type Type of covariance matrix for the delta. "iid" for standard covariance matrix, "var" or "varhac" to use prewhitened covariance matrix using VAR models, "varhac" selects the order of the VAR automaticaly and "nw" for Newey West. In the last case the user may select the kernel type and combine the kernel with the VAR prewhitening. For more details see Andrews and Monahan (1992). VCOV.lag Lag used on the robust covariance matrix if VCOV.type is different from "iid". bandwidth.kernel Kernel bandwidth. If NULL the bandwidth is automatically calculated. kernel.type Kernel to be used for VCOV.type="nw". VHAC.max.lag Maximum lag of the VAR in case VCOV.type="varhac". prewhitening.kernel If TRUE and VCOV.type="nw", the covariance matrix is calculated with prewhitening (default=false).... Additional arguments used in the function fn. Details Value This description may be useful to clarify the notation and understand how the arguments must be supplied to the functions. units: Each unit is indexed by a number between 1,..., n. They are for exemple: countries, states, municipalities, firms, etc. Variables: For each unit and for every time period t = 1,..., T we observe q i 1 variables. They are for example: GDP, inflation, sales, etc. Intervention: The intervention took place only in the treated unit at time t 0 = λ 0 T, where λ 0 is in (0,1). An object with S3 class fitarco. cf fitted.values model delta p.value data t0 treated.unit omega residuals boot.cf call estimated counterfactual In sample fitted values for the pre-treatment period. A list with q estimated models, one for each variable. Each element in the list is the output of the fn function. The delta statistics and its confidence interval. ArCo p-value. The data used. The intervention period used. The treated unit used. Residual standard deviation. model residuals. A list with the bootstrap result (boot.cf=true) or logical FALSE (boot.cf=false). In the first case, each element in the list refers to one bootstrap replication of the counterfactual, i. e. the list length is R. The matched call.

fitarco 7 References Carvalho, C., Masini, R., Medeiros, M. (2016) "ArCo: An Artificial Counterfactual Approach For High-Dimensional Panel Time-Series Data.". Andrews, D. W., & Monahan, J. C. (1992). An improved heteroskedasticity and autocorrelation consistent covariance matrix estimator. Econometrica: Journal of the Econometric Society, 953-966. See Also plot, estimate_t0, panel_to_arco_list Examples ## === Example for q=1 === ## data(data.q1) # = First unit was treated on t=51 by adding # a constant equal to one standard deviation data=list(data.q1) # = Even if q=1 the data must be in a list ## == Fitting the ArCo using linear regression == ## # = creating fn and p.fn function = # fn=function(x,y){ return(lm(y~x)) } p.fn=function(model,newdata){ b=coef(model) return(cbind(1,newdata) %*% b) } ArCo=fitArCo(data = data,fn = fn, p.fn = p.fn, treated.unit = 1, t0 = 51) ## === Example for q=2 === ## # = First unit was treated on t=51 by adding constants of one standard deviation # for the first and second variables data(data.q2) # data is already a list ## == Fitting the ArCo using the package glmnet == ## ## == Quadratic Spectral kernel weights for two lags == ## ## == Fitting the ArCo using the package glmnet == ## ## == Bartlett kernel weights for two lags == ## require(glmnet) set.seed(123) ArCo2=fitArCo(data = data.q2,fn = cv.glmnet, p.fn = predict,treated.unit = 1, t0 = 51,

8 panel_to_arco_list VCOV.type = "nw",kernel.type = "QuadraticSpectral",VCOV.lag = 2) inflationnfp Dataset used on the empirical example by Carvalho, Masini and Medeiros (2016). This is the data from the nota fiscal paulista (NFP) example from Carvalho, Masini and Medeiros (2016). The variables are the food away from home component of the inflation and the GDP for 9 metropolitan areas in Brazil. Each variable is represented by a matrix inside the list. The treated unit is the Sao Paulo metropolitan area, which is the first column in each matrix. The treatment took place at t 0 = 34. data(inflationnfp) Format A list with two matrixes of 56 rows and 9 variables. References Carvalho, C., Masini, R., Medeiros, M. (2016) "ArCo: An Artificial Counterfactual Approach For High-Dimensional Panel Time-Series Data.". panel_to_arco_list Transforms a balanced panel into a list of matrices compatible with the fitarco function Transforms a balanced panel into a list of matrices compatible with the fitarco function. The user must identify the columns with the time, the unit identifier and the variables. panel_to_arco_list(panel, time, unit, variables) Arguments panel time unit variables Balanced panel in a data.frame with columns for units and time. Name or index of the time column. Name or index of the unit column. Names or indexes of the columns containing the variables.

plot.fitarco 9 See Also fitarco Examples # = Generate a small panel as example = # set.seed(123) time=sort(rep(1:100,2)) unit=rep(c("u1","u2"),100) v1=rnorm(200) v2=rnorm(200) panel=data.frame(time=time,unit=unit,v1=v1,v2=v2) head(panel) data=panel_to_arco_list(panel,time="time",unit="unit",variables = c("v1","v2")) head(data$v1) plot.fitarco Plots realized values and the counterfactual estimated by the fitarco function Plots realized values and the counterfactual estimated by the fitarco function. The plotted variables will be on the same level as supplied to the fitarco function. ## S3 method for class 'fitarco' plot(x, ylab = NULL, main = NULL, plot = NULL, ncol = 1, display.fitted = FALSE, y.min = NULL, y.max = NULL, confidence.bands = FALSE, alpha = 0.05,...) Arguments x ylab main plot ncol An ArCo object estimated using the fitarco function. n dimensional character vector, where n is the length of the plot argument or n=q if plot=null. n dimensional character vector, where n is the length of the plot argument or n=q if plot=null. n dimensional numeric vector where each element represents an ArCo unit. If NULL, all units will be plotted. If, for example, plot=c(1,2,5) only units 1 2 and 5 will be plotted according to the order specified by the user on the fitarco. Number of columns when multiple plots are displayed. display.fitted If TRUE the fitted values of the first step estimation are also plotted (default=false).

10 plot.fitarco y.min n dimensional numeric vector defining the lower bound for the y axis. n is the length of the plot argument or n=q if plot=null y.max n dimensional numeric vector defining the upper bound for the y axis. n is the length of the plot argument or n=q if plot=null confidence.bands TRUE to plot the counterfactual confidence bands (default=false). If the ArCo was estimated without bootstrap this argument will be forced to FALSE. alpha See Also Significance level for the confidence bands.... Other graphical parameters to plot. fitarco Examples ################# ## === Example based on the q=1 fitarco === ## ################# # = First unit was treated on t=51 by adding # a constant equal to one standard deviation data(data.q1) data=list(data.q1) # = Even if q=1 the data must be in a list ## == Fitting the ArCo using linear regression == ## # = creating fn and p.fn function = # fn=function(x,y){ return(lm(y~x)) } p.fn=function(model,newdata){ b=coef(model) return(cbind(1,newdata) %*% b)} ArCo=fitArCo(data = data,fn = fn, p.fn = p.fn, treated.unit = 1, t0 = 51) plot(arco)

Index Topic ArCo fitarco, 5 Topic datasets data.q1, 2 data.q2, 2 inflationnfp, 8 data.q1, 2 data.q2, 2 estimate_t0, 3, 7 fitarco, 4, 5, 9, 10 inflationnfp, 8 panel_to_arco_list, 7, 8 plot, 7 plot.fitarco, 9 11