Announcement

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

  • How to use collapse funtion to generate a clean two-way graph

    Hi,

    I want to generate a two-way graph for a difference in difference analysis. I have a dummy variable (d_MA) that measures whether or not a firm engaged in M&A. I have another dummy variable (SICMatch) that measures the relatedness of the two firms (I only have data for this variable for the M&A firms). . I want to create a graph with time on the x axis and patent output on the y axis that shows 5 lines: 4 for the different relatedness dummies, and 1 for the non-M&A group.

    I tried to do this, however I need to have only 1 value per time variable in order to generate a clean graph. I think the collapse function could provide a solution. However I was not able to use it properly.


    I used the following stata commands to do this:
    Code:
    egen Average_Patents_merged1 = mean(lnPatents) if d_MA==1 & SICMatch ==1, by(time)
    egen Average_Patents_merged2 = mean(lnPatents) if d_MA==1 & SICMatch ==2, by(time)
    egen Average_Patents_merged3 = mean(lnPatents) if d_MA==1 & SICMatch ==3, by(time)
    egen Average_Patents_merged4 = mean(lnPatents) if d_MA==1 & SICMatch ==4, by(time)
    egen Average_Patents_Notmerged = mean(lnPatents) if d_MA==0, by(time)
    
    graph twoway (line Average_Patents_merged1 time if d_MA==1) (line Average_Patents_merged2 time if d_MA==1) (line Average_Patents_merged3 time if d_MA==1) (line Average_Patents_merged4 time if d_MA==1)(line Average_Patents_Notmerged time if d_MA==0), xtitle("Time") ytitle("Patent output") xlabel(-3 (1)3) xline(0)

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(lnPatents d_MA) byte(time SICMatch)
     6.383507 0  1 0
     6.487684 0 -3 0
     6.725034 0 -1 0
      8.02027 1 -2 4
     7.608871 1  3 4
     7.860185 1  1 4
     4.317488 0 -1 0
     4.859812 0  2 0
     4.406719 0 -3 0
      4.59512 0  1 0
    4.7361984 0  3 0
    4.7361984 0  0 0
      4.26268 0 -2 0
    2.3025851 0 -2 0
     2.890372 0  3 0
     2.944439 0  2 0
     2.995732 0 -3 0
     2.833213 0 -1 0
     3.178054 0  1 0
    3.3322046 0  0 0
    2.0794415 1 -2 0
     2.995732 1  2 0
     2.833213 1  0 0
     2.397895 1  1 0
     2.995732 1  3 0
    2.1972246 1 -1 0
    1.7917595 0  1 0
     2.397895 0  0 0
     .6931472 0  1 0
      1.94591 0  3 0
    1.0986123 0 -3 0
    2.1972246 0 -2 0
      1.94591 0 -1 0
      5.09375 0  3 0
     5.129899 0 -1 0
    end

    This is the graph I get:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	207.0 KB
ID:	1585193








    Attached Files

  • #2
    Code:
    collapse lnPatents d_MA, by(SICMatch time)
    xtset SICMatch time
    xtline lnPatents if d_MA, overlay addplot(line lnPatents time if !d_MA, sort)

    Comment


    • #3
      Thanks a lot! Really helpful

      Comment


      • #4
        After applying your code. I used the following code for a comparable graph. I want to plot confidence interals to this graph, is there any way to do this?

        Code:
         
         egen Average_Patents_merged1 = mean(lnPatents) if d_MA==1 & SICMatch ==1, by(time) egen Average_Patents_merged2 = mean(lnPatents) if d_MA==1 & SICMatch ==2, by(time) egen Average_Patents_merged3 = mean(lnPatents) if d_MA==1 & SICMatch ==3, by(time) egen Average_Patents_merged4 = mean(lnPatents) if d_MA==1 & SICMatch ==4, by(time) egen Average_Patents_Notmerged = mean(lnPatents) if d_MA==0, by(time)  graph twoway (line Average_Patents_merged1 time if d_MA==1) (line Average_Patents_merged2 time if d_MA==1) (line Average_Patents_merged3 time if d_MA==1) (line Average_Patents_merged4 time if d_MA==1)(line Average_Patents_Notmerged time if d_MA==0), xtitle("Time") ytitle("Patent output") xlabel(-3 (1)3) xline(0)


        Lastly I want to generate a graph with the patents on the Y axis, and the SICMatch variable on the x axis, only for the M&A=1 variable. However I'm not able to do this because I think I also have to use the collapse function but this generates a graph in the same manner that is not collapsed.

        Do you have any idea? Your help is much appreciated!

        Comment


        • #5
          twoway will not generate confidence intervals, but it can plot these if you have them.

          Lastly I want to generate a graph with the patents on the Y axis, and the SICMatch variable on the x axis, only for the M&A=1 variable. However I'm not able to do this because I think I also have to use the collapse function but this generates a graph in the same manner that is not collapsed.

          Do you have any idea? Your help is much appreciated!
          If SICMatch is an ordinal variable, you can collapse to get means of patents at each level of the variable and create a line graph. Otherwise, opt for a bar graph or a dot chart if the categories are assigned arbitrarily. Note that taking the average of logged values gives the geometric mean of the original values. So you may want to collapse the original values first before taking the logs.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(lnPatents d_MA) byte(time SICMatch)
           6.383507 0  1 0
           6.487684 0 -3 0
           6.725034 0 -1 0
            8.02027 1 -2 4
           7.608871 1  3 4
           7.860185 1  1 4
           4.317488 0 -1 0
           4.859812 0  2 0
           4.406719 0 -3 0
            4.59512 0  1 0
          4.7361984 0  3 0
          4.7361984 0  0 0
            4.26268 0 -2 0
          2.3025851 0 -2 0
           2.890372 0  3 0
           2.944439 0  2 0
           2.995732 0 -3 0
           2.833213 0 -1 0
           3.178054 0  1 0
          3.3322046 0  0 0
          2.0794415 1 -2 0
           2.995732 1  2 0
           2.833213 1  0 0
           2.397895 1  1 0
           2.995732 1  3 0
          2.1972246 1 -1 0
          1.7917595 0  1 0
           2.397895 0  0 0
           .6931472 0  1 0
            1.94591 0  3 0
          1.0986123 0 -3 0
          2.1972246 0 -2 0
            1.94591 0 -1 0
            5.09375 0  3 0
           5.129899 0 -1 0
          end
          
          collapse lnPatents if d_MA, by(SICMatch)
          line lnPatents SICMatch
          *OR
          gr bar lnPatents, over(SICMatch)

          Comment

          Working...
          X