Announcement

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

  • Calculating difference between estimated marginal means at 1-year intervals from multilevel growth model

    Hi everyone,

    I am conducting a study on free sugar intake at 2 years ('freesugarsg_av3dayst2' , continuous variable) and BMI growth over a 14.1 year follow up period. Data are in long format. I have estimated marginal means for if intake was 11 g/d and for if it were 43 g/d. I used this code:

    Code:
    * Model 0 with Quadratic Terms
    mixed bmi ageyt2 c.ageyt2#c.ageyt2 c.ageyt2#c.ageyt2#c.ageyt2 freesugarsg_av3dayst2 c.ageyt2#c.freesugarsg_av3dayst2 c.ageyt2#c.ageyt2#c.freesugarsg_av3dayst2 c.ageyt2#c.ageyt2#c.ageyt2#c.freesugarsg_av3dayst2 || famid: || twinid: ageyt2, cov(un)
    * Graph to show curves for 10th and 90th percentile of intake at t2
    margins, at(ageyt2=(0(1)14.1) freesugarsg_av3dayst2=(11 43))
    marginsplot, legend(pos(3) col(3) rows(2)) xlabel(0(1)14.1) xscale(range(0 14.1)) ///
    title (" ", size(small)) ///
    ytitle(BMI (kg/m{sup:2})) ///
    ylabel (, ticks tposition(inside)) ///
    xtitle(Follow up (years)) ///
    xlabel(, tposition(inside) nogrid) ///
    legend(order(1 "11 g/d" 2 "43 g/d") size(small) title("FS intake", size(small)) region(lstyle(none))) ///
    text(12 25 "FS intake", size(small) placement(north) justification(center)) ///
    graphregion(color(white) lwidth(none) lpattern(none) fcolor(white)) //
    graph export "deleted name of location", replace
    margins, at(ageyt2=(0(1)14.1) freesugarsg_av3dayst2=(11 43))
    The results I am interested in for this question are:

    Code:
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
             _at |
              1  |   17.21046   .0575406   299.10   0.000     17.09768    17.32324
              2  |   17.10447   .0632919   270.25   0.000     16.98042    17.22852
              3  |   16.35555   .0534842   305.80   0.000     16.25073    16.46038
              4  |   16.28873   .0589246   276.43   0.000     16.17324    16.40422
              5  |   15.75475   .0539382   292.09   0.000     15.64903    15.86047
              6  |   15.74549   .0598335   263.16   0.000     15.62822    15.86276
              7  |   15.38717   .0566486   271.62   0.000     15.27614     15.4982
              8  |   15.44858    .063258   244.22   0.000      15.3246    15.57257
              9  |   15.23192   .0609106   250.07   0.000     15.11254     15.3513
             10  |   15.37184   .0684589   224.54   0.000     15.23767    15.50602
             11  |   15.26811   .0667524   228.73   0.000     15.13728    15.39895
             12  |   15.48909   .0755538   205.01   0.000     15.34101    15.63717
             13  |   15.47487    .074205   208.54   0.000     15.32943    15.62031
             14  |   15.77416   .0845749   186.51   0.000     15.60839    15.93992
             15  |   15.83129     .08301   190.72   0.000      15.6686    15.99399
             16  |   16.20086   .0951491   170.27   0.000     16.01438    16.38735
             17  |   16.31651   .0926729   176.07   0.000     16.13487    16.49814
             18  |   16.74304   .1066227   157.03   0.000     16.53407    16.95202
             19  |   16.90961    .102663   164.71   0.000      16.7084    17.11083
             20  |   17.37452    .118344   146.81   0.000     17.14257    17.60647
             21  |   17.58973   .1126312   156.17   0.000     17.36898    17.81049
             22  |   18.06912   .1299511   139.05   0.000     17.81442    18.32382
             23  |   18.33598   .1226217   149.53   0.000     18.09564    18.57631
             24  |   18.80067   .1416524   132.72   0.000     18.52303     19.0783
             25  |   19.12745     .13329   143.50   0.000     18.86621     19.3887
             26  |     19.543   .1545147   126.48   0.000     19.24015    19.84584
             27  |   19.94328   .1460968   136.51   0.000     19.65694    20.22963
             28  |   20.26993   .1706911   118.75   0.000     19.93538    20.60448
             29  |   20.76258   .1633585   127.10   0.000      20.4424    21.08275
             30  |   20.95529   .1933842   108.36   0.000     20.57627    21.33432
    ------------------------------------------------------------------------------
    I want to add code to produce a table with the calculated differences in the estimated mean BMI measures between consuming 11 g/d and 43 g/d, with 95% CI. I am imagining there will only be 15 timepoints in this subsequent table (the intercept timepoint and 14 follow up years, instead of 30). Is there a way to do this? Thank you very much for any help you can give me.

    To add, I have tried individually adding the below to the code:

    Code:
    margins, dydx(ageyt2) at(ageyt2=(0(1)14.1) freesugarsg_av3dayst2=(11 43))
    Code:
    margins, dydx(freesugarsg_av3dayst2) at(ageyt2=(0(1)14.1))
    Both result in a table with 30 timepoints and do not seem to account for the estimations for 11 g/d vs 43 g/d intake. Thanks again.
    Last edited by Lisa Heggie; 20 Mar 2024, 18:14. Reason: To add tags

  • #2
    I may be missing something, but I don't think that -margins- can construct the particular table of results you are looking for here. I think you have to run a bunch of -margins- commands and put the results together yourself. Something like this (untested, as no example data provided):
    Code:
    frame create differences int ageyt2 float difference se ll ul
    forvalues a = 0/14 {
        margins, at(ageyt2 == `a' freesugarsg_av3dayst2 = (11 43))
        matrix M = r(table)
        frame post differences (`a') (M["b", 1]) (M["se", 1]) (M["ll", 1]) (M["ul", 1])
    }
    
    frame differences: list, noobs clean
    Added: No doubt you can get a prettier looking table by using -collect-, but I have not yet mastered it well enough to do that. Then again, if you are ultimately going to a Word docoument, using -putdocx- on the data in frame differences at the end will also give you something attractive looking.

    Comment


    • #3
      Hi Clyde,

      Thank you very much for your response, I really appreciate it! I added the code which gave me 15 different tables like this, which makes sense to me:

      Code:
      Expression: Linear prediction, fixed portion, predict()
      1._at: ageyt2           =  0
             freesugarsg_av~2 = 11
      2._at: ageyt2           =  0
             freesugarsg_av~2 = 43
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   std. err.      z    P>|z|     [95% conf. interval]
      -------------+----------------------------------------------------------------
               _at |
                1  |   17.21046   .0575406   299.10   0.000     17.09768    17.32324
                2  |   17.10447   .0632919   270.25   0.000     16.98042    17.22852
      ------------------------------------------------------------------------------
      
      Adjusted predictions                                    Number of obs = 20,934
      However, it finished with this table:

      Code:
       
      . frame differences: list, noobs clean
      
          ageyt2   differ~e         se         ll         ul  
               0   17.21046   .0575406   17.09768   17.32324  
               1   16.35555   .0534842   16.25073   16.46038  
               2   15.75475   .0539382   15.64903   15.86047  
               3   15.38717   .0566486   15.27614    15.4982  
               4   15.23192   .0609106   15.11254    15.3513  
               5   15.26811   .0667524   15.13728   15.39895  
               6   15.47487    .074205   15.32943   15.62031  
               7    15.8313     .08301    15.6686   15.99399  
               8   16.31651   .0926729   16.13487   16.49814  
               9   16.90961    .102663    16.7084   17.11083  
              10   17.58973   .1126312   17.36898   17.81049  
              11   18.33598   .1226217   18.09564   18.57631  
              12   19.12745     .13329   18.86621    19.3887  
              13   19.94328   .1460968   19.65694   20.22963  
              14   20.76258   .1633585    20.4424   21.08275
      I would expect the 'difference' to be e.g. '0.10599' instead of '17.21046' for timepoint 0. I was hoping to be able to have the ll and ul intervals be closer too (something like -0.38 - 0.09). Do you think there is anything I can do to edit the code you kindly shared to do this? I might be missing something too - I am not sure whether this can be done. Thanks a lot for your help.

      Comment


      • #4
        Sorry, I don't know how I messed that up. I know that's not the code I had intended to post--it's something I had written as a starting point before fully solving your problem. Don't know how I ended up copying that instead of the final result. It was supposed to be:

        Code:
        frame create differences int ageyt2 float difference se ll ul
        forvalues a = 0/14 {
            margins, at(ageyt2 == `a' freesugarsg_adv3dayst2 = (11 43)) pwcompare(effect)
            matrix M = r(table_vs)
            frame post differences (`a') (M["b", 1]) (M["se", 1]) (M["ll", 1]) (M["ul", 1])
        }
        
        frame differences: list, noobs clean

        Comment


        • #5
          Hi Clyde,

          No need to apologise, thank you so much for this code. It worked!! I am so grateful for your help. Thanks again.

          PS, so you can see the results:

          Code:
          .    frame differences: list, noobs clean
          
              ageyt2   differe~e         se          ll    ul     
              0   -.1484912   .0899686   -.3248264    .0278439     
              1   -.1108851   .0833992   -.2743445    .0525743     
              2   -.0546862   .0846065   -.2205119    .1111394     
              3    .0147947   .0895664   -.1607522    .1903415     
              4    .0922467     .09713   -.0981246    .282618     
              5    .1723591   .1074091   -.0381588    .382877     
              6    .2498211   .1204276    .0137873    .485855     
              7     .319322   .1356539    .0534452    .5851988     
              8    .3755508   .1521641    .0773145    .6737871     
              9    .4131968   .1690317    .0819009    .7444928     
              10    .4269493    .185725    .0629349    .7909637     
              11    .4114974   .2024974    .0146098    .808385     
              12    .3615303   .2207883   -.0712068    .7942675     
              13    .2717373   .2435524   -.2056167    .7490913     
              14    .1368076   .2752399   -.4026527    .6762678     
              15   -.0485697   .3211334   -.6779796    .5808402
          Thanks again.
          Last edited by Lisa Heggie; 21 Mar 2024, 13:10. Reason: Adding results example

          Comment

          Working...
          X