Announcement

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

  • Can you help me with error r322 after STATA margins?

    After successfully conducting a tobit regression with STATA I am trying to use the command:
    Code:
    margins, predict (ystar(0,1)) at(X=(0(1)10))
    with X an independent variable in the tobit regression whose values are included from 0 to 10.
    STATA then returns the following error:

    Code:
    missing predicted values encountered within the estimation sample
    r(322);
    Can you please help me to overcome this problem?
    Thank you very much

  • #2
    You have a space between "predict and (ystar(0,1))". Remove the space first.

    Code:
      
     margins, predict(ystar(0,1)) at(X=(0(1)10))
    Roman

    Comment


    • #3
      Thanks Roman, but that is not the problem (I tried it, without success). Please note that if I run the tobit regression with another dependent variable and the same independent and control variables, with the same sample, the problem does not arise...

      Comment


      • #4
        Hi Marco,
        We need a little more. Can you post your full comands from the tobit model , the output and the margins commands (as per forum rule: FAQ, sec-12). The problem could be the margins command itself, but unless we see the whole model, it is difficult to advice I am afraid.
        best,
        Roman

        Comment


        • #5
          Code:
          tobit Y1 c.x1##c.x1 c.x2##c.x2 c.x3##c.x3  c1 c2 c3 c4 c5 i.c6 i.c7 i.c8,ll ul vce(robust) vsquish cformat(%9.3f) pformat(%5.4f) sformat(%8.2f)
          This is the output of the regression:

          Code:
          Tobit regression                                  Number of obs   =         33931
          F(  38,  33893) = 83.13
          Prob > F        =     0.0000
          Log pseudolikelihood = -22908.216                 Pseudo R2       =           0.0596
           
                                        
          Robust
          Y1 Coef.   Std. Err.      t    P>t            [95% Conf.          Interval]
                                        
          x1       0.081      0.005    17.22   0.0000          0.071     0.090
          c.x1#c.x1      -0.006      0.000   -12.31   0.0000           -0.006    -0.005
          x2       0.012      0.005     2.28   0.0227            0.002     0.022
          c.x2#c.x2      -0.001      0.001    -1.20   0.2315            -0.003    0.001
          x3       0.009      0.003     2.93   0.0034            0.003     0.014
          c.x3#c.x3      -0.000      0.000    -1.03   0.3036            -0.001    0.000
          c1       0.125      0.043     2.93   0.0034            0.042     0.209
          c2      -0.038      0.016    -2.38   0.0174           -0.070    -0.007
          c3       0.023      0.008     3.03   0.0024            0.008     0.038
          c4       0.015      0.008     1.92   0.0550            -0.000    0.031
          c5       0.103      0.007    15.27   0.0000           0.090     0.116
          c6
          2        0.073      0.027     2.65   0.0079             0.019     0.126
          3       -0.112      0.017    -6.68   0.0000            -0.145    -0.079
          4       -0.328      0.016   -20.45   0.0000          -0.360    -0.297
          5       -0.210      0.035    -5.97   0.0000            -0.279    -0.141
          6       -0.233      0.015   -16.00   0.0000          -0.261    -0.204
          7       -0.330      0.027   -12.19   0.0000          -0.383    -0.277
          8       -0.272      0.015   -17.68   0.0000          -0.302    -0.241
          9       -0.151      0.054    -2.79   0.0052            -0.257    -0.045
          10        0.059      0.046     1.28   0.2012           -0.031    0.148
          12       -0.314      0.022   -14.05   0.0000        -0.357    -0.270
          13        0.208      0.030     7.04   0.0000           0.150     0.266
          15       -0.016      0.049    -0.33   0.7438         -0.112    0.080
          C7
          2       -0.039      0.061    -0.63   0.5270            -0.159    0.082
          3        0.123      0.044     2.82   0.0048             0.038     0.209
          4       -0.096      0.058    -1.65   0.0987            -0.211    0.018
          5        0.009      0.050     0.18   0.8573             -0.089    0.107
          6        0.066      0.046     1.44   0.1507             -0.024    0.157
          7        0.005      0.045     0.11   0.9158             -0.083    0.093
          8        0.069      0.047     1.47   0.1424             -0.023    0.162
          9        0.157      0.051     3.05   0.0023             0.056     0.257
          10        0.132      0.045     2.94   0.0033           0.044     0.220
          11        0.110      0.046     2.36   0.0181           0.019     0.201
          12        0.094      0.091     1.04   0.3006           -0.084    0.272
          13        0.051      0.045     1.11   0.2650           -0.038    0.140
          14        0.012      0.051     0.23   0.8185           -0.089    0.112
          C8
          1       -0.035      0.008    -4.47   0.0000            -0.050    -0.020
          2       -0.030      0.011    -2.81   0.0050            -0.051    -0.009
          _cons      -0.254      0.046    -5.48   0.0000   -0.344    -0.163
                                        
          /sigma       0.508      0.005                0.498     0.518
                                        
          Obs. summary:      18640  left-censored observations at turnin<=0
          13539     uncensored observations
          1752 right-censored observations at turnin>=1
          then if I use the command

          Code:
          margins, predict (ystar(0,1)) at(c1=(0(1)9))
          I have this in return

          Code:
          missing predicted values encountered within the estimation sample
          r(322);
          Y1 is a percentage included in the range 0-1, please note that instead the command
          Code:
          margins, at(c1=(0(1)9))
          works properly and returns the following output

          Code:
          Predictive margins    Number    of obs   =    33931
          Model VCE    : Robust
          
          Expression   : Linear prediction, predict()
          
          1._at        : c1=           0
          
          2._at        : c1=           1
          
          3._at        : c1=           2
          
          4._at        : c1=           3
          
          5._at        : c1=           4
          
          6._at        : c1=           5
          
          7._at        : c1=           6
          
          8._at        : c1=           7
          
          9._at        : c1=           8
          
          10._at       : c1=           9
          
                      
          Delta-method
          Margin   Std. Err.      z    P>z    [95% Conf.    Interval]
                      
          _at
          1    -.1326868     .00465   -28.53    0.000    -.1418006    -.1235729
          2     -.121772   .0038761   -31.42    0.000    -.1293691    -.114175
          3     -.113147   .0055858   -20.26    0.000    -.124095    -.1021991
          4    -.1068118   .0070486   -15.15    0.000    -.1206267    -.0929969
          5    -.1027662   .0084835   -12.11    0.000    -.1193935    -.0861389
          6    -.1010103   .0110243    -9.16    0.000    -.1226176    -.079403
          7    -.1015442   .0156149    -6.50    0.000    -.1321489    -.0709395
          8    -.1043677   .0225002    -4.64    0.000    -.1484672    -.0602682
          9     -.109481   .0315946    -3.47    0.001    -.1714052    -.0475568
          10     -.116884    .042791    -2.73    0.006    -.2007527    -.0330152
                    
          
          .
          Finally, if I run the regression with Y2, another percentage, the command

          Code:
          margins, predict (ystar(0,1)) at(c1=(0(1)9))
          works properly

          Comment


          • #6

            Try this:
            Code:
              margins, dydx(c1) predict(ystar(0,1)) at(c1=(0(1)9))
            Roman

            Comment


            • #7
              Same sad error...

              Comment


              • #8
                To be honest no idea. Hope someone else can help. But I would go for one last try keeping it within the sample size used for estimation:


                Code:
                 
                 margins if e(sample), dydx(c1) predict(ystar(0,1)) at(c1=(0(1)9))
                Roman

                Comment


                • #9
                  Also this is not working, thanks anyway

                  Comment


                  • #10
                    We can't see what is not working for you. Here is an example that worked for me and you were adviced the same. As the forum rule suggests (FAQ, section-12), you are kindly adviced to show us outputs you get rather just saying "its not working" or whatsoever. That is how you help everyone to help you. I suggest you check your codes and the model. Example:


                    Code:
                    //generate random data:
                    
                     set obs 20000
                    obs was 0, now 20000
                    
                    set seed 12345
                    
                    
                    gen y = (35-5)*runiform()+5
                    
                    gen x1 = (runiform()+2)*y
                    
                    ************************************
                    // Run tobit model:
                    
                    tobit y x1, ll ul vce(robust)
                    
                    Tobit regression                                  Number of obs   =      20000
                                                                      F(   1,  19999) =  245260.88
                                                                      Prob > F        =     0.0000
                    Log pseudolikelihood = -46020.344                 Pseudo R2       =     0.3574
                    
                    ------------------------------------------------------------------------------
                                 |               Robust
                               y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                    -------------+----------------------------------------------------------------
                              x1 |   .3684694    .000744   495.24   0.000     .3670111    .3699278
                           _cons |   1.562869   .0313624    49.83   0.000     1.501396    1.624342
                    -------------+----------------------------------------------------------------
                          /sigma |   2.416326   .0116529                      2.393485    2.439166
                    ------------------------------------------------------------------------------
                      Obs. summary:          1  left-censored observation  at y<=5.0003381
                                         19998     uncensored observations
                                             1 right-censored observation  at y>=34.9995
                    
                    *********************************************
                    
                    //Margins:
                    
                    margins,dydx(x1) predict(ystar(0,1)) at(x1=(0(1)2))
                    
                    Conditional marginal effects                      Number of obs   =      20000
                    Model VCE    : Robust
                    
                    Expression   : E(y*|0<y<1), predict(ystar(0,1))
                    dy/dx w.r.t. : x1
                    
                    1._at        : x1              =           0
                    
                    2._at        : x1              =           1
                    
                    3._at        : x1              =           2
                    
                    ------------------------------------------------------------------------------
                                 |            Delta-method
                                 |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
                    -------------+----------------------------------------------------------------
                    x1           |
                             _at |
                              1  |   .0549094   .0004753   115.53   0.000     .0539779     .055841
                              2  |   .0508102   .0005088    99.86   0.000      .049813    .0518075
                              3  |   .0459515   .0005221    88.01   0.000     .0449281    .0469748
                    ------------------------------------------------------------------------------
                    Roman

                    Comment


                    • #11
                      Hi Roman, I did actually share the output of the margins command, which is the following:
                      Code:
                      missing predicted values encountered within the estimation sample r(322) The output did not change also with the commands you suggested me.
                      Unfortunately I cannot share the dataset due to the obligation of confidentiality, I know this would have been much more effective...

                      Do you know how to manually calculate the output of the following code?
                      Code:
                      margins, predict (ystar(0,1)) at(c1=(0(1)9))
                      As emphasized before, the same exact code (including margins) works for another similar dependent variable, therefore I suspect that the problem may lay in something that happens in the predictions that are generated for this specific dependent variable
                      Last edited by Marco Greco; 28 Jan 2016, 00:18.

                      Comment


                      • #12
                        Have you tried at different values of x? I am guessing there might be something wrong witht the `at' values. The model perhaps failing to estimate the derivatives at either lower or upper end. See what happens if you change the values of x.


                        Code:
                          
                         margins, predict(ystar(0,1)) at(X=(2(1)6))  
                        Roman

                        Comment


                        • #13
                          No way... same error... looks like the problem is in the dependent variable as I get the same problem if I use margins with X1, X2 or X3

                          Comment


                          • #14
                            Have you tried just predicting the ystar values with the original data?

                            I suspect there will be missing values for some observations in the new variable.
                            This is typically caused by extreme linear predictions in some observations.

                            Marco can determine in which observations this happens, and use an if condition to mark those observations out of the sample used by margins.

                            Here is a simple simulated example that reproduces what Marco has been experiencing, along with a possible work-around.

                            Code:
                            . set obs 10000
                            number of observations (_N) was 0, now 10,000
                            
                            . set seed 12345
                            
                            . 
                            . gen x = mod(_n-1, 10)
                            
                            . gen z = rnormal()*8
                            
                            . gen y = normal(x/10 + z - 3.5)
                            
                            . 
                            . sum y
                            
                                Variable |        Obs        Mean    Std. Dev.       Min        Max
                            -------------+---------------------------------------------------------
                                       y |     10,000     .355969     .450515          0          1
                            
                            . count if y == 0
                              1,039
                            
                            . count if y == 1
                              1,447
                            
                            . 
                            . tobit y x z, ll ul
                            
                            Tobit regression                                Number of obs     =     10,000
                                                                            LR chi2(2)        =   18015.81
                                                                            Prob > chi2       =     0.0000
                            Log likelihood = -5.3604234                     Pseudo R2         =     0.9994
                            
                            ------------------------------------------------------------------------------
                                       y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                            -------------+----------------------------------------------------------------
                                       x |   .0076051   .0008824     8.62   0.000     .0058755    .0093347
                                       z |   .0768126    .000472   162.73   0.000     .0758873    .0777378
                                   _cons |   .2926097   .0047131    62.08   0.000      .283371    .3018484
                            -------------+----------------------------------------------------------------
                                  /sigma |   .2268928   .0018294                      .2233068    .2304788
                            ------------------------------------------------------------------------------
                                     1,039  left-censored observations at y <= 0
                                     7,514     uncensored observations
                                     1,447 right-censored observations at y >= 1
                            
                            . 
                            . * the following command exits with an error
                            . cap noi margins, predict(ystar(0,1)) at(x=(0(1)9))
                            missing predicted values encountered within the estimation sample
                            
                            . 
                            . * look at predicted ystar with the original data
                            . predict ystar, ystar(0,1)
                            (1 missing value generated)
                            
                            . sum ystar
                            
                                Variable |        Obs        Mean    Std. Dev.       Min        Max
                            -------------+---------------------------------------------------------
                                   ystar |      9,999    .4078264    .3518931   7.90e-17          1
                            
                            . * mark out observations with missing predicted ystar
                            . gen touse = e(sample)
                            
                            . replace touse = 0 if missing(ystar)
                            (1 real change made)
                            
                            . 
                            . margins if touse, predict(ystar(0,1)) at(x=(0(1)9)) noatlegend
                            
                            Predictive margins                              Number of obs     =      9,999
                            Model VCE    : OIM
                            
                            Expression   : E(y*|0<y<1), predict(ystar(0,1))
                            
                            ------------------------------------------------------------------------------
                                         |            Delta-method
                                         |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
                            -------------+----------------------------------------------------------------
                                     _at |
                                      1  |   .3893445   .0025563   152.31   0.000     .3843342    .3943548
                                      2  |   .3934288   .0021854   180.02   0.000     .3891454    .3977122
                                      3  |   .3975251   .0018568   214.09   0.000     .3938857    .4011644
                                      4  |   .4016329   .0015995   251.10   0.000     .3984979    .4047678
                                      5  |   .4057517   .0014545   278.95   0.000     .4029009    .4086026
                                      6  |   .4098812   .0014585   281.02   0.000     .4070225    .4127399
                                      7  |   .4140209   .0016125   256.76   0.000     .4108605    .4171814
                                      8  |   .4181703   .0018817   222.24   0.000     .4144824    .4218583
                                      9  |    .422329   .0022258   189.74   0.000     .4179665    .4266916
                                     10  |   .4264966   .0026166   163.00   0.000     .4213681     .431625
                            ------------------------------------------------------------------------------

                            Comment


                            • #15
                              Yes indeed, it works! Thank you very much Jeff!

                              Comment

                              Working...
                              X