Announcement

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

  • margins at percentiles

    Hi Statalists,

    I have code like this:

    Code:
    regress y x1 x2 x3
    egen p10 = pctile(x1), p(10)
    egen p20 = pctile(x1), p(20)
    margins, at (x1= (p10 p20))
    which gives me the error "invalid numlist".

    I know that x1 is between 0 and 1, so my first (and prefered) attempt was

    Code:
    margins, at (x1=(0(0.1)1))
    but margins only works, if 0, 0.1, 0.2, ... are explicitly in my data, which is not the case, as I rather have values like 0.1223123 in my data set.

    Can you help me to make either the first (with percentiles) or the second version (evaluation margins at values not in the data set) work? I have the feeling it's just about type casting the variables p10 and p20 correctly.

    I really tried to find help online but very different errors are treated under "invalid numlist".

    All your help is highly appreciated!

    Best,
    Chris


  • #2
    Fantasy syntax unfortunately, but there is an alternative that is no more work. What you want to specify are some constants, so putting them in variables is overkill.

    Code:
    sysuse auto, clear
    gen recmpg = 100/mpg
    regress recmpg weight
    _pctile weight, p(10 20)
    margins, at(weight = (`r(r1)' `r(r2)'))

    Comment


    • #3
      Nick, thank you so much, this works! Coming from R, I still need to get used to the Stata syntax.

      Now that the margins command actually works, for the "simple" regression it says "not estimable" for the lower percentiles. If I extend this to my full regression model, it cannot estimate any margin. My understanding of margin is that it is the prediction of the regression model at certain values. It should be possible to compute these for all values of x (D1_sd in my code below). Can anyone give me an intuition, why this happens? Or help me fixing it, if it can be fixed?


      Code:
      . regress Stackel_Q  c.D1_sd##c.D1_sd##c.D1_sd##c.D1_sd
      
            Source |       SS       df       MS              Number of obs =   19000
      -------------+------------------------------           F(  4, 18995) =  958.20
             Model |  1043821.67     4  260955.417           Prob > F      =  0.0000
          Residual |  5173086.67 18995  272.339387           R-squared     =  0.1679
      -------------+------------------------------           Adj R-squared =  0.1677
             Total |  6216908.33 18999  327.222924           Root MSE      =  16.503
      
      -------------------------------------------------------------------------------------------------
                            Stackel_Q |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
      --------------------------------+----------------------------------------------------------------
                                D1_sd |   .1949844   .0045501    42.85   0.000     .1860658     .203903
                                      |
                      c.D1_sd#c.D1_sd |  -.0003915   .0000189   -20.67   0.000    -.0004286   -.0003543
                                      |
              c.D1_sd#c.D1_sd#c.D1_sd |   1.99e-07   1.15e-08    17.39   0.000     1.77e-07    2.22e-07
                                      |
      c.D1_sd#c.D1_sd#c.D1_sd#c.D1_sd |  -2.48e-11   1.51e-12   -16.43   0.000    -2.78e-11   -2.19e-11
                                      |
                                _cons |   16.13776   .2007369    80.39   0.000     15.74429    16.53122
      -------------------------------------------------------------------------------------------------

      Code:
      . _pctile D1_sd, p(10 20 30 40 50 60 70 80 90 )
      
       margins, at (D1_sd= (`r(r1)' `r(r2)' `r(r3)' `r(r4)' `r(r5)' `r(r6)' `r(r7)' `r(r8)' `r(r9)' ))
      
      Adjusted predictions                              Number of obs   =      19000
      Model VCE    : OLS
      
      Expression   : Linear prediction, predict()
      
      1._at        : D1_sd           =    7.010523
      
      2._at        : D1_sd           =     11.0442
      
      3._at        : D1_sd           =    16.23711
      
      4._at        : D1_sd           =    23.91321
      
      5._at        : D1_sd           =    33.50388
      
      6._at        : D1_sd           =    59.51202
      
      7._at        : D1_sd           =    73.52291
      
      8._at        : D1_sd           =    93.14239
      
      9._at        : D1_sd           =    151.2926
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
      -------------+----------------------------------------------------------------
               _at |
                1  |          .  (not estimable)
                2  |          .  (not estimable)
                3  |          .  (not estimable)
                4  |          .  (not estimable)
                5  |   22.23854    .128628   172.89   0.000     21.98642    22.49066
                6  |   26.39697    .136241   193.75   0.000     26.12992    26.66401
                7  |   28.43602   .1527117   186.21   0.000     28.13669    28.73535
                8  |   31.06222    .178869   173.66   0.000     30.71162    31.41282
                9  |   37.35465   .2470615   151.20   0.000     36.87038    37.83891
      ------------------------------------------------------------------------------
      
      .

      Comment


      • #4
        Hi everyone, I am using linear mixed models to analyse foetal growth data (e.g., head circumference, HC). From the LMM, I am predicting the mean HC at different gestational ages using the margins command.

        However, I want to predict the median, 10th and 3rd centile head circumference. How can I do this?

        Please see my code for mean prediction. Note, hivpositive (HIV status) is my main exposure which is inserted in the model as a covariate, and I am predicting the means by HIV status.

        Linear mixed model:
        mixed HC_mm i.hivpositive ///
        age_mat ib1.bmicat_bas_mat i.smoke_mat i.alcohol_mat i.nullipara_cat i.maristat ///
        i.sex_foet i.ironpreg i.folpreg i.wealth_index3 i.occup i.apo_bas ///
        GA_div10_sq GA_div10_sqln || ///
        id: ga_at_usd_visits_wks_, ///
        cov(unstructured) nolog

        Predicting mean HC at 16 weeks:
        margins hivpositive, at(GA_div10_sq = 2.56 GA_div10_sqln = 1.2032093) //16 weeks

        Your kind help is highly appreciated.

        Comment

        Working...
        X