Announcement

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

  • Graphics, collapse, addplot by subgroups

    Hi everyone,

    If you combine graphs by using addplot, and you want to see the combination graphs from two subgroups, how should you/can you use the by(subgroup) -option? Example:
    twoway scatter TMGLUC min,sort
    preserve
    collapse (mean) mean_T= TMGLUC (semean) se_T= TMGLUC (sd) sd_T= TMGLUC (count) n_T= TMGLUC (p95) p95_T= TMGLUC,by(min)
    gen lo= mean_T-1.96* se_T
    gen hi= mean_T+1.96* se_T
    graph addplot line mean_T lo hi min,sort legend(off)
    restore
    I would like to have both the scatter and the collapse -yieldings for both groups; let's say subgroups is sex (male=1, female=2). So, where should I add the by(sex)?
    I have tried to add it to the twoway -command, and that gives me two scatterplots but w/o the collapse-products, and if I stuff by(sex) to other script lines, it won't be accepted.

    Anyone?

    Thanks in advance!

    Mikko

  • #2
    You posted this before http://www.statalist.org/forums/foru...s-in-one-graph and got no answer.

    I know why I didn't answer and correspondingly I guess why no one else did.

    You didn't provide any example data or show your graph so far. So, anyone answering would have to work quite hard to see what's going on. Busy people often won't answer a question if it requires hard work.

    Use dataex (SSC) to create an example. Show your graph as .png (FAQ Advice #12).

    Comment


    • #3
      Yes, I thought it did not get visibility there But thanks for the instructions. This is a time series data of test meal results where glucose has been measured 0,20,40 etc min, like:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str10 patient int(min sex) double TMGLUC
      "5008"   0 2 5.4
      "5008"  20 2 6.6
      "5008"  40 2 7.6
      "5008"  70 2 5.9
      "5008" 100 2 7.5
      "5008" 130 2 9.1
      "5008" 160 2 8.9
      "5008" 190 2 7.5
      "5195"   0 1 4.7
      "5195"  20 1   5
      "5195"  40 1 7.1
      "5195"  70 1 6.9
      "5195" 100 1 6.3
      "5195" 130 1 6.1
      "5195" 160 1 5.7
      "5195" 190 1 5.2
      "5826"   0 1 4.4
      "5826"  20 1   6
      "5826"  40 1 7.7
      "5826"  70 1 5.7
      end
      By using the script in my first mail, it gives this kind of graph including all observations:
      Click image for larger version

Name:	Statalist1.png
Views:	1
Size:	65.9 KB
ID:	1337747



      And if i add twoway scatter TMGLUC min,sort by(sex):
      Click image for larger version

Name:	Statalist2.png
Views:	1
Size:	58.5 KB
ID:	1337748



      I.e. what I miss are the hi, mean and lo -lines. I have tried to add the by(sex)to different places but no luck.
      Last edited by Mikko Lehtovirta; 27 Apr 2016, 03:44.

      Comment


      • #4
        Mikko, the following allows you to accomplish your task with a single twoway command (no need to collapse your data)

        Code:
        bysort min sex: egen mean_T=mean(TMGLUC) 
        bysort min sex: egen sd_T=sd(TMGLUC)
        bysort min sex: gen se_T= sd_T / sqrt(_N)
        gen lo= mean_T-1.96* se_T
        gen hi= mean_T+1.96* se_T
        egen tag=tag(min sex)
        twoway scatter TMGLUC min,sort by(sex, legend(off)) || line mean_T lo hi min if tag==1,sort
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Carole, perfect! Thank you so much! You know, when you have a hammer, everything looks like nails. I was unable to think any other way.
          This made my day
          Best wishes,
          Mikko

          Comment

          Working...
          X