The -margins- command uses a normal approximation for computing confidence intervals (which is fine for many situations), but in case of non-linear models (such as logistic regression) the bounds of the confidence intervals can become lower 0 or larger 1. I would like to have logit transformed confidence intervals for predictions (similarly as with -proportions-).
Jeff Pitblado provided the .ado -transform_margins- (available via -install transform_margins, from(http://www.stata.com/users/jpitblado)- ) that allows this (and by the way can easily transform proportions into percentages), see https://stats.stackexchange.com/ques...rgins-in-stata .
This works well as long as I have only one predictor: In that case the confidence intervals using -transform_margins- are comparatively close to the confidence intervals produced by -proportions- and -ci proportions-:
Example:
However, this methods fails if I add additional predictors to the model, in this case the predicted proportions using -margins- and applying -transform_margins- will differ:
a) Is there any way to solve this problem (or is the difference of the predicted proportions/percentages acceptable)?
b) If this is possible, is there an easy way (such as with -marginsplot-) to plot the predicted proportions (or percentages) with confidence intervals after having transformed the margins?
Jeff Pitblado provided the .ado -transform_margins- (available via -install transform_margins, from(http://www.stata.com/users/jpitblado)- ) that allows this (and by the way can easily transform proportions into percentages), see https://stats.stackexchange.com/ques...rgins-in-stata .
This works well as long as I have only one predictor: In that case the confidence intervals using -transform_margins- are comparatively close to the confidence intervals produced by -proportions- and -ci proportions-:
Example:
Code:
. install transform_margins, from(http://www.stata.com/users/jpitblado) . sysuse auto (1978 automobile data) . recode rep78 (1/3=0) (4/5=1), gen(rep78_d) (69 differences between rep78 and rep78_d) . . qui logit rep78_d i.foreign . margins foreign Adjusted predictions Number of obs = 69 Model VCE: OIM Expression: Pr(rep78_d), predict() ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- foreign | Domestic | .2291667 .0606646 3.78 0.000 .1102662 .3480671 Foreign | .8571429 .0763604 11.22 0.000 .7074793 1.006806 ------------------------------------------------------------------------------ . . qui margins foreign, predict(xb) . transform_margins 100*invlogit(@) ---------------------------------------------- | b ll ul -------------+-------------------------------- foreign | Domestic | 22.91667 13.16886 36.82027 Foreign | 85.71429 63.86495 95.32031 ---------------------------------------------- . . proportion rep78_d, over(foreign) Proportion estimation Number of obs = 69 ----------------------------------------------------------------- | Logit | Proportion Std. err. [95% conf. interval] ----------------+------------------------------------------------ rep78_d@foreign | 0 Domestic | .7708333 .0606646 .6289563 .8696994 0 Foreign | .1428571 .0763604 .0458191 .3664757 1 Domestic | .2291667 .0606646 .1303006 .3710437 1 Foreign | .8571429 .0763604 .6335243 .9541809 ----------------------------------------------------------------- . ci proportions rep78_d if foreign==0, agresti Agresti–Coull Variable | Obs Proportion Std. err. [95% conf. interval] -------------+--------------------------------------------------------------- rep78_d | 48 .2291667 .0606646 .131484 .3669869 . ci proportions rep78_d if foreign==1, agresti Agresti–Coull Variable | Obs Proportion Std. err. [95% conf. interval] -------------+--------------------------------------------------------------- rep78_d | 21 .8571429 .0763604 .6451855 .9586438 . ci proportions rep78_d if foreign==1, wilson Wilson Variable | Obs Proportion Std. err. [95% conf. interval] -------------+--------------------------------------------------------------- rep78_d | 21 .8571429 .0763604 .6536394 .9501899
Code:
. qui logit rep78_d i.foreign mpg . margins foreign Predictive margins Number of obs = 69 Model VCE: OIM Expression: Pr(rep78_d), predict() ------------------------------------------------------------------------------ | Delta-method | Margin std. err. z P>|z| [95% conf. interval] -------------+---------------------------------------------------------------- foreign | Domestic | .2547349 .0670069 3.80 0.000 .1234038 .386066 Foreign | .8189145 .096934 8.45 0.000 .6289273 1.008902 ------------------------------------------------------------------------------ . . qui margins foreign, predict(xb) . transform_margins 100*invlogit(@) ---------------------------------------------- | b ll ul -------------+-------------------------------- foreign | Domestic | 24.694 14.12082 39.53907 Foreign | 82.70865 57.52516 94.41136 ----------------------------------------------
b) If this is possible, is there an easy way (such as with -marginsplot-) to plot the predicted proportions (or percentages) with confidence intervals after having transformed the margins?
Comment