Search code examples
statamlogitmarginal-effectscoefplot

Dropping variables from the coefplot of marginal effects (mlogit)


I want to create a coefficient plot by coefplot on the marginal effects after running a multinomial logistic regression in Stata. What I would like to get help with:

  • I would like to keep only one variable in the graph - i.cohort which has 6 categories and want other variables dropped from the plot.

  • In addition, I would like to keep all the categories of i.cohort including the base category in the plot if that is possible. So, as the attached plot shows currently I can only get 5 categories with the following codes.

  • For the categories of the outcome variable, I want to keep it as shown, that is, coefficient values for all four categories but with the horizontal indication of the confidence intervals (CIs).

This is my code and output so far:

mlogit edattain i.cohort3 both sex age agesqr i.ownershipd i.electric, base(1) nolog

margins, dydx(*) post

coefplot (, keep(*:1._predict) label(No Education)) ///
(, keep(*:2._predict) label(Primary))   ///
(, keep(*:3._predict) label(Secondary))  ///
(, keep(*:4._predict) label(Higher))  ///
, drop(both sex age agesqr 100.ownershipd 210.ownershipd 250.ownershipd 999.ownershipd ///
1.electric 2.electric _cons) swapnames xline(0) legend(rows(1))

enter image description here

Below you can find an example dataset:

* Example generated by -dataex-. To install: ssc install    dataex
clear
input int rep78 byte foreign int(price length weight)
. 0 6486 182 2520
2 0 4060 201 3330
4 0 5798 214 3700
1 0 4934 198 3470
3 0 5222 201 3210
3 0 4723 199 3200
. 0 4424 203 3420
2 0 4172 179 2690
5 1 9690 189 2830
3 1 6295 174 2070
4 1 9735 177 2650
4 1 6229 170 2370
5 1 4589 165 2020
4 1 5079 170 2280
4 1 8129 184 2750
3 1 4296 161 2130
end
label values foreign origin
label def origin 0 "Domestic", modify
label def origin 1 "Foreign", modify

In this case, I would like to keep both categories of i.foreign in the cofficient plot, instead of just 1 and 0 ommitted as the base outcome, along with all 5 outcome categories from rep78. I would like other predictors to be dropped.


Solution

  • The following works for me:

    sysuse auto, clear
    
    mlogit rep78 i.foreign price length weight, base(1) nolog
    margins, dydx(*) post
    
    coefplot (, keep(*.foreign:1._predict) label(rep78 1)) ///
    (, keep(*.foreign:2._predict) label(rep78 2))   ///
    (, keep(*.foreign:3._predict) label(rep78 3))  ///
    (, keep(*.foreign:4._predict) label(rep78 4))  ///
    (, keep(*.foreign:5._predict) label(rep78 5))  ///
    ,  omitted xline(0) legend(rows(1))
    

    enter image description here