Announcement

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

  • Graphs by category

    Dear all,

    I am using unbalanced panel data with 3,000 companies over a period of 70 months. The companies are divided into two groups by a dummy variable. I am trying to plot a graph with two lines - the means of some variable, for instance turnover, for both groups over the period.

    Any advice?
    Thanks!

  • #2
    There are several ways of doing it. One straight forward way is to use 'lgraph' programme written by Timothy Mark:

    Code:
    ssc install lgraph //Install the programme
    
    lgraph turnover month group, err(95)
    Roman

    Comment


    • #3
      Also

      Code:
      egen mean = mean(turnover), by(month group) 
      separate mean, by(group) veryshortlabel 
      line `r(varlist)' month 

      Comment


      • #4
        Thank you for your help!

        I find the lgraph command very convenient in this case. Nick's code connects every point of the time series with each other. I might be doing something wrong, though.

        But what if I want to plot the difference between the two groups as well?

        Furthermore, how do I add vertical lines at some point to illustrate that a particular event occurred? With the graph editor?

        Comment


        • #5
          It would help if you give us some example data to work with. That way we can show you complete code of what we suggest. You can show us complete code of what you did. And we don't have to guess to find out what went wrong

          To give us example data you can use the dataex command.
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            I am familiar with the dataex command, but I don't think it would work in this case since I have a very large panel dataset with 3,000 stocks over 7 years. The securities are divided into two groups by a dummy variable treated. I would like to graphically represent the effect of a treatment by plotting a line chart of the mean of turnover for both groups and the difference of the two means for every month. Therefore, there are three lines - one is the mean of turnover for treated stocks, the other is the mean of turnover for non-treated stocks, and the third is the difference between the two. I am using monthly observations. Furthermore, I would like to add a vertical line at some point in time to illustrate that the treatment has occurred.

            Thanks a lot for your advice!
            Cheers

            Comment


            • #7
              The purpose of dataex is to give an example, not to show all of a large dataset. It doesn't seem that you have even tried it as by default it would give you 100 observations.

              Also, wanting to plot the difference is new here in #4.

              I am still struggling a bit with your word description but perhaps this will help. Two ways to do it.

              Code:
              webuse grunfeld, clear
              gen treated = company > 5
              collapse mvalue, by(treated year)
              reshape wide mvalue, i(year) j(treated)
              gen diff = mvalue0 - mvalue1 
              line mvalue* diff year , legend(order(2 "treated" 1 "untreated" 3 "difference"))
              
              webuse grunfeld, clear 
              gen treated = company > 5
              egen mean0 = mean(cond(treated == 0, mvalue, .)), by(year) 
              egen mean1 = mean(cond(treated == 1, mvalue, .)), by(year)  
              gen diff = mean0 - mean1 
              sort year 
              line mean* diff year , legend(order(2 "treated" 1 "untreated" 3 "difference"))
              If there isn't an answer there, phrase your next question in terms of the Grunfeld data or give an example as we do ask.

              Comment


              • #8
                Perhaps this example will be helpful to you:

                Code:
                webuse pig
                gen group = id > 24
                by week group, sort : egen float mymean = mean(weight)
                twoway(connect mymean week if group ==0, sort) (connect mymean week if group ==1, sort)
                Best regards,

                Marcos

                Comment


                • #9
                  Marcos Almeida Fanetti wants to see the difference explicitly You need a twist on your solution to get at the difference, as the two groups of observations are disjoint.

                  Comment


                  • #10
                    Thanks for the correction, Nick.
                    Best regards,

                    Marcos

                    Comment


                    • #11
                      Plotting the means for two groups and their difference in the same graph may not be a good idea as their scales are largely different. However, because your data is repeated for each firm making observations dependent, you probably should think of mixed/random effect models. I guess you need something like this (some fake data):


                      Code:
                       li compid group month turnover in 77/108, noobs clean
                          compid   group   month   turnover  
                               7       0       5    17.1941  
                               7       0       6   16.91908  
                               7       0       7   10.01516  
                               7       0       8   16.87846  
                               7       0       9   10.14716  
                               7       0      10   12.73021  
                               7       0      11   20.21414  
                               7       0      12   13.48389  
                               8       0       1   5.428607  
                               8       0       2   12.60844  
                               8       0       3   6.544689  
                               8       0       4   9.319549  
                               8       0       5   19.85889  
                               8       0       6   18.24235  
                               8       0       7   6.413465  
                               8       0       8   8.232611  
                               8       0       9   8.427597  
                               8       0      10   15.06595  
                               8       0      11   12.22824  
                               8       0      12   18.25638  
                               9       1       1    7.90609  
                               9       1       2   13.41509  
                               9       1       3   5.642486  
                               9       1       4   18.07815  
                               9       1       5   5.291037  
                               9       1       6   18.93428  
                               9       1       7   16.36238  
                               9       1       8   8.293083  
                               9       1       9   7.135417  
                               9       1      10   9.416173  
                      
                      xtset compid month //Declare the dataset as nested
                      
                      mixed turnover i.group##i.month ||compid: //Random intercept model
                      
                      margins i.month#i.group, saving(file1, replace) //Means for each month by two groups
                      margins i.month, dydx(group) saving(file2, replace) // Means difference for each month by two groups
                      
                      combomarginsplot file1 file2 , xline(6, lpatt(dash)lcol(red)) ///
                          legend(order(1 "Control" 2 "Treatment" 3 "Mean Difference")) ti("Means & Difference")  //Combine two plots and a vertical line

                      Click image for larger version

Name:	test.png
Views:	1
Size:	108.6 KB
ID:	1440266
                      Roman

                      Comment


                      • #12
                        Roman: You're right that plotting difference on the same scale might not be a good idea but Fanetti is ignoring all requests to give example data.

                        Comment


                        • #13
                          Agree with Nick #12. It saves lots of time for those who contributes. The confusing part is Fanetti used -dataex- in one of the previous posts here!
                          Roman

                          Comment


                          • #14
                            Thank you for your inputs, guys! Really appreciate it!

                            Apologies for not showing my dataset with -dataex-, but as you mentioned, dataex is limited to 100 observations, and my dataset is quite large. It covers 3,000 stocks per month over 7 years. The 100 observations cannot really capture the cross-sectional and time-series specifics of the data. That was the main reason for not showing it.

                            Nevertheless, you understood what I meant, even thought I might have been a bit ambiguous in my explanation of the issue. I tried Nick's second approach and it works fine. Thanks a lot!

                            Code:
                            webuse grunfeld, clear
                            gen treated = company > 5
                            egen mean0 = mean(cond(treated == 0, mvalue, .)), by(year)
                            egen mean1 = mean(cond(treated == 1, mvalue, .)), by(year)
                            gen diff = mean0 - mean1
                            sort year
                            line mean* diff year , legend(order(2 "treated" 1 "untreated" 3 "difference"))
                            Roman, you might be right about the different scale of the means and their difference, but what other options do I have? I don't think I should include a second figure for the difference. This graph is meant to accompany the results of a Difference-in-Differences regression with firm and time fixed effects. I hope that the simple approach that I use - plot the means for both groups and their difference - should be enough for this simple purpose. I haven't really thought about the mixed/random effects models that you talk about in #11.

                            Comment

                            Working...
                            X