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

st: AW: Estimate marginal effects with interaction terms in non-linnear models.


From   <Colin.Vance@dlr.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: AW: Estimate marginal effects with interaction terms in non-linnear models.
Date   Tue, 6 Sep 2005 12:38:35 +0200

Predictnl and nlcom are both useful for calculating interaction effects. The following code gives examples of each, including when the model has a quadratic term. I wrote the code with the generous help of a Stata technician, but it hasn't been "peer reviewed" in any sense. Nevertheless, the final example produces the same result as inteff (since no quadratic is involved).

Colin

Colin Vance, Ph.D.
German Aerospace Center
Institute of Transport Research
Rutherfordstrasse 2
12489 Berlin
Germany

tel: +49 30 67055147
fax: +49 30 67055202
email: colin.vance@dlr.de



/*Set up random data set*/
clear
set obs 200
set seed 12345
gen usecar =uniform()>0.5
gen age =uniform()*50 +15
gen agesquare = age^2
gen female  =uniform()>0.5
gen inter=female*age
gen otherstuff1= uniform()
gen otherstuff2= uniform()>0.5

/*Estimate model*/
probit usecar age agesquare female inter otherstuff1 otherstuff2

/*Set up local macros for evaluating marginal effects at mean values*/
sum age if e(sample)
local age=r(mean)
sum female if e(sample)
local female=r(mean)
sum otherstuff1 if e(sample)
local otherstuff1=r(mean)
sum otherstuff2 if e(sample)
local otherstuff2=r(mean)

/*Get predicted values from the model*/
local xb _b[age]*`age'+_b[agesquare]*`age'^2 + _b[female]*`female' + _b[inter]*`female'*`age' +_b[otherstuff1]*`otherstuff1'+_b[otherstuff2]*`otherstuff2'+_b[_cons]

/*The following gives the marginal effect of age*/
local dpda normden(`xb')*(_b[age]+2*_b[agesquare]*`age'+_b[inter]*`female')
nlcom `dpda'

/*The following gives the marginal effect of the female dummy variable*/
local xb1 _b[age]*`age'+_b[agesquare]*`age'^2 + _b[female]*1 + _b[inter]*1*`age' +_b[otherstuff1]*`otherstuff1'+ _b[otherstuff2]*`otherstuff2'+_b[_cons]
local xb0 _b[age]*`age'+_b[agesquare]*`age'^2 + _b[female]*0 + _b[inter]*0*`age'+_b[otherstuff1]*`otherstuff1' +_b[otherstuff2]*`otherstuff2'+_b[_cons] 
local dpdf norm(`xb1')-norm(`xb0')
nlcom `dpdf'

/*The following gives the marginal effect of otherstuff1*/
local dpdo1 normden(`xb')*_b[otherstuff1] 
nlcom `dpdo1'

/*The following gives the marginal effect of the dummy otherstuff1*/
local xb1 _b[age]*`age'+_b[agesquare]*`age'^2 + _b[female]*`female' /*
	*/ + _b[inter]*`female'*`age' /*
	*/ +_b[otherstuff1]*`otherstuff1'+_b[otherstuff2]*1+_b[_cons]
local xb0 _b[age]*`age'+_b[agesquare]*`age'^2 + _b[female]*`female' /*
	*/ + _b[inter]*`female'*`age' /*
	*/ +_b[otherstuff1]*`otherstuff1'+_b[otherstuff2]*0+_b[_cons]
local dpdo2 norm(`xb1')-norm(`xb0')
nlcom `dpdo2'


/*The following gives the marginal effect of the interaction of age and female, noting that age is specified as a quadratic*/
local d2pdfda normden(`xb1')*(_b[age]+2*_b[agesquare]*`age'+_b[inter]*1)- normden(`xb0')*(_b[age]+2*_b[agesquare]*`age'+_b[inter]*0)
nlcom `d2pdfda'





/*THIS PART USES PREDICTNL TO CALCULATE THE INTERACTION EFFECT FOR A CONTINUOUS VARIABLE TO COMPARE WITH THE RESULT FROM INTEFF*/
clear
set obs 200 
set seed 12345 
gen usecar =uniform()>0.5 
gen contin1  =uniform() 
gen contin2 = uniform() 
gen inter=contin1*contin2 
gen otherstuff1= uniform() 

/*Now estimate a probit model*/
probit usecar contin1 contin2 inter otherstuff  

/*Now type the code for the interaction effect, inter*/


local xbu _b[contin1]* contin1 +_b[contin2]* contin2 + _b[inter]*contin1*contin2 + _b[otherstuff] * otherstuff + _b[_cons] 


local coef _b[inter]-(_b[contin1]+_b[inter]* contin2)*(_b[contin2]+_b[inter]* contin1)*( _b[contin1]* contin1 +_b[contin2]* contin2 + _b[inter]*contin1*contin2 + _b[otherstuff] * otherstuff + _b[_cons]) 

local d2pdfda (`coef')*(normden(`xbu')) 



predictnl dinter= `d2pdfda', se(seinter) 

/*Here is the interaction effect drawing on the above code*/

sum dinter seinter 



/*Now calculate the interaction effect using inteff to make sure you get the same answer*/


probit usecar contin1 contin2 inter otherstuff  


inteff usecar contin1 contin2 inter otherstuff  



-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Dozet Alexander
Gesendet: Monday, September 05, 2005 5:55 PM
An: statalist@hsphsun2.harvard.edu
Betreff: st: Estimate marginal effects with interaction terms in non-linnear models.

Hi folks!

I'm beginner when it comes to using Stata (Stata 9). Currently I'm trying to estimate marginal effects with a logit model with 10 interaction terms (one continuous and one dummy). Furthermore one of the interaction terms is age sqr*male. Hence the use of -inteff- is not possible. I'm trying to learn how to use the -predictnl- command but have run into some difficulties. I tried to re-estimate the probit model that is to be found in  "FAQ:I am using a model with interactions. How can I obtain marginal effects and their standard errors?" may 2004 at  http://www.stata.com/support/faqs/stat/mfx_interact.html.
However when I estimate
...
local xb _b[turn]*`meantur'  + /*
               */  _b[dum]*`meandum' + _b[td]*`meantur'*`meandum' + _b[_cons] predictnl dydt = normden(`xb')*(_b[turn] + _b[td]*`meandum') in 1, se(set) ....
just after -predictnl- i get the error message invalid syntax r(198); Surely the error is obvious and is staring right in my face but I have not yet found it. Does any one know where the misstake is?
Also, does any one know if there exists any command, like -inteff- by Norton et.al., 2004, The Stata Journal, but without its limits when it comes to number of interaction terms or non-linear terms? Predictnl feels a bit difficult for a beginner.
I like the grafical presentation of the interaction effects and z-statistics, found in -inteff-, and would like to do the same thing with my model. 

Thanx and great having someone to ask

Kind regards
Alexander Dozet



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