Announcement

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

  • Creating box plot for 13 variables

    Hi
    I work with the prescription registry data. I have 13 drugs, and each drug is used in 4 different follow-up periods. I have to describe my data using a box plot that illustrates data for 13 drugs with 4 follow-up periods for each drug. My data looks as follow,

    foreach x in a b c d e f g h i j k l m {
    `x'_bl_pl90 `x'_pl91_pl180 `x'_pl181_pl270 `x'_pl271_pl365
    }

    In this example, a b c d e f g h i j k l m are the 13 drugs
    `x'_bl_pl90 is the data for x drug during follow-up from baseline to 90 days
    `x'_pl91_pl180 is the data for x drug during follow-up from 91-180 days
    `x'_pl181_pl270 is the data for x drug during follow-up from 181-270 days
    `x'_pl271_pl365 is the data for x drug during follow-up from 271-365 days

  • #2
    So you really have 52 variables to plot? That is possible, but the result will be very crowded. Also, at least with graph box, it will be a struggle to see which box belongs to which variable.

    Code:
    clear 
    set obs 100 
    
    forval j = 1/52 {
        gen x`j' = rnormal(0, 1)
    }
    
    graph box x*

    Comment


    • #3
      Thank you Nick, could you please explain what ' j ', normal (0,1) takes in the value? My data are ratios for the variables that I mentioned above.

      Comment


      • #4
        That is just some code faking 52 variables to show for anyone curious what 52 box plots look like.

        I created them using a loop. You can learn more about such loops from (e.g.)

        Code:
        help forvalues
        or

        Code:
        SJ-21-2 pr0074_1  . . . . . .  Erratum: Speaking Stata: Loops, again and again
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q2/21   SJ 21(2):555                                     (no commands)
                erratum for a line of code
        
        SJ-20-4 pr0074  . . . . . . . . . . . . Speaking Stata: Loops, again and again
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q4/20   SJ 20(4):999--1015                               (no commands)
                provides updated guidance on using foreach and forvalues for
                looping through lists of values and repeating commands using
                members of those lists in turn
        Clearly I don't have your data, and conversely the code is not needed by you because you do.

        Comment


        • #5
          Great !!, Thank you

          Comment

          Working...
          X