Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • coefplot - creating a vertical coefficient plot with multiple lines across the entire x-axis

    Dear Stata users,

    I'm having a problem creating a coefficient plot with Stata that produces vertical lines across the entire x-axis.

    The code works fine, but it somehow clusters my coefplot-lines to the left of the x-axis, leaving most of the axis unused.

    It is mainly this line of code I am concerned with, which uses some data produced in the loop at the bottom of my text.

    Code:
    coefplot `all_stores',  keep(sibling_1_female) saving(COEFPLOT_ALL_SEPARATELY_`sex', replace) ytitle("") xlabel(`xlabels') xscale(range(1(1)6)) ylabel(-3(1)3) yscale(range(`lower_bound' `upper_bound')) scheme(s2mono) graphregion(color(white)) vertical yline(0) legend(off)        //create one coefficient plot for each dataset
    And the code above produces the following figure:
    Click image for larger version

Name:	Stata help request.jpg
Views:	1
Size:	22.3 KB
ID:	1492255


    What I want it to look like, though, is one vertical line aligned with each of the x-axis labels. So that the first line is above BHPSUKHLS, the second is above CFPS, the third above HILDA, etc.

    Does anyone know what I'm missing? Maybe there is an option I should be adding to the coefplot function?

    Please let me know if anything is unclear and I will try to provide more detailed information as needed.

    Many thanks,
    Thomas


    Below is the loop that creates the variables/data that's used in the code above

    Code:
    replace age = .                        //first, we need to make sure we use the appropriate age variable: we replace age with the respective age variable for each dependent variable !!!
    replace age = age_risk_tol
    replace age = age_gamblerisk_tol if age == .
    replace age = age_finarisk_tol if age == .
    replace age_squared = age^2
    replace age_cubed = age^3
    
    local depvar Z_risk                //then, we need to make sure we use the appropriate dependent variable: define the local to equal to the name of the respective dependent variable !!!
    global depvar Z_risk
    
    quietly reg $depvar female if $sample_restrictions , robust
    quietly ttest $depvar if $sample_restrictions, by(female)
    global mmean= round(r(mu_1), .001)
    global wmean= round(r(mu_2), .001)
    global msd= round(r(sd_1), .001)
    global wsd= round(r(sd_2), .001)
    local pval= r(p)
    
    global title         "Risk Tolerance [0-10] "    
    global summary        "Summary statistics of first-borns' $title"
    
    global dataset        BHPSUKHLS CFPS HILDA IFLS LISS MCS NLSY79 SOEP                 //must also change the list of datasets for each dependent variable !!!
    
    
    forval i=1/2{    //begin gender loop
        local sex : word `i' of $gender
        local j = `i' -1
    
    global Title        "First-born `sex''s $title"
    
    //all datasets combined in one.
    quietly reg $depvar sibling_1_female $good_controls if female==`j' & $sample_restrictions, robust
    est store ALL_`sex'
    outreg2 using `depvar'_`sex', title(The Effect of Having a Younger Sister on $Title ) ctitle(`sex') stats(coef se ci) paren(se) bracket(ci) bdec(3) sdec(3) cdec(3) adjr2 nocons addtext(Dataset, ALL) nonotes addnote(*** p<0.01 ** p<0.05 * p<0.1. , Robust standard errors in parentheses and confidence intervals in brackets. , "Controlling for subject's age, age spacing between siblings and parents, " , $summary, Men: mean=$mmean stdev=$msd . Women: mean=$wmean stdev=$wsd . DiM p-value=$pval .) replace
    
    local lower_bound = ( _b[sibling_1_female] - invttail(e(df_r),0.025)*_se[sibling_1_female] ) *2        //must change 0.025 to another value if not estimated for level(95)
    local upper_bound = ( _b[sibling_1_female] + invttail(e(df_r),0.025)*_se[sibling_1_female] ) *2
    
    coefplot ALL_`sex', title(The Effect of Having a Younger Sister on $Title ) xtitle("All datasets") keep(sibling_1_female) saving(COEFPLOT_ALL_`sex', replace) ytitle("") yscale(range(`lower_bound' `upper_bound')) scheme(s2mono) graphregion(color(white)) vertical yline(0) legend(off)
        
        //for each dataset separately.
        forval i=1/6{    //begin dataset loop        --NOTE: must change the loop counter to equal the amount of datasets used (see dataset list above)
        local data : word `i' of $dataset
        
        quietly reg $depvar sibling_1_female $good_controls if female==`j' & dataset=="`data'" & $sample_restrictions, robust
        est store `data'_`sex'                //stored regression results used for coefplot below
        outreg2 using `depvar'_`sex', ctitle(`sex') stats(coef se ci) paren(se) bracket(ci) bdec(3) sdec(3) cdec(3) adjr2 nocons addtext(Dataset, `data') append excel        //append the output into the same Excel file
        
        coefplot `data'_`sex',  keep(sibling_1_female) saving(COEFPLOT_`data'_`sex', replace) xtitle("`data'") ytitle("") nolabels yscale(range(`lower_bound' `upper_bound')) scheme(s2mono) graphregion(color(white)) vertical yline(0) legend(off)        //create one coefficient plot for each dataset
        
        local coefplotcombine "`coefplotcombine' COEFPLOT_`data'_`sex'.gph"
        local all_stores `all_stores' `data'_`sex'
        local xlabels `xlabels' `i' "`data'"
    
        }    //end dataset loop incl controls
    }    //end gender loop

  • #2
    It seems it is just a matter of recoding the x value for your y values. Do you have a real so that we can help you out better?

    Comment


    • #3
      Hi Tiago. What do you mean by "do you have a real"? I'm not super familiar with all Stata terms, if that is one... I also don't understand what you mean by "recoding the x value for your y values". Thnks for your help!

      Comment


      • #4
        I tried to decompose and simplify the code above. Hopefully that makes it easier for others to understand what the code is (not) doing.

        Code:
        quietly reg $depvar sibling_1_female $good_controls if female==0 & $sample_restrictions, robust
        est store ALL_Men
        outreg2 using `depvar'_Men, title(The Effect of Having a Younger Sister on $Title ) ctitle(`sex') stats(coef se ci) paren(se) bracket(ci) bdec(3) sdec(3) cdec(3) adjr2 nocons addtext(Dataset, ALL) nonotes addnote(*** p<0.01 ** p<0.05 * p<0.1. , Robust standard errors in parentheses and confidence intervals in brackets. , "Controlling for subject's age, age spacing between siblings and parents, " , $summary, Men: mean=$mmean stdev=$msd . Women: mean=$wmean stdev=$wsd . DiM p-value=$pval .) replace
        
        local lower_bound = ( _b[sibling_1_female] - invttail(e(df_r),0.025)*_se[sibling_1_female] ) *2        //must change 0.025 to another value if not estimated for level(95)
        local upper_bound = ( _b[sibling_1_female] + invttail(e(df_r),0.025)*_se[sibling_1_female] ) *2
        
        coefplot ALL_Men, title(The Effect of Having a Younger Sister on $Title ) xtitle("All datasets") keep(sibling_1_female) ytitle("") yscale(range(`lower_bound' `upper_bound')) scheme(s2mono) graphregion(color(white)) vertical yline(0) legend(off)
            
            //for each dataset separately.
            
            reg $depvar sibling_1_female $good_controls if female==0 & dataset=="BHPSUKHLS" & $sample_restrictions, robust
            est store BHPSUKHLS_Men                //stored regression results used for coefplot below
            
            reg $depvar sibling_1_female $good_controls if female==0 & dataset=="CFPS" & $sample_restrictions, robust
            est store CFPS_Men                //stored regression results used for coefplot below
            
            reg $depvar sibling_1_female $good_controls if female==0 & dataset=="HILDA" & $sample_restrictions, robust
            est store HILDA_Men                //stored regression results used for coefplot below    
            
            reg $depvar sibling_1_female $good_controls if female==0 & dataset=="IFLS" & $sample_restrictions, robust
            est store IFLS_Men                //stored regression results used for coefplot below        
        
            reg $depvar sibling_1_female $good_controls if female==0 & dataset=="LISS" & $sample_restrictions, robust
            est store LISS_Men                //stored regression results used for coefplot below        
        
            reg $depvar sibling_1_female $good_controls if female==0 & dataset=="MCS" & $sample_restrictions, robust
            est store MCS_Men                //stored regression results used for coefplot below    
            
            reg $depvar sibling_1_female $good_controls if female==0 & dataset=="NLSY79" & $sample_restrictions, robust
            est store NLSY79_Men                //stored regression results used for coefplot below    
            
            reg $depvar sibling_1_female $good_controls if female==0 & dataset=="SOEP" & $sample_restrictions, robust
            est store SOEP_Men                //stored regression results used for coefplot below        
            
            coefplot BHPSUKHLS_Men CFPS_Men HILDA_Men IFLS_Men LISS_Men MCS_Men NLSY79_Men SOEP_Men,  keep(sibling_1_female) ytitle("") xlabel(1 "BHPSUKHLS" 2 "CFPS" 3 "HILDA" 4 "IFLS" 5 "LISS" 6 "MCS" 7 "NLSY79" 8 "SOEP") xscale(range(1(1)8)) ylabel(-0.2(.1)0.2) yscale(range(-0.3 0.3)) scheme(s2mono) graphregion(color(white)) vertical yline(0) legend(off)        //create one coefficient plot for each dataset in one graph along the whole x-axis; each coefplot should be aligned with the dataset along the x-axis.

        Comment


        • #5
          Dear Thomas,

          Please consult the FAQ page, and read the paragraph: 12.2 What to say about your data

          Next: use dataex to provide some sample of your data (i.e. variables).
          That should be helpful for those who possibly can help you.

          All the best,
          Eric
          http://publicationslist.org/eric.melse

          Comment

          Working...
          X