Announcement

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

  • Problem in generating simple mean and graph

    Dear all,

    It might be a silly a question but I've a simple problem in generating a mean variable. In this dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long gvkey float(modate trn t_matched event_time)
    1050 439  .04544039 0 -5
    1050 440  .06523648 0 -4
    1050 441 .070073605 0 -3
    1050 442  .08033606 0 -2
    1050 443  .06696292 0 -1
    1050 444          . 0  0
    1050 445          . 0  1
    1050 446          . 0  2
    1050 447  .02373088 0  3
    1050 448  .09264925 0  4
    1056 379 .013338055 1 -5
    1056 380  .01713881 1 -4
    1056 381  .03750573 1 -3
    1056 382 .064970195 1 -2
    1056 383  .04966758 1 -1
    1056 384  .06059147 1  0
    1056 385  .04347776 1  1
    1056 386  .04417698 1  2
    1056 387  .04510546 1  3
    1056 388 .010568546 1  4
    1056 403  .15264153 1 -5
    1056 404  .14115226 1 -4
    1056 405  .10177388 1 -3
    1056 406  .05430648 1 -2
    1056 407  .08125668 1 -1
    1056 408  .18746273 1  0
    1056 409   .1538455 1  1
    1056 410  .10503364 1  2
    1056 411  .03939188 1  3
    1056 412  .03664935 1  4
    1056 439   .0554457 1 -5
    1056 440 .037111163 1 -4
    1056 441  .04478774 1 -3
    1056 442  .03789423 1 -2
    1056 443  .04094375 1 -1
    1056 444  .06175835 1  0
    1056 445  .04464729 1  1
    1056 446  .04800224 1  2
    1056 447 .036143806 1  3
    1056 448   .0468172 1  4
    1056 511  .46155715 1 -5
    1056 512  .14318317 1 -4
    1056 513  .17015287 1 -3
    1056 514   .2357336 1 -2
    1056 515  .14718385 1 -1
    1056 516   .1545227 1  0
    1056 517   .1759009 1  1
    1056 518   .1343986 1  2
    1056 519   .2120673 1  3
    1056 520   .2011091 1  4
    1094 391 .011197286 0 -5
    1094 392  .01187591 0 -4
    1094 393  .02359416 0 -3
    1094 394  .08205312 0 -2
    1094 395  .22778177 0 -1
    1094 396  .09571713 0  0
    1094 397  .07067729 0  1
    1094 398  .03328685 0  2
    1094 399 .011504777 0  3
    1094 400 .007384554 0  4
    1094 403 .032703027 1 -5
    1094 404   .0191879 1 -4
    1094 405 .016653419 1 -3
    1094 406  .04310413 1 -2
    1094 407  .11329491 1 -1
    1094 408  .04042383 1  0
    1094 409  .05237905 1  1
    1094 410  .08144742 1  2
    1094 411  .01872128 1  3
    1094 412 .007332667 1  4
    1098 403  .08070259 1 -5
    1098 404  .05674773 1 -4
    1098 405   .0338261 1 -3
    1098 406  .05636624 1 -2
    1098 407   .0397711 1 -1
    1098 408  .07092672 1  0
    1098 409  .03913527 1  1
    1098 410  .05148625 1  2
    1098 411  .21315873 1  3
    1098 412  .15266667 1  4
    1109 379  .16524065 0 -5
    1109 380  .12299465 0 -4
    1109 381 .072792366 0 -3
    1109 382  .12410501 0 -2
    1109 383  .09880668 0 -1
    1109 384      .1784 0  0
    1109 385      .0756 0  1
    1109 386      .0566 0  2
    1109 387  .08416834 0  3
    1109 388  .19569138 0  4
    1109 391   .1717019 1 -5
    1109 392  .09154078 1 -4
    1109 393  .04508612 1 -3
    1109 394  .03779129 1 -2
    1109 395  .09138805 1 -1
    1109 396  .09223603 1  0
    1109 397  .13809524 1  1
    1109 398   .2015528 1  2
    1109 399  .14112821 1  3
    1109 400  .12317949 1  4
    end
    format %tm modate
    I have some firms (gvkey) on a monthly basis. For every month, I have their turnover (trn) and all the firms belong to two groups.
    If the firm belongs to t_matched==0 the firm doesn't receive the treatment in event_time==0.
    On the other hand, if the firm belongs to t_matched==1, the firm receive the treatment in event_time==0.
    Event time goes to -5+4 indicating the months before and after the treatment.

    My final objective: a graph with Y axis trn and X axis the event_time. I'm expecting that, visually, there are differences in the trn for the treated and control group in the event_time=0. So the graph should include two lines trend (for treated and control group)


    In order to do so, I guess I firslty need a variable that calculates the average trn of all the firm belonging to each group by each months of the event_time.
    Nevertheless I'm struggling on how to implement it and then reporting the graph.
    I did many attempts that I didn't post just to avoid further confusion.

    Do you have any helps/suggestion?
    Many thanks, as always, for your time


  • #2
    Code:
    ssc install lgraph
    
    lgraph trn event_time t_matched, ///
    legend(order(0 "Control" 1 "Treatment"))
    Roman

    Comment


    • #3
      Thanks you so much Roman Mostazir , You really solved all the issue with that command.
      I'm just wondering why it doesn't show in the legen that the control group is with red line. It only shows the legen for the treated one.
      Click image for larger version

Name:	Graph_trn.png
Views:	1
Size:	61.0 KB
ID:	1582103



      Last edited by Marco Errico; 17 Nov 2020, 06:26.

      Comment


      • #4
        Problem was in legend ordering. Sorry, my mistake:
        Code:
        legend(order(1 "Control" 2 "Treatment"))
        Roman

        Comment


        • #5
          lgraph is a helpful command I've only used occasionally. Without wanting to undermine it, let's note that a direct route is available here too:


          Code:
          egen mean = mean(trn), by(event_time t_matched)
          
          separate mean, by(t_matched) veryshortlabel
          
          twoway connected mean? event_time , sort
          Last edited by Nick Cox; 17 Nov 2020, 08:07.

          Comment


          • #6
            Many thanks Roman Mostazir .
            Something that I don't get is how do you know that firstly you will state your control (indicator=0) and 2 the treatment one (indicator=1).
            Maybe I'm not clear in my question and apologies for it. Nevertheless, is not clear why the first thing that you have to state is the control group (0) and then your treated one (1).

            Hope you get my point, maybe is something in the option of lgraph that I'm missing

            Comment


            • #7
              Many thanks Nick Cox . This is really helpful and I need to take notes on the command that you suggested in your codes.

              Comment


              • #8
                #6: At first the plot is made when treatment = 0 as it comes chronologically before 1 and then the 2nd plot is overlayed when treatment = 1. You are then asking with the 'legend' option followed by the suboption 'order' to order the legends as per the sequence of the plot being made and label them as "control" for the 1st drawn plot (thus 1 = 'control') and "treatment" for the 2nd plot (thus 2 = Treatment). Play around with changing them to see what actually happens i.e. legend(order (2 "Treatment" 1"Control")). That will be more helpful than my thousands words.
                Roman

                Comment

                Working...
                X