Package: localprojections
localprojectionsThis module implements the local projections models for single entity time series, and panel / longitudinal data settings, due to Jorda (2005), and based on codes availablehere.Installationpip install localprojectionsImplementationPanel Local Projections ModelDocumentationlocalprojections.PanelLP(data,Y,response,horizon,lags,varcov,ci_width)Parametersdata :Pandas MultiIndex dataframe with entity as the outer index, and time as the inner index.Y :List of column names indatato be used in the model estimationresponse :List of column names inYto be used as response variables when estimating the impulse response functions (IRFs)horizon :Integer indicating the estimation horizon of the IRFslags :Integer indicating the number of lags to be included in the model estimationvarcov :Variance-covariance estimator to be used in estimating standard errors; refer to thelinearmodels package.ci_width :Float higher than 0 and less than 1, i.e., (0, 1), indicating the width of the confidence intervals of the IRFs;ci_width=0.95indicates a 95% confidence intervalOutputThis function returns a pandas dataframe of 6 columns:Shockindicates the shock variableResponseindicates the response variableHorizonindicates the response horizon of the IRFMeanindicates the point estimate of the IRFLBindicates the lower bound of the confidence interval of the IRFLBindicates the upper bound of the confidence interval of the IRFFor instance, the estimates of the 6-period ahead IRF of y from a shock in x, can be found in the row withShock=x,Response=y, andHorizon=6Examplefromstatsmodels.datasetsimportgrunfeldimportlocalprojectionsaslpdf=grunfeld.load_pandas().data# import the Grunfeld investment data setdf=df.set_index(['firm','year'])# set entity-year indices (as per requirements in bashtage's linearmodels)endog=['invest','value','capital']# cholesky ordering: invest --> value --> capitalresponse=endog.copy()# estimate the responses of all variables to shocks from all variablesirf_horizon=8# estimate IRFs up to 8 periods aheadopt_lags=2# include 2 lags in the local projections modelopt_cov='robust'# HAC standard errorsopt_ci=0.95# 95% confidence intervalsirf=lp.PanelLP(data=df,# input dataframeY=endog,# variables in the modelresponse=response,# variables whose IRFs should be estimatedhorizon=irf_horizon,# estimation horizon of IRFslags=opt_lags,# lags in the modelvarcov=opt_cov,# type of standard errorsci_width=opt_ci# width of confidence band)irfplot=lp.IRFPlot(irf=irf,# take output from the estimated modelresponse=['invest'],# plot only response of invest ...shock=endog,# ... to shocks from all variablesn_columns=2,# max 2 columns in the figuren_rows=2,# max 2 rows in the figuremaintitle='Panel LP: IRFs of Investment',# self-defined title of the IRF plotshow_fig=True,# display figure (from plotly)save_pic=False# don't save any figures on local drive)Panel Local Projections Model with Exogenous Variables (Panel LPX)Documentationlocalprojections.PanelLPX(data,Y,X,response,horizon,lags,varcov,ci_width)Parametersdata :Pandas MultiIndex dataframe with entity as the outer index, and time as the inner index.Y :List of column names indatato be used in the model estimation as endogenous variablesX :List of column names indatato be used in the model estimation as exogenous variablesresponse :List of column names inYto be used as response variables when estimating the impulse response functions (IRFs)horizon :Integer indicating the estimation horizon of the IRFslags :Integer indicating the number of lags to be included in the model estimationvarcov :Variance-covariance estimator to be used in estimating standard errors; refer to thelinearmodels package.ci_width :Float higher than 0 and less than 1, i.e., (0, 1), indicating the width of the confidence intervals of the IRFs;ci_width=0.95indicates a 95% confidence intervalOutputThis function returns a pandas dataframe of 6 columns:Shockindicates the shock variableResponseindicates the response variableHorizonindicates the response horizon of the IRFMeanindicates the point estimate of the IRFLBindicates the lower bound of the confidence interval of the IRFLBindicates the upper bound of the confidence interval of the IRFFor instance, the estimates of the 6-period ahead IRF of y from a shock in x, can be found in the row withShock=x,Response=y, andHorizon=6Examplefromstatsmodels.datasetsimportgrunfeldimportlocalprojectionsaslpdf=grunfeld.load_pandas().data# import the Grunfeld investment data setdf=df.set_index(['firm','year'])# set entity-year indices (as per requirements in bashtage's linearmodels)endog=['invest','value','capital']# cholesky ordering: invest --> value --> capitalresponse=endog.copy()# estimate the responses of all variables to shocks from all variablesirf_horizon=8# estimate IRFs up to 8 periods aheadopt_lags=2# include 2 lags in the local projections modelopt_cov='robust'# HAC standard errorsopt_ci=0.95# 95% confidence intervalsirf=lp.PanelLP(data=df,# input dataframeY=endog,# variables in the modelresponse=response,# variables whose IRFs should be estimatedhorizon=irf_horizon,# estimation horizon of IRFslags=opt_lags,# lags in the modelvarcov=opt_cov,# type of standard errorsci_width=opt_ci# width of confidence band)irfplot=lp.IRFPlot(irf=irf,# take output from the estimated modelresponse=['invest'],# plot only response of invest ...shock=endog,# ... to shocks from all variablesn_columns=2,# max 2 columns in the figuren_rows=2,# max 2 rows in the figuremaintitle='Panel LP: IRFs of Investment',# self-defined title of the IRF plotshow_fig=True,# display figure (from plotly)save_pic=False# don't save any figures on local drive)Panel Local Projections Model with Exogenous Variables (Panel LPX)Documentationlocalprojections.ThresholdPanelLPX(data,Y,X,threshold_var,response,horizon,lags,varcov,ci_width)Parametersdata :Pandas MultiIndex dataframe with entity as the outer index, and time as the inner index.Y :List of column names indatato be used in the model estimation as endogenous variablesX :List of column names indatato be used in the model estimation as exogenous variablesiablesthreshold_var :String indicating column indatato be used as the threshold variable; must take values 0 or 1 for technically correct implementationresponse :List of column names inYto be used as response variables when estimating the impulse response functions (IRFs)horizon :Integer indicating the estimation horizon of the IRFslags :Integer indicating the number of lags to be included in the model estimationvarcov :Variance-covariance estimator to be used in estimating standard errors; refer to thelinearmodels package.ci_width :Float higher than 0 and less than 1, i.e., (0, 1), indicating the width of the confidence intervals of the IRFs;ci_width=0.95indicates a 95% confidence intervalOutputThis function returnstwopandas dataframes of 6 columns each, with the first output corresponding to whenthreshold_vartakes value 1, and the second when ```threshold_var`` takes value 0:Shockindicates the shock variableResponseindicates the response variableHorizonindicates the response horizon of the IRFMeanindicates the point estimate of the IRFLBindicates the lower bound of the confidence interval of the IRFLBindicates the upper bound of the confidence interval of the IRFFor instance, the estimates of the 6-period ahead IRF of y from a shock in x, can be found in the row withShock=x,Response=y, andHorizon=6Single Entity Time Series Local Projections ModelDocumentationlocalprojections.TimeSeriesLP(data,Y,response,horizon,lags,newey_lags,ci_width)Parametersdata :Pandas dataframeY :List of column names indatato be used in the model estimationresponse :List of column names inYto be used as response variables when estimating the impulse response functions (IRFs)horizon :Integer indicating the estimation horizon of the IRFslags :Integer indicating the number of lags to be included in the model estimationnewey_lags :Maximum number of lags to be used when estimating the Newey-West standard errorsci_width :Float higher than 0 and less than 1, i.e., (0, 1), indicating the width of the confidence intervals of the IRFs;ci_width=0.95indicates a 95% confidence intervalOutputThis function also returns a pandas dataframe of 6 columns:Shockindicates the shock variableResponseindicates the response variableHorizonindicates the response horizon of the IRFMeanindicates the point estimate of the IRFLBindicates the lower bound of the confidence interval of the IRFLBindicates the upper bound of the confidence interval of the IRFFor instance, the estimates of the 6-period ahead IRF of y from a shock in x, can be found in the row withShock=x,Response=y, andHorizon=6Examplefromstatsmodels.datasetsimportgrunfeldimportlocalprojectionsaslpdf=grunfeld.load_pandas().data# import the Grunfeld investment data setdf=df[df['firm']=='General Motors']# keep only one entity (as an example of a single entity time series setting)df=df.set_index(['year'])# set time variable as indexendog=['invest','value','capital']# cholesky ordering: invest --> value --> capitalresponse=endog.copy()# estimate the responses of all variables to shocks from all variablesirf_horizon=8# estimate IRFs up to 8 periods aheadopt_lags=2# include 2 lags in the local projections modelopt_cov='robust'# HAC standard errorsopt_ci=0.95# 95% confidence intervals# Use TimeSeriesLP for the single entity caseirf=lp.TimeSeriesLP(data=df,# input dataframeY=endog,# variables in the modelresponse=response,# variables whose IRFs should be estimatedhorizon=irf_horizon,# estimation horizon of IRFslags=opt_lags,# lags in the modelnewey_lags=2,# maximum lags when estimating Newey-West standard errorsci_width=opt_ci# width of confidence band)irfplot=lp.IRFPlot(irf=irf,# take output from the estimated modelresponse=['invest'],# plot only response of invest ...shock=endog,# ... to shocks from all variablesn_columns=2,# max 2 columns in the figuren_rows=2,# max 2 rows in the figuremaintitle='Single Entity Time Series LP: IRFs of Investment',# self-defined title of the IRF plotshow_fig=True,# display figure (from plotly)save_pic=False# don't save any figures on local drive)Plotting Impulse Response FunctionsDocumentationlocalprojections.IRFPlot(irf,response,shock,n_columns,n_rows,maintitle,show_fig,save_pic,out_path,out_name,annot_size,font_size)Parametersirf :Output fromPanelLP(), orTimeSeriesLP()response :List of variables contained inirf'sResponsecolumn whose IRFs is to be plottedshock :List of variables contained inirf'sShockcolumn whose IRFs is to be plottedn_columns :Integer indicating the number of IRF figures per row in the overall figuren_rows :Integer indicating the number of IRF figures per column in the overall figuremaintitle :Strings to be used as the title of the overall figure; default is''Local Projections Model: Impulse Response Functions'show_fig :Boolean indicating whether to render the overall figuresave_pic :Boolean indicating whether to save the overall figure in the local directory; ifTrue, ahtmlfile and apngfile will be savedout_path :Strings indicating the directory at which the overall figure should be saved in; only used ifsave_picisTrueout_name :Strings indicating the name of the file in which the overall figure should be saved as; only used ifsave_picisTrue, and default isIRFPlotannot_size :Integer indicating the font size of titles of each subplot in the figure; defaults to 6font_size :Integer indicating the font size of the title, and axes labels; defaults to 9OutputThis function returns aplotly graph objects figurewithn_columns(columns) xn_rows(rows) subplots. Depending on arguments passed, the figure may be rendered during implementation and / or saved in the local directory.ExamplesSee above.RequirementsPython Packagespandas>=1.4.3numpy>=1.23.0linearmodels>=4.27plotly>=5.9.0statsmodels>=0.13.2