You are on page 1of 3

capture log close

clear
use http://www.stata-press.com/data/imeus/womenwk, clear
log using mfx.log, replace

/* Adapted from Baum's "An Introduction to Modern Econometrics Using Stata" */


summ work age married children education
keep work age married children education
capture save c:\econometrics\logit_and_probit_mfx\imeus_mod.dta, replace

/* Per pg. 250 of IMEUS, the effect of an increase in xj on the probability


is the product of the PDF evaluated at XB times the regression parameter
estimate. Note also that the CDF of XB is used for the predicted
probability but NOT the marginal effect (i.e., the marginal effect uses the
derivative and the chain rule whereas the predicted probability
just uses XB input into the CDF). */

probit work age

/* As we will see later, the marginal effect of a change in age does not
change the predicted probability by 4%. Rather, it changes it by 1.5% */

*************************************************
/* Predicted Probabilities- Probit */
********************************************

/* Calculate "predicted probabilities" as a function of XB only (i.e. calculate


the predicted probability based on beta_hat times the values of the independent
variable). As discussed earlier, XB needs to be input into a PDF and then
multiplied by the regression coefficient for the marginal effect to be
calculated or XB needs to be input into the CDF for the predicted probability
to be calculated. Note that XB differs for all observations in the sample with
different values of each independent variable. */

predict y_xb, xb
summ y_xb

/* Above we can see that y_xb is not bounded between zero and one and there are
also negative projections. Both of these facts illustrate that just
multiplying each regression parameter estimate by the values of the independent
variables is not how the probit model calculates predicted probabilities.
This piece is just one part of the equation. The other part is inputting XB into
the CDF of the relevant distribution which is the standard normal for porbit
and the logistic for logit. */

/* Look at y_xb for the first observation */


list y_xb age in 1

/* Manually calculate XB for the first observation */


di _b[_cons] + _b[age]*22

/* Our manual calculation agrees to Stata's prediction using xb, but we know
the predicted probability is not a actually negative number */

/* Next, calculate the actual predicted probabilities using CDF(XB). Note here
we are actually using the CDF and *not* the PDF because the CDF is used for
the overall actual predicted probability whereas the PDF is used for the
marginal effect. */
predict y_pr, p
summ y_pr

/* Above, can see that the actual predicted probabilities are bounded between
zero and one. Next, we manually calculate this probability for the first
observation by feeding in the value of XB into the cumulative standard
normal distribution */

gen y_pr_hand= normal(_b[_cons]*1 + _b[age]*age)


gen y_pr_hand_alt= normal(-.1434218) if [_n]==1

list y_xb y_pr y_pr_hand y_pr_hand_alt in 1

/* Stata's predicted probability agrees to both of our estimates */

**************************************************
/* Marginal Effects- Probit */
******************************************
probit work age
mfx compute

/* The marginal effect at the sample averages is .0154701, or 1.5% */

/* Per pg. 250 of IMEUS, the effect of an increase in xj on the probability


is the product of the PDF evaluated at XB times the regression parameter
estimate. We use the PDF of the standard normal at XB times Beta */

gen marginal= normalden(_b[_cons]*1 + _b[age]*age) * _b[age]

summ age

/* Mean of age is 30.208. We use this to manually calculate the marginal


effect at the sample means below. Note that the mean of the constant, which
is a vector of 1's, is one. */

gen marginal_at_mean= normalden(_b[_cons] + _b[age]*36.208)* _b[age]

summ marginal_at_mean

/* .0154701 agrees to what -mfx- gave us earlier. However, if we want to


calculate the average marginal effect, rather than the marginal effect at the
average, then we need to use the user-written command margeff */

margeff

/* Above, we see the average marginal effect of a change in age is .0146303,


or 1.4%. This is slightly smaller than the marginal effect at the average.
To manually calculate this, we simply summarize our manual marginal
effects we calculated earlier. This variable was named "marginal." */

summ marginal

/* Our by-hand calculation agrees to the estimate obtained by margeff. Note


that in IMEUS Baum specifies that certain variables are treated as "count"
variables per an margeff option, but here we do not enforce this. It makes
more sense to specify the count option, but if we do that the estimate
from margeff is slightly different than the mean of our projections. However
the results differ only at the fourth decimal place. See below */
margeff, count
drop y_xb- marginal_at_mean

****************************************
/* Predicted Probabilities- Logit */
**************************************

logit work age


mfx compute

/* Can see above that the marginal effect is approximately 1.5%, similar to
the probit model. And again, the marginal effect is different from the
coefficient estimate of .0726359. */

predict y_xb, xb
summ y_xb

/* Again, there are negative projections and projections greater than one if we
simply multiply each Beta_hat times the independent variable values. */

predict y_pr, p
summ y_pr

/* When putting XB into the logistic CDF, probabilities are now bounded
between zero and 1 and there are no negative probabilities. */

gen y_hand= logistic(_b[_cons] + _b[age]* age)

list y_xb y_pr y_hand in 1

/* Our by-hand predicted probabilities agree to those generated by Stata. Next


we calculate the marginal effect by hand */

gen marginal= logisticden(_b[_cons] + _b[age]* age) * _b[age]

gen marginal_at_mean= logisticden(_b[_cons] + _b[age]*36.208)* _b[age]

qui logit work age


mfx compute
summ marginal_at_mean

/* Above there is no difference between the -mfx- estimate of the marginal


effect at the mean of .0156766 and our manual calculation */

/* We already calculated the marginal effect for each observation and


called this variable "margin." Let's compare that to the user-written command
margeff */

qui logit work age


margeff

summ marginal

/* Our manual calculation agrees to the estimate obtained by margeff. Again,


using the count option changes the fourth decimal place */
margeff, count

log close

You might also like