I am using Stata 13.1 to fit a logistic model and I am getting confidence intervals below 0 and above 1 when I predict probabilities using the margins command.
MRE:
sysuse auto, clear
* simple logistic regression
logit foreign mpg
* get predicted probabilities
margins, at(mpg=(5(5)40)) predict(pr)
* same result with expression
margins, at(mpg=(5(5)40)) exp(invlogit(predict(xb)))
The output is:
------------------------------------------------------------------------------
| Delta-method
| Margin Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
_at |
1 | .0271183 .0252542 1.07 0.283 -.0223792 .0766157
2 | .0583461 .0389888 1.50 0.135 -.0180704 .1347627
3 | .1210596 .0509373 2.38 0.017 .0212244 .2208948
4 | .2344013 .0547344 4.28 0.000 .127124 .3416787
5 | .4049667 .0743318 5.45 0.000 .259279 .5506543
6 | .6020462 .1162814 5.18 0.000 .3741387 .8299536
7 | .7707955 .1266899 6.08 0.000 .5224879 1.019103
8 | .8820117 .1004224 8.78 0.000 .6851874 1.078836
I'm trying to figure out what is Stata doing so I extracted the results with:
* save table as matrix
mat pr = r(table)
mat p = pr' //transpose matrix
Then I predicted the log odds (linear prediction), saved the results and transformed to probability (inverse logit)
* predict log odds
margins, at(mpg=(5(5)40)) exp(predict(xb))
* save table as matrix
mat tab = r(table)
mat t = tab'
clear
svmat t
* transform logit to probability and display
gen prob = invlogit(t1)
gen problo = invlogit(t5)
gen probhi = invlogit(t6)
format %9.3f prob*
list prob* in 1/8, noobs clean // correct confidence intervals?!
prob problo probhi
0.027 0.004 0.154
0.058 0.015 0.199
0.121 0.051 0.260
0.234 0.144 0.358
0.405 0.271 0.555
0.602 0.369 0.797
0.771 0.452 0.932
0.882 0.530 0.980
Now if I take the results from the margins output in probability scale predict(pr) and (wrongly) use the SE from that output to produce CIs I get the same as Stata:
clear
* convert results to data
svmat p
* generate confidence intervals (the wrong way)
gen problo = p1 - (1.96*p2)
gen probhi = p1 + (1.96*p2)
format %9.3f p*
list p5 problo p6 probhi in 1/8, noobs clean
p5 problo p6 probhi
-0.022 -0.022 0.077 0.077
-0.018 -0.018 0.135 0.135
0.021 0.021 0.221 0.221
0.127 0.127 0.342 0.342
0.259 0.259 0.551 0.551
0.374 0.374 0.830 0.830
0.522 0.522 1.019 1.019
0.685 0.685 1.079 1.079
Sorry it took me so long but here is the question:
Is there a way of getting the right CIs using the margins command directly? and does this problem happens with other GLMs?
Thanks