Announcement

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

  • Adding prefix to each element in a global

    I have a long list of variable names stored in a global. I use these to perform a series of regressions in models excluding and including control variables. I want to plot the coefficients but don't want to write out each coefficient name when using -coefplot. For simplicity I just use three variable names in the example:

    Code:
    global y y1 y2 y3
    foreach y of varlist $y{
    reg `y' x
    estimates store naiv_`y'
    reg `y' x z1 z2 z3
    estimates store cov_`y'
    }
    Now my coefficients are stored with the names
    Code:
    naiv_y1 naiv_y2 naiv_y3 cov_y1 cov_y2 cov_y3
    so writing
    Code:
    coefplot $y
    won't do, because I need the prefixes 'naiv_' and 'cov_'. So how do I make two new globals, which are the elements in $y with the prefix 'naiv_' and 'cov_', which I then can use the plot the coefficients from the different models? Ultimately I would like to end up with two plots, 1) containing the coefficients from all the models excluding control variables, 2) contatining the coefficients from all models including control variables.


  • #2
    One answer is never to use (your own) globals without good reason, and Clyde Schechter will affirm that there is never a good reason.

    I don't follow all of #1 but here is some technique

    Code:
    foreach y in y1 y2 y3 {
        
    reg `y' x
    estimates store naiv_`y'
    local wanted1 `wanted1' naiv_`y'
    reg `y' x z1 z2 z3
    estimates store cov_`y'
    local wanted2 `wanted2' cov_`y'
    
    }
    You may seek the concatenation

    Code:
    local wanted `wanted1' `wanted2'

    Comment


    • #3
      Nick Cox I started using globals since the confusion my use of locals caused to me here https://www.statalist.org/forums/for...r-the-variable. That is, I don't see how globals can cause similar errors, because they can work even when running code one line at a time. Not that I am strongly convinced that globals don't have their weaknesses, I just haven't experienced them yet in my use of Stata.

      Anyway, thanks for the code!

      Comment

      Working...
      X