Package: econtools
econtoolsecontoolsis a Python package of econometric functions and convenient
shortcuts for data work withpandasandnumpy. Full documentationhere.InstallationYou can install directly from PYPI:$pipinstallecontoolsOr you can clone from Github and install directly.$gitclonehttp://github.com/dmsul/econtools
$cdecontools
$pythonsetup.pyinstallEconometricsOLS, 2SLS, LIMLOption to absorb any variable via within-transformation (a laaregin Stata)Robust standard errorsHAC (robust/hc1,hc2,hc3)Clustered standard errorsSpatial HAC (SHAC, aka Conley standard errors) with uniform and triangle
kernelsF-tests by variable name orRmatrix.Local linear regression.WARNING [31 Oct 2019]: Predicted values (yhat and residuals) may not be as
expected in transformed regressions (when using fixed effects or using
weights). That is, the current behavior is different from Stata. I am looking
into this and will post a either a fix or a justification of current behavior
in the near future.importecontoolsimportecontools.metricsasmt# Read Stata DTA filedf=econtools.read('my_data.dta')# Estimate OLS regression with fixed-effects and clustered s.e.'sresult=mt.reg(df,# DataFrame to use'y',# Outcome['x1','x2'],# Indep. Variablesfe_name='person_id',# Fixed-effects using variable 'person_id'cluster='state'# Cluster by state)# Resultsprint(result.summary)# Print regression resultsbeta_x1=result.beta['x1']# Get coefficient by variable namer_squared=result.r2a# Get adjusted R-squaredjoint_F=result.Ftest(['x1','x2'])# Test for joint significanceequality_F=result.Ftest(['x1','x2'],equal=True)# Test for coeff. equalityRegression and Summary Stat Tablesoutregtakes regression results and creates a LaTeX-formatted tabular
fragment.table_statrowcan be used to add arbitrary statistics, notes, etc. to a
table. Can also be used to create a table of summary statistics.write_notesmakes it easy to save table notes that depend on your data.Misc. Data Manipulation Toolsstata_mergewrapspandas.mergeand adds a lot of Stata's merge niceties
like a'_m'flag for successfully merge observations.group_idgenerates an ID based on the variables past (compareegen group).Crosswalks of commonly used U.S. state labels.State abbreviation to state name (and reverse).State fips to state name (and reverse).Data I/Oreadandwrite: Use the passed file path's extension to determine whichpandasI/O method to use. Useful for writing functions that
programmatically read DataFrames from disk which are saved in different
formats. See examples above and below.load_or_build: A function decorator that caches datasets to disk.
This function builds the requested dataset and saves it to disk if it
doesn't already exist on disk. If the dataset is already saved, it simply
loads it, saving computational time and allowing the use of a single function
to both load and build data.fromecontoolsimportload_or_build,read@load_or_build('my_data_file.dta')defbuild_my_data_file():"""Cleans raw data from CSV format and saves as Stata DTA."""df=read('raw_data.csv')# Clean the DataFramereturndfFile type  is automatically detected from the passed filename. In this case,
Stata DTA frommy_data_file.dta.save_cli: Simple wrapper forargparsethat let's you use a--saveflag
on the command line. This lets you run a regression without over-writing the
previous results and without modifying the code in any way (i.e., commenting
out the "save" lines).In your regression script:fromecontoolsimportsave_clidefregression_table(save=False):""" Run a regression and save output if `save == True`.  """# Regression gutsif__name__=='__main__':save=save_cli()regression_table(save=save)In the command line/bash script:pythonrun_regression.py# Runs regression without saving outputpythonrun_regression.py--save# Runs regression and saves outputRequirementsPython 3.6+Pandas and its dependencies (Numpy, etc.)Scipy and its dependenciesPytables (optional, if you use HDF5 files)PyTest (optional, if you want to run the tests)