Oren Rosenberg
Oren Rosenberg

Reputation: 55

Problems with x-axis labels in event study graph

I have the following data:

gen yr  coef        ci_l        ci_h
F   -8  0.0059488   -0.004565   0.0164627
F   -7  0.0040078   -0.0057631  0.0137786
F   -6  0.0049703   -0.0085357  0.0184763
F   -5  -0.0019084  -0.0211634  0.0173466
F   -4  0.0013469   -0.0215072  0.0242009
F   -3  0.036477    0.017118    0.0558361
F   -2  0.1271414   0.089147    0.1651358
F   -1  0.1431927   0.1021379   0.1842475
F   0   0.1398031   0.1025783   0.177028
F   1   0.1349187   0.0997429   0.1700946
F   2   0.1355518   0.0997538   0.1713498
F   3   0.1194403   0.0808667   0.1580139
F   4   0.1060962   0.0668593   0.1453331
F   5   0.1166223   0.0726827   0.1605619
M   -8  0.0161407   0.0028669   0.0294144
M   -7  0.0043735   -0.0065912  0.0153381
M   -6  0.0114297   -0.0035314  0.0263908
M   -5  0.0059428   -0.0149018  0.0267874
M   -4  0.0081406   -0.0175235  0.0338046
M   -3  0.0436267   0.0240535   0.0632
M   -2  0.1387672   0.0848806   0.1926538
M   -1  0.1582463   0.1001987   0.2162939
M   0   0.1512713   0.0993361   0.2032066
M   1   0.1477444   0.1023816   0.1931072
M   2   0.1513944   0.107969    0.1948197
M   3   0.1290913   0.0853945   0.1727881
M   4   0.1182751   0.0722187   0.1643315
M   5   0.1234578   0.0724435   0.1744721

What I want is to create a graph like this:

Event Study Graph

Where the 0 on the x-axis corresponds to the the year variable, and there is one line for each gender rather than for peaceful/divergence events.

Currently, when I run the following Stata code:

twoway (sc coef year if gender == "M", mcolor(navy) lcolor(navy) connect(direct)) ///
(rcap ci_low ci_high year if gender == "M", lcolor(navy)) (sc coef year if gender == "F", ///
mcolor(maroon) lcolor(maroon) connect(direct)) (rcap ci_low ci_high year if gender == "F", ///
lcolor(maroon)), legend(lab(1 "Male") lab(2 "Male CI") lab(3 "Female") lab(4 "Female CI")) ///
xlab(,val)

I get something that looks like this:

Current Stata Output

I believe maybe the lines are right, but why is it appearing strange like that with the x-axis labels?

Upvotes: 1

Views: 3743

Answers (2)

user8682794
user8682794

Reputation:

Using your example data:

clear

input str1 gender year coef ci_l ci_h
F   -8  0.0059488   -0.004565   0.0164627
F   -7  0.0040078   -0.0057631  0.0137786
F   -6  0.0049703   -0.0085357  0.0184763
F   -5  -0.0019084  -0.0211634  0.0173466
F   -4  0.0013469   -0.0215072  0.0242009
F   -3  0.036477    0.017118    0.0558361
F   -2  0.1271414   0.089147    0.1651358
F   -1  0.1431927   0.1021379   0.1842475
F   0   0.1398031   0.1025783   0.177028
F   1   0.1349187   0.0997429   0.1700946
F   2   0.1355518   0.0997538   0.1713498
F   3   0.1194403   0.0808667   0.1580139
F   4   0.1060962   0.0668593   0.1453331
F   5   0.1166223   0.0726827   0.1605619
M   -8  0.0161407   0.0028669   0.0294144
M   -7  0.0043735   -0.0065912  0.0153381
M   -6  0.0114297   -0.0035314  0.0263908
M   -5  0.0059428   -0.0149018  0.0267874
M   -4  0.0081406   -0.0175235  0.0338046
M   -3  0.0436267   0.0240535   0.0632
M   -2  0.1387672   0.0848806   0.1926538
M   -1  0.1582463   0.1001987   0.2162939
M   0   0.1512713   0.0993361   0.2032066
M   1   0.1477444   0.1023816   0.1931072
M   2   0.1513944   0.107969    0.1948197
M   3   0.1290913   0.0853945   0.1727881
M   4   0.1182751   0.0722187   0.1643315
M   5   0.1234578   0.0724435   0.1744721
end

You can use graph twoway line instead of scatter:

twoway (line coef year if gender == "M", lcolor(red)) ///
       (line coef year if gender == "F", lcolor(gray) lpattern(dash)), ///
       title("Event study graph") ytitle("Coefficient") xtitle("Years") ///
       legend(label(1 "Males") label(2 "Females"))

enter image description here


EDIT:

A comment on @NickCox's answer:

The theory that the variable year has a value label, which is mis-specified does have merit. I can replicate the strange x-axis labeling observed in the OP's second graph with a value label defining a wrong label for value -10:

label define yearlabel -10 "0" 
label values year yearlabel

label list yearlabel
yearlabel:
         -10 0

The simplest solution in this case is to get rid of the xlab(, val) option and let Stata determine the labels automatically. And the desired result can also be achieved with equivalent syntax like the one I provided above.

Upvotes: 2

Nick Cox
Nick Cox

Reputation: 37208

Your axis labels are given by the option xla(, val): the rest of the syntax is irrelevant.

Here is an easy guess: your value labels are defined as 5 "-5" 10 "0" 15 "5". Stata in addition wants to show a literal zero. You can fix this either by defining a value label for 0 as -10 or on the fly by spelling out all the text labels you want.

Another possibility is that you made a small error in defining your value labels.

It follows that your problem is not reproducible fully without knowing the value labels you defined for year.

In summary: if you use value labels in determining axis labels, look carefully at what you have defined.

Upvotes: 1

Related Questions