# Re: st: Marginal effect of interaction var in multinomial logit model

 From Guido Heineck To statalist@hsphsun2.harvard.edu Subject Re: st: Marginal effect of interaction var in multinomial logit model Date Mon, 17 Sep 2007 08:46:30 +0200

Maarten,

by providing an 'extended' code incl. the probabilities of falling in cat. 2 and 3, you anticipated what would have been my subsequent question?!

Actually, I have 4 outcomes, but adapting your code (see below) worked fine - though I had to play a bit with the auto-data to get a stable model.

Best, Guido

*-------------- begin example -----------------
set more off
#delimit ;
sysuse auto, clear;
xtile lhs = price, nq(4);
tab lhs;

gen forXmpg = foreign * mpg ;

mlogit lhs foreign mpg forXmpg ;

sum mpg if e(sample) ;
scalar mmpg = r(mean) ;

local exp2f "exp(_cons + foreign*1 + mpg*scalar(mmpg) + forXmpg*1*scalar(mmpg))" ;
local exp2d "exp(_cons + foreign*0 + mpg*scalar(mmpg) + forXmpg*0*scalar(mmpg))" ;

local exp3f "exp(_cons + foreign*1 + mpg*scalar(mmpg) + forXmpg*1*scalar(mmpg))" ;
local exp3d "exp(_cons + foreign*0 + mpg*scalar(mmpg) + forXmpg*0*scalar(mmpg))" ;

local exp4f "exp(_cons + foreign*1 + mpg*scalar(mmpg) + forXmpg*1*scalar(mmpg))" ;
local exp4d "exp(_cons + foreign*0 + mpg*scalar(mmpg) + forXmpg*0*scalar(mmpg))" ;

local p1f "1/(1 + `exp2f' + `exp3f' + `exp4f')" ;
local p1d "1/(1 + `exp2d' + `exp3d' + `exp4d')" ;

local p2f "`exp2f'/(1 + `exp2f' + `exp3f' + `exp4f')" ;
local p2d "`exp2d'/(1 + `exp2d' + `exp3d' + `exp4d')" ;

local p3f "`exp3f'/(1 + `exp2f' + `exp3f' + `exp4f')" ;
local p3d "`exp3d'/(1 + `exp2d' + `exp3d' + `exp4d')" ;

local p4f "`exp4f'/(1 + `exp2f' + `exp3f' + `exp4f')" ;
local p4d "`exp4d'/(1 + `exp2d' + `exp3d' + `exp4d')" ;

nlcom (Dp1DmpgF: `p1f'*(0-(`p2f'*mpg + `p3f'*mpg + `p4f'*mpg)))
(Dp1DmpgD: `p1d'*(0-(`p2d'*mpg + `p3d'*mpg + `p4d'*mpg)))
(Dp2DmpgF: `p2f'*(mpg+forXmpg-(`p2f'*mpg + `p3f'*mpg + `p4f'*mpg)))
(Dp2DmpgD: `p2d'*(mpg-(`p2d'*mpg + `p3d'*mpg + `p4d'*mpg)))
(Dp3DmpgF: `p3f'*(mpg+forXmpg-(`p2f'*mpg + `p3f'*mpg + `p4f'*mpg)))
(Dp3DmpgD: `p3d'*(mpg-(`p2d'*mpg + `p3d'*mpg + `p4d'*mpg)))
(Dp4DmpgF: `p4f'*(mpg+forXmpg-(`p2f'*mpg + `p3f'*mpg + `p4f'*mpg)))
(Dp4DmpgD: `p4d'*(mpg-(`p2d'*mpg + `p3d'*mpg + `p4d'*mpg))) ;
*--------------------- end example ------------------

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

Date: Sat, 15 Sep 2007 13:30:17 +0100 (BST)
From: Maarten buis <maartenbuis@yahoo.co.uk>
Subject: Re: st: Marginal effect of interaction var in multinomial logit model

What you showed are the marginal effect of foreign, mpg, and forXmpg on
the probability of falling in the reference category of rep78. More
sensible to me would be the effect of mpg for foreign cars and the
effect of mpg for domestic cars. Moreover, you also have marginal
effects on the probabilities of falling in category 2 and category 3.

In the example below Dp1DmpgF is the marginal effect of mpg on the
probability of falling in category 1 of rep78 for foreign cars,
Dp1DmpgD is the marginal effect of mpg on the probability of falling in
category 1 of rep78 for domestic cars, Dp2DmpgF is the marginal effect
of mpg on the probability of falling in category 2 of rep78 for foreign
cars, etc.

Hope this helps,
Maarten

*-------------- begin example -----------------
set more off
#delimit ;
sysuse auto, clear;
recode rep78 2=1 3=1 4=2 5=3 .=3 ;
* WARNING: recode .=3 is something you would not do in a
* normal analysis but in this case it stabalizes the model,
* and I don't care about the results, I just use it to
* show the mechanics of creating marginal effects. You
* should never ever do this in an analysis that you

gen forXmpg = foreign * mpg ;

mlogit rep78 foreign mpg forXmpg ;

sum mpg if e(sample) ;
scalar mmpg = r(mean) ;

local exp2f "exp(_cons + foreign*1
+ mpg*scalar(mmpg) + forXmpg*1*scalar(mmpg))" ;
local exp2d "exp(_cons + foreign*0
+ mpg*scalar(mmpg) + forXmpg*0*scalar(mmpg))" ;

local exp3f "exp(_cons + foreign*1
+ mpg*scalar(mmpg) + forXmpg*1*scalar(mmpg))" ;
local exp3d "exp(_cons + foreign*0
+ mpg*scalar(mmpg) + forXmpg*0*scalar(mmpg))" ;

local p1f "1/(1 + `exp2f' + `exp3f')" ;
local p1d "1/(1 + `exp2d' + `exp3d')" ;

local p2f "`exp2f'/(1 + `exp2f' + `exp3f')" ;
local p2d "`exp2d'/(1 + `exp2d' + `exp3d')" ;

local p3f "`exp3f'/(1 + `exp2f' + `exp3f')" ;
local p3d "`exp3d'/(1 + `exp2d' + `exp3d')" ;

nlcom (Dp1DmpgF: `p1f'*(0-(`p2f'*mpg + `p3f'*mpg)))
(Dp1DmpgD: `p1d'*(0-(`p2d'*mpg + `p3d'*mpg)))
(Dp2DmpgF: `p2f'*(mpg+forXmpg-(`p2f'*mpg +
`p3f'*mpg)))
(Dp2DmpgD: `p2d'*(mpg-(`p2d'*mpg + `p3d'*mpg)))
(Dp3DmpgF: `p3f'*(mpg+forXmpg-(`p2f'*mpg +
`p3f'*mpg)))
(Dp3DmpgD: `p3d'*(mpg-(`p2d'*mpg + `p3d'*mpg))) ;
*--------------------- end example ------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

- --- Guido Heineck <guido.heineck@gmx.net> wrote:

> > Dear all.
> >
> > I'm somewhat struggling with calculating marginal effects for
> > interaction variables in a multinomial logit model framework.
> >
> > Adapting two codes by Maarten L. Buis from prior posts that address
> > related issues
> > (http://www.stata.com/statalist/archive/2005-09/msg00126.html and
> > http://www.datasets.org/statalist/archive/2006-05/msg01057.html) I
> > get
> > some results with the following code but I am unsure as to whether it
> > is
> > correctly programmed.
> >
> > *-------------- begin example -----------------
> > #delimit ;
> > sysuse auto, clear;
> > recode rep78 2=1 3=1 4=2 5=3 ;
> >
> > gen forXmpg = foreign * mpg ;
> >
> > mlogit rep78 foreign mpg forXmpg ;
> >
> > sum foreign if e(sample) ;
> > scalar mfor = r(mean) ;
> > sum mpg if e(sample) ;
> > scalar mmpg = r(mean) ;
> >
> > local exp2 "exp(_cons + foreign*scalar(mfor)
> > + mpg*scalar(mmpg) + forXmpg*scalar(mfor)*scalar(mmpg))" ;
> >
> > local exp3 "exp(_cons + foreign*scalar(mfor)
> > + mpg*scalar(mmpg) + forXmpg*scalar(mfor)*scalar(mmpg))" ;
> >
> > local p1 "1/(1 + `exp2' + `exp3')" ;
> > local p2 "`exp2'/(1 + `exp2' + `exp3')" ;
> > local p3 "`exp3'/(1 + `exp2' + `exp3')" ;
> >
> > nlcom (foreign: `p1'*(0-(`p2'*foreign + `p3'*foreign)))
> > (mpg: `p1'*(0-(`p2'*mpg + `p3'*mpg)))
> > (forXmpg: `p1'*(0-(`p2'*forXmpg + `p3'*forXmpg))) ;
> >
> > *--------------------- end example ------------------
> >
> > Any comment will be appreciated.
> > Guido
> >
> >
--