示例#1
0
def get_table3(df):

    ### regressions:
    rslt = smf.ols(formula="stdgrade ~ treat + pol1+ pol1t",
                   data=df,
                   weights=df["kwgt"]).fit(
                       cov_type='cluster',
                       cov_kwds={'groups': df["studentid"]})
    rslt1 = rslt

    formula2 = "stdgrade ~ treat + treatmentvol + treatmentfor + volcourse + forcourse + pol1 + pol1t + pol1vol + pol1tvol + pol1for + pol1tfor"
    rslt = smf.ols(formula=formula2, data=df, weights=df["kwgt"]).fit(
        cov_type='cluster', cov_kwds={'groups': df["studentid"]})
    rslt2 = rslt

    ### Table stargazer:
    stargazer = Stargazer([rslt1, rslt2])
    stargazer.custom_columns(["column 1", "column 4"], [1, 1])
    stargazer.title("Table 3 - Effects on standardized grades")
    stargazer.show_model_numbers(False)
    stargazer.significant_digits(2)
    stargazer.covariate_order(["treat", "treatmentvol", "treatmentfor"])
    stargazer.rename_covariates({
        "treat":
        "1st-year GPA is below 7",
        "treatmentvol":
        "Attendance is voluntary x treatment",
        "treatmentfor":
        "Absence is penalized x treatment"
    })
    stargazer.show_degrees_of_freedom(False)
    stargazer.add_line('Fixed Effects', ['No', 'No'])

    return stargazer
示例#2
0
reg_df = pd.merge(reg_df, ff_df, left_index=True, right_index=True)

strategy_name_list = list(strategy_ret_df.columns)
results_list = []
for name in strategy_name_list:
    # to have the same name for all variables
    reg_df_tmp = reg_df.rename({name: "ret"}, axis=1)
    results_list.append(
        smf.ols(formula="ret ~ MKT + SMB + HML + CMA + RMW",
                data=reg_df_tmp * 12).fit())

# Outputting short regression results:
stargazer = Stargazer([results_list[0], results_list[3], results_list[6]])
stargazer.custom_columns(['D 30', 'prob 20', 'prob 40'], [1, 1, 1])
stargazer.covariate_order(['Intercept', 'MKT', 'SMB', 'HML', 'RMW', 'CMA'])
stargazer.show_degrees_of_freedom(False)
f = open(
    "/Users/rsigalov/Dropbox/2019_Revision/Writing/Predictive Regressions/tables/disaster_sort_reg_on_ff.tex",
    "w")
f.write(stargazer.render_latex())
f.close()

# Doing extended regression table where I do regressions of strategy return on
# (1) just the market, (2) FF 3 factors and (3) FF 5 factors.
results_list = []
for name in ["D_30", "p_20_30"]:
    # to have the same name for all variables
    reg_df_tmp = reg_df.rename({name: "ret"}, axis=1)
    results_list.append(
        smf.ols(formula="ret ~ MKT", data=reg_df_tmp * 12).fit())
    results_list.append(
示例#3
0
def ols_regression_formatted(data,
                             specifications,
                             as_latex=False,
                             covariates_names=None,
                             covariates_order=None):
    """
    Creates formatted tables for different dependent variables and specifications
    Input:
    data (df): Dataframe containing all necessary variables for OLS regression
    specifications (dictionary): dependent variables as keys and list of specifications
    as values
    as_latex (bool): specify whether Output as table or Latex code
    covariate_names (dict): dictionary with covariate names as in "data" as keys and new
    covariate names as values
    Output:
    list_of_tables (list of stargazer tables): list of formatted tables
    """

    # Create dictionary which connects dependent variables with formatted tables
    dict_regression_tables = {}

    # Generate regressions
    for depvar in specifications.keys():

        regression_list = []
        specification_list = specifications[depvar]
        list_all_covariates = []

        for specification in specification_list:

            estimation_equation = depvar + " ~ " + specification
            regression = smf.ols(data=data, formula=estimation_equation).fit()
            regression_list.append(regression)

            # Create set of all variables for this dependent variable
            list_all_covariates = list(
                set(list_all_covariates +
                    regression.params.index.values.tolist()))

        # Format table with stargazer
        formatted_table = Stargazer(regression_list)

        # No dimension of freedoms and blank dependent variable
        formatted_table.show_degrees_of_freedom(False)
        formatted_table.dependent_variable_name("")

        # Optional: Change order of covariates
        if covariates_order is not None:

            covariates_order_depvar = list(
                OrderedSet(covariates_order).intersection(list_all_covariates))
            list_remaining_covariates = list(
                OrderedSet(list_all_covariates).difference(
                    OrderedSet(covariates_order_depvar)))
            covariates_sorted = list(
                OrderedSet(covariates_order_depvar).union(
                    list_remaining_covariates))
            covariates_sorted.remove("Intercept")
            covariates_sorted = covariates_sorted + ["Intercept"]

            formatted_table.covariate_order(covariates_sorted)

        # Optional: Change name of covariates
        if covariates_names is not None:

            formatted_table.rename_covariates(covariates_names)

        # Add table or latex code to dictionary
        if as_latex is True:

            dict_regression_tables[depvar] = formatted_table.render_latex()

            # Delete tabular environment around it
            dict_regression_tables[depvar] = dict_regression_tables[
                depvar].replace("\\begin{table}[!htbp] \\centering\n", "")
            dict_regression_tables[depvar] = dict_regression_tables[
                depvar].replace("\\end{table}", "")

        else:
            dict_regression_tables[depvar] = formatted_table

    return dict_regression_tables