Announcement

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

  • Marginal effects precision issue in decimal places

    Dear forum members,

    I am trying to estimate and verify the marginal effects using margins Stata command and manual calculations:

    Code for manual and Stata estimate comparisons:

    Code:
    logit success age income i.education i.male
    
    * Store coefficients for manual calculation
    matrix b = e(b)
    matrix list b
    
    * Extract coefficients (education=1 is reference category)
    scalar b_cons = b[1,8]           // constant
    scalar b_age = b[1,1]            // age coefficient
    scalar b_income = b[1,2]         // income coefficient  
    scalar b_ed2 = b[1,4]            // education=2
    scalar b_ed3 = b[1,5]            // education=3
    scalar b_male = b[1,7]           // male binary
    
    display ""
    display "Extracted coefficients:"
    display "Constant: " b_cons
    display "Age: " b_age
    display "Income: " b_income
    display "Education=2: " b_ed2
    display "Education=3: " b_ed3
    display "Male: " b_male
    
    
    // Pr Prob
    gen xb = b_cons + b_age*age + b_income*income + b_ed2*ed2 + b_ed3*ed3 + b_male*male
    
    * Transform to probability
    gen pred_prob = exp(xb)/(1 + exp(xb))
    
    **# PART 1. Calculate average predicted probability
    summarize pred_prob
    scalar manual_avg_prob = r(mean)
    
    * Compare with Stata's margins
    margins // matches perfectly
    
    
    **# PART 2. Pr Prob at education category
    
    forvalues i = 1/3 {
        quietly summarize pred_prob if education == `i'
        display "Education = `i': Manual avg predicted prob = " r(mean) " (n=" r(N) ")"
    }
    
    * Compare with Stata
    margins  i.education // matches on two decimal places for category 3 and one decimal place for others
    
    
    **# PART 3. Pr Prob by gender indicator variable
     
    forvalues i = 0/1 {
        quietly summarize pred_prob if male == `i'
        display "Male = `i': Manual avg predicted prob = " r(mean) " (n=" r(N) ")"
    }
    
    * Compare with Stata
    margins male // matches on two decimal for female and one decimal place for male
    
    **# PART 4. Pr Prob at specific value
    * It matches well with a combination of age, edu, and gender

    Is it an issue of precision of estimate or Stata's reporting of the decimal points.?

    There was was recent post on decimal places issues in Stata. My guess is that the post was by Ben Jann or Todd Jones!

    Code:
    clear
    input float(age income education) byte(male success ed1 ed2 ed3)
    34.808598 30026.297 2 0 1 0 1 0
    36.820805 30368.934 3 1 0 0 0 1
     50.40102 68532.984 1 0 0 1 0 0
     44.30757  61299.93 3 1 1 0 0 1
     44.99921  75855.37 2 0 1 0 1 0
    27.761456   76174.6 3 0 1 0 0 1
    19.347147  48395.77 2 0 0 0 1 0
     50.37945  23332.28 1 1 0 1 0 0
     40.05914  36837.52 3 1 1 0 0 1
     27.73617  35538.74 2 1 0 0 1 0
    18.184816  38770.33 2 1 0 0 1 0
    18.612396  58270.08 3 1 1 0 0 1
    37.759853  49814.74 3 1 0 0 0 1
       46.961  21248.98 3 0 0 0 0 1
     60.05953 28588.674 1 0 1 1 0 0
    37.299896   64486.6 2 0 1 0 1 0
     30.25613  57315.14 2 0 0 0 1 0
     19.25626 36195.605 3 1 1 0 0 1
     23.07329  32959.68 2 1 0 0 1 0
    35.234146  49966.11 2 0 1 0 1 0
    18.182678   31027.3 2 0 0 0 1 0
     62.18434  54698.35 1 1 1 1 0 0
     50.48997  66573.72 3 1 1 0 0 1
     61.17728   22856.6 1 1 0 1 0 0
     37.72742   40931.7 2 0 0 0 1 0
     55.79249 33507.332 2 1 1 0 1 0
     46.13565  48414.46 1 0 1 1 0 0
    18.162035  86912.84 2 1 1 0 1 0
     40.95094  47900.51 3 1 0 0 0 1
     59.46362  49501.05 1 0 1 1 0 0
     61.91032  45963.34 3 0 1 0 0 1
     64.32726  49525.49 1 1 1 1 0 0
     38.27052  44192.48 3 0 1 0 0 1
    24.016577  60406.81 3 0 0 0 0 1
     58.89691  57728.08 2 1 1 0 1 0
     54.26258  46393.44 2 0 1 0 1 0
     50.90836   47417.9 2 0 1 0 1 0
     48.27848  43714.39 1 1 0 1 0 0
        47.88  58613.79 2 0 1 0 1 0
     64.89766  26306.64 3 1 1 0 0 1
    37.819225  57575.61 1 0 1 1 0 0
    23.682735  68223.81 3 0 1 0 0 1
    23.453726   49912.8 1 1 0 1 0 0
    37.957638  44849.27 3 0 1 0 0 1
     26.08287   39257.9 3 0 1 0 0 1
    22.827616  44006.71 3 0 0 0 0 1
      29.2445  45690.43 1 0 0 1 0 0
     63.86313 14838.464 1 0 1 1 0 0
    34.368965  64578.91 1 0 1 1 0 0
     48.64857   79930.1 1 1 1 1 0 0
     42.93251  43341.45 2 0 1 0 1 0
    34.927242 37474.676 3 1 1 0 0 1
       59.278  55699.98 3 0 1 0 0 1
    21.830706  60313.25 1 1 0 1 0 0
      41.2889  45420.51 3 1 1 0 0 1
    34.110233  51577.42 2 1 1 0 1 0
     40.21544  34275.23 2 0 1 0 1 0
       50.717  47113.78 3 1 1 0 0 1
     21.48841  47124.61 3 0 1 0 0 1
    37.151695  61161.95 2 0 1 0 1 0
    27.247793  49997.74 1 0 1 1 0 0
     49.11787 25561.145 3 1 0 0 0 1
       59.226  49312.18 2 0 1 0 1 0
    27.128956  55133.83 1 0 0 1 0 0
     27.39462 65899.734 3 0 0 0 0 1
     41.06339  45363.67 2 1 0 0 1 0
     62.13575  30056.61 3 1 1 0 0 1
    37.322308 32222.047 1 0 1 1 0 0
     55.62333   47989.4 1 0 0 1 0 0
    26.042366 13732.013 1 0 0 1 0 0
     39.91064  47921.66 2 0 1 0 1 0
     62.09574  50746.76 2 0 1 0 1 0
     36.91353   31170.9 2 0 0 0 1 0
     64.25463  33338.12 1 1 1 1 0 0
     24.60607  50066.18 1 0 0 1 0 0
     38.67326  83594.55 3 0 1 0 0 1
     60.09041  58866.58 1 0 1 1 0 0
     49.77311 31337.045 1 0 0 1 0 0
     63.43037  8862.519 1 1 0 1 0 0
    37.358097  70399.45 1 1 1 1 0 0
    28.466526   62110.2 3 1 1 0 0 1
     52.21246  29574.72 3 0 1 0 0 1
     41.63192  49627.25 3 0 0 0 0 1
    20.309317  83168.24 1 1 1 1 0 0
     35.31579  68843.87 3 1 1 0 0 1
    34.919106  70991.58 3 0 1 0 0 1
     52.71086 104806.46 3 0 1 0 0 1
     61.41342  54569.62 2 1 1 0 1 0
     18.46523  24836.62 2 1 1 0 1 0
    25.184624  39433.07 1 0 0 1 0 0
      59.5514  49167.56 2 1 1 0 1 0
      23.9647  49941.44 2 0 1 0 1 0
      29.3416   42108.5 1 1 0 1 0 0
     38.81062  58050.05 2 1 1 0 1 0
     58.59231  41217.62 1 0 0 1 0 0
     57.50946  54923.31 1 0 1 1 0 0
      30.2697  64132.93 1 0 1 1 0 0
     60.01218  61233.31 3 1 1 0 0 1
     58.25903  54737.01 1 1 1 1 0 0
      22.4374  32307.74 1 0 0 1 0 0
     63.98761   60096.9 2 1 1 0 1 0
      63.5452  65791.68 2 0 1 0 1 0
    22.383095  45852.66 3 0 1 0 0 1
     48.87363  39123.55 3 0 1 0 0 1
     55.17607  64196.62 1 0 1 1 0 0
    18.372522  61484.73 2 1 1 0 1 0
     43.25864  53517.46 3 0 1 0 0 1
    29.121075 28416.955 2 0 1 0 1 0
     46.44524  64478.64 1 1 1 1 0 0
     45.36893  58056.59 1 0 0 1 0 0
     24.69216 30500.186 2 0 0 0 1 0
    20.105463  62267.14 2 1 1 0 1 0
     49.20757  65379.19 3 1 1 0 0 1
      34.2903  60634.55 2 0 1 0 1 0
     63.85077  44276.52 1 1 0 1 0 0
     48.03522  48554.25 2 1 1 0 1 0
     58.63946  53315.83 2 1 1 0 1 0
     62.69132  65229.54 2 1 1 0 1 0
     27.57439  45706.27 1 1 0 1 0 0
    64.517136  46504.88 2 0 1 0 1 0
    37.844677  50188.82 3 1 1 0 0 1
     42.21183  73454.19 1 1 1 1 0 0
     38.63707  39003.38 2 1 1 0 1 0
     44.92413 35647.887 1 0 0 1 0 0
    36.953606  52718.56 3 0 1 0 0 1
     38.89361  47126.29 2 0 0 0 1 0
    20.920843  48358.71 2 1 0 0 1 0
    18.368364   29309.6 1 1 1 1 0 0
     38.33406  47406.49 2 1 1 0 1 0
     21.79863  42764.89 3 1 1 0 0 1
     56.20117  65401.76 3 1 1 0 0 1
     46.29274 24983.047 1 0 1 1 0 0
     34.62326 28934.855 2 0 0 0 1 0
     48.51978  50313.65 3 1 1 0 0 1
     22.42135  61299.02 2 1 1 0 1 0
      43.0927  45118.52 1 0 1 1 0 0
     42.31855  31408.22 1 0 1 1 0 0
     58.73748  51858.68 1 1 1 1 0 0
     61.70041  61834.59 1 0 1 1 0 0
     58.93798  54959.23 3 0 1 0 0 1
     63.94025  80399.64 1 1 1 1 0 0
     50.21995 35228.316 1 1 1 1 0 0
     41.01347  42050.85 3 0 1 0 0 1
     48.40077  56319.66 3 0 1 0 0 1
      25.8324  53518.47 2 0 0 0 1 0
     50.85942  83721.66 3 1 0 0 0 1
     54.43066  43765.52 2 1 0 0 1 0
    35.428833  58273.23 1 1 0 1 0 0
    34.635403 13689.454 3 0 0 0 0 1
     23.23332 38905.813 1 1 1 1 0 0
    31.367983  41204.94 1 0 1 1 0 0
     49.75023  50631.79 3 0 1 0 0 1
     28.36272  41572.83 2 0 1 0 1 0
     52.65728  42674.11 3 0 1 0 0 1
     53.68295  52571.22 3 0 1 0 0 1
     43.88898 34846.273 2 0 1 0 1 0
     40.83735  60005.96 3 1 1 0 0 1
    37.108673  59686.76 2 0 1 0 1 0
     55.67829   80508.5 3 0 1 0 0 1
    23.404194  59858.97 2 1 1 0 1 0
      58.5042 33706.242 1 1 1 1 0 0
     23.66254  55852.36 3 1 1 0 0 1
      54.0767  62288.95 3 1 1 0 0 1
     38.77032  55028.97 1 0 1 1 0 0
     38.25363  71404.33 3 1 1 0 0 1
     57.53648  5760.611 3 1 0 0 0 1
     64.00684  42352.03 1 0 1 1 0 0
     55.04185  43881.57 2 0 1 0 1 0
     45.21367   76707.6 2 0 1 0 1 0
     54.44492  51201.21 2 0 1 0 1 0
     53.30386  57876.11 3 1 1 0 0 1
     19.67689  52156.07 3 0 1 0 0 1
     61.49991  31406.18 1 1 1 1 0 0
    31.460445  46358.27 1 0 1 1 0 0
     45.30388 12766.278 2 1 1 0 1 0
     28.96929  58710.47 3 0 0 0 0 1
    33.083614  57178.04 2 0 1 0 1 0
    18.783228  58000.27 3 0 1 0 0 1
     47.48726  50331.98 3 0 1 0 0 1
     55.45809  70549.15 1 0 1 1 0 0
     26.71681  38215.92 2 1 1 0 1 0
     39.52606 66447.586 3 0 0 0 0 1
     22.50454  74393.04 3 1 1 0 0 1
    37.650345  50737.71 3 1 1 0 0 1
     54.84882  55784.34 3 0 1 0 0 1
    35.434025  46612.39 2 1 1 0 1 0
     56.80647  77279.47 2 0 1 0 1 0
      39.7132   54028.1 1 1 1 1 0 0
     53.14025  73742.36 1 1 1 1 0 0
     29.50664 34294.965 3 1 1 0 0 1
     24.91583  59009.32 1 1 1 1 0 0
     38.86174  41032.29 3 1 1 0 0 1
     57.02653  65284.61 2 0 0 0 1 0
     41.04926 38059.797 2 0 1 0 1 0
     49.50742  35960.41 3 1 1 0 0 1
      37.9941  70258.27 2 0 1 0 1 0
     62.76217  57428.02 2 0 1 0 1 0
     64.84615  41537.66 2 1 1 0 1 0
     45.97017  62273.26 1 0 0 1 0 0
     44.37502  45870.36 2 1 1 0 1 0
     20.71212  70190.12 2 0 1 0 1 0
    36.345844  43367.54 1 0 0 1 0 0
     42.45639  39866.89 1 1 0 1 0 0
     60.18188 37950.113 2 0 0 0 1 0
    34.255142  44734.55 1 1 1 1 0 0
     33.09557  35682.16 3 0 1 0 0 1
    34.614605  73061.27 3 0 1 0 0 1
     57.35038  52101.04 3 1 1 0 0 1
    27.413683  43382.72 3 0 1 0 0 1
    33.560764  35769.53 2 1 1 0 1 0
     21.91315   44685.3 3 0 0 0 0 1
    21.354784  60042.75 2 1 1 0 1 0
     19.92669  50359.06 1 1 0 1 0 0
     37.87893   49174.7 3 1 0 0 0 1
    24.400053 33801.055 2 1 1 0 1 0
     45.96469  71635.93 3 1 1 0 0 1
      43.6259  70535.17 3 1 0 0 0 1
     42.63342 36435.953 2 0 0 0 1 0
       60.927  25620.36 3 0 1 0 0 1
      34.1887  46935.71 3 0 1 0 0 1
     56.97813  53280.24 2 0 1 0 1 0
    30.464884  70902.73 2 0 1 0 1 0
     42.68031  63537.99 2 0 1 0 1 0
     27.63559  75091.35 1 1 0 1 0 0
    35.679985  43711.53 3 0 1 0 0 1
     39.43053   63237.3 1 1 0 1 0 0
    26.833843  58644.18 2 0 1 0 1 0
     60.59365  56974.96 3 0 1 0 0 1
      50.6678  27080.98 1 0 1 1 0 0
     23.31768   44712.9 1 0 1 1 0 0
     31.92334  48571.36 3 1 1 0 0 1
      56.8584  58168.76 1 1 1 1 0 0
     45.36961  51711.11 2 0 1 0 1 0
       50.484  40539.83 2 1 1 0 1 0
     46.53864  66839.84 3 0 1 0 0 1
    21.188555  62353.37 3 0 1 0 0 1
    34.967937 27064.406 3 0 1 0 0 1
     22.38558  35233.49 3 1 0 0 0 1
      49.6321  49844.17 1 1 1 1 0 0
     44.82763 20298.684 3 0 1 0 0 1
    24.115494     55709 3 1 0 0 0 1
     52.57116  48658.68 3 0 1 0 0 1
     62.47164  52647.59 3 1 1 0 0 1
     54.95887 71968.055 3 1 1 0 0 1
     45.86432  70465.16 3 0 1 0 0 1
     60.36528 71444.305 1 0 0 1 0 0
     26.09579 39036.258 2 0 1 0 1 0
     55.68723   54126.6 3 0 1 0 0 1
     50.22318  70611.72 3 1 1 0 0 1
     53.08611  38472.62 3 1 1 0 0 1
    25.776783  41333.03 1 0 1 1 0 0
     27.74772  42259.02 1 1 1 1 0 0
     63.15483  33601.24 1 1 1 1 0 0
     48.41903  43091.55 1 0 0 1 0 0
    30.296833  27236.64 1 0 0 1 0 0
     22.21845  53628.57 3 0 0 0 0 1
     21.80379  41549.75 2 0 1 0 1 0
     22.61292  48964.48 2 0 0 0 1 0
     27.19483  56550.22 3 0 1 0 0 1
     57.74195  51287.27 3 1 1 0 0 1
    37.538437   43093.8 3 1 1 0 0 1
     54.44075  42364.61 2 1 1 0 1 0
    64.850746  56325.55 3 0 1 0 0 1
     21.02203   25128.4 1 0 0 1 0 0
     63.15663  34547.13 1 0 1 1 0 0
     55.58351  62703.72 1 0 1 1 0 0
     45.62667 66516.055 1 1 1 1 0 0
     61.16496   19615.9 1 0 0 1 0 0
     60.36068 26835.596 1 0 0 1 0 0
     64.57123  51111.12 2 1 1 0 1 0
     30.04583 18197.012 1 0 0 1 0 0
     48.92127  54229.92 1 0 1 1 0 0
    31.266375  48286.05 3 1 1 0 0 1
     32.39179   31705.2 1 0 0 1 0 0
     27.05151  71561.71 3 0 1 0 0 1
      44.5084  69536.18 1 0 1 1 0 0
     43.79229  74638.91 1 1 0 1 0 0
     41.62699  63186.67 1 0 0 1 0 0
     40.34202 67803.945 1 1 1 1 0 0
    34.083298  60796.12 1 1 0 1 0 0
     23.27261  67829.36 1 0 0 1 0 0
     42.22701  49812.33 3 1 1 0 0 1
    31.196247 37748.953 2 0 1 0 1 0
      51.5799  50848.62 3 1 1 0 0 1
     36.58783  50055.06 2 1 1 0 1 0
     54.16001 17678.904 3 1 1 0 0 1
    27.145687  55817.11 3 0 1 0 0 1
     38.23981 35839.887 1 1 0 1 0 0
     31.76435  50697.68 3 0 0 0 0 1
    27.005236  62171.58 1 1 1 1 0 0
     24.38466 69085.484 2 0 1 0 1 0
     57.59991  48316.67 1 0 0 1 0 0
     44.68926  64097.59 3 1 1 0 0 1
     64.20805  54594.49 1 1 1 1 0 0
     62.96055   72983.2 1 0 1 1 0 0
     28.09965  30047.58 2 1 1 0 1 0
    19.899933  48483.17 1 0 1 1 0 0
     26.37604  34811.49 1 0 0 1 0 0
     38.38337  48989.23 3 1 0 0 0 1
     58.24363  13660.69 2 1 1 0 1 0
      32.8989  81488.71 3 1 1 0 0 1
     38.90966     39489 1 0 1 1 0 0
     50.37875  25212.65 1 0 1 1 0 0
    26.910055  22121.73 1 1 1 1 0 0
    31.872374  65821.76 3 0 1 0 0 1
     60.63135  44771.36 3 0 1 0 0 1
     53.63963  42105.51 2 1 1 0 1 0
     31.66673   39003.4 3 0 0 0 0 1
     21.63912 32015.127 2 0 0 0 1 0
     42.48678     60729 3 1 1 0 0 1
     60.60085  30578.42 3 0 1 0 0 1
    25.106525 28911.195 2 1 1 0 1 0
    37.640823  50559.14 1 1 0 1 0 0
    36.980106  51662.04 1 1 1 1 0 0
     42.60213  50154.91 1 1 1 1 0 0
     57.31755 66425.836 3 1 1 0 0 1
    34.000385  61108.07 2 0 1 0 1 0
     19.40152 70637.445 3 0 1 0 0 1
    20.593925   70223.3 2 0 0 0 1 0
     51.17383  88434.43 3 0 1 0 0 1
     62.16003  31508.89 1 0 0 1 0 0
     63.32101  47103.35 2 1 1 0 1 0
     28.32268  41319.73 3 0 0 0 0 1
     28.86284  41237.82 2 0 0 0 1 0
    35.642242  54850.98 2 1 0 0 1 0
     48.89295  88995.01 1 1 1 1 0 0
      49.8559  69329.69 1 1 1 1 0 0
     20.78996  46620.75 1 1 0 1 0 0
     50.43922  64584.18 3 1 1 0 0 1
     38.88373  43397.29 3 0 1 0 0 1
     55.50215  64194.02 1 1 1 1 0 0
     39.23495  51929.91 3 0 1 0 0 1
     38.42119  61387.37 2 0 0 0 1 0
     62.58794  61593.98 3 0 1 0 0 1
     59.86561 36047.492 1 1 1 1 0 0
    37.767227  60237.48 3 1 1 0 0 1
    30.891216  53531.92 3 1 1 0 0 1
     57.55078  60677.48 2 1 1 0 1 0
    37.086964  47782.23 3 1 1 0 0 1
     33.22537  61774.86 1 1 1 1 0 0
    36.853954  25893.99 2 1 1 0 1 0
    18.395943  40840.66 2 1 0 0 1 0
     51.86687 34032.438 1 1 1 1 0 0
     59.08722  64331.83 2 1 1 0 1 0
     49.33652   51791.2 3 0 1 0 0 1
     54.72119  72659.93 3 1 1 0 0 1
     42.26329 33767.984 1 0 0 1 0 0
     54.62866  55219.56 2 1 1 0 1 0
     45.45475  39664.35 2 1 0 0 1 0
     20.41602  67424.49 1 1 0 1 0 0
    31.067776  60842.44 3 1 1 0 0 1
     48.26917  50564.31 3 1 1 0 0 1
     46.36769  54318.83 1 0 0 1 0 0
    26.488256   76728.8 2 0 0 0 1 0
     55.09842  37204.27 2 0 0 0 1 0
     49.97754  82192.64 3 1 1 0 0 1
     29.21102  42225.23 1 0 0 1 0 0
    30.656286  48738.15 1 1 0 1 0 0
     41.16167  56519.84 2 0 1 0 1 0
       47.159  65088.06 3 0 1 0 0 1
     63.68025  60492.53 2 0 1 0 1 0
     44.57832  46408.96 2 1 1 0 1 0
     59.52298 28513.676 2 0 1 0 1 0
    28.433487   48410.5 2 1 1 0 1 0
     63.22673  63593.13 1 1 1 1 0 0
        51.17 16094.155 2 1 0 0 1 0
    21.990374  29573.93 3 0 0 0 0 1
     33.82478 67262.164 3 1 1 0 0 1
     42.26315  52829.18 1 0 1 1 0 0
      26.2165  43207.25 3 1 1 0 0 1
     47.35039  28350.51 1 1 1 1 0 0
     48.12115  64924.98 2 1 1 0 1 0
    64.410675  47604.81 3 0 1 0 0 1
    36.363903  85094.34 3 0 1 0 0 1
      43.4516  41994.09 1 0 0 1 0 0
     63.36535   78505.5 1 0 1 1 0 0
     42.78205  62275.52 1 0 0 1 0 0
    22.330107  24186.34 1 0 1 1 0 0
     56.42128  72878.09 1 1 0 1 0 0
     26.73701  60852.92 1 0 0 1 0 0
     22.82654  52931.33 3 1 0 0 0 1
     33.12747  40375.01 3 1 1 0 0 1
     39.23193  53212.25 3 0 1 0 0 1
    34.401054   45384.7 3 0 1 0 0 1
     48.19333  80835.94 1 1 1 1 0 0
     30.32204  39568.28 3 1 1 0 0 1
     38.61949  55918.17 3 1 0 0 0 1
    22.386066  32695.25 1 0 0 1 0 0
     40.45595  51464.36 3 0 1 0 0 1
    33.926846  62348.93 1 0 1 1 0 0
     31.69378  56183.42 1 1 1 1 0 0
    18.621624   45287.6 2 0 0 0 1 0
     51.30519  56206.73 3 0 1 0 0 1
     26.12568  25808.39 3 1 1 0 0 1
     59.11717  24627.52 2 1 1 0 1 0
    27.107193  65622.54 2 1 1 0 1 0
    32.143448  53230.53 1 0 0 1 0 0
     29.89672   51368.4 3 1 1 0 0 1
      62.8045   46169.5 3 1 1 0 0 1
    22.522106  8425.236 2 1 1 0 1 0
     53.49617  39846.37 2 1 1 0 1 0
     23.84248  38967.02 1 0 0 1 0 0
    36.370106  50498.85 3 1 1 0 0 1
     43.52947 35858.816 2 1 1 0 1 0
     44.22529 68122.305 1 0 0 1 0 0
     64.23325 38220.234 3 1 1 0 0 1
    32.270958  39320.52 2 1 1 0 1 0
     29.54527  36208.08 3 1 0 0 0 1
     61.80906  59507.64 3 0 1 0 0 1
     59.60973  71950.54 1 0 1 1 0 0
    37.223442  62562.21 3 1 0 0 0 1
      49.5434  51378.29 3 0 1 0 0 1
     37.53869  44126.23 3 1 1 0 0 1
     40.38818  48862.55 3 1 1 0 0 1
    33.033836  53492.22 1 0 0 1 0 0
     61.62782 34401.703 1 1 0 1 0 0
     53.40845 32981.816 2 1 1 0 1 0
     43.36905  32850.64 1 1 1 1 0 0
       52.056  45951.45 1 0 1 1 0 0
     31.69546 35844.438 1 1 1 1 0 0
    32.918697  62488.29 2 1 1 0 1 0
     55.21479  47568.01 2 1 1 0 1 0
    27.460564   52695.2 3 1 1 0 0 1
    31.440956  48130.32 1 1 1 1 0 0
     34.14222  44767.45 2 0 1 0 1 0
     41.85394  30839.27 1 0 0 1 0 0
     44.26042  53418.31 3 0 1 0 0 1
     62.61776  50059.61 2 0 1 0 1 0
     24.72752  41199.98 3 0 1 0 0 1
     40.65713  52998.18 1 0 1 1 0 0
     44.33776 23242.273 2 0 1 0 1 0
    36.748135  34135.85 1 1 1 1 0 0
     47.19722  46465.52 1 0 0 1 0 0
     30.97086 15845.414 2 1 0 0 1 0
     46.05628   52674.4 3 0 1 0 0 1
     32.09546  44550.51 3 1 1 0 0 1
     53.10231  60150.77 2 1 1 0 1 0
    32.361385  58502.27 2 1 1 0 1 0
     49.65127  52906.46 3 1 0 0 0 1
     21.15419  47661.09 1 1 1 1 0 0
     28.87555  54246.97 1 0 0 1 0 0
     47.19776  60700.38 3 1 1 0 0 1
     62.46229  60527.49 1 1 1 1 0 0
     60.74959  52940.05 1 1 1 1 0 0
    36.958923  29355.14 3 1 1 0 0 1
    19.014132  61634.69 3 0 1 0 0 1
     59.10707  54198.18 3 0 1 0 0 1
     20.85997  39454.45 2 1 1 0 1 0
      52.6323   62747.2 3 1 1 0 0 1
     45.22585 66566.734 2 0 1 0 1 0
    18.271784  55253.59 2 0 1 0 1 0
    26.346443  59769.77 3 0 0 0 0 1
     22.44904   37653.5 3 0 1 0 0 1
    31.071524  70404.25 2 1 1 0 1 0
     49.46861  49371.05 2 0 1 0 1 0
      61.3357  61306.68 2 0 1 0 1 0
     56.59058  72523.48 2 0 1 0 1 0
     63.94477 37694.156 1 1 1 1 0 0
     26.21011  64754.11 3 0 1 0 0 1
     51.38161  33705.38 3 0 1 0 0 1
    21.508535 37744.945 2 0 0 0 1 0
    19.711117  19594.39 3 0 1 0 0 1
     41.21696  37738.83 1 0 1 1 0 0
     53.27036  39904.42 1 0 1 1 0 0
    27.045494 70342.375 1 1 0 1 0 0
     31.15078  62429.16 1 1 0 1 0 0
    25.490557  44168.54 1 0 1 1 0 0
     59.37346 35477.164 2 1 0 0 1 0
     25.06769   46474.8 1 0 0 1 0 0
    20.392935  57796.45 1 0 0 1 0 0
     44.92712  58469.04 1 1 0 1 0 0
     25.13137  41416.43 1 0 0 1 0 0
      61.4815 29942.904 3 0 1 0 0 1
    29.305716 20794.855 3 1 1 0 0 1
      35.5412  73180.32 2 0 1 0 1 0
     41.55409  31922.33 3 0 0 0 0 1
    19.025679  41414.52 2 1 1 0 1 0
     45.80318  45426.83 3 0 1 0 0 1
    38.065525  53604.72 2 0 1 0 1 0
     19.65232  46732.11 2 1 1 0 1 0
     37.70623  40299.18 1 0 1 1 0 0
     40.30716  46119.46 2 0 0 0 1 0
     44.87535  54893.52 1 1 1 1 0 0
      28.6241 67319.164 1 0 1 1 0 0
     22.17618  61143.04 3 0 1 0 0 1
    34.713715 32025.477 3 1 1 0 0 1
    26.795824  56326.25 1 0 0 1 0 0
     23.50544  56391.88 3 0 1 0 0 1
      58.9365  64147.21 1 1 1 1 0 0
    27.108717  44875.04 2 1 0 0 1 0
     59.49856  25628.49 1 0 1 1 0 0
     21.83527  52209.15 1 1 1 1 0 0
    25.080454 19266.014 3 0 0 0 0 1
     32.37155  65963.61 3 0 1 0 0 1
    20.778713  69829.25 2 0 1 0 1 0
    19.024103  36556.85 2 1 1 0 1 0
     45.56555  25465.98 1 1 1 1 0 0
     50.98348  47270.46 1 0 1 1 0 0
    36.600143 15537.672 3 0 1 0 0 1
     35.95688  62218.01 1 1 1 1 0 0
    end
    label values education ed_lab
    label def ed_lab 1 "High School", modify
    label def ed_lab 2 "College", modify
    label def ed_lab 3 "Graduate", modify
    label values male male_lab
    label def male_lab 0 "Female", modify
    label def male_lab 1 "Male", modify
    Stata 18 SE
    Thank you - Mukesh
    Last edited by Mukesh Punia; 24 Jul 2025, 09:10.
    Best regards,
    Mukesh

  • #2
    Give the expressions (generate) and conditions (if) you are using, the correct/matching margins syntax calls are
    Code:
    margins, over(education)
    margins, over(male)

    Comment


    • #3
      Jeff Pitblado is absolutely right in pointing out that the manual calculations performed in #1 correspond to -margins, over(education)-, not to -margins i.education-.

      I am confident that will account for the differences you are showing. BUT, given that -margins, over(education)- and -margins i.education- are different things, you have to decide which one you actually want for your purposes. The difference between them is that -margins, over(education)- calculates the mean predicted value using only the observations with the given value of education, whereas -margins i.education- calculates the mean predicted value from a hypothetical data set in which the value of education is set to the given value of education in each observation. Putting this (perhaps too) briefly, -margins, over(education)- adjusts for the effects of other covariates but does not adjust for the different distributions of other model variables in each education level, whereas -margins i.education- adjusts for both of these.

      Comment


      • #4
        Thank you Dear Jeff Pitblado (StataCorp) and Clyde Schechter for insightful clarification.

        Based on #3 my another small query is what will be the manual way to match with the predications given by i.education or "whereas -margins i.education- adjusts for both of these".

        Thank you - Mukesh
        Best regards,
        Mukesh

        Comment


        • #5
          Code:
          //    GET PREDICTIVE MARGINS FROM STATA -margins- COMMAND
          margins i.education
          
          //    REPRODUCE THEM MANUALLY
          frame create results int education float predictive_margin
          levelsof education, local(eds)
          preserve
          foreach e of local eds {
              replace education = `e'
              predict phat
              summ phat, meanonly
              frame post results (`e') (`r(mean)')
              drop phat
          }
          frame results: list, noobs clean

          Comment


          • #6
            Great! Thank you Clyde Schechter for helping out. I am able to reproduce it for education and male as well.
            I am not well versed with frame thing. I tried without frames. Is it a proper way to do it without frames.

            Code:
            // matrix for manual results
            
            display " "
            display "=== METHOD 2: MANUAL REPRODUCTION (WITHOUT FRAMES) ==="
            
            * Create matrix to store results
            matrix manual_results = J(3,1,.)
            matrix rownames manual_results = "Education_1" "Education_2" "Education_3"
            matrix colnames manual_results = "Predictive_Margin"
            
            * Save original education values
            generate orig_education = education
            forvalues e = 1/3 {
                display "Processing education level `e'..."
                
                * Set everyone to education level e
                replace education = `e'
                
                * Predict wages for this counterfactual scenario
                predict phat_`e'
                
                * Calculate mean prediction (this is the predictive margin)
                summarize phat_`e', meanonly
                local manual_margin_`e' = r(mean)
                
                * Store in matrix
                matrix manual_results[`e',1] = `manual_margin_`e''
                
                display "  Manual margin for education `e': " %10.4f `manual_margin_`e''
                
                * Clean up prediction variable
                drop phat_`e'
            }
            
            * Restore original education values
            replace education = orig_education
            drop orig_education

            Thank you - Mukesh
            Last edited by Mukesh Punia; 24 Jul 2025, 13:53.
            Best regards,
            Mukesh

            Comment


            • #7
              Sure. What recent Stata does in frames is usually doable with -postfile-s in earlier versions. The code is almost the same:
              Code:
              //    GET PREDICTIVE MARGINS FROM STATA -margins- COMMAND
              margins i.education
              
              //    REPRODUCE THEM MANUALLY
              tempfile results
              postfile handle int education float predictive_margin using `results'
              levelsof education, local(eds)
              preserve
              foreach e of local eds {
                  replace education = `e'
                  predict phat
                  summ phat, meanonly
                  post handle (`e') (`r(mean)')
                  drop phat
              }
              postclose handle
              use `results', clear
              list, noobs clean
              In the future, as recommended in the Forum FAQ, if you are not using the current version (19.0 or 19.5 as of this writing) of Stata, you should indicate in your post the version that you are using. That way respondents will give you code that you can actually use.

              Comment


              • #8
                Dear Clyde Schechter I greatly appreciate your efforts and help . In #1 I mentioned the version (18.0). However, I work or share with people on earlier version, therefore, I asked for the other way.

                Can you please suggest specific readings which can help conceptually and theoretically every bit of margins. I see people, not all, throwing margins in anyway. As I discuss it here and find new things and clarification everytime. I am getting sceptical using it and as I mentioned in my earlier post I have to turn back the pages of my references again to (re)learn.

                This user forum is full of resourceful, humble, and helpful persons!!
                Thank you - Mukesh
                Best regards,
                Mukesh

                Comment


                • #9
                  I think the clearest explanations of the -margins- command are the excellent work of Richard Williams. These can be found at https://www3.nd.edu/~rwilliam/stats3/index.html, which is a syllabus for one of his statistics courses at Notre Dame University. Scroll down to the section headed Interpreting results: Adjusted Predictions and Marginal effects. There you will find 6 handouts and accompanying Stata code that, I think, cover the topic nicely.

                  Comment


                  • #10
                    You might also be interested in this quick blog post that has links to other good summaries too: https://www.kai-arzheimer.com/me-at-the-margins-average-marginal-effects-marginal-effects-at-the-mean-and-statas-margins-command/



                    Comment

                    Working...
                    X