Multiple coefplots: loop quotes issue

55 Views Asked by At

I'm trying to create a loop in STata that creates coefplots, which show the coefficients for multiple regressions for subgroups of a population. Each of the plots would use different regressors.

I'm running into an issue with quotations using the loop. Ideally each of the graphs would look like the picture I included. enter image description here

sysuse auto, clear 
replace foreign = 3 if (rep78 == 3) 
regress price mpg length if foreign == 1
estimates store mpg_foreign_1_length
regress price mpg length if foreign == 0
estimates store mpg_foreign_0_length
regress price mpg length if foreign == 3
estimates store mpg_foreign_3_length

regress price mpg trunk if foreign == 1
estimates store mpg_foreign_1_trunk 
regress price mpg trunk if foreign == 0
estimates store mpg_foreign_0_trunk 
regress price mpg trunk if foreign == 3
estimates store mpg_foreign_3_trunk 


global list_graphing "trunk" "length"

foreach x of global list_graphing {
        coefplot "mpg_foreign_3_`x' mpg_foreign_1_`x'" ||, drop(_cons) 
   }
   

  foreach x of global list_graphing {
        coefplot "mpg_foreign_3_`x' mpg_foreign_1_`x' mpg_foreign_0_`x'" ||, drop(_cons)
   }
   
 //invalid something: quotes do not match


   foreach x of global list_graphing {
        coefplot "mpg_foreign_3_`x'" "mpg_foreign_1_`x'" "mpg_foreign_0_`x'" 
   }
  

The first graph displays essentially what I'm trying to create; however, it doesn't work when a third variable is added. I can't split them up with quotes because, as the code clearly shows, that makes it so the coefficients are no longer on the same graph.

2

There are 2 best solutions below

0
joshb On

I don't have much experience with coefplot, but it seems this works if you just don't use quotes (indeed, I'm somewhat surprised that it does work with quotes sometimes).

sysuse auto

replace foreign = 3 if (rep78 == 3) 
regress price mpg length if foreign == 1
estimates store mpg_foreign_1_length
regress price mpg length if foreign == 0
estimates store mpg_foreign_0_length
regress price mpg length if foreign == 3
estimates store mpg_foreign_3_length

regress price mpg trunk if foreign == 1
estimates store mpg_foreign_1_trunk 
regress price mpg trunk if foreign == 0
estimates store mpg_foreign_0_trunk 
regress price mpg trunk if foreign == 3
estimates store mpg_foreign_3_trunk 


global list_graphing trunk length

foreach x of global list_graphing {
        coefplot mpg_foreign_3_`x' mpg_foreign_1_`x' ||, drop(_cons) 
   }
   

  foreach x of global list_graphing {
        coefplot mpg_foreign_3_`x' mpg_foreign_1_`x' mpg_foreign_0_`x' ||, drop(_cons)
   }
   

   foreach x of global list_graphing {
        coefplot mpg_foreign_3_`x' mpg_foreign_1_`x' mpg_foreign_0_`x' 
   }
0
Nick Cox On

The problem lies in your definition

global list_graphing "trunk" "length"

Stata strips the outermost quotation marks as delimiters and your macro is left holding

trunk" "length 

which then causes problems downstream. If you needed to have the matched quotation marks inside the global macro, then your definition should be

global list_graphing `" "trunk" "length" "' 

but as already answered you don't need any quotation marks here.