[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

# RE: st: marginal effects for user defined programs

 From "Steve Stillman" To Subject RE: st: marginal effects for user defined programs Date Wed, 5 Oct 2005 13:53:17 +1300

```Hi May (and other statalisters).  I have a general question on this.  What would the mypredict program look like if we want to generalise and have be able to get the marginal effects for the predicted probability of every outcome?

I gather we would need to pass the number of outcomes through to mypredict and then loop through the appropriate predicted probability formulas.  I thought you might be able to provide insight into how best to do this (and how to show predict that you have different possible predicted probabilities).

Cheers,
Steve

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of May Boggess
Sent: Wednesday, October 05, 2005 12:25 PM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: marginal effects for user defined programs

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/

*
*   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–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index