|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: question on margins and interaction between factor and continuous var
From |
[email protected] (Jeff Pitblado, StataCorp LP) |
To |
[email protected] |
Subject |
Re: st: question on margins and interaction between factor and continuous var |
Date |
Mon, 05 Oct 2009 09:44:04 -0500 |
Robert Duval <[email protected]> is using the -margins- postestimation command
with a model fit containing the interaction of a factor variable with a
continuous variable:
> I am a bit confused about the syntax of margins.
>
> I have a model where I interact a categorical variable, say cat, with
> a continuous variable say cont. For simplicity assume cat has 3
> levels.
>
> The command would generically look like
>
> command depvar i.cat cont i.cat#c.cont
>
> I want to obtain the marginal effects of
>
> cat2
> cat3
> cont
> cat2#cont
> cat3#cont
To make things a little more concrete lets use a similar setup with the auto
data:
. sysuse auto
. keep if inlist(rep78,3,4,5)
. gen cat = rep78 - 2
. rename turn cont
. rename mpg depvar
. regress depvar cat##c.cont
> However, when i run margins as
>
> margins , dydx(cat cont)
>
> I obtain the overall average marginal effects for each variable
> (averaging out the interaction)
Using our dataset, here is what Robert would get:
***** BEGIN:
. margins, dydx(cat cont)
Average marginal effects Number of obs = 59
Model VCE : OLS
Expression : Linear prediction, predict()
dy/dx w.r.t. : 2.cat 3.cat cont
------------------------------------------------------------------------------
| Delta-method
| dy/dx Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
cat |
2 | .1310132 1.218829 0.11 0.914 -2.257848 2.519874
3 | -4.835326 3.448432 -1.40 0.161 -11.59413 1.923476
|
cont | -1.266052 .193393 -6.55 0.000 -1.645095 -.8870083
------------------------------------------------------------------------------
Note: dy/dx for factor levels is the discrete change from the base level.
***** END:
Given this simple model, we can verify the above results by computing these
average marginal effects by hand without too much trouble.
***** BEGIN:
* individual predictions for the levels of 'cat', then get the differences
* from the base level of '1'
gen p_cat1 = _b[_cons]+_b[cont]*cont
gen p_cat2 = _b[_cons]+_b[cont]*cont+_b[2.cat]+_b[2.cat#cont]*cont
gen p_cat3 = _b[_cons]+_b[cont]*cont+_b[3.cat]+_b[3.cat#cont]*cont
gen d_cat2 = p_cat2 - p_cat1
gen d_cat3 = p_cat3 - p_cat1
* derivative of the linear prediction with respect to 'cont'
gen d_cont = _b[cont]+_b[2.cat#cont]*2.cat+_b[3.cat#cont]*3.cat
* compute the mean for each of these new predictions
sum d_cat2 d_cat3 d_cont
***** END:
Next Robert mentions the following:
> if i run
>
> margins cat , dydx(cont)
>
> i obtain what it seems to be the average marginal effects for
> different levels of cat, i.e. cat2#cont cat3#cont
***** BEGIN:
. margins cat, dydx(cont)
Average marginal effects Number of obs = 59
Model VCE : OLS
Expression : Linear prediction, predict()
dy/dx w.r.t. : cont
------------------------------------------------------------------------------
| Delta-method
| dy/dx Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
cont |
cat |
1 | -.7499364 .1713144 -4.38 0.000 -1.085706 -.4141664
2 | -.9800797 .2020792 -4.85 0.000 -1.376148 -.5840117
3 | -3.141593 .8650601 -3.63 0.000 -4.83708 -1.446106
------------------------------------------------------------------------------
***** END:
Let's reproduce these calculations by hand.
***** BEGIN:
* derivative of the linear prediction with respect to 'cont', assuming 'cat'
* is equal to each of its levels
gen dd_cat1 = _b[cont]
gen dd_cat2 = _b[cont]+_b[2.cat#cont]
gen dd_cat3 = _b[cont]+_b[3.cat#cont]
* compute the mean (NOTE: these variables are constant)
sum dd_cat1 dd_cat2 dd_cat3
***** END:
Finally, Robert asks
> But then how to obtain the marginal effects for cat2 and cat2 (i.e.
> when cont==0)?
The -at()- option allows you to set the value of predictors (both continuous
and factor) at fixed values. In this case, Robert probably wants to use
-at(cont=0)-. Here is result using our dataset.
***** BEGIN:
. margins, dydx(cat) at(cont=0)
Conditional marginal effects Number of obs = 59
Model VCE : OLS
Expression : Linear prediction, predict()
dy/dx w.r.t. : 2.cat 3.cat
at : cont = 0
------------------------------------------------------------------------------
| Delta-method
| dy/dx Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
cat |
2 | 9.169014 10.55421 0.87 0.385 -11.51686 29.85489
3 | 89.08786 31.65038 2.81 0.005 27.05426 151.1215
------------------------------------------------------------------------------
Note: dy/dx for factor levels is the discrete change from the base level.
***** END:
The following reproduces these calculations by hand.
***** BEGIN:
* individual predictions for the levels of 'cat', assuming 'cont' is zero,
* then get the differences from the base level of '1'
gen p0_cat1 = _b[_cons]
gen p0_cat2 = _b[_cons]+_b[2.cat]
gen p0_cat3 = _b[_cons]+_b[3.cat]
gen d0_cat2 = p0_cat2 - p0_cat1
gen d0_cat3 = p0_cat3 - p0_cat1
* compute the mean (NOTE: these variables are constant)
sum d0_cat2 d0_cat3
***** END:
--Jeff
[email protected]
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/