Home  /  Resources & support  /  FAQs  /  Scaling and marginal effects
Note: This FAQ is for Stata 10 and older versions of Stata.

In Stata 11, the margins command replaced mfx.

When I run mfx, I am getting the warning message "warning: derivative missing; try rescaling variable mpg". What does that mean?

Title   Scaling and marginal effects
Author May Boggess, StataCorp

It means that mfx has run into trouble, but it’s the kind of trouble you can usually fix quite easily. Let’s look at an example:

 . sysuse auto, clear
 (1978 Automobile Data)
 
 . generate mpg2=mpg*mpg
 
 . mlogit rep78 mpg mpg2 turn, nolog
 
 Multinomial logistic regression                   Number of obs   =         69
                                                   LR chi2(12)     =      44.42
                                                   Prob > chi2     =     0.0000
 Log likelihood = -71.481648                       Pseudo R2       =     0.2371
 
 ------------------------------------------------------------------------------
        rep78 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
 -------------+----------------------------------------------------------------
 1            |
          mpg |   1.582486   2.374699     0.67   0.505    -3.071838     6.23681
         mpg2 |  -.0309519   .0553442    -0.56   0.576    -.1394246    .0775209
         turn |   .2116734   .3392475     0.62   0.533    -.4532395    .8765863
        _cons |  -30.37028   30.59297    -0.99   0.321     -90.3314    29.59083
 -------------+----------------------------------------------------------------
 2            |
          mpg |   .7890715    .824858     0.96   0.339    -.8276205    2.405763
         mpg2 |  -.0116654   .0198124    -0.59   0.556     -.050497    .0271663
         turn |   .4555263   .2210946     2.06   0.039     .0221889    .8888636
        _cons |  -31.30217   15.53334    -2.02   0.044    -61.74695   -.8573931
 -------------+----------------------------------------------------------------
 4            |
          mpg |  -.3924813   .6002901    -0.65   0.513    -1.569028    .7840657
         mpg2 |   .0089306   .0133784     0.67   0.504    -.0172906    .0351517
         turn |  -.1758045   .1274112    -1.38   0.168    -.4255259    .0739169
        _cons |   10.59927    10.0245     1.06   0.290    -9.048393    30.24693
 -------------+----------------------------------------------------------------
 5            |
          mpg |  -3.074932   1.142964    -2.69   0.007      -5.3151   -.8347631
         mpg2 |   .0633047   .0234417     2.70   0.007     .0173599    .1092495
         turn |   -.757644   .2742985    -2.76   0.006    -1.295259   -.2200287
        _cons |   62.32653   22.10485     2.82   0.005     19.00182    105.6512
 ------------------------------------------------------------------------------
 (rep78==3 is the base outcome)
 
 . mfx, predict(p outcome(1)) varlist(mpg) tracelvl(1)
 
 calculating dydx (nonlinear method)
 
 -------------------------
 variable |      dy/dx
 ---------+---------------
      mpg |       .03299
 -------------------------
 
 
 calculating standard errors (nonlinear method)
 
 mpg ... continuous
  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 
 mpg: Std. Err. = .0247271
 
 Marginal effects after mlogit
       y  = Pr(rep78==1) (predict, p outcome(1))
          =  .01857657
 ------------------------------------------------------------------------------
 variable |      dy/dx    Std. Err.     z    P>|z|  [    95% C.I.   ]      X
 ---------+--------------------------------------------------------------------
      mpg |   .0329856      .02473    1.33   0.182  -.015479   .08145   21.2899
 ------------------------------------------------------------------------------

So far, everything is looking OK. Let’s see if we can create a problem:

 . sysuse auto, clear
 (1978 Automobile Data)

 . replace mpg=mpg*100
 (74 real changes made)

 . generate mpg2=mpg*mpg

 . quietly mlogit rep78 mpg mpg2 turn

 . mfx, predict(p outcome(1)) varlist(mpg)

 Marginal effects after mlogit
       y  = Pr(rep78==1) (predict, p outcome(1))
          =  .01857657
 ------------------------------------------------------------------------------
 variable |      dy/dx    Std. Err.     z    P>|z|  [    95% C.I.   ]      X
 ---------+--------------------------------------------------------------------
      mpg |   .0003299      .00025    1.33   0.182  -.000155  .000814   2128.99
 ------------------------------------------------------------------------------

It is still working OK. We can see the effect of multiplying mpg by 100: the marginal effect and its standard error are both divided by 100, which means we get the same test statistic and p-value. This is all reasonable. Let’s try a little harder:

 . sysuse auto, clear
 (1978 Automobile Data)

 . replace mpg=mpg*10000
 mpg was int now long
 (74 real changes made)

 . generate mpg2=mpg*mpg

 . quietly mlogit rep78 mpg mpg2 turn

 . mfx, predict(p outcome(1)) varlist(mpg)

 Marginal effects after mlogit
       y  = Pr(rep78==1) (predict, p outcome(1))
          =  .01857655
 ------------------------------------------------------------------------------
 variable |      dy/dx    Std. Err.     z    P>|z|  [    95% C.I.   ]      X
 ---------+--------------------------------------------------------------------
      mpg |   3.30e-06      .00000    1.32   0.187  -1.6e-06  8.2e-06    212899
 ------------------------------------------------------------------------------

The marginal effect and its standard error are both divided by 10,000, and the test statistic is almost the same. Let’s try once more:

 . sysuse auto, clear
 (1978 Automobile Data)

 . replace mpg=mpg*1000000000
 mpg was int now double
 (74 real changes made)

 . generate mpg2=mpg*mpg

 . quietly mlogit rep78 mpg mpg2 turn

 . mfx, predict(p outcome(1)) varlist(mpg) tracelvl(1)

 calculating dydx (nonlinear method)

 -------------------------
 variable |      dy/dx
 ---------+---------------
      mpg |       3.3e-11
 -------------------------


 calculating standard errors (nonlinear method)

 mpg ... continuous
  1 
 warning: derivative missing; try rescaling variable mpg


 mpg: Std. Err. = .

 Marginal effects after mlogit
       y  = Pr(rep78==1) (predict, p outcome(1))
          =  .01857659
 ------------------------------------------------------------------------------
 variable |      dy/dx    Std. Err.     z    P>|z|  [    95% C.I.   ]      X
 ---------+--------------------------------------------------------------------
      mpg |   3.30e-11           .       .       .         .        .   2.1e+10
 ------------------------------------------------------------------------------

Now, Stata is upset. If you run this example yourself, you will see that the word “rescaling” is in blue, which means you can click it to display a help file with an explanation of the warning. The message says mfx suspects the variable mpg is the culprit. Let’s try a slightly different scale and see what happens when we calculate the marginal effect for turn:

 . sysuse auto, clear
 (1978 Automobile Data)

 . replace mpg=mpg*10000
 mpg was int now long
 (74 real changes made)

 . generate mpg2=mpg*mpg

 . quietly mlogit rep78 mpg mpg2 turn

 . mfx, predict(p outcome(1)) varlist(turn) tracelvl(1)

 calculating dydx (nonlinear method)

 -------------------------
 variable |      dy/dx
 ---------+---------------
     turn |       .00507
 -------------------------


 calculating standard errors (nonlinear method)

 turn ... continuous
  1  2 
 warning: derivative missing; try rescaling variable mpg2


 turn: Std. Err. = .

 Marginal effects after mlogit
       y  = Pr(rep78==1) (predict, p outcome(1))
          =  .01857655
 ------------------------------------------------------------------------------
 variable |      dy/dx    Std. Err.     z    P>|z|  [    95% C.I.   ]      X
 ---------+--------------------------------------------------------------------
     turn |    .005067           .       .       .         .        .   39.7971
 ------------------------------------------------------------------------------

This time, mpg slipped through without a problem, but mpg2 was still troubled.

It is interesting to use the tracelvl(3) on the above command to see how many iterations mfx is going through in its attempts to reach a solution.

The lesson here is that the marginal effect depends on scale because a marginal effect is a derivative, which is a slope. Remember, in the easiest case of a straight line, the slope is the change in y for a one-unit change in x. If you change the scale of x (by multiplying or dividing it by a number), the slope will change accordingly.

Using summarize, you can check to see if any of your variables are very, very small or very, very large. If so, you can really make mfx’s job easier by multiplying or dividing the offending variables appropriately.

This advice is worth remembering, even if you didn’t get the scale warning message. mfx is pretty smart, but how do we know it can always tell when it’s in trouble? When using the tracelvl(3) option, we can see how much effort mfx had to put in to get the second derivatives. Many iterations, together with the very, very small numbers in the results, would be reason enough to check if rescaling would be appropriate. Then, you can summarize your data before you run your model and rescale any poorly scaled variables. Then, you can use option tracelvl(3) when running mfx to watch for large numbers of iterations and very large or small numbers coming up in the computation of the second derivative.

You may be wondering why mfx continued trying to calculate standard errors after it had already figured out there was a scaling problem, since in the previous example it couldn’t compute any standard errors at all. Well, it is possible to only have trouble with some variables and not with others. If you are interested in the marginal effect of only some of the variables, it may not matter to you that it is having trouble at one of the other variables.

Here’s an example that has trouble at only two variables:

 . sysuse auto, clear
 (1978 Automobile Data)

 . drop if rep78<=2
 (10 observations deleted)

 . generate mpg2=mpg*mpg

 . quietly mlogit rep78 mpg mpg2 displacement

 . mfx, predict(p outcome(3))

 Marginal effects after mlogit
       y  = Pr(rep78==3) (predict, p outcome(3))
          =  .52038591
 ------------------------------------------------------------------------------
 variable |      dy/dx    Std. Err.     z    P>|z|  [    95% C.I.   ]      X
 ---------+--------------------------------------------------------------------
      mpg |   .2610697      .19241    1.36   0.175  -.116047  .638186   21.5932
     mpg2 |  -.0058319      .00416   -1.40   0.161  -.013979  .002316    503.22
 displa~t |    .002884      .00189    1.53   0.126  -.000813  .006581   192.237
 ------------------------------------------------------------------------------

 . sysuse auto, clear
 (1978 Automobile Data)

 . drop if rep78<=2
 (10 observations deleted)

 . replace mpg=mpg*10000
 mpg was int now long
 (64 real changes made)

 . generate mpg2=mpg*mpg

 . quietly mlogit rep78 mpg mpg2 displacement

 . mfx, predict(p outcome(3))

 warning: derivative missing; try rescaling variable mpg


 warning: derivative missing; try rescaling variable mpg2


 Marginal effects after mlogit
       y  = Pr(rep78==3) (predict, p outcome(3))
          =  .52038589
 ------------------------------------------------------------------------------
 variable |      dy/dx    Std. Err.     z    P>|z|  [    95% C.I.   ]      X
 ---------+--------------------------------------------------------------------
      mpg |   .0000261      .00002    1.36   0.174  -.000012  .000064    215932
     mpg2 |  -5.83e-11           .       .       .         .        .   5.0e+10
 displa~t |    .002884           .       .       .         .        .   192.237
 ------------------------------------------------------------------------------

Although the standard errors for mpg2 and displacement cannot be calculated, the test statistic and p-value for the variable mpg are the same as the first time, which is what we wanted. Also, the marginal effect and standard error of mpg are divided by 10,000, as we would expect.