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

st: Re: interactions, slopes, and standard errors


From   "Scott Merryman" <smerryman@kc.rr.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: interactions, slopes, and standard errors
Date   Tue, 30 Mar 2004 20:40:08 -0600

Chip,

Below is an updated version. It can use values that are returned
from -summarize, detail- or  specific values. It does not perform all the error
trapping it should, but it should still be of use.

Examples:

. sysuse auto
(1978 Automobile Data)

. gen mpgXprice = mpg*price

. qui reg weight price mpg mpgXprice

. ****Evaluated at the mean

. marginalx, x(price) y(mpg) xy(mpgXprice)

Marginal effect of price interacted with mpg
Evaluated at the mean (21.297297) of mpg
--------------------------------------------------------------------------------
----
      variable |   mfx       Std. Err.     z       P>|z|     [ 95% Conf.
Interval ]
---------------+----------------------------------------------------------------
----
        price  | .0475011     .026751     1.78     0.076     -.0049308
.099933
--------------------------------------------------------------------------------
----

. *****Evaluated at the 75th percentile

. marginalx, x(price) y(mpg) xy(mpgXprice) stat(p75)

Marginal effect of price interacted with mpg
Evaluated at the p75 (25) of mpg
--------------------------------------------------------------------------------
----
      variable |   mfx       Std. Err.     z       P>|z|     [ 95% Conf.
Interval ]
---------------+----------------------------------------------------------------
----
        price  | .0417085     .038794     1.08     0.282     -.0343262
.1177433
--------------------------------------------------------------------------------
----

. ****Evaluated at a value of 30

. marginalx, x(price) y(mpg) xy(mpgXprice) value(30)

Marginal effect of price interacted with mpg
Evaluated at the value (30) of mpg
--------------------------------------------------------------------------------
----
      variable |   mfx       Std. Err.     z       P>|z|     [ 95% Conf.
Interval ]
---------------+----------------------------------------------------------------
----
        price  | .0338864     .057716     0.59     0.557     -.0792354
.1470082
--------------------------------------------------------------------------------
----

Hope this helps,

Scott

--------------------------------------------

*! version 1.1,  Scott Merryman 3/30/2004
* statistic can any value returned from -summarize-
* the value option needs to be number, this error should be trapped
* does not check if variables x and y were in model

program marginalx, rclass
version 8.2
syntax , x(varname) y(varname) xy(varname) [stat(str) value(str) level(integer
95)]

if e(cmd)~="regress" & e(cmd)~="areg" & e(cmd)~="xtreg" & e(cmd)~="xtgls"{
        display in red "marginalx only works with regress, xtreg, xtgls or areg"
        exit 301
}

if "`stat'" ~= "" & `"`value'"' != "" {
 disp in red "Select either a value or statistic for the interaction term"
 exit 301
}

if "`value'" == "" {
 if "`stat'" == "" {
  local stat mean
 }

qui sum `y' if e(sample), detail
local stat2 r(`stat')
local eval `stat'
}

else {
 local stat2 = `value'
 local eval value
}

local marginal = _b[`x'] + _b[`xy']*`stat2'

matrix A = e(V)
matrix B1 = A["`x'" , "`x'"]
local var_b1 = B1[1,1]

matrix B2 = A["`xy'" , "`xy'"]
local var_b2 = B2[1,1]

matrix B3 = A["`x'" , "`xy'"]
local covar_b1b2 = B3[1,1]

local variance = `var_b1' + (`stat2'^2)*`var_b2' + 2*`stat2'*`covar_b1b2'

if (`level'<10 | `level'>99) {
        local level 95
}

tempname Z

local se = sqrt(`variance')
scalar `Z' = invnorm(1-(1-`level'/100)/2)
local ll = `marginal' - `Z'*`se'
local ul = `marginal' + `Z'*`se'
local z =  `marginal'/`se'
local pvalue = 2*normprob(-abs(`z'))

disp ""
disp in smcl in gr "Marginal effect of `x' interacted with `y' "
disp in smcl in gr "Evaluated at the `eval' (" in ye `stat2' in gr ") of `y'"

disp in smcl in gr "{hline 15}{c TT}{hline 68}"
disp in smcl in gr   "{ralign 14:variable}"  _col(15) " {c |} " ///
        _col(20) "mfx"  ///
        _col(30)  `"Std. Err."' ///
        _col(44) "z" ///
        _col(52) "P>|z|" ///
        _col(62) `"[ `level'% Conf. Interval ]"' ///
        _n  "{hline 15}{c +}{hline 68}"

di in smcl in  gr "{ralign 14: `x' }" _col(15) " {c |} " ///
 _col(18) in ye %-9.0g `marginal' ///
 _col(30) in ye %8.0g `se' ///
 _col(42) in ye %5.2f `z' ///
 _col(52) in ye %5.3f  `pvalue' ///
 _col(62) in ye %9.0g `ll' "   " in ye %9.0g `ul' ///
 _n "{hline 15}{c BT}{hline 68}"


return scalar mfx = `marginal'
return scalar se_mfx = `se'
return scalar pvalue = `pvalue'

end

----- Original Message ----- 
From: "Chip Hunter" <Lhunter@bus.wisc.edu>
To: <statalist@hsphsun2.harvard.edu>
Sent: Friday, March 26, 2004 12:13 PM
Subject: st: interactions, slopes, and standard errors


> Has anyone written a general program to calculate and display the estimated
> slope and associated error of Y on x1 at various values of x2, where Y = b0
> + b1x1 + b2x2 + b3x1x2
>
> Last fall Scott Merryman posted a nice program (which I copy in, below)
> which takes care of this at the mean of x2. Thanks, Scott!, We could also
> be interested in doing it for other values.
>
> Chip Hunter
>

*
*   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