cluster_cols_var = 'msamd' FE_cols_vars = ['fips', 'cert'] how = 'fips, cert' # Set File names ## OLS file = 'Results/lpm_results_{}.{}' #------------------------------------------------------------ # Run benchmark Model #------------------------------------------------------------ # Loop over all years for year in range(2004, 2019 + 1): # Set df df = dd_main[dd_main.date == year].compute(scheduler='threads') df.reset_index(drop=True, inplace=True) # Run results_benchmark = MultiDimensionalOLS().fit(df[y], df[x],\ cov_type = cov_type, cluster_cols = df[cluster_cols_var],\ transform_data = True, FE_cols = df[FE_cols_vars], how = how) # Transform to pandas df df_results_benchmark = results_benchmark.to_dataframe() # Save to excel and csv df_results_benchmark.to_excel(file.format(year, 'xlsx')) df_results_benchmark.to_csv(file.format(year, 'csv')) del df, results_benchmark #, ols_benchmark
# Perform 2SLS #------------------------------------------------------------ #------------------------------------------------------------ #------------------------------------------------------------ # FULL SAMPLE #------------------------------------------------------------ # First Stage ## Compute first stage results results_fs = MultiDimensionalOLS().fit(df_trans[x_endo], df_trans[[z] + x_exo],\ cov_type = cov_type, cluster_cols = df_trans[cluster_cols_var]) ## Transform to df df_results_fs = results_fs.to_dataframe() ''' TURN ON IF MULTIPLE INSTRUMENTS ## Perform partial f-test ### First calculate results without instrument results_fs_noinstr = MultiDimensionalOLS().fit(df_trans[x_endo], df_trans[x_exo],\ cov_type = cov_type, cluster_cols = df_trans[cluster_cols_var]) ### Do f-test f_stat = ((results_fs_noinstr.rss - results_fs.rss) / 1) / results_fs.mse_resid ''' ## Save to csv df_results_fs.to_csv(file.format('fs', 'csv')) ## Calculate LS hat and append to df df_trans['ls_hat'] = results_fs.fittedvalues
else: FE_cols = FE_cols_vars h = how # Run Model if counter == 4: # pooled OLS results = MultiDimensionalOLS().fit(data[y], data[x], cov_type = cov_type, cluster_cols = data[cluster_cols_var]) else: results = MultiDimensionalOLS().fit(data[y], data[x],\ cov_type = cov_type, cluster_cols = data[cluster_cols_var],\ transform_data = True, FE_cols = data[FE_cols], how = h) if counter == 2: results_split = copy.deepcopy(results) # Transform results to pd.DataFrame df_results = results.to_dataframe() # Add count for msamd en cert df_results['msamd'] = msamd df_results['cert'] = cert # Save to csv df_results.to_csv(file) # Add one to counter counter += 1 # Test the three different loan sale variables in model 2 # Joint Wald test of b1 = b2 and b2 = b3 R = pd.DataFrame([[1, -1, 0],[0, 1, -1]]) h_beta = R @ pd.DataFrame(results_split.params[:3])
# Set File names file_local = 'Results/ratespread_results_local.{}' file_local_year = 'Results/ratespread_results_local_{}.{}' #------------------------------------------------------------ # Run Model #------------------------------------------------------------ # Run ## Local results_local = MultiDimensionalOLS().fit(df[y], df[x_local],\ cov_type = cov_type, cluster_cols = df[cluster_cols_var],\ transform_data = True, FE_cols = df[FE_cols_vars], how = how) ### Transform to pandas df df_results_local = results_local.to_dataframe() ### Save to excel and csv df_results_local.to_excel(file_local.format('xlsx')) df_results_local.to_csv(file_local.format('csv')) del results_local ## 2018 and 2019 separate for year in range(2018, 2019 + 1): # Set df df_year = df[df.date == year] # Run results_yearly = MultiDimensionalOLS().fit(df_year[y], df_year[x_local],\ cov_type = cov_type, cluster_cols = df_year[cluster_cols_var],\
file2 = 'Robustness_checks/Distance_robust_remote.{}' file3 = 'Robustness_checks/Distance_robust_lssplit.{}' file4 = 'Robustness_checks/Distance_robust_lsever.{}' file5 = 'Robustness_checks/Distance_robust_loancosts.{}' #------------------------------------------------------------ # Run Model #------------------------------------------------------------ # Run for y, x, file in zip(y_lst, [x0, x0, x1, x2], [file1, file2, file3, file4]): results = MultiDimensionalOLS().fit(df_trans[y], df_trans[x],\ cov_type = cov_type, cluster_cols = df_trans[cluster_cols_var]) ### Transform to pandas df df_results = results.to_dataframe() ## Do wald test for file3 if file == file3: R = pd.DataFrame([[1, -1, 0], [0, 1, -1]]) h_beta = R @ pd.DataFrame(results.params[:3]) C = results.nobs * results.cov.iloc[:3, :3] test_stat = results.nobs * h_beta.T @ np.linalg.inv( R @ C @ R.T) @ h_beta ## F test pval_wald = stats.chi2.sf(test_stat, R.shape[0]) df_results['wald_stat'] = test_stat.iloc[0, 0] df_results['wald_pval'] = float(pval_wald)
# Set other parameters cov_type = 'clustered' cluster_cols_var = 'msamd' FE_cols_vars = ['fips', 'cert'] how = 'fips, cert' # Set File names file1 = 'Robustness_checks/Ratespread_robust_distance.{}' file2 = 'Robustness_checks/Ratespread_robust_cdd.{}' file3 = 'Robustness_checks/Ratespread_robust_lsever.{}' #------------------------------------------------------------ # Run Model #------------------------------------------------------------ # Run for x, file in zip([x1, x2, x3], [file1, file2, file3]): results_local = MultiDimensionalOLS().fit(df[y], df[x],\ cov_type = cov_type, cluster_cols = df[cluster_cols_var],\ transform_data = True, FE_cols = df[FE_cols_vars], how = how) ### Transform to pandas df df_results_local = results_local.to_dataframe() ### Save to excel and csv df_results_local.to_excel(file.format('xlsx')) df_results_local.to_csv(file.format('csv')) del results_local
file_re_msatcert_int_res = 'Results/Results_re_msatcert_int_res.{}' file_re_msatcert_lsint_res = 'Results/Results_re_msatcert_lsint_res{}' #------------------------------------------------------------ # 1) Pooled OLS -- loan sales + controls -- Full sample #------------------------------------------------------------ # Run results_ols_ls_full = MultiDimensionalOLS().fit( df_full[y_var], df_full[x_ls_var + ['intercept']], cov_type=cov_type, cluster_cols=df_full[cluster_cols_var]) # Transform to pandas df df_results_ols_ls_full = results_ols_ls_full.to_dataframe() # Add count for msamd en cert df_results_ols_ls_full['msamd'] = msamd_full df_results_ols_ls_full['cert'] = cert_full # Save to excel and csv df_results_ols_ls_full.to_excel(file_ols_ls_full.format('xlsx')) df_results_ols_ls_full.to_csv(file_ols_ls_full.format('csv')) #------------------------------------------------------------ # 2) Pooled OLS -- loan sales + controls -- Reduced sample #------------------------------------------------------------ # Run results_ols_ls_res = MultiDimensionalOLS().fit(