Announcement

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

  • Graph box plot with loops using var label in ytitle

    I am using Stata/IC 13.1 for Mac (64-bit Intel)

    I want to produce 9 box plots of percentages of nine survey responses coded in nine variables pc_q21_`i'. (i=1…9)

    The variable labels were defined earlier.For example,the coding for the label of the ninth variable q21_9 is as follows:

    label variable q21_9 "Admission cause=none of above"
    label define q21_9 9 "yes",modify
    label values q21_9 q21_9

    Two variables describe the state of residence ("state") and the wave of the survey("wave").There are eight states and three waves.

    The following code sets up the loops for nine box plots of the proportions answering yes to each question, thanks to the graph "var label" hints in the new Cox-Newton 119 Stata Tips book, third edition (see page 263), and the egen code hint also due (I think) to Nick Cox in a recent Statalist response

    forvalues i=1(1)9 {
    egen pc_q21_`i' = mean(100 * (q21_`i' ==`i' & q21_`i' != . )), by(state wave)
    tab pc_q21_`i' wave,missing row col
    graph box pc_q21_`i' if tag, over(state) ytitle("`:var label q21_`i''") title("Percent of patients in PSS reporting", size(vsmall)) saving(AdmCause`i',replace)
    }

    Only in the ninth graph does the variable label (in this case "Admission cause=none of above") appear correctly in the ytitle. In the preceding eight graphs,only the relevant pc_q21_`i' appears.

    Any ideas?

    Paul Gross


  • #2
    I am copying your code and formatting it as such to make it a little more readable.

    Code:
     
    label variable q21_9 "Admission cause=none of above"
    label define q21_9 9 "yes",modify 
    label values q21_9 q21_9
    
    forvalues i=1(1)9 {
         egen pc_q21_`i' = mean(100 * (q21_`i' ==`i' & q21_`i' != . )), by(state wave)
         tab pc_q21_`i' wave,missing row col
         graph box pc_q21_`i' if tag, over(state) ytitle("`:var label q21_`i''") title("Percent of patients in PSS reporting", size(vsmall)) saving(AdmCause`i',replace)
    }
    It is hard to guess what the problem is here. Please show us the results of

    Code:
     
    describe q21_?
    A secondary detail is that the second condition in

    Code:
    q21_`i' == `i'  & q21_`i' != .
    is redundant as observations equal in turn to 1(1)9 cannot be missing. But that does no harm.

    Comment


    • #3
      The suggestion is good.
      Here is the coding that produced two of the variables:
      . label variable q21_5 "Admission cause=DVT"

      . label define q21_5 5 "yes",modify

      . label values q21_5 q21_5

      . tab q21_5,missing

      Admission |
      cause=DVT | Freq. Percent Cum.
      ------------+-----------------------------------
      yes | 215 1.74 1.74
      . | 12,109 98.26 100.00
      ------------+-----------------------------------
      Total | 12,324 100.00

      . label variable q21_6 "Admission cause=infection prev stay"

      . label define q21_6 6 "yes",modify

      . label values q21_6 q21_6

      . tab q21_6,missing

      Admission |
      cause=infec |
      tion prev |
      stay | Freq. Percent Cum.
      ------------+-----------------------------------
      yes | 215 1.74 1.74
      . | 12,109 98.26 100.00
      ------------+-----------------------------------
      Total | 12,324 100.00
      So we seem to have the acceptable label.

      I next ran Nick's suggestion, here is the result:

      . describe q21_?

      storage display value
      variable name type format label variable label
      -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      q21_1 byte %10.0g q21_1 Admission cause=fall
      q21_2 byte %10.0g q21_2 Admission cause=problem with meds
      q21_3 byte %10.0g q21_3 Admission cause=prob with hip replacement
      q21_4 byte %10.0g q21_4 Admission cause=mental health issue
      q21_5 byte %10.0g q21_5 Admission cause=DVT
      q21_6 byte %10.0g q21_6 Admission cause=infection prev stay
      q21_7 byte %10.0g q21_7 Admission cause=pressure sore prev stay
      q21_8 byte %10.0g q21_8 Admission cause=other complication prev stay
      q21_9 byte %10.0g q21_9 Admission cause=none of above

      It seems that the variables are correctly lab led.

      But when I run the series of codes that are intended to produce the nine box plots, I get the same result that produced my query to Statalist

      Tiz a puzzlement as Yul Bruyner once opined.

      Paul Gross

      Comment


      • #4
        That was Yul Brynner. We bald guys stick together. You're referring to "The King and I".

        Poem from the late 1950s

        Tough luck on poor Sid
        His hair was a winner
        Took three years to grow
        Then along came Yul Brynner.

        In terms of your Stata problem, I get no problems with this code

        Code:
         
        clear 
        set obs 42 
        set seed 2803 
        forval j = 1/9 { 
            gen q21_`j' = 100 * runiform() 
        }
        label var q21_1 "Admission cause=fall"
        label var q21_2 "Admission cause=problem with meds"
        label var q21_3 "Admission cause=prob with hip replacement"
        label var q21_4 "Admission cause=mental health issue"
        label var q21_5 "Admission cause=DVT"
        label var q21_6 "Admission cause=infection prev stay"
        label var q21_7 "Admission cause=pressure sore prev stay"
        label var q21_8 "Admission cause=other complication prev stay"
        label var q21_9 "Admission cause=none of above"
        
        forvalues i=1(1)9 {
              graph box q21_`i', ytitle("`:var label q21_`i''") title("Percent of patients in PSS reporting", size(vsmall)) saving(AdmCause`i',replace)
        }
        and I can't see that the differences in your code should make any difference. Are you sure that you are talking about the same version of the same data in the same Stata session? Sorry to descend to that level, but I am out of ideas otherwise.

        Comment


        • #5
          Can I join discussion and ask for the additional two lines of code that would automatically save the graph in GPH and TIF (or similar format). I tried using the code below but did not achieve satisfactory results.
          Code:
              graph save map_`mapvar' "C:\Stata Folder\Graphs\map_`mapvar'.gph", ///
                  replace
                  
              graph export map_`mapvar' "C:\Stata Folder\Exports\map_`mapvar'.tif", ///
                  replace
          Mapvar is defined as a simple local macro and passed to the spmap program. Spmap works flawlessly so I am guessing that I made mistake in the two lines of code concerned with export syntax.
          Kind regards,
          Konrad
          Version: Stata/IC 13.1

          Comment


          • #6
            Someone asked me to give a proper reference. Here it is http://www.cartoons.ac.uk/record/GAA131588

            Comment


            • #7
              Thanks to all.

              Nick:It was the same data and version but I restarted the session.
              The coding below gave me the different state averages, with all nine graphs titled correctly and stored in the Documents folder.

              egen tag = tag(state wave)
              forvalues i=1(1)9 {
              egen pc_q21_`i' = mean(100 * (q21_`i' ==`i' & q21_`i' != .)), by(state wave)
              tab pc_q21_`i' wave,missing row col
              graph box pc_q21_`i' if tag, over(state) ytitle("`:var label q21_`i''") title("Percent of patients in PSS reporting", size(vsmall)) saving(AdmCause`i',replace)
              }

              However in the Graph window only the first and ninth graphs are shown in the vertical panel of graphs, the first titled "AdmCause1" and the last "Graph". To access the others I must open the Graph folder and click on them. A small inconvenience, but no longer a puzzlement to we who are follicly challenged but remember Yul with his surname now spelt correctly.

              Paul Gross

              Comment


              • #8
                A simple tip is that if you name a graph, it remains open in its own window. So an extra option like

                Code:
                name(g1, replace)
                ensures that. First time round, the replace is not needed, but if you like me you usually make a series of small changes it is a good idea for later versions.

                Comment

                Working...
                X