Announcement

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

  • Title of graph with local that uses label using coefplot in Stata 14.1MP

    Hello,
    I wrote the following code that saves variables' labels into a local, and then creates plots of the sum of two coefficients from a quantile regression using coefplot in Stata 14.1MP.
    The idea is that for each graph (created with coefplot) I get the variable label to be included in the title of the plot (e.g. `"Mg. Effect of `l`var'' for EMs On Conditional Quantile Q{sub:{&tau}}"' ).

    However, when I produce the plots, the label part is left blank (as if not specified or as if label was non-existent). I get something like "Mg. Effect of for EMs On Conditional Quantile Q{sub:{&tau}}" What am I not seeing?
    I am posting my original code, but if need be, I could also post a replicable example. Also please see attached a picture (png) of the plot created.

    Thank you.

    Code:
        global assets1 "L_fin_a L_fin_a_debt L_liquid L_a2 L_assets" 
    
        set more off
        estimates clear
    
        label var L_fin_a "Total assets" /* label vars for graph */
        label var L_fin_a_debt "Assets (debt)"
        label var L_liquid "Assets (liquid)"
        label var L_a2 "Assets (highly liquid)"
        label var L_assets "Assets(WEO)"
            
    *** This code: for each asset: Produces the gamma+beta coef and plots w std. errors. 
    foreach var of global assets1 {
    
        local quantiles 1 5 10 25 50 75 90 95    // K quantiles that you care about
        local models ""                             // names of K quantile models for coefplot to graph 
        local xlabel ""                             // for x-axis labels
        local j=1                                   // counter for quantiles
        foreach q of numlist `quantiles' {
            qreg risk D_EM  L_cab_mean L_growth L_default L_reserves vix  L_inflation L_gross_debt `var' `var'_EM ,quantile(`q')
            nlcom (me_tu:_b[`var']+_b[`var'_EM]), post
            estimates store me_tu`q'
            local models `"`models' me_tu`q' || "'
            local xlabel `"`xlabel' `j++' "Q{sub:`q'}""'
        }
        
        di "`models'
        di `"`xlabel'"'
        di "`l`var''"
        
    *Keep labels for graphs
        foreach var of var * { 
                local l`var' : variable label `var'
                if `"`l`var''"' == "" {
                    local l`var' "`var'"
                    }
                }    
        
        coefplot `models' /// 
        , vertical bycoefs  ///
        xlab(none) xlabel(`xlabel', add) ///
        title(`"Mg. Effect of `l`var'' for EMs On Conditional Quantile Q{sub:{&tau}}"', size(medsmall)) ///
        ytitle("Bond Spread Basis Points" "") yline(0) ciopts(recast(rcap))
        graph save `v'_gamma_beta, replace
        graph export `v'_gamma_beta.png, replace
                
            }

    Click image for larger version

Name:	_gamma_beta.png
Views:	1
Size:	112.1 KB
ID:	1357122


  • #2
    Well, I don't have a suitable data set to test your code on, but I think I see the problem. You have two nested loops, both ostensibly controlled by the same parameter, namely var. You can't use var to mean two different things at the same time. I think if you change the inner loop to
    Code:
        foreach var1 of var * { 
                local l`var1' : variable label `var1'
                if `"`l`var1''"' == "" {
                    local l`var1' "`var1'"
                    }
                }
    By (attempting to) redfine var in the inner loop while it is also controlling the outer loop, I think you have created chaos. My best guess is that your inner loop isn't actually executing at all. In a way, I'm surprised things didn't work out even worse than they did.

    Additionally, it is perfectly legal syntax to abbreviated -varlist- in -foreach x of varlist *- to -var-, I think that when you are also using var (or var1) as the name of the loop parameter you are maximizing the potential for confusing, not Stata, but yourself. In fact, it wouldn't surprise me if that is part of the reason you used the same name loop to attempt to parameterize both loops.

    Finally, I rarely pass up the chance to point out that the use of global macros instead of local macros is unsafe and is poor programming practice unless there is no alternative. The usual reasons that people give for using global macros don't stand up to scrutiny. I won't go into the details of what those are and why not, but I will point out that if you are not familiar with the -include- command, learning it will probably liberate you from the dangers of global macros.

    Comment

    Working...
    X