Announcement

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

  • Generating mean of a score within bands of age

    Hi everybody,

    I have 2 variables which are measured continuously, "LOP" and "Age"
    I would like to split the age into 5 year age bands.

    Then, I would like to calculate the mean age and mean LOP within each age band.
    (ie, I would like to know the mean age within each Age Band, and its corresponding mean LOP)

    Then, I would like to plot each of these, with their corresponding standard errors, and then fit a line of best fit between these points.

    I have absolutely no idea how to do this, and I am very lost. Any help from anybody would be extremely appreciated.

    Thank you.








  • #2
    Alan:
    you might be interested in something along the following lines:
    Code:
    . sysuse auto.dta
    (1978 Automobile Data)
    
    . reg price i.rep78
    
          Source |       SS           df       MS      Number of obs   =        69
    -------------+----------------------------------   F(4, 64)        =      0.24
           Model |  8360542.63         4  2090135.66   Prob > F        =    0.9174
        Residual |   568436416        64     8881819   R-squared       =    0.0145
    -------------+----------------------------------   Adj R-squared   =   -0.0471
           Total |   576796959        68  8482308.22   Root MSE        =    2980.2
    
    ------------------------------------------------------------------------------
           price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |
              2  |   1403.125   2356.085     0.60   0.554    -3303.696    6109.946
              3  |   1864.733   2176.458     0.86   0.395    -2483.242    6212.708
              4  |       1507   2221.338     0.68   0.500    -2930.633    5944.633
              5  |     1348.5   2290.927     0.59   0.558    -3228.153    5925.153
                 |
           _cons |     4564.5   2107.347     2.17   0.034     354.5913    8774.409
    ------------------------------------------------------------------------------
    
    
    . margins i.rep78
    
    Adjusted predictions                            Number of obs     =         69
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           rep78 |
              1  |     4564.5   2107.347     2.17   0.034     354.5913    8774.409
              2  |   5967.625   1053.673     5.66   0.000     3862.671    8072.579
              3  |   6429.233   544.1145    11.82   0.000      5342.24    7516.227
              4  |     6071.5   702.4489     8.64   0.000     4668.197    7474.803
              5  |       5913   898.5756     6.58   0.000     4117.889    7708.111
    ------------------------------------------------------------------------------
    
    . marginsplot
    
      Variables that uniquely identify margins: rep78
    
    .
    Attached Files
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3


      Thanks Carlo, this is the exact type of graph I am looking for!

      I managed to sort it all out; do you know if there is a way to show this main line, as well as seperate lines, one for males and one for females?

      Last edited by Alan Jeddi; 04 Jul 2018, 10:57.

      Comment


      • #4
        Alan:
        I find difficult to reply without taking a look at what you typed and what Stata gave you back.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Originally posted by Carlo Lazzaro View Post
          Alan:
          I find difficult to reply without taking a look at what you typed and what Stata gave you back.
          Hi Carlo,
          My apologies. I generated a new variable, age5, which is my original age cut into 5 year bands, using the egen.
          My code for the plot was:

          regress iopcc_out i.age5 sex
          margins i.age5, by(i.sex)
          marginsplot

          regress iopcc_out i.age5
          margins i.age5
          margins plot

          I end up producing the following graphs.
          This is in the ballpark of what I want, but it seems like the margins when stratified by sex are identical so I think I must have done something incorrectly.
          What I am really hoping to have is a single chart, with 3 lines (one overall, one for females and one for males)..







          Last edited by Alan Jeddi; 04 Jul 2018, 19:21.

          Comment


          • #6
            charts
            Click image for larger version

Name:	Screen Shot 2018-07-05 at 2.18.00 AM.png
Views:	1
Size:	50.0 KB
ID:	1451951
            Click image for larger version

Name:	Screen Shot 2018-07-05 at 2.18.53 AM.png
Views:	1
Size:	41.2 KB
ID:	1451952

            Comment

            Working...
            X