Announcement

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

  • how to plot Mean and 95% CI in longitudinal models

    Hello everyone,

    I'm working on a longitudinal model, I'm using the Long format of the dataset.

    I generated the Mean of my continuous Variable with egen;

    then I used

    collapse (mean) Var, by(Group Time)

    xtset Group Time
    xtline Var, overlay

    so far so good; now how can I add the 95% CI for the 2 Groyups in the same graph?

    Thanks a lot for your help.

  • #2
    Originally posted by Stefano Mastrobuoni View Post

    I'm working on a longitudinal model, I'm using the Long format of the dataset.
    Does this mean that you have several observations for each group-time combination? If so, collapse has the option -semean- for calculating the standard error of the mean. You can generate the group-time means and their standard errors as follows:


    Code:
    collapse (mean) mean= Var (semean) se= Var, by(Group Time)
    You can then generate approximate 95% confidence intervals as:

    Code:
    gen cl = mean - 1.96*se
    gen cu = mean + 1.96*se
    If, on the other hand, you have only one observation for each group-time combination, you cannot estimate within-cell standard errors and hence cannot construct confidence intervals for the group-time means from those observations alone.

    Comment


    • #3
      Originally posted by Stefano Mastrobuoni View Post
      I'm working on a longitudinal model . . .

      I generated the Mean of my continuous Variable with egen;

      then I used

      collapse (mean) Var, by(Group Time)

      . . .

      so far so good; now how can I add the 95% CI for the 2 Groyups in the same graph?
      Like Andrew, I wish that you had provided a better description of your data, perhaps a sample as the FAQ advises or (better) the complete dataset attached to your post.

      Regardless, I recommend not using egen . . . mean() or egen . .. rowmean() or whatever it was in order to get intermediate means, followed by collapse (mean) . . . in order to get the mean of those intermediate means, as this will obscure the sources of variation contributing to the uncertainty represented by the confidence interval.

      Instead, fit a hierarchical regression model in order to explicitly include all sources of variation, then use margins followed by marginsplot in order to plot the group × time cell means and confidence intervals. In the regression model, you would specify i.Group##i.Time interaction as the predictor list in order to get the group × time cell means.

      Also, if you're planning on scanning the overlap of the two groups' confidence intervals in order to assess a difference, then don't do that, either. Instead, plot the timewise differences between the two groups and their confidence intervals, i.e., the confidence interval of the difference between the means at each time point. You can do that, after fitting the appropriate hierarchical model, with margins Time, dydx(Group) followed by marginsplot. For this, it's probably best to have the two groups coded 0/1 as opposed to something arbitrary.

      Comment


      • #4
        Originally posted by Andrew Musau View Post

        Does this mean that you have several observations for each group-time combination? If so, collapse has the option -semean- for calculating the standard error of the mean. You can generate the group-time means and their standard errors as follows:


        Code:
        collapse (mean) mean= Var (semean) se= Var, by(Group Time)
        You can then generate approximate 95% confidence intervals as:

        Code:
        gen cl = mean - 1.96*se
        gen cu = mean + 1.96*se
        If, on the other hand, you have only one observation for each group-time combination, you cannot estimate within-cell standard errors and hence cannot construct confidence intervals for the group-time means from those observations alone.
        Hi Andrew,

        thank you very much for your help. Let me explain my dataset briefly: I have 2 groups of 10 people each; I measured the concentration of 2 metabolites at 8 time points (within one hour) for each participants.
        It was easy to plot the mean concnetration at each time point for the 2 groups; now I want to add the 95% CI for the mean at each time point for each group.

        I have generated the Standard Error and the CI but how can I plot the CI associated to each mean for each group?

        For the mean I used:


        Code:
        xtset Group Time
        xtline Var, overlay
        Thanks a lot for your help.

        Comment


        • #5
          Originally posted by Stefano Mastrobuoni View Post
          I have generated the Standard Error and the CI but how can I plot the CI associated to each mean for each group?
          Use twoway. The CIs can be graphed as lines or area plots (shaded regions). Here is a similar example.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(Group individual Time Var)
          1 1 1  20
          1 1 2  22
          1 1 3  27
          1 1 4  35
          1 1 5  24
          1 1 6  21
          1 1 7  15
          1 1 8  14
          1 2 1  28
          1 2 2  29
          1 2 3  32
          1 2 4  35
          1 2 5  28
          1 2 6  27
          1 2 7  25
          1 2 8  22
          1 3 1  27
          1 3 2  32
          1 3 3  34
          1 3 4  35
          1 3 5  30
          1 3 6  25
          1 3 7  23
          1 3 8  22
          2 1 1 2.5
          2 1 2 5.5
          2 1 3 6.7
          2 1 4 7.2
          2 1 5 8.5
          2 1 6 5.5
          2 1 7 4.5
          2 1 8   3
          2 2 1 2.5
          2 2 2 2.2
          2 2 3 4.8
          2 2 4 7.6
          2 2 5 9.9
          2 2 6 7.1
          2 2 7 9.2
          2 2 8   9
          2 3 1 1.8
          2 3 2   5
          2 3 3 8.2
          2 3 4 7.6
          2 3 5 9.1
          2 3 6 8.8
          2 3 7 7.2
          2 3 8 6.5
          end
          
          
          collapse (mean) mean= Var (semean) se= Var, by(Group Time)
          gen cl = mean - 1.96*se
          gen cu = mean + 1.96*se
          
          tw line mean Time if Group==1, sort || ///
          line mean Time if Group==2, sort || ///
          rarea cl cu Time if Group==1, color(gray%30) || ///
          rarea cl cu Time if Group==2, color(gray%30) ///
          ytitle("XXX concentration levels") xlab(1/8) ||,  ///
          leg(order(1 "Group 1" 2 "Group 2" 3 "95% CI"))

          Click image for larger version

Name:	Graph.png
Views:	1
Size:	53.5 KB
ID:	1787373

          Comment


          • #6
            Originally posted by Andrew Musau View Post

            Use twoway. The CIs can be graphed as lines or area plots (shaded regions). Here is a similar example.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float(Group individual Time Var)
            1 1 1 20
            1 1 2 22
            1 1 3 27
            1 1 4 35
            1 1 5 24
            1 1 6 21
            1 1 7 15
            1 1 8 14
            1 2 1 28
            1 2 2 29
            1 2 3 32
            1 2 4 35
            1 2 5 28
            1 2 6 27
            1 2 7 25
            1 2 8 22
            1 3 1 27
            1 3 2 32
            1 3 3 34
            1 3 4 35
            1 3 5 30
            1 3 6 25
            1 3 7 23
            1 3 8 22
            2 1 1 2.5
            2 1 2 5.5
            2 1 3 6.7
            2 1 4 7.2
            2 1 5 8.5
            2 1 6 5.5
            2 1 7 4.5
            2 1 8 3
            2 2 1 2.5
            2 2 2 2.2
            2 2 3 4.8
            2 2 4 7.6
            2 2 5 9.9
            2 2 6 7.1
            2 2 7 9.2
            2 2 8 9
            2 3 1 1.8
            2 3 2 5
            2 3 3 8.2
            2 3 4 7.6
            2 3 5 9.1
            2 3 6 8.8
            2 3 7 7.2
            2 3 8 6.5
            end
            
            
            collapse (mean) mean= Var (semean) se= Var, by(Group Time)
            gen cl = mean - 1.96*se
            gen cu = mean + 1.96*se
            
            tw line mean Time if Group==1, sort || ///
            line mean Time if Group==2, sort || ///
            rarea cl cu Time if Group==1, color(gray%30) || ///
            rarea cl cu Time if Group==2, color(gray%30) ///
            ytitle("XXX concentration levels") xlab(1/8) ||, ///
            leg(order(1 "Group 1" 2 "Group 2" 3 "95% CI"))

            [ATTACH=CONFIG]n1787373[/ATTACH]
            Dear Andrew,

            thanks a lot! It works perfectly!!

            Many thanks

            Comment

            Working...
            X