Announcement

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

  • r(mean) , panel subgroup

    Dear Statalisters,

    I saw this old forum discussion , my question is about understanding the code :

    original forum list is here
    https://www.stata.com/statalist/arch.../msg01036.html


    Johanna asked the question
    Code:
    I have a panel of European regions over time. I would like to plot a
    variable against time for these regions grouped by countries - that is, I would
    like to have all the regions of one country in one plot, and the plots of all
    countries together on the same overall graph.
    Nick helped him/her out with a code sketch here:
    Code:
    egen country2 = group(country) 
    sum country2, meanonly 
    
    forval i = 1/`r(mean)' { 
        xtline response time if country2 == `i' , overlay name(gr`i') 
        local graphs `graphs' gr`i'
    } 
    
    graph combine `graphs'

    i know Nick uses egen to generate country code . I think the sum function meanonly, will generate the mean of the country2 variable.

    e.g. original data has 3 countries, then

    sum country2, meanonly

    gives 2 ?

    my question is about what value goes in to the forval loop, is it the average 2 from above? without the data of the orignal poster and my limited stata knowledge, I am not sure about my interpretation.

    Thank you in advance !

    Code:
    forval i = 1/`r(mean)'
    Rochelle

  • #2
    Sorry; the result used should be r(max) not r(mean).

    Comment


    • #3
      Hi Nick, thank you for the correction !

      why do you use

      Code:
       
       sum country2, meanonly
      would it be the same effect if I use

      Code:
      sum country2, d
      if the goal is to feed the r(max) to the loop ?

      Comment


      • #4
        Look at the manual entry for summarize. All I want is the maximum. None of the extra stuff detail calculates is of interest. There is no point even to a plain summarize.

        Comment


        • #5
          thank you, Nick.

          From manual, it says:

          meanonly suppress the display; calculate only the mean;


          what I am confused about is the loop requests

          r(max)

          so you are saying

          Code:
           
            sum country2, meanonly
          still calculates r(max) and keep it in memory, right?


          Comment


          • #6
            Rochelle:
            strange as it may seem (LOL), Nick is right, as you can see from the following toy-example:
            Code:
            . sysuse auto.dta
            (1978 Automobile Data)
            
            . sum price, meanonly
            
            . return list
            
            scalars:
                              r(N) =  74
                          r(sum_w) =  74
                            r(sum) =  456229
                           r(mean) =  6165.256756756757
                            r(min) =  3291
                            r(max) =  15906
            
            . di r(max)
            15906
            
            .
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              You're right that the manual doesn't explain this very well. https://journals.sagepub.com/doi/pdf...867X0700700311 was an attempt to do better.

              Comment


              • #8
                Thanks Nick again ! Also, many thanks to Carlos for the code sketch !

                I tried the following code (post #1 above) for my panel data - replacing the variable names "response time" with my variable of interest x and year

                Code:
                sum cpair, meanonly 
                
                forval i = 1/`r(max)' { 
                    xtline x year if cpair == `i' , overlay name(gr`i') 
                    local graphs `graphs' gr`i'
                } 
                
                graph combine `graphs'
                option overlay may not be specified with multiple variables

                This is my first time using xtline, I searched some posts for discussion on overlay, but did not know how to fix this.

                My data : company annual observation across time, the panel is multiple firms multiple years, x variable is firm-year specific. cpair takes 0 or 1 value that stratify the sample into 2 subgroups.

                Sincerely,

                R

                Comment


                • #9
                  The error message explains itself. The syntax is -- from the help --

                  xtline varname [if] [in], overlay [overlaid_options]
                  and in this flavour of the command, and generally, you need not (must not!) specify the time variable.


                  Comment


                  • #10
                    Thanks Nick. I thought your original posting was using panelid (response) and yearid (time) . I misunderstood it.

                    Comment

                    Working...
                    X