def main_reg(df):
    # store (exogenous) regressors for first and second stage in a list
    regressors_base = ['const', 'mean_hvac_share', 'mean_unempl', 'munempldiff', 'mskattekrdiff', 'mbefdiff', 'ekbistandpc_diff',
                        'dsmastad', 'dstorstad', 'dsv_maj_1', 'dseatsmp_1', 'dseatsop_1', 'panel8891', 'panel9194', 'flyktingandel']
    exog_base = ['const', 'mean_hvac_share', 'mean_unempl', 'munempldiff', 'mskattekrdiff', 'mbefdiff', 'ekbistandpc_diff', 'dsmastad', 
                    'dstorstad', 'dsv_maj_1', 'dseatsmp_1', 'dseatsop_1', 'panel8891', 'panel9194']

    ### standard errors clustered at municipality level
    # first stage
    reg1 = mt.reg(df, y_name = 'nonOECDshare_diff', x_name = regressors_base, cluster = 'kommun')
    # second stage
    iv1 = mt.ivreg(df, y_name = 'soc_bidr_diff', x_name = 'nonOECDshare_diff', z_name = 'flyktingandel', w_name = exog_base, 
                   iv_method = '2sls', cluster = 'kommun')

    ### standard errors clustered at county level
    # first stage
    reg2 = mt.reg(df, y_name = 'nonOECDshare_diff', x_name = regressors_base, cluster = 'countykod')
    # second stage
    iv2 = mt.ivreg(df, y_name = 'soc_bidr_diff', x_name = 'nonOECDshare_diff', z_name = 'flyktingandel', 
                   w_name = exog_base, iv_method = '2sls', cluster = 'countykod')

    # call function from auxiliary file that creates Latex table fragments for Table 2
    get_table2(df, reg1, iv1, reg2, iv2)
    
    
    # perform F-test for relevance of the instrument
    F_stat1 = reg1.Ftest('flyktingandel')[0]
    F_stat2 = reg2.Ftest('flyktingandel')[0]
    
    return(reg1, iv1, reg2, iv2, F_stat1, F_stat2)
def reg_ivtable7(df, y, var_basic, var_add):
    """
    Parameters:
        df (DataFrame) – Data with any relevant variables.
        y_name (str) – Column name in df of the dependent variable.
        x_name (str or list) – Column name(s) in df of the endogenous regressor(s).
        z_name (str or list) – Column name(s) in df of the excluded instrument(s)
        w_name (str or list) – Column name(s) in df of the included instruments/exogenous regressors
        awt_name (str) – Column name in df to use for analytic weights in regression.
        cluster (str) – Column name in df used to cluster standard errors.
    """
    ydict = {
        "chrswork": "Usual hours | H > 0 ",
        "work50": "P(Hours >= 50)",
        "work60": "P(Hours >= 60)"
    }
    x = ['indep']  # endogenous regressor
    ins = ['ins']  # excluded instrument(s)
    wt = 'perwt'  # Weight
    clt = 'metaready'  # Cluster

    # OLS regression for outcome Y
    formulaOLS = y + ' ~ indep +' + ' + '.join(var_basic[0:])
    OLS = wls_cluster(formulaOLS, df, wt, clt)

    # Intrumental variables basic
    IVb = emt.ivreg(df,
                    y_name=y,
                    x_name=x,
                    z_name=ins,
                    w_name=var_basic,
                    awt_name=wt,
                    cluster=clt,
                    addcons=True)

    #Intrumental variables additonal
    IVa = emt.ivreg(df,
                    y_name=y,
                    x_name=x,
                    z_name=ins,
                    w_name=var_add,
                    awt_name=wt,
                    cluster=clt,
                    addcons=True)

    # Extract regression results
    ivtable7 = pd.DataFrame(np.full((3, 3), ""))
    ivtable7.iloc[0, :] = [
        "{:.3f}".format(OLS.params['indep']),
        "{:.3f}".format(IVb.beta['indep']), "{:.3f}".format(IVa.beta['indep'])
    ]
    ivtable7.iloc[1, :] = [
        "({:.3f})".format(OLS.bse['indep']),
        "({:.3f})".format(IVb.se['indep']), "({:.3f})".format(IVa.se['indep'])
    ]
    ivtable7.iloc[2, :] = ["Basic", "Basic", "Additional"]
    ivtable7.columns = pd.MultiIndex.from_product([[ydict[y]],
                                                   ["OLS", "IV", "IV"]])

    return ivtable7
def placebo_reg(df_placebo):
    # store (exogenous) regressors for first and second stage in a list
    regr = ['const', 'mean_hvac_share', 'mean_unempl', 'munempldiff', 'mskattekrdiff', 'mbefdiff', 'ekbistandpc_diff', 'dsmastad', 
            'dstorstad', 'dsv_maj_1', 'dseatsmp_1', 'dseatsop_1', 'flyktingandel_tplus1', 'flyktingandel_tplus2', 'flyktingandel_tplus3']
    exog = ['const', 'mean_hvac_share', 'mean_unempl', 'munempldiff', 'mskattekrdiff', 'mbefdiff', 'ekbistandpc_diff', 'dsmastad', 
            'dstorstad', 'dsv_maj_1', 'dseatsmp_1', 'dseatsop_1']

    # first stage
    reg_placebo1 = mt.reg(df_placebo, y_name = 'nonOECDshare_diff_tplus1', x_name = regr, cluster = 'kommun')
    reg_placebo2 = mt.reg(df_placebo, y_name = 'nonOECDshare_diff_tplus2', x_name = regr, cluster = 'kommun')
    reg_placebo3 = mt.reg(df_placebo, y_name = 'nonOECDshare_diff_tplus3', x_name = regr, cluster = 'kommun')

    # second stage
    iv_placebo = mt.ivreg(df_placebo, y_name = 'soc_bidr_diff', x_name = ['nonOECDshare_diff_tplus1', 'nonOECDshare_diff_tplus2',
    'nonOECDshare_diff_tplus3'], z_name = ['flyktingandel_tplus1', 'flyktingandel_tplus2', 'flyktingandel_tplus3'], w_name = exog, 
                          iv_method = '2sls', cluster = 'kommun')

    # call function from auxiliary file that creates LaTex table fragments for Table 6
    get_table6(df_placebo, reg_placebo1, reg_placebo2, reg_placebo3, iv_placebo)
    
    
    # perform joint test of the three placebo treatments in the second stage regression
    # return p-value of the test
    pvalue = iv_placebo.Ftest(['nonOECDshare_diff_tplus1', 'nonOECDshare_diff_tplus2', 'nonOECDshare_diff_tplus3'])[1]
    
    return(reg_placebo1, reg_placebo2, reg_placebo3, pvalue)
def reg_tableA1(df, clt, var_basic):
    x = ['indep']  # endogenous regressor(s)
    ins = ['ins']  # excluded instrument(s)
    wt = 'perwt'  # Weight

    #Intrumental variables
    IV_ch = emt.ivreg(df,
                      y_name="chrswork",
                      x_name=x,
                      z_name=ins,
                      w_name=var_basic,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)
    IV_w50 = emt.ivreg(df,
                       y_name="work50",
                       x_name=x,
                       z_name=ins,
                       w_name=var_basic,
                       awt_name=wt,
                       cluster=clt,
                       addcons=True)
    IV_w60 = emt.ivreg(df,
                       y_name="work60",
                       x_name=x,
                       z_name=ins,
                       w_name=var_basic,
                       awt_name=wt,
                       cluster=clt,
                       addcons=True)

    tableA1 = pd.DataFrame(np.full((2, 3), ""))
    tableA1.iloc[0, :] = [
        "{:.3f}".format(IV_ch.beta['indep']),
        "{:.3f}".format(IV_w50.beta['indep']),
        "{:.3f}".format(IV_w60.beta['indep'])
    ]
    tableA1.iloc[1, :] = [
        "({:.3f})".format(IV_ch.se['indep']),
        "({:.3f})".format(IV_w50.se['indep']),
        "({:.3f})".format(IV_w60.se['indep'])
    ]
    tableA1.columns = [
        "Usual hours|H > 0", "P (Hours >= 50)", "P (Hours >= 60)"
    ]

    return tableA1
def reg4iv(year,data,control):
    for i in data["year"]:
        if i == year:
            regiv= mt.ivreg(df=data.loc[(data["year"]==i)],                     # DataFrame to use
            y_name="dNazi_share",                           # Outcome
            x_name="lis_share",
            z_name="ss_nl",
            w_name=control,          # Indep. Variables
            fe_name="wkr",                         # Fixed-effects 
            cluster='wkr')  
            return regiv.beta["lis_share"],regiv.se["lis_share"], regiv.N
def reg_ext(df, y, wt, var_basic):
    x = ['indep', 'indepmarried']  # endogenous regressor(s)
    ins = ['ins', 'insmarried']  # excluded instrument(s)
    clt = 'metaready'  # Cluster

    pndict = {
        'chrswork': "A1.Usual market hours worked/week(census)",
        'weekhswork': "A2. Hours per week spent doing household chores",
        'dum340310c1': "B1. Dummy for expenditures >0",
        'avcost340310': "B2. Level of expenditures(unconditional)"
    }

    # OLS
    formulaOLS = y + ' ~ indep + indepmarried +' + ' + '.join(var_basic[0:])
    OLS = wls_cluster(formulaOLS, df, wt, clt)

    #Intrumental variables
    IV = emt.ivreg(df,
                   y_name=y,
                   x_name=x,
                   z_name=ins,
                   w_name=var_basic,
                   awt_name=wt,
                   cluster=clt,
                   addcons=True)

    # Regression results
    ext = pd.DataFrame(np.full((4, 2), ""))
    ext.iloc[0, :] = [
        "{:.3f}".format(OLS.params['indep']), "{:.3f}".format(IV.beta['indep'])
    ]
    ext.iloc[1, :] = [
        "({:.3f})".format(OLS.bse['indep']), "({:.3f})".format(IV.se['indep'])
    ]
    ext.iloc[2, :] = [
        "{:.3f}".format(OLS.params['indepmarried']),
        "{:.3f}".format(IV.beta['indepmarried'])
    ]
    ext.iloc[3, :] = [
        "({:.3f})".format(OLS.bse['indepmarried']),
        "({:.3f})".format(IV.se['indepmarried'])
    ]

    ext.index = [
        'ln((LS Imm. + LS Nat.)/LF)', "",
        'ln((LS Imm. + LS Nat.)/LF) x married', ""
    ]
    ext.columns = pd.MultiIndex.from_product([[pndict[y]], ["OLS", " IV "]])
    return ext
def sens1_reg(df, df_sens1):
    # run regression without dummy for large-sized municipal population as we excluded big city counties
    # otherwise all values of that variable are zero and we cannot invert the regressor matrix
    regressors_sens1 = ['const', 'mean_hvac_share', 'mean_unempl', 'munempldiff', 'mskattekrdiff', 'mbefdiff', 'ekbistandpc_diff', 'dsmastad', 
                        'dsv_maj_1', 'dseatsmp_1', 'dseatsop_1', 'panel8891', 'panel9194', 'flyktingandel']
    exog_sens1 = ['const', 'mean_hvac_share', 'mean_unempl', 'munempldiff', 'mskattekrdiff', 'mbefdiff', 'ekbistandpc_diff', 'dsmastad', 
                  'dsv_maj_1', 'dseatsmp_1', 'dseatsop_1', 'panel8891', 'panel9194']

    # first stage
    reg_sens1 = mt.reg(df_sens1, y_name = 'nonOECDshare_diff', x_name = regressors_sens1, cluster = 'kommun')
    # second stage
    iv_sens1 = mt.ivreg(df_sens1, y_name = 'soc_bidr_diff', x_name = 'nonOECDshare_diff', z_name = 'flyktingandel', 
                        w_name = exog_sens1, iv_method = '2sls', cluster = 'kommun')

    # call function from auxiliary file that creates LaTex table fragments for Table 7
    get_table7(df, reg_sens1, iv_sens1)
    
    return(reg_sens1, iv_sens1)
    def IV_regression(variable):

        if variable == 'Income < p15':
            df['regressor'] = df['Income < p15']
            df['interaction_first'] = df['Refugee inflow * (y < p15)']
            df['interaction_second'] = df['DeltaIM * (y < p15)']
        elif variable == 'Income < p40':
            df['regressor'] = df['Income < p40']
            df['interaction_first'] = df['Refugee inflow * (y < p40)']
            df['interaction_second'] = df['DeltaIM * (y < p40)']
        elif variable == 'Income > p85':
            df['regressor'] = df['Income > p85']
            df['interaction_first'] = df['Refugee inflow * (y > p85)']
            df['interaction_second'] = df['DeltaIM * (y > p85)']
        elif variable == 'Wealth < p40':
            df['regressor'] = df['Wealth < p40']
            df['interaction_first'] = df['Refugee inflow * (w < p40)']
            df['interaction_second'] = df['DeltaIM * (w < p40)']
        elif variable == 'Wealth < p60':
            df['regressor'] = df['Wealth < p60']
            df['interaction_first'] = df['Refugee inflow * (w < p60)']
            df['interaction_second'] = df['DeltaIM * (w < p60)']
        elif variable == 'Wealth > p85':
            df['regressor'] = df['Wealth > p85']
            df['interaction_first'] = df['Refugee inflow * (w > p85)']
            df['interaction_second'] = df['DeltaIM * (w > p85)']
        elif variable == 'tjman_t_1':
            df['regressor'] = df['tjman_t_1']
            df['interaction_first'] = df['Refugee inflow * white-collar']
            df['interaction_second'] = df['DeltaIM * white-collar']
        elif variable == 'arbetare_t_1':
            df['regressor'] = df['arbetare_t_1']
            df['interaction_first'] = df['Refugee inflow * blue-collar']
            df['interaction_second'] = df['DeltaIM * blue-collar']

        exog = ['const', 'mean_hvac_share', 'mean_unempl', 'munempldiff', 'mskattekrdiff', 'mbefdiff', 'ekbistandpc_diff', 'dsmastad', 
                'dstorstad', 'dsv_maj_1', 'dseatsmp_1', 'dseatsop_1', 'panel8891', 'panel9194', 'regressor']
        endog = ['nonOECDshare_diff', 'interaction_second']
        instr = ['flyktingandel', 'interaction_first']
        second_stage = mt.ivreg(df, y_name = 'soc_bidr_diff', x_name = endog, z_name = instr, w_name = exog, iv_method = '2sls',
                                cluster = 'kommun')
        return second_stage
def sens2_reg(df, df_sens2):
    # run regression without dummy for panel period 1991/94 as we excluded the observations of that panel
    # otherwise all values of that variable are zero and we cannot invert the regressor matrix
    regressors_sens2 = ['const', 'mean_hvac_share', 'mean_unempl', 'munempldiff', 'mskattekrdiff', 'mbefdiff', 'ekbistandpc_diff', 
                        'dsmastad', 'dstorstad', 'dsv_maj_1', 'dseatsmp_1', 'dseatsop_1', 'panel8891', 'flyktingandel']
    exog_sens2 = ['const', 'mean_hvac_share', 'mean_unempl', 'munempldiff', 'mskattekrdiff', 'mbefdiff', 'ekbistandpc_diff', 'dsmastad', 
                  'dstorstad', 'dsv_maj_1', 'dseatsmp_1', 'dseatsop_1', 'panel8891']

    # first stage
    reg_sens2 = mt.reg(df_sens2, y_name = 'nonOECDshare_diff', x_name = regressors_sens2, cluster = 'kommun')
    # second stage
    iv_sens2 = mt.ivreg(df_sens2, y_name = 'soc_bidr_diff', x_name = 'nonOECDshare_diff', z_name = 'flyktingandel', w_name = exog_sens2, 
                        iv_method = '2sls', cluster = 'kommun')

    # call function from auxiliary file that creates LaTex table fragments for Table 8
    get_table8(df, reg_sens2, iv_sens2)
    
    
    # perform F-test for relevance of the instrument
    F_stat = reg_sens2.Ftest('flyktingandel')[0]

    return(reg_sens2, iv_sens2, F_stat)
def reg_tableA3(df, y, wt, var_basic):
    x_coll = ['indep', 'indepCollegeplus']  # endogenous regressor(s)
    x_grad = ['indep', 'indepGraduate']
    ins_coll = ['ins', 'insCollegeplus']  # excluded instrument(s)
    ins_grad = ['ins', 'insGraduate']
    var_coll = ['collegeplus'] + var_basic  # exogenous regressor(s)
    var_grad = ['graduate'] + var_basic
    clt = 'metaready'  # Cluster

    pndict = {
        'uhrswork': "A1.Usual market hours worked/week(census)",
        'weekhswork': "A2. Hours per week spent doing household chores",
        'dum340310c1': "B1. Dummy for expenditures >0",
        'avcost340310': "B2. Level of expenditures(unconditional)"
    }

    # OLS
    formulaOLS = y + ' ~ indep + indepCollegeplus + collegeplus +' + ' + '.join(
        var_basic[0:])
    OLS = wls_cluster(formulaOLS, df, wt, clt)

    #Intrumental variables
    IV_coll = emt.ivreg(df,
                        y_name=y,
                        x_name=x_coll,
                        z_name=ins_coll,
                        w_name=var_coll,
                        awt_name=wt,
                        cluster=clt,
                        addcons=True)

    IV_grad = emt.ivreg(df,
                        y_name=y,
                        x_name=x_grad,
                        z_name=ins_grad,
                        w_name=var_grad,
                        awt_name=wt,
                        cluster=clt,
                        addcons=True)

    # Regression results
    tableA3 = pd.DataFrame(np.full((6, 3), ""))
    tableA3.iloc[0, :] = [
        "{:.3f}".format(OLS.params['indep']),
        "{:.3f}".format(IV_coll.beta['indep']),
        "{:.3f}".format(IV_grad.beta['indep'])
    ]
    tableA3.iloc[1, :] = [
        "({:.3f})".format(OLS.bse['indep']),
        "({:.3f})".format(IV_coll.se['indep']),
        "({:.3f})".format(IV_grad.se['indep'])
    ]
    tableA3.iloc[2, 0:2] = [
        "{:.3f}".format(OLS.params['indepCollegeplus']),
        "{:.3f}".format(IV_coll.beta['indepCollegeplus'])
    ]
    tableA3.iloc[3, 0:2] = [
        "({:.3f})".format(OLS.bse['indepCollegeplus']),
        "({:.3f})".format(IV_coll.se['indepCollegeplus'])
    ]
    tableA3.iloc[4, 2] = "{:.3f}".format(IV_grad.beta['indepGraduate'])
    tableA3.iloc[5, 2] = "({:.3f})".format(IV_grad.se['indepGraduate'])

    tableA3.index = [
        'ln((LS Imm. + LS Nat.)/LF)', "",
        'ln((LS Imm. + LS Nat.)/LF)xCollege or more', "",
        'ln((LS Imm. + LS Nat.)/LF)xGraduate education ', ""
    ]
    tableA3.columns = pd.MultiIndex.from_product([[pndict[y]],
                                                  ["OLS", " IV ", "IV"]])

    return tableA3
def reg_tableA2(df, var_basic, edu):
    idict = {
        "grad": "Graduate education",
        'Advanced': "Professionals and PhDs",
        'Master': "Master’s degree ",
        'College': "College graduates",
        'SomeCollege': "Some college ",
        "Highschool": "HS grad less "
    }
    x = ['indep']  # endogenous regressor(s)
    ins = ['ins']  # excluded instrument(s)
    wt = 'perwt'  # Weight
    clt = 'metaready'

    IV_uh = emt.ivreg(df,
                      y_name="uhrswork",
                      x_name=x,
                      z_name=ins,
                      w_name=var_basic,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)
    IV_lf = emt.ivreg(df,
                      y_name="lflw",
                      x_name=x,
                      z_name=ins,
                      w_name=var_basic,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)
    IV_ch = emt.ivreg(df,
                      y_name="chrswork",
                      x_name=x,
                      z_name=ins,
                      w_name=var_basic,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)
    IV_w50 = emt.ivreg(df,
                       y_name="work50",
                       x_name=x,
                       z_name=ins,
                       w_name=var_basic,
                       awt_name=wt,
                       cluster=clt,
                       addcons=True)
    IV_w60 = emt.ivreg(df,
                       y_name="work60",
                       x_name=x,
                       z_name=ins,
                       w_name=var_basic,
                       awt_name=wt,
                       cluster=clt,
                       addcons=True)

    tableA2 = pd.DataFrame(np.full((2, 5), ""))
    tableA2.iloc[0, :] = [
        "{:.3f}".format(IV_uh.beta['indep']),
        "{:.3f}".format(IV_lf.beta['indep']),
        "{:.3f}".format(IV_ch.beta['indep']),
        "{:.3f}".format(IV_w50.beta['indep']),
        "{:.3f}".format(IV_w60.beta['indep'])
    ]
    tableA2.iloc[1, :] = [
        "({:.3f})".format(IV_uh.se['indep']),
        "({:.3f})".format(IV_lf.se['indep']),
        "({:.3f})".format(IV_ch.se['indep']),
        "({:.3f})".format(IV_w50.se['indep']),
        "({:.3f})".format(IV_w60.se['indep'])
    ]
    tableA2.columns = [
        "Usual hours", " LFP", "Usual hours|H > 0", "P (Hours >= 50)",
        "P (Hours >= 60)"
    ]
    tableA2.index = [idict[edu], ""]

    return tableA2
def reg_ivtable10(df, panel, var_add):
    x = ['indep', 'indepFemale']  # endogenous regressor(s)
    ins = ['ins', 'insFemale']  # excluded instrument(s)
    wt = 'perwt'  # Weight
    clt = 'metaready'  # Cluster

    pdict = {
        "p90100": "90 - 100",
        "p75100": "75 - 100",
        "p5075": " 50 - 75",
        "p2550": " 25 - 50",
        "p025": "0 - 25"
    }

    #Intrumental variables additional
    # Usual hours|H>0
    IVch = emt.ivreg(df,
                     y_name='chrswork',
                     x_name=x,
                     z_name=ins,
                     w_name=var_add,
                     awt_name=wt,
                     cluster=clt,
                     addcons=True)
    #log(Usual hours|H>0)
    IVlh = emt.ivreg(df,
                     y_name="lhrswork",
                     x_name=x,
                     z_name=ins,
                     w_name=var_add,
                     awt_name=wt,
                     cluster=clt,
                     addcons=True)

    #log(wage)
    IVlw = emt.ivreg(df,
                     y_name="lwage",
                     x_name=x,
                     z_name=ins,
                     w_name=var_add,
                     awt_name=wt,
                     cluster=clt,
                     addcons=True)

    # Extract regression results
    ivtable10 = pd.DataFrame(np.full((2, 6), ""))
    ivtable10.iloc[:, 0] = [
        "{:.3f}".format(IVch.beta['indep']),
        "({:.3f})".format(IVch.se['indep'])
    ]
    ivtable10.iloc[:, 1] = [
        "{:.3f}".format(IVch.beta['indepFemale']),
        "({:.3f})".format(IVch.se['indepFemale'])
    ]
    ivtable10.iloc[:, 2] = [
        "{:.3f}".format(IVlh.beta['indep']),
        "({:.3f})".format(IVlh.se['indep'])
    ]
    ivtable10.iloc[:, 3] = [
        "{:.3f}".format(IVlh.beta['indepFemale']),
        "({:.3f})".format(IVlh.se['indepFemale'])
    ]
    ivtable10.iloc[:, 4] = [
        "{:.3f}".format(IVlw.beta['indep']),
        "({:.3f})".format(IVlw.se['indep'])
    ]
    ivtable10.iloc[:, 5] = [
        "{:.3f}".format(IVlw.beta['indepFemale']),
        "({:.3f})".format(IVlw.se['indepFemale'])
    ]

    ivtable10.columns = pd.MultiIndex.from_product(
        [["Usual hours|H>0 ", "log(Usual hours|H>0)", "log(Wage)"],
         ["Ln(LS Skilled)", "Ln(LS Skilled)xFemale"]])
    ivtable10.index = [pdict[panel], ""]
    return ivtable10
def reg_table9(df, panel, var_add):
    pdict = {
        "p90100": "90 - 100",
        "p75100": "75 - 100",
        "p5075": " 50 - 75",
        "p2550": " 25 - 50",
        "p025": "0 - 25"
    }
    x = ['indep', 'indepchild5']  # endogenous regressor(s)
    ins = ['ins', 'inschild5']  # excluded instrument(s)
    wt = 'perwt'  # Weight
    clt = 'metaready'  # Cluster

    #Intrumental variables additonal

    #work50: P (Hours >= 50)
    IVw50 = emt.ivreg(df,
                      y_name='work50',
                      x_name=x,
                      z_name=ins,
                      w_name=var_add,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)

    #work60: P (Hours >= 60)
    IVw60 = emt.ivreg(df,
                      y_name='work60',
                      x_name=x,
                      z_name=ins,
                      w_name=var_add,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)

    #chrswork: Usual hours|H > 0
    IVchr = emt.ivreg(df,
                      y_name='chrswork',
                      x_name=x,
                      z_name=ins,
                      w_name=var_add,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)

    # Extract regression results
    ivtable9 = pd.DataFrame(np.full((2, 6), ""))
    ivtable9.iloc[:, 0] = [
        "{:.3f}".format(IVchr.beta['indep']),
        "({:.3f})".format(IVchr.se['indep'])
    ]
    ivtable9.iloc[:, 1] = [
        "{:.3f}".format(IVchr.beta['indepchild5']),
        "({:.3f})".format(IVchr.se['indepchild5'])
    ]
    ivtable9.iloc[:, 2] = [
        "{:.3f}".format(IVw50.beta['indep']),
        "({:.3f})".format(IVw50.se['indep'])
    ]
    ivtable9.iloc[:, 3] = [
        "{:.3f}".format(IVw50.beta['indepchild5']),
        "({:.3f})".format(IVw50.se['indepchild5'])
    ]
    ivtable9.iloc[:, 4] = [
        "{:.3f}".format(IVw60.beta['indep']),
        "({:.3f})".format(IVw60.se['indep'])
    ]
    ivtable9.iloc[:, 5] = [
        "{:.3f}".format(IVw60.beta['indepchild5']),
        "({:.3f})".format(IVw60.se['indepchild5'])
    ]

    ivtable9.columns = pd.MultiIndex.from_product(
        [["Usual hours|H>0 ", "P(Hours>= 50)", "P(Hours>= 60)"],
         ["Ln(LS Skilled)", "Ln(LS Skilled)x child 0-5"]])
    ivtable9.index = [pdict[panel], ""]

    return ivtable9
def reg8(df, var_add, panel, ydict):
    x = ['indep']  # endogenous regressor
    ins = ['ins']  # excluded instrument(s)
    wt = 'perwt'  # Weight
    clt = 'metaready'  # Cluster

    #Intrumental variables additonal
    #uhrswork: Usual hours|week
    IVuhr = emt.ivreg(df,
                      y_name='uhrswork',
                      x_name=x,
                      z_name=ins,
                      w_name=var_add,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)

    #lflw:Labor Force Participation
    IVlfp = emt.ivreg(df,
                      y_name='lflw',
                      x_name=x,
                      z_name=ins,
                      w_name=var_add,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)

    #work50: P (Hours>= 50)
    IVw50 = emt.ivreg(df,
                      y_name='work50',
                      x_name=x,
                      z_name=ins,
                      w_name=var_add,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)

    #work60: P (Hours>= 60)
    IVw60 = emt.ivreg(df,
                      y_name='work60',
                      x_name=x,
                      z_name=ins,
                      w_name=var_add,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)

    #chrswork:Usual hours|H >0
    IVchr = emt.ivreg(df,
                      y_name='chrswork',
                      x_name=x,
                      z_name=ins,
                      w_name=var_add,
                      awt_name=wt,
                      cluster=clt,
                      addcons=True)

    ivtable8 = pd.DataFrame(np.full((2, 5), ""))
    ivtable8.iloc[:, 0] = [
        "{:.3f}".format(IVuhr.beta['indep']),
        "({:.3f})".format(IVuhr.se['indep'])
    ]
    ivtable8.iloc[:, 1] = [
        "{:.3f}".format(IVlfp.beta['indep']),
        "({:.3f})".format(IVlfp.se['indep'])
    ]
    ivtable8.iloc[:, 2] = [
        "{:.3f}".format(IVchr.beta['indep']),
        "({:.3f})".format(IVchr.se['indep'])
    ]
    ivtable8.iloc[:, 3] = [
        "{:.3f}".format(IVw50.beta['indep']),
        "({:.3f})".format(IVw50.se['indep'])
    ]
    ivtable8.iloc[:, 4] = [
        "{:.3f}".format(IVw60.beta['indep']),
        "({:.3f})".format(IVw60.se['indep'])
    ]
    ivtable8.index = [ydict[panel], ""]
    ivtable8.columns = [
        "Usual hours per week ", "LFP", "Usual hours|H>0 ", "P (Hours>=50)",
        "P (Hours>=60)"
    ]

    return ivtable8