Announcement

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

  • how to plot different variables of different observations

    Hey All,
    I have a question concerning the plotting functions. Consider:

    Code:
    // IMPORT DATA
    
    // The first part just generates data the way I have it,
    //such that you know my data structure.
    //Skip to "PLOT MY RESULTS"
    
    clear
    webuse grunfeld, clear
    g ret=invest/1500
    bys company (year): g cumul=ret if _n==1
    replace cumul=ret + l.cumul if missing(cumul)
    gen country = "empty"
    keep if company <8
    replace country = "FR" if company ==1
    replace country = "US" if company ==2
    replace country = "DE" if company ==3
    replace country = "FR" if company ==4
    replace country = "US" if company ==5
    replace country = "DE" if company ==6     
    replace country = "FR" if company ==7
        
    
    // Generate EMU identifier
    gen emu                  = 0
    replace emu              = 1  if (country == "AT") | (country == "BE") |  (country == "ES") | (country == "DE") | (country == "CY")| (country == "FI") | (country == "FR") | (country == "IT") | (country == "NL") | (country == "GR") | (country == "SI")
    
    // compute abnormal returns for countries and countrygroups (EMU)
    egen mean_country_cum = mean(cumul), by(year country)  // averages for each country
    egen mean_euro_cum = mean(cumul), by(year emu)         // emu average based on averaging all emu companies
    
    
    // PLOT MY RESULTS 
    // here begins the interesting part
    
    // Cumulative returns Euro vs. US
    // code that displays mean_country_cum(US) vs. mean_euro_cum against time
    As described in the code I want to plot the cumulative abnormal returns of the EU area vs. the cumulative abnormal returns of the States. I guess in terms of Stata I want to plot two different variables of two different observations against time. Is this possible? If yes, how? if no, does anyone know a workarround that procudes what I am looking for?

    I guess in pseudo code I look for:

    Code:
    twoway plot mean_country_cum if company == 1, mean_euro_cum if company == 2
    It seems like cheap trick to just choose a company of which I know it is based in Euro / US, but this could work I guess. Unfortunately Stata syntax is still a bit of a mystery to me. Can anyone help out?

    Thanks in advance!

    Best,
    Harvey

  • #2
    I guess in terms of Stata I want to plot two different variables of two different observations against time. Is this possible?
    Yes, each plot statement can have its own if exp and in range restrictions. I believe you would want something like this:

    Code:
    twoway line mean_country_cum year if company == 1 || line mean_euro_cum year if company == 2

    Comment


    • #3
      Yes this solves it perfectly! Thanks so much!

      CLOSED

      Comment


      • #4
        actually not closed. one more thing.

        is there a way to manually overwrite the legend of the graph? So the legend automatically included names the variables drawn. i would prefer it just read the countries. Is there a way to get this done?

        Thanks again!

        Best,
        Harvey

        Comment


        • #5
          see:
          help twoway_options
          help legend_options
          Code:
          twoway line mean_country_cum year if company == 1 ||  ///
          line mean_euro_cum year if company == 2, ///
          legend(order(1 "company 1" 2 "company 2"))

          Comment


          • #6
            Harvey Spector An easy way to do that is to use value labels with a given variable. You may not be able to overlay both lines quite as easily, but if you have time series/panel data, I'd suggest using -xtline-

            Code:
            // Load sample data
            webuse grunfeld, clear
            
            // Rescale the investment variable
            qui: g double ret = invest/1500
            
            // Simplification of some of the previously used code
            bys company (year): g cumul = cond(_n == 1, ret, ret + l.ret)    
            
            // Drop all but records for a few companies
            qui: keep if company < 8
            
            // Define value labels for a country variable that will be created
            la def country     1 "FR" 2 "US" 3 "DE" 4 "AT" 5 "BE" 6 "ES" 7 "CY" 8 "FI"      ///   
                            9 "IT" 10 "NL" 11 "GR" 12 "SI", modify
            
            // Create a country indicator
            qui: g byte country =     cond(inlist(company, 1, 4, 7) == 1, 1,                  ///   
                                    cond(inlist(company, 2, 5) == 1, 2,                     ///   
                                    cond(inlist(company, 3, 6) == 1, 3, .)))
            
            // Apply value labels to the country variable                        
            la val country country                        
            
            // Generate EMU identifier
            qui: g byte emu = cond(country == 2, 0, 1)    
            
            // compute abnormal returns for countries and countrygroups (EMU)
            // averages for each country
            egen mcc = mean(cumul), by(year country)  
            
            // emu average based on averaging all emu companies
            egen mec = mean(cumul), by(year emu)         
            
            // Maintain current state of the data in memory
            preserve
            
                // Reduces the data to only unique country by year records
                qui: collapse (firstnm) mcc mec, by(country year)
            
                // Adds variable labels to populate the legend
                la var mcc "Mean Abnormal Returns by Country"
                la var mec "Mean Abnormal Returns by Country Group"
            
                // Sets the data as panel
                xtset country year
                
                // Use xtline to graph the two overlaid lines in subplots
                // Uses the graph scheme from the brewscheme examples
                xtline mcc mec, scheme(ggplot2ex1)
                
                // Export so it can be shared on the StataList
                gr export ~/Desktop/Programs/StataPrograms/stataExamples/post1332782-ex1.png, as(png) replace
            
                // Can also overlay the lines in separate graphs for each variable
                // This scheme file is also illustrated in the brewscheme examples
                xtline mcc, scheme(manycolorex1) overlay
                
                // Export so it can be shared on the StataList
                gr export ~/Desktop/Programs/StataPrograms/stataExamples/post1332782-ex2.png, as(png) replace
            
            // Return to previous state of the data
            restore
            Click image for larger version

Name:	post1332782-ex1.png
Views:	1
Size:	313.8 KB
ID:	1332803
            Click image for larger version

Name:	post1332782-ex2.png
Views:	1
Size:	271.7 KB
ID:	1332804

            Comment


            • #7
              wbuchanan thanks this is awesome! I really appreciate the time you must have put into this! In particular the preserve/restore feature is gold! However as I need country level, and Euro area, I think I would lose these once I collapse by country?

              Just one follow up question: I have 18 countries I would like to plot, but it seems like I cannot plot more than 15, even though data is available. I don't understand why...
              Last edited by Jannic Cutura; 28 Mar 2016, 04:50.

              Comment


              • #8
                Harvey Spector when you use the -egen- commands:

                Code:
                egen mcc = mean(cumul), by(year country)
                egen mec = mean(cumul), by(year emu)
                Each record contains the year by country and year by country-group aggregates (e.g., for all records for the country France the mcc variable has the aggregates specific to France only and the mcc variable has the aggregates based on the value of emu). When the data get collapsed, it is basically removing the values of mcc and mec that are duplicated within country and year (e.g., the value of mcc for a given year for the country France is constant for all firm-level observations within that year and country). I'm not sure what you mean regarding not being able to plot more than 15 though.

                Since we don't know what specifically you tried to do when the second issue arose it is difficult to try to give you any advice. If you show the code you used and the output (or have a reproducible example you can share) it will be much easier for others to give you ideas on what to do.

                Comment

                Working...
                X