Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: marginal effects for user defined programs


From   May Boggess <mboggess@stata.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: marginal effects for user defined programs
Date   Tue, 04 Oct 2005 18:24:40 -0500

On Tuesday morning Paul asked about calculating marginal effects after
user written estimation commands.

Paul has made a good start by storing the estimation results and the name of his predict command. This is important since -mfx- uses predict:


program define myreoprob, eclass
reoprob wagecat inc female, i(id)
estimates local cmd="myreoprob"
estimates local predict="mypredict"
end

I have made one slight change to Paul's program: I have changed the single quotes to double quotes.

Now we need to define the program -predict- will call:

cap program drop mypredict
program define mypredict
syntax newvarlist [in] [if], [x]
tempvar touse xb
mark `touse'
_predict double `xb'
gen double `varlist'= norm([_cut1]_b[_cons]-`xb') if `touse'
end

With these two programs defined we can use -mfx- after -reoprob-:

clear
use http://www.stata-press.com/data/r7/auto
keep rep mpg for turn
drop if rep<3
replace rep=rep-3
rename turn id
rename for female
rename mpg inc
rename rep wagecat
myreoprob wagecat inc female, i(id)
mfx compute

Now, given that taking the derivates and differences is not too tricky
in the instance, I'm going to do that just so I can verify I have done
this correctly.

clear
use http://www.stata-press.com/data/r7/auto
keep rep mpg for turn
drop if rep<3
replace rep=rep-3
rename turn id
rename for female
rename mpg inc
rename rep wagecat
reoprob wagecat inc female, i(id)

sum inc
local minc=r(mean)
sum female
local mfemale=r(mean)
local xb (_b[inc]*`minc'+_b[female]*`mfemale')

display " p = " norm([_cut1]_b[_cons]-`xb')
display "dpdinc: " normd([_cut1]_b[_cons]-`xb')*(-_b[inc])

local xb1 (_b[inc]*`minc'+_b[female]*1)
local xb0 (_b[inc]*`minc'+_b[female]*0)
local p1 norm([_cut1]_b[_cons]-`xb1')
local p0 norm([_cut1]_b[_cons]-`xb0')
display "dpdfemale: " `p1'-`p0'

The marginal effects I calculated here "by hand" should agree with those
give by -mfx-. Of course, -mfx- has the advantage of being able to also give the standard errors. In version 9, one can use -nlcom- instead of
-display- in my "by hand" method to get the standard errors.

-- May
mmb@stata.com

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index