Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • Ana Diaz
    Participant
    Post count: 1

    I am trying to create my own program to do a mean difference tests controlling for stratification variables, as iebaltab is not supported in asdocx I created the following program but I am having problems to include brackets for standard error and to include stars :
    this is my program:

    
    capture program drop balance_fe
    program define balance_fe
    version 15.1
    
    syntax varlist [if] [in], Doc(name)
    asdocx setfile, save(`doc')
    flexmat reset
    flexmat addrow, data(\i, T=0 , \i, \i, T=1, \i,\i,\i) row(1) col(1)
    flexmat addrow, data(Var, T , C , Diff, T , C , Diff) row(2) col(1)
    flexmat addrow, data(\i, (mean) , (mean) , \i , (mean) , (mean) , \i) row(3) col(1)
    
    local i = 4
    local j = 5
    * Loop through each variable and run the regression separately for entrada == 0 and entrada == 1
    foreach var of varlist `varlist' {
    
    * Regression for entrada == 0
    reg `var' tratados PcD_siempre_req_ayuda i.localidad_cuidador if encuesta == 0
    
    * Capture the coefficient for TRATADOS (this is the adjusted difference)
    local coef_0 = _b[tratados]
    local se_0 = _se[tratados]
    local p0 =2*ttail(e(df_r),abs(`coef_0' / `se_0'))
    
    if `p0'<=0.01 {
    local star0 "***"
    }
    else if `p0'<=0.05{
    local star0 "**"
    }
    else if `p0'<=0.1{
    local star0 "*"
    }
    else {
    local star0 " "
    }
    local star0 `star0'
    
    * Calculate the mean for treatment and control groups from the regression for entrada == 0
    local intercept_0 = _b[_cons]
    local mean_treated_0 = `intercept_0' + `coef_0'
    local mean_control_0 = `intercept_0'
    
    * Regression for entrada == 1
    reg `var' tratados PcD_siempre_req_ayuda i.localidad_cuidador if encuesta == 1
    
    * Capture the coefficient for TRATADOS (this is the adjusted difference)
    local coef_1 = _b[tratados]
    local se_1 = _se[tratados]
    local p1 =2*ttail(e(df_r),abs(`coef_1' / `se_1'))
    
    if `p1'<=0.01 {
    local star1 "***"
    }
    else if `p1'<=0.05{
    local star1 "**"
    }
    else if `p1'<=0.1{
    local star1 "*"
    }
    else {
    local star1 " "
    }
    local star1 `star1'
    
    * Calculate the mean for treatment and control groups from the regression for entrada == 1
    local intercept_1 = _b[_cons]
    local mean_treated_1 = `intercept_1' + `coef_1'
    local mean_control_1 = `intercept_1'
    
    * Create a table with asdoc, combining results for entrada == 0 and entrada == 1
    flexmat addrow, data(`var', `mean_treated_0', `mean_control_0', `coef_0' `star0', `mean_treated_1', `mean_control_1', `coef_1' \i `star1') row(`i') col(1) dec(3)
    flexmat addrow, data(\i, \i, \i, `se_0', \i, \i, `se_1' ) row(`j') col(1) dec(3)
    
    local i = `i'+2
    local j = `j'+2
    }
    asdocx export
    end
    
    global attributes_cuid female_cuidador
    balance_fe $attributes_cuid, doc(opinion)
    
    And this is the outcome:
    
    Table: Results
    T=0 T=1
    Var T C Diff T C Diff
    (mean) (mean) (mean) (mean)
    female_cuidador 0.873 0.924 -0.050 0.867 0.927 -0.060
    [ .0452214174207373 ] [ .048581842896113 ]
    age_lt_30_cuidador 0.057 0.049 0.008 0.044 0.014 0.029
    [ .0426844928298504 ] [ .0415409747837886 ]
    age_30_50_cuidador 0.241 0.179 0.062 0.239 0.237 0.002
    [ .073076540587936 ] [ .0742507558857683 ]
    age_50_60_cuidador 0.312 0.508 -.1956913921969597 ** 0.240 0.453 -.2129306381240828 **
    [ .0814750225770947 ] [ .0833018715024365 ]
    age_gt_60_cuidador 0.390 0.264 0.126 0.477 0.296 .1817895469990658 **
    [ .0835857734074484 ] [ .0860492248712231 ]
    priorizada 0.440 0.132 .3073178094970312 *** 0.465 0.140 .3254840236746013 ***
    [ .0655266549007595 ] [ .0681533091773399 ]
    cuidadora_discapacidad 0.170 0.058 .1120905381892791 ** 0.225 0.086 .1395610387216294 ***
    [ .0454970747702299 ] [ .0502376831545808 ]
    mujer 0.873 0.924 -0.050 0.867 0.927 -0.060
    [ .0452214174207373 ] [ .048581842896113 ]
    edad_cuidadora 54.589 54.070 0.518 55.759 54.635 1.124
    [ 2.16929560964669 ] [ 2.189432474744678 ]
    n_pers_hogar 4.147 3.828 0.319 2.329 1.403 .9260936001084251 ***
    [ .3021497679431961 ] [ .2216258246935809 ]
    n_menores_14_hogar 0.659 0.430 0.229 0.419 0.041 .3778422609978668 *
    [ .1418399116962401 ] [ .2197291495056808 ]
    n_mayores_60_hogar 1.128 0.861 .2667670745044388 * 0.043 0.148 -.1057040886296859 ***
    [ .1401457791987369 ] [ .0313695565069849 ]
    n_pcd_hogar 1.521 1.281 .2404322106105557 ** 1.737 1.054 .6829594017094021 ***
    [ .0947067888919658 ] [ .2181724798049691 ]
    total_pers_cuidadas 1.926 1.575 .3512428920031583 * 1.593 0.560 1.033627705018304 ***
    [ .1799038148979991 ] [ .1191785744184205 ]
    cuida_pers_fh 0.224 0.294 -0.071 0.593 0.593 0
    [ .0749677895012531 ] [ 0 ]
    se1 0.127 0.143 -0.016 0.184 0.154 0.030
    [ .0584293965100804 ] [ .0608237943052473 ]
    se2 0.857 0.662 .1949894161611641 ** 0.818 0.644 .1733109590273359 **
    [ .076221864108779 ] [ .0802719781862108 ]
    se3 0.016 0.195 -.1787278004923131 *** -0.002 0.201 -.2033394441448079 ***
    [ .0547931987304561 ] [ .0569456792879725 ]
    num_integrantes 4.211 4.143 0.068 4.102 4.044 0.058
    [ .2777245732340444 ] [ .2860700800367504 ]
    num_pcd 1.469 1.268 .2008300262031517 ** 1.462 1.256 .2054844805290875 **
    [ .0926290532980009 ] [ .0828875011581481 ]
    num_pcd_mayores_18 1.512 1.276 .235442078250814 ** 1.544 1.306 .2382187474605085 **
    [ .1091369904053218 ] [ .1079032213275395 ]
    cuidadora_lee_escribe 0.954 0.969 -0.015 0.954 0.969 -0.016
    [ .0195141030886512 ] [ .0211065311070289 ]
    escolaridad_culminada 0.491 0.453 0.038 0.470 0.453 0.017
    [ .0860886017385185 ] [ .0894893653640295 ]
    conflicto_armado 0.117 0.095 0.022 0.101 0.088 0.013
    [ .0586480380858076 ] [ .0596551089641583 ]
    sisben 0.741 0.685 0.056 0.750 0.694 0.056
    [ .0574786859417874 ] [ .0598015466824094 ]
    afiliada_salud 1.011 0.968 .0427163763712066 ** 1.011 0.967 .0438500961794923 **
    [ .0193704046519501 ] [ .0209437802491101 ]
    cuidadora_discapacidad 0.170 0.058 .1120905381892791 ** 0.225 0.086 .1395610387216294 ***
    [ .0454970747702299 ] [ .0502376831545808 ]
    apoyo_otro 0.494 0.486 0.008 0.483 0.442 0.041
    [ .08189650174139 ] [ .0862232580621543 ]
    cuidadora_sostiene 0.350 0.297 0.053 0.284 0.290 -0.006
    [ .0855307692328213 ] [ .0890051940349824 ]
    ayuda_actividades 0.780 0.865 -0.085 0.763 0.850 -0.087
    [ .0833482695040922 ] [ .0868920467799025 ]
    live_in_antonio -0.000 -0.000 -0.000 0.000 0.000 -0.000
    [ 0 ] [ 0 ]
    live_in_rafael -0.000 -0.000 0.000 -0.000 -0.000 -0.000
    [ 0 ] [ 0 ]
    live_in_sancristobal 1.000 1.000 0.000 1.000 1.000 0.000
    [ 0 ] [ 0 ]
    live_in_tunjuelito 0.000 0.000 0.000 0.000 0.000 -0.000
    [ 0 ] [ 0 ]
    live_in_usme -0.000 -0.000 0.000 -0.000 -0.000 0.000
    [ 0 ] [ 0 ]
    
    Attaullah Shah
    Moderator
    Post count: 80

    Hello Ana Diaz,
    You have mentioned a problem with decimal points in the subject, but in the text, you have mentioned that you have problems with brackets and stars.
    In the following code, I have processed the macro to format the values as %9.3f. flexmat can format single values, but if the values involve text or symbols, you have to format the value for decimal points before combining it with other items, such as *, -, or +. I have also shown how to add brackets and *.

    
    capture program drop balance_fe
    program define balance_fe
    version 15.1
    
    syntax varlist [if] [in], Doc(name)
    asdocx setfile, save(`doc')
    flexmat reset
    flexmat addrow, data(\i, T=0 , \i, \i, T=1, \i,\i,\i) row(1) col(1)
    flexmat addrow, data(Var, T , C , Diff, T , C , Diff) row(2) col(1)
    flexmat addrow, data(\i, (mean) , (mean) , \i , (mean) , (mean) , \i) row(3) col(1)
    
    local i = 4
    local j = 5
    * Loop through each variable and run the regression separately for entrada == 0 and entrada == 1
    foreach var of varlist `varlist' {
    
    * Regression for entrada == 0
    reg `var' tratados PcD_siempre_req_ayuda i.localidad_cuidador if encuesta == 0
    
    * Capture the coefficient for TRATADOS (this is the adjusted difference)
    local coef_0 = _b[tratados]
    
    local se_0 = _se[tratados]
    * decimal points
    loc coef_0 : dis %9.3f = `coef_0'
    loc se_0 : dis %9.3f = `se_0'
    local p0 =2*ttail(e(df_r),abs(`coef_0' / `se_0'))
    
    if `p0'<=0.01 {
    local star0 "***"
    }
    else if `p0'<=0.05{
    local star0 "**"
    }
    else if `p0'<=0.1{
    local star0 "*"
    }
    else {
    local star0 " "
    }
    local star0 `star0'
    
    * Calculate the mean for treatment and control groups from the regression for entrada == 0
    local intercept_0 = _b[_cons]
    local mean_treated_0 = `intercept_0' + `coef_0'
    local mean_control_0 = `intercept_0'
    
    * decimal points
    loc intercept_0 : dis %9.3f = `intercept_0'
    loc mean_treated_0 : dis %9.3f = `mean_treated_0'
    loc mean_control_0 : dis %9.3f = `mean_control_0'
    
    * Regression for entrada == 1
    reg `var' tratados PcD_siempre_req_ayuda i.localidad_cuidador if encuesta == 1
    
    * Capture the coefficient for TRATADOS (this is the adjusted difference)
    local coef_1 = _b[tratados]
    local se_1 = _se[tratados]
    local p1 =2*ttail(e(df_r),abs(`coef_1' / `se_1'))
    
    * decimal points
    loc coef_1 : dis %9.3f = `coef_1'
    loc se_1 : dis %9.3f = `se_1'
    loc p1 : dis %9.4f = `p1'
    
    if `p1'<=0.01 {
    local star1 "***"
    }
    else if `p1'<=0.05{
    local star1 "**"
    }
    else if `p1'<=0.1{
    local star1 "*"
    }
    else {
    local star1 " "
    }
    local star1 `star1'
    
    * Calculate the mean for treatment and control groups from the regression for entrada == 1
    local intercept_1 = _b[_cons]
    local mean_treated_1 = `intercept_1' + `coef_1'
    local mean_control_1 = `intercept_1'
    
    * decimal points
    loc intercept_1 : dis %9.3f = `intercept_1'
    loc mean_treated_1 : dis %9.3f = `mean_treated_1'
    loc mean_control_1 : dis %9.3f = `mean_control_1'
    
    * Create a table with asdoc, combining results for entrada == 0 and entrada == 1
    flexmat addrow, data(`var', `mean_treated_0', `mean_control_0', `coef_0' `star0', `mean_treated_1', `mean_control_1', `coef_1' \i `star1') row(`i') col(1) dec(3)
    flexmat addrow, data(\i, \i, \i, [`se_0''], \i, \i, [`se_1'] ) row(`j') col(1) dec(3)
    
    local i = `i'+2
    local j = `j'+2
    }
    asdocx export
    end
    
    global attributes_cuid female_cuidador
    balance_fe $attributes_cuid, doc(opinion)
    
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.