Announcement

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

  • Loop for xtline with graph combine Stata code

    Dear all, I want to plot calories for the each panel id over the year. Here I have written the loop code, however, there is some error. The error is "graph combine" is not working. Please suggest.

    sysuse xtline1.dta
    egen id=group( person)
    forval i=1(1)2 {
    xtline calories if id==`i', overlay legend (off) saving (id_`i', replace)
    graph combine id_`i'.gph, r(2) iscale (1)
    }


  • #2
    If each graph shows one person then there is nothing to overlay. You can get line graphs side by side -- which is what graph combine does -- directly with say

    Code:
    xtline calories if inlist(person, 1, 2)
    That needs a bit of tidying up, but it does the main job.

    Otherwise the problems here with the graph combine syntax start with a need to specify several graphs to combine, which means once your loop is finished. The point is perhaps best made with a different example.

    Code:
    sysuse auto, clear 
    
    foreach v in mpg price length headroom { 
        scatter `v' weight, name(G`v') ms(Oh)
        local Gall `Gall' G`v'
    }
    
    graph combine `Gall'
    We loop over a series of graphs, giving each a name. Every time we do that we put the new name into a bag. a local macro containing all the names so far. Then when the loop is done, we fire up graph combine with all the names so far.

    Otherwise put, graph combine is not sequential, and doesn't mean "combine this graph with the others so far in my loop".

    Comment


    • #3
      #2 made the main points.

      In addition pushing person through egen, group() maps a variable with values 1 2 3 to another variable with values 1 2 3. It does no harm but is redundant in this example. It may be a good idea for the real problem you don't show us if identifiers are irregularly spaced.

      More serious problems, which may be what triggered your error message, are the spaces in your option calls.

      Code:
      legend (off) saving (id_`i', replace)
      The arguments of options should not be separated from their names.

      Code:
      legend(off) saving(id_`i', replace)

      Comment


      • #4
        Originally posted by Nick Cox View Post
        If each graph shows one person then there is nothing to overlay. You can get line graphs side by side -- which is what graph combine does -- directly with say

        Code:
        xtline calories if inlist(person, 1, 2)
        That needs a bit of tidying up, but it does the main job.

        Otherwise the problems here with the graph combine syntax start with a need to specify several graphs to combine, which means once your loop is finished. The point is perhaps best made with a different example.

        Code:
        sysuse auto, clear
        
        foreach v in mpg price length headroom {
        scatter `v' weight, name(G`v') ms(Oh)
        local Gall `Gall' G`v'
        }
        
        graph combine `Gall'
        We loop over a series of graphs, giving each a name. Every time we do that we put the new name into a bag. a local macro containing all the names so far. Then when the loop is done, we fire up graph combine with all the names so far.

        Otherwise put, graph combine is not sequential, and doesn't mean "combine this graph with the others so far in my loop".
        Thank you very much. The code xtline calories if inlist(person, 1, 2) is working when the 'person' variable is in float format. However, my original panel data, the variable (i.e., country) is in 'str14' format which contains nonnumeric characters. Therefore, i am getting error. Also, please explain the code inlist(person, 1, 2)

        Comment


        • #5
          Code:
          help inlist() 
          explains the function in question and

          Code:
          search inlist
          points to more resources. You aren't new to Stata or Statalist so the advice at https://www.statalist.org/forums/help#before fairly applies.


          If you wanted say to select 4 countries out of several you could go say


          Code:
          ... if inlist(country, "A", "B", "C", "D")

          where naturally the ... stand for your main code and you need to use the names in question. The detail always needed is " " around literal strings.

          If you wanted to plot all panels, you don't need any such qualifier, but your example back in #1 was selecting 2 panels out of 3.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Code:
            help inlist() 
            explains the function in question and

            Code:
            search inlist
            points to more resources. You aren't new to Stata or Statalist so the advice at https://www.statalist.org/forums/help#before fairly applies.


            If you wanted say to select 4 countries out of several you could go say


            Code:
            ... if inlist(country, "A", "B", "C", "D")

            where naturally the ... stand for your main code and you need to use the names in question. The detail always needed is " " around literal strings.

            If you wanted to plot all panels, you don't need any such qualifier, but your example back in #1 was selecting 2 panels out of 3.
            Thank you so much. Now its working

            Comment

            Working...
            X