Announcement

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

  • Plotting distribution by group in separate graphs

    Hi. I have data on drug names under a variable named "Product" with corresponding payments under a variable named "rxsf". I want to plot the distribution of payments for each drug separately with the file titaled with the drug name. So, in the code below, I'd have one graph that illustrates the distribution of FEMRING and this file would be titled "femring", followed by a separate file each for VYTORIN, TRULICITY, etc.

    I'm a little confused on how to do this since it's the distribution of one variable (rxsf) by different categories (drug names) but where the drug names don't follow a specific pattern to call them out separately in the code, and since a separate graph is to be generated for each drug. Any help will be much appreciated.


    Code:
    input str35 Product float rxsf
    "FEMRING"        40
    "FEMRING"       200
    "FEMRING"        75
    "FEMRING"       105
    "FEMRING"        60
    "VYTORIN"        40
    "VYTORIN"         0
    "VYTORIN"        55
    "VYTORIN"        40
    "TRULICITY"        55
    "TRULICITY"        55
    "TRULICITY"  50.09998
    "TRULICITY"        40
    "TRULICITY"       150
    "TRULICITY"       375
    "TRULICITY"        60
    "TALTZ"              90
    "TALTZ"            0
    "TALTZ"           35
    "TALTZ"     46.65997
    "TALTZ"          100
    "TALTZ"           35
    "TALTZ"           80
    "TALTZ"           80
    "TALTZ"           80

  • #2
    if there are only the four, you can just go
    Code:
    density rxsf if Product=="Taltz'
    and add whatever options you want

    if you want to do this in a loop, see
    Code:
    help levelsof
    and look at the examples where you are shown how to use this command as a lead in to -foreach-

    Comment


    • #3
      Rich Goldstein Thanks. Unfortunately there are 300+ unique drug names, so I ran the following loop. However, I get the error message pasted below. I am using Stata/BE 17 and am not sure what to do here. Is there any other workaround to this?

      Code:
      levelsof rxsf, local(levels)
      
       foreach l of local levels {
        histogram rxsf, frequency fcolor("0 64 128") ytitle(Frequency) ///
      ytitle(, size(small)) xtitle(OOP) xtitle(, size(small))) ///
      title(Distribution of OOP at the Claim Level, size(medsmall))
      
      graph export "$output/Distribution by drug/`l'.png", replace
      }
      
      Error:
      levelsof rxsf, local(levels)
      macro substitution results in line that is too long
          The line resulting from substituting macros would be longer than allowed.  The maximum allowed length is 264,408 characters, which
          is calculated on the basis of set maxvar.
      
          You can change that in Stata/SE and Stata/MP.  What follows is relevant only if you are using Stata/SE or Stata/MP.
      
          The maximum line length is defined as 16 more than the maximum macro length, which is currently 264,392 characters.  Each unit
          increase in set maxvar increases the length maximums by 129.  The maximum value of set maxvar is 32,767.  Thus, the maximum line
          length may be set up to 4,227,159 characters if you set maxvar to its largest value.
      macro substitution results in line that is too long

      Comment


      • #4
        You want the levels of Product, not of expenditure. But there is another problem. Each time around the loop you're asking for exactly the same graph for all values of Product. The loop does not instruct the code inside the loop to vary each time around the loop: your code inside the loop must vary for this command to work as you want.

        You'll possibly find it simpler to go

        Code:
        egen product = group(Product), label 
        su product
        and then loop over 1 to the maximum product number. But you still need to put an if qualifier on each graph command and arrange for a subtitle to show which graph is which.

        I don't know how easy you'll find it to make sense of a portfolio of hundreds of graphs.

        Comment


        • #5
          Here's a sketch. Untested code. See method 1 at https://www.stata.com/support/faqs/d...-with-foreach/

          Code:
          egen product = group(Product), label 
          
          local opts frequency fcolor("0 64 128") ytitle(Frequency) xtitle(OOP) 
          local opts `opts' title(Distribution of OOP at the Claim Level, size(medsmall))
          
          su product 
          
          forval g = 1/`r(max)' { 
              local which : label (product) `g' 
              histogram rxsf if product == `g', `opts' subtitle("`which'") 
              graph export "$output/Distribution by drug/`g'.png", replace
          }

          Comment


          • #6
            Nick Cox Thank you! That worked perfectly to generate the graphs. Is there anyway I can get the individual graphs to save with the drug name in place of the `g'? At this point it saves as a number. And the ytitle and xtitles also did not work. I'm not sure how the locals are to work here
            Last edited by Scott Rick; 15 May 2022, 09:38.

            Comment


            • #7
              Naturally. Just as you used the drug name for the graph subtitle, so also you can use it for the filename.

              Comment


              • #8
                Apologies for the multiple messages. I got it to work with the modified code below. Thanks again, Nick!

                Code:
                  
                egen product = group(Product), label    
                
                su product  
                
                forval g = 1/`r(max)' {      
                                local which : label (product) `g'      
                                histogram rxsf if product == `g', frequency fcolor("0 64 128") ytitle(Frequency) xtitle(OOP) ///    
                                title(Distribution of OOP at the Claim Level, size(medsmall)) subtitle("`which'")      
                                graph export "$output/Distribution by drug/`which'.png", replace
                }
                Last edited by Scott Rick; 15 May 2022, 10:21.

                Comment

                Working...
                X