Announcement

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

  • How to plot connected lines in a event study type of plot using multiple models with same variable?

    I am trying to plot an event study type of graph where I have several models with the two key variables in each. In each model, the response (dependent variable) is in a future time point (F1.var, F2.var, etc).

    I aim to get a connected plot because it better indicates this dynamic property of the models. The best I could get so far, when using the recast(connected) option of coefplot (available in SSC), results in connected lines of the same model, while I would like to have connected lines of the same variable and across models.

    I think the following example will clarify what I mean and aim.

    Code:
    sysuse auto, clear
    reg trunk weight price if rep78==3
    est sto m1
    reg trunk weight price if rep78==4
    est sto m2
    reg trunk weight price if rep78==5
    est sto m3

    Code:
    coefplot m?, drop(_cons) vertical recast(connected)
    Click image for larger version

Name:	getting.png
Views:	1
Size:	15.2 KB
ID:	1734791



    What I would like to get is the following. (Note: the connected lines were added by hand.)

    Click image for larger version

Name:	aiming.png
Views:	1
Size:	15.1 KB
ID:	1734790

    - I am also aware of the gen() option, with which I could genearte the necessary variables and flexibly recreate the plot I want, but I am wondering if I am not missing a more straightforward alternative.
    - I am using version "1.8.6 22feb2023 Ben Jann" of coefplot.
    Last edited by Marc Rain; 22 Nov 2023, 12:13.

  • #2
    Would be easier if you just wanted one connecting line through all the points, but wrap your head around the following (thanks for the data example BTW):

    Code:
    sysuse auto, clear
    reg trunk weight price if rep78==3
    est sto m1
    reg trunk weight price if rep78==4
    est sto m2
    reg trunk weight price if rep78==5
    est sto m3
    
    coefplot m?, drop(_cons) vertical ///
    addplot(line @b @at if @at<=1.5, lc(black)|| ///
    line @b @at if @at>1.5, lc(black))

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	30.7 KB
ID:	1734810


    Comment


    • #3
      Oh. This looks like sorcery, but indeed does the job. Many thanks!

      The only thing I'll have to tweak to try to automatize is the cut-off point (here: `@at<=1.5` or `@at>1.5`).

      In any case, this syntax with `@` is very new to me. Do you know of any resource I could look to dig a bit deeper in how it works? Is it a coefplot or addplot thing?

      Comment


      • #4
        Each variable on the x-axis is centered on an integer, starting at 1. So @at=1 for weight, @at=2 for price and so on. On where to find references to @at and @b, see

        Code:
        help coefplot
        under "Accessing internal temporary variables". A loop is one way to automate the added lines. Make sure you run the loop immediately following the regression so that it can access the coefficients matrix e(b).

        Code:
        sysuse auto, clear
        reg trunk weight price gear turn if rep78==3
        est sto m1
        reg trunk weight price gear turn if rep78==4
        est sto m2
        reg trunk weight price gear turn if rep78==5
        est sto m3
        
        local toadd
        forval i=1/`=`=colsof(e(b))'-1'{
            local toadd `toadd' line @b @at if @at>`=`i'-0.5' & @at<=`=`i'+0.5', lc(black)|| 
        }
        
        coefplot m?, drop(_cons) vertical ///
        addplot(`toadd')
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	30.5 KB
ID:	1734862

        Comment


        • #5
          Perfect. Thank you very much for your time and attention to this... and also pointing me to the resource, I did miss it earlier on.

          The loop implementation to add multiple plots is also very clever. Many thanks again.

          Comment

          Working...
          X