# Re: st: interactions, slopes, and standard errors

 From "Jesper B. Sorensen" To statalist@hsphsun2.harvard.edu Subject Re: st: interactions, slopes, and standard errors Date Fri, 26 Mar 2004 13:50:30 -0500

Here's a stab at a hack that would do it for the 25th, 50th and 75th percentiles. For arbitrary values you could change the forvalues to a foreach and simply list the values you want (and change the display'ed text) or have this be something the user supplies.

program marginalx2, rclass
version 8.1
syntax , x(varname) y(varname) xy(varname)

qui sum `y' if e(sample), det

forvalues p = 25(25)75 {

local marginal = _b[`x'] + _b[`xy']*r(p`p')

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' + (r(p`p')^2)*`var_b2' + 2*r(p`p')*`covar_b1b2'

dis ""
disp in gr "Marginal effect of `x' at the " `p' "th percentile of `y' = " `marginal'
disp in gr "Variance of marginal effect of `x' at the " `p' "th percentile `y' = " `variance'

return scalar margx`p' = `marginal'
return scalar var_margx`p' = `variance'
}
end

At 12:13 PM 3/26/2004 -0600, you wrote:

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

**
- Scott Merryman's program to calculate the marginal effects and the variance of interaction terms -

program marginalx, rclass
version 8.1
syntax , x(varname) y(varname) xy(varname)

sum `y' if e(sample), meanonly

local marginal = _b[`x'] + _b[`xy']*r(mean)

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' + (r(mean)^2)*`var_b2' + 2*r(mean)*`covar_b1b2'

dis ""
disp in gr "Marginal effect of `x' at the mean of `y' = " `marginal'
disp in gr "Variance of marginal effect of `x' at the mean of `y' = " `variance'

return scalar margx = `marginal'
return scalar var_margx = `variance'
end

**************************************************

Chip Hunter
Management and Human Resources Dept.
4114 Grainger Hall
975 University Ave.
608-265-3533
**************************************************

*
* 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/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jesper B. Sørensen
Richard S. Leghorn (1939) Associate Professor of Strategic Management
Sloan School of Management
Massachusetts Institute of Technology
E52-581
Cambridge, MA 02142
http://web.mit.edu/sorensen/www/
(617) 253 7945 -- voice
(617) 253 2660 -- fax

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