Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: RE: Stata list example - use of the margins command

 From Steve Samuels To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Stata list example - use of the margins command Date Fri, 9 Dec 2011 12:38:58 -0500

```

As Brendan pointed out. -logistic- computes CIs for log odds ratios, then presents the CIs on the OR scale. I wouldn't expect -nlcom-, a very general purpose program, to "know" to do this, but we as analysts know and can tell it what to do.

*************CODE BEGINS*************
capture program drop _all
program antilog
local bound  invnorm(.975)*sqrt(el(r(V),1,1))
local lparm el(r(b),1,1)
local ll  exp(`lparm'  - `bound')
local ul  exp( `lparm' + `bound')
di _col(1) "parm =" %8.4g exp(`lparm')  ///
_col(20) "ll ="   %8.4g `ll'  ///
_col(40) "ul ="   %8.4g `ul'
end

sysuse nlsw88, clear
gen byte high_occ = occupation < 3 if occupation < .
gen byte black = race == 2 if race < .
drop if race==3
drop if _n > 1000

quietly logistic high_occ i.race##i.collgrad
quietly margins, over(race collgrad) expression(exp(xb())) post

antilog
**************CODE ENDS***************

Steve
sjsamuels@gmail.com

On Dec 8, 2011, at 5:42 PM, Peter Baade wrote:

Maarten,

Thanks for responding. However on further examination of nlcom command, and reading the online stata manual, I'm not sure that nlcom is the idea method for calculating confidence intervals for odds ratios following the margins command. This is noted on page 1208 of the stata manual (nlcom), in which they say that while the exponential coefficients are identical, the calculated confidence intervals are not, probably due to the nlcom using asymptotically-equivalent calculations for the CIs.

In the example below (with a reduced sample size) I generate the odds ratios using three methods, margins with nlcom, tabodds, and stratified logistic regression. While the ORs and standard errors are very similar, nlcom's confidence intervals seem to be incorrect.

Peter.

*********** Begin example ******************
. sysuse nlsw88, clear
(NLSW, 1988 extract)

. gen byte high_occ = occupation < 3 if occupation < .
(9 missing values generated)

. gen byte black = race == 2 if race < .

. drop if race==3
(26 observations deleted)

. drop if _n > 1000
(1220 observations deleted)

. quietly logistic high_occ i.race##i.collgrad
. quietly margins, over(race collgrad) expression(exp(xb())) post

.
. ** Odds ratio (black:white) when collgrad=1

------------------------------------------------------------------------------
|      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
_nl_1 |   .8181818   .3766025     2.17   0.030     .0800545    1.556309
------------------------------------------------------------------------------

.
. ** Odds ratio (black:white) when collgrad=1
. tabodds high_occ race if collgrad==1, or

---------------------------------------------------------------------------
race |  Odds Ratio       chi2       P>chi2     [95% Conf. Interval]
-------------+-------------------------------------------------------------
white |    1.000000          .           .              .          .
black |    0.818182       0.19       0.6631      0.331220   2.021078
---------------------------------------------------------------------------
Test of homogeneity (equal odds): chi2(1)  =     0.19
Pr>chi2  =   0.6631

Score test for trend of odds:     chi2(1)  =     0.19
Pr>chi2  =   0.6631

.
. ** Odds ratio (black:white) when collgrad=1
. logistic high_occ i.race if collgrad==1

Logistic regression                               Number of obs   =        251
LR chi2(1)      =       0.19
Prob > chi2     =     0.6618
Log likelihood = -173.54746                       Pseudo R2       =     0.0006

------------------------------------------------------------------------------
high_occ | Odds Ratio   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
2.race |   .8181819   .3766026    -0.44   0.663     .3319316    2.016746
------------------------------------------------------------------------------

**************** End Example *****************

-----Original Message-----
From: Maarten Buis [mailto:maartenlbuis@gmail.com]
Sent: Thursday, 8 December 2011 7:21 PM
To: statalist@hsphsun2.harvard.edu
Subject: Stata list example - use of the margins command

--- Peter Baade wrote me privately:
> I noticed that you posted the following example on the Stata list in
> May this year.
> <http://www.stata.com/statalist/archive/2011-05/msg00776.html>
>
> I was wondering if I could ask you one question

You can, but convention on Statalist is that these questions are asked on the list rather than privately. If you found something strange than odds are that others will also find that to be the case, and would thus appreciate an answer as well. It also has advantages for you, because it regularly happens that others participate in the discussion contributing another point of view or pointing out mistakes or ommisions in my answer. This is discussed in the Statalist FAQ:
<http://www.stata.com/support/faqs/res/statalist.html#private> So I forwarded this messages to statalist.

> when you used the "di" command below, could I use the nlcom command to get confidence intervals? I've used this and get very wide intervals, inconsistent with the p-value.

Below is an adaptation of the example you refer to, replacing -di- with -nlcom-. The p-value in -nlcom- is in this case useless as it tests the hypothesis that the odds ratio is 0 rather than the meaningfull hypothesis that the odds ratio is 1.

You can also see the confidence interval produced by -nlcom- is somewhat different from the confidence interval produced by -logit- even though the coefficient and the standard error match exactly. The reason for that is explained here:
<http://www.stata.com/support/faqs/stat/2deltameth.html>

*------------------------ begin example ------------------------------------
sysuse nlsw88, clear
gen byte high_occ = occupation < 3 if occupation < .
gen byte black = race == 2 if race < .
drop if race == 3
gen byte baseline = 1

sum ttl_exp
gen c_ttl_exp = ttl_exp - r(mean)

// fix at average ttl_exp (12.5 years)

logit high_occ black##collgrad c_ttl_exp baseline, or nocons

margins, over(black collgrad) expression(exp(xb())) ///
at(c_ttl_exp = 0) post

nlcom (_b[1.black#1.collgrad] / _b[0.black#1.collgrad] ) / ///
*-------------------------- end example ------------------------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------

IMPORTANT

Please consider our environment before printing this email

This email and any attachments is intended for the named recipient(s) only and may contain privileged and confidential information. If you have received this email in error, please inform the sender and delete this email and any copies from your computer network. If you are not the intended recipient of this email, any unauthorised use is expressly prohibited. It is your responsibility to check any attachments for viruses and defects before opening or sending them on.

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

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