Announcement

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

  • Limiting range on vertical axis on qnorm by variable group

    I have a variable called 'expenditure' and another variable called 'location', which contains a group of location. If I want to see the histogram distribution of expenditure in a specific location, I can do so by
    Code:
    histogram expenditure if location=="CityA", discrete frequency
    Ideally, I'd like to use qnorm, instead, e.g.
    Code:
    qnorm expenditure if location=="CityA"
    However, when I use the the above qnorm command and add the "if" statement to specify my desired location, the y-axis of the resulting graphs takes the whole range of my 'expenditure' variable, rather than the range of my 'expenditure' variable that are relevant for my specified location. This results in some graphs being flat and completely unusable.

    Is there a way to generate graphs with qnorm in which the y-axis contains the range of my 'expenditure' variable, but only limited to, say, location=="CityA" ?


  • #2
    Try this

    Code:
    preserve 
    keep if location == "CityA"
    qnorm expenditure 
    restore

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Try this

      Code:
      preserve
      keep if location == "CityA"
      qnorm expenditure
      restore
      Hi Nick, thank you for your input. That works, yet I should've mentioned what my ultimate goal is. I want to generate qnorm graphs not just for one specified location, but for all (i.e. 30 cities), and then place them next to each other to compare their distributions. Something like this, where each graph has the appropriate range on the vertical side.

      Do you have any suggestions if this is what I'm ultimately after?

      Comment


      • #4
        So, that's a loop over cities then.

        But notice the trade-off. Comparisons will be easier if the data fill each graph, but more difficult if the graphs are on different scales.

        You will need a big sheet of paper or a big monitor to compare 30 graphs easily.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          So, that's a loop over cities then.

          But notice the trade-off. Comparisons will be easier if the data fill each graph, but more difficult if the graphs are on different scales.

          You will need a big sheet of paper or a big monitor to compare 30 graphs easily.
          Indeed. The plan is to make a poster of large dimensions. I am not too familiar with Stata, but I think I may be confusing the syntax to write out the proper commands. For a loop over "groups" of a variable, am I misconstruing something by starting off with the below?

          Code:
          foreach x in location

          Comment


          • #6
            See e.g. http://www.stata.com/support/faqs/da...ach/index.html

            Perhaps (warning: completely untested)

            Code:
            egen group = group(location), label 
            su group, meanonly 
            
            forval j = 1/`r(max)' { 
                preserve
                local label : label (group) `j' 
                keep if group == `j' 
                qnorm expenditure, subtitle("`label'") name(g`j') 
                local names `names' g`j' 
                restore 
            }
            
            graph combine `names'

            Comment


            • #7
              Thanks again, Nick. It worked great, except for the label option on the 'egen' command. But that's a nonissue. I want to add the "capture" command to the loop, so that the graph generation process doesn't stop if a city has no observations.

              Where does one introduce "capture" in loops? I tried different options, but I'm starting to doubt "capture" is the right command for what I'm after.

              Comment


              • #8
                What went wrong with the label option? I need a report on what you actually used and a precise error report to suggest better code.

                You don't need capture here, I think. Tackle the problem at source.

                Code:
                 
                 egen group = group(location) if expenditure < ., label
                will automatically ignore cities without non-missing values.

                Comment


                • #9
                  Including the 'label' option gives the error "too few quotes". And I just tried your last suggestion, it worked great. I did realize, however, that the reason the graph loop kept getting stuck was not non-missing values, but rather too few values, i.e. some cities only have one expenditure data point.

                  Is there a way to "force" the loop, so it just ignores those cases where there aren't enough data points to generate a graph?

                  EDIT: the loop continued, forcefully, by adding "capture" before "qnorm", within the loop code. This then created another issue in the "graph combine `names'" command: "c114 is not a memory graph". This is the same city that was causing my previous problem. I tried "capture" before the "graph combine" command, and no error was given, but no graph was generated. I am out of luck for now it seems.
                  Last edited by Ernestina delPiero; 31 Jan 2017, 17:19.

                  Comment


                  • #10
                    Show the results of

                    Code:
                    tab location
                    To remove singletons, do this:

                    Code:
                    bysort location : egen OK  = count(expenditure) 
                    replace OK = OK > 1 
                    bysort OK location : egen group = group(location) if OK 
                    su group, meanonly
                    and so forth.

                    Comment


                    • #11
                      Thanks again. It worked. Yet it seems that 'bysort' cant be used with 'group()'. The following error came up: "egen ... group() may not be combined with by"

                      In any case, I ran it simply as "egen group = group(location) if OK" and it seems like it work. It obvious to me why "bysort" would be needed here.

                      Comment


                      • #12
                        Sorry; that was indeed my silly mistake. The code should have been

                        Code:
                         
                         egen group = group(location) if OK

                        Comment

                        Working...
                        X