Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using coefplot to plot odds ratios with p-values label after mi estimate, cmdok : melogit

    I would like to plot odds ratios of mixed-effects logistic models of the association of four sexual risk-taking behaviours (Sex after substance use "SexDrunkDrugs", infrequent condom use "SexLastUnsafeConsist", multiple sexual partnership "SexPartnerFreq2More", and inequitable sexual partnership "InequitSex") with mobile phone use for health content "XC" and mobile phone use for social media "XM" by gender (boys and girls). I also want to show the p-values labels on the graphs.

    I used and adapted Ben Jann coefplot at http://repec.sowi.unibe.ch/stata/coe...g-started.html for estimates from multiply imputed data. Here is the code:

    Code:
    local y     SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex
    local x1     XC XM
    local xsex    Rural AgeC HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j
    
    foreach ys of local y {
        forvalues f = 0/1 {
            mi estimate, saving(`ys'`f', replace) cmdok : melogit `ys' `x1' `xsex' if Sex == `f' || ID:, or cov(un) 
            estimate store `ys'`f'
        }
    }
    
    coefplot    (SexDrunkDrugs0, label("Boy") msymbol(S) mcolor(black) mfcolor(white))     (SexDrunkDrugs1, label("Girl") msymbol(O) mcolor(black) mfcolor(black))    , bylabel("Sex after substance use") mlabel("{it:p} = " + string(@pval,"%9.3f")) mlabpos(1)                                            ///
          ||    (SexLastUnsafeConsist0)            (SexLastUnsafeConsist1)            , bylabel("Infrequent condom use")                 ///
          ||    (SexPartnerFreq2More0)            (SexPartnerFreq2More1)            , bylabel("Multiple sexual partnership")        ///
          ||     (InequitSex0)                    (InequitSex1)                    , bylabel("Inequitable sexual partnership")        ///
          ||      , eform keep(XC XM) ciopts(lcolor(gs8) lwidth(thick)) xtitle("Adjusted odds ratios (ORs)") xlab(0(1)4,format(%3.0fc)) xline(1, lcolor(gs10)) ylab(1 `" "Health" "content" "use" "(Ref. no access)" "' 2 `" "Social" "media" "use" "(Ref. no access)" "') byopts(xrescale) subtitle(, fcolor(none) lstyle(none)) legend(order(2 "ORs for boys" 4 "ORs for girls" 3 "95% CI") row(1) ring(0) pos(10) region(lstyle(none)))
    My data looks like
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex) float(XC XM) byte(Sex Rural) double AgeC byte(HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j) long ID int _mi_id byte _mi_miss int _mi_m
    0 0 0 0 0 0 0 1  -.8304964539007091 1 1 1 0 1 0  10   1 0 0
    0 1 0 . 1 1 0 1  -.8304964539007091 1 1 1 0 1 1  10   2 1 0
    0 1 1 1 0 0 1 0  -.8304964539007091 0 1 1 0 1 0  20   3 0 0
    0 1 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 1  20   4 0 0
    0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0  50   5 0 0
    0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1  50   6 0 0
    . 0 0 0 1 1 1 0  .16950354609929086 0 0 1 0 1 0  60   7 1 0
    0 1 0 0 0 1 1 0  .16950354609929086 0 0 1 0 1 1  60   8 0 0
    0 0 0 0 0 0 0 1  -.8304964539007091 0 1 0 0 1 0  70   9 0 0
    0 0 0 0 1 1 0 1  -.8304964539007091 0 1 0 0 1 1  70  10 0 0
    0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 0  90  11 0 0
    0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 1  90  12 0 0
    0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 0 100  13 0 0
    0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 1 100  14 0 0
    0 0 0 0 0 1 0 0  -2.830496453900709 0 0 1 0 1 0 140  15 0 0
    0 0 0 0 0 0 0 0  -2.830496453900709 0 0 1 0 1 1 140  16 0 0
    0 0 0 0 0 0 1 0  -2.830496453900709 0 0 0 0 1 0 150  17 0 0
    0 0 0 0 0 1 1 0  -2.830496453900709 0 0 0 0 1 1 150  18 0 0
    . 0 0 0 1 1 0 0  -.8304964539007091 0 0 1 1 1 0 170  19 1 0
    1 1 1 . 0 1 0 0  -.8304964539007091 0 0 1 1 1 1 170  20 1 0
    0 0 0 0 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 200  21 0 0
    0 0 0 0 0 1 1 1  -.8304964539007091 0 0 1 0 1 1 200  22 0 0
    . 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 0 210  23 1 0
    0 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 1 210  24 0 0
    0 0 0 0 0 1 1 1 -1.8304964539007091 0 0 1 0 1 0 230  25 0 0
    0 0 0 0 0 1 1 1 -1.8304964539007091 0 0 1 0 1 1 230  26 0 0
    0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0 240  27 0 0
    0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1 240  28 0 0
    0 0 0 0 0 1 1 1  .16950354609929086 0 1 0 0 1 0 280  29 0 0
    0 0 0 0 0 0 1 1  .16950354609929086 0 1 0 0 1 1 280  30 0 0
    0 0 0 0 0 0 0 0 -1.8304964539007091 0 1 1 0 1 0 290  31 0 0
    0 0 0 0 0 0 0 0 -1.8304964539007091 0 1 1 0 1 1 290  32 0 0
    0 0 1 0 0 0 0 0   6.169503546099291 0 0 1 1 1 0 300  33 0 0
    0 0 1 0 0 0 0 0   6.169503546099291 0 0 1 1 1 1 300  34 0 0
    0 0 0 0 0 0 1 0   3.169503546099291 0 0 1 1 1 0 310  35 0 0
    0 0 0 0 0 0 1 0   3.169503546099291 0 0 1 1 1 1 310  36 0 0
    0 0 0 0 0 0 0 0  -.8304964539007091 0 1 0 0 1 0 340  37 0 0
    1 1 0 . 0 0 0 0  -.8304964539007091 0 1 0 0 1 1 340  38 1 0
    . 0 0 0 0 1 1 0   3.169503546099291 0 0 1 0 1 0 360  39 1 0
    0 0 0 0 0 1 1 0   3.169503546099291 0 0 1 0 1 1 360  40 0 0
    0 0 0 0 0 1 1 0  .16950354609929086 0 0 1 0 1 0 390  41 0 0
    0 0 0 0 0 0 1 0  .16950354609929086 0 0 1 0 1 1 390  42 0 0
    0 0 0 0 0 0 1 0 -1.8304964539007091 0 0 1 0 1 0 410  43 0 0
    0 0 0 0 0 1 1 0 -1.8304964539007091 0 0 1 0 1 1 410  44 0 0
    0 0 0 0 0 0 1 1  -3.830496453900709 0 0 0 0 1 0 440  45 0 0
    0 0 0 0 0 1 1 1  -3.830496453900709 0 0 0 0 1 1 440  46 0 0
    1 0 1 1 0 1 0 0  .16950354609929086 0 1 1 0 1 0 450  47 0 0
    . 1 0 . 0 1 0 0  .16950354609929086 0 1 1 0 1 1 450  48 1 0
    0 0 0 0 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 470  49 0 0
    0 0 0 0 0 0 1 1  -.8304964539007091 0 0 1 0 1 1 470  50 0 0
    0 0 0 0 0 0 0 1  -3.830496453900709 1 1 1 0 1 0 480  51 0 0
    0 0 0 0 0 1 0 1  -3.830496453900709 1 1 1 0 1 1 480  52 0 0
    0 0 0 0 0 1 1 0   3.169503546099291 0 0 0 0 1 0 490  53 0 0
    0 1 0 0 0 1 1 0   3.169503546099291 0 0 0 0 1 1 490  54 0 0
    0 0 0 0 0 1 1 0  -.8304964539007091 0 0 0 1 1 0 510  55 0 0
    0 0 0 0 0 1 1 0  -.8304964539007091 0 0 0 1 1 1 510  56 0 0
    0 0 1 0 0 1 1 0  1.1695035460992909 0 0 0 1 1 0 530  57 0 0
    0 0 1 1 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 530  58 0 0
    0 0 1 0 0 0 1 0  1.1695035460992909 0 0 1 0 1 0 540  59 0 0
    0 1 0 1 0 1 1 0  1.1695035460992909 0 0 1 0 1 1 540  60 0 0
    0 0 1 0 0 0 1 1   3.169503546099291 0 1 1 1 1 0 580  61 0 0
    0 1 1 1 0 1 1 1   3.169503546099291 0 1 1 1 1 1 580  62 0 0
    0 0 1 0 0 1 1 1   2.169503546099291 0 0 1 1 1 0 600  63 0 0
    0 0 0 0 0 1 1 1   2.169503546099291 0 0 1 1 1 1 600  64 0 0
    0 0 0 0 0 0 1 0  -.8304964539007091 0 0 1 0 1 0 620  65 0 0
    0 0 0 0 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 620  66 0 0
    0 1 1 0 0 0 1 0  .16950354609929086 0 0 0 1 1 0 650  67 0 0
    0 1 0 0 0 1 1 0  .16950354609929086 0 0 0 1 1 1 650  68 0 0
    1 1 1 0 0 0 1 0  -.8304964539007091 0 0 0 0 1 0 680  69 0 0
    0 0 0 1 0 0 1 0  -.8304964539007091 0 0 0 0 1 1 680  70 0 0
    0 0 0 0 0 0 0 0  -3.830496453900709 0 1 1 0 1 0 690  71 0 0
    . 0 0 . 0 0 0 0  -3.830496453900709 0 1 1 0 1 1 690  72 1 0
    0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 0 700  73 0 0
    0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 1 700  74 0 0
    0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 0 710  75 0 0
    0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 1 710  76 0 0
    0 0 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 0 750  77 0 0
    0 0 0 0 0 1 1 0  -.8304964539007091 0 1 1 0 1 1 750  78 0 0
    0 0 0 0 1 1 1 1   2.169503546099291 0 0 1 1 1 0 780  79 0 0
    0 1 0 0 0 1 1 1   2.169503546099291 0 0 1 1 1 1 780  80 0 0
    . 0 0 0 0 1 0 0  1.1695035460992909 0 0 0 1 1 0 790  81 1 0
    0 1 0 0 0 1 0 0  1.1695035460992909 0 0 0 1 1 1 790  82 0 0
    0 0 0 0 0 1 1 0  -.8304964539007091 0 0 1 0 1 0 800  83 0 0
    0 0 0 0 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 800  84 0 0
    . 0 0 0 0 0 1 0  1.1695035460992909 0 0 0 1 1 0 810  85 1 0
    0 0 0 0 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 810  86 0 0
    . 0 0 0 0 1 1 1  .16950354609929086 0 1 0 0 1 0 830  87 1 0
    0 1 0 0 1 1 1 1  .16950354609929086 0 1 0 0 1 1 830  88 0 0
    0 0 0 0 0 0 1 0 -1.8304964539007091 0 1 0 0 1 0 850  89 0 0
    0 1 0 . 1 1 1 0 -1.8304964539007091 0 1 0 0 1 1 850  90 1 0
    0 0 0 0 0 0 1 1  -2.830496453900709 0 1 0 0 1 0 870  91 0 0
    0 0 0 0 0 0 1 1  -2.830496453900709 0 1 0 0 1 1 870  92 0 0
    0 0 0 0 1 1 1 0  1.1695035460992909 0 0 0 0 1 0 930  93 0 0
    0 0 0 0 0 0 1 0  1.1695035460992909 0 0 0 0 1 1 930  94 0 0
    0 0 0 0 0 0 1 0  -3.830496453900709 0 0 1 0 1 0 940  95 0 0
    0 0 0 0 0 0 1 0  -3.830496453900709 0 0 1 0 1 1 940  96 0 0
    . 0 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 0 950  97 1 0
    0 1 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 1 950  98 0 0
    0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 0 970  99 0 0
    0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 1 970 100 0 0
    end
    label values SexLastUnsafeConsist yes_no
    label values SexPartnerFreq2More yes_no
    label def yes_no 0 "0. No", modify
    label def yes_no 1 "1. Yes", modify
    label values Sex Sex
    label def Sex 0 "Male", modify
    label def Sex 1 "Female", modify
    label values Rural UrbRur
    label def UrbRur 0 "0. Urban", modify
    label def UrbRur 1 "1. Rural", modify
    label values ARTever binary
    label values HomeTypeInformal binary
    label values Relationship binary
    label values SchEnrol binary
    label def binary 0 "0. No", modify
    label def binary 1 "1. Yes", modify
    label values NecessitiesAll_r binary_r
    label def binary_r 0 "0. Yes", modify
    label def binary_r 1 "1. No", modify
    I got a figure like the one I attached to this post Graph.gph .

    Please, is there any reason why the p-value for health content use for infrequent condom use is missing.

    I tried the same coding for non-imputed dataset and everything works.

    I would appreciate any help from you.

    Best,
    Boladé

  • #2
    Perhaps you need the -post- option of mi estimate. It's not possible to replicate the issue without the imputation codes.

    Code:
    mi estimate, post saving(`ys'`f', replace) cmdok : melogit ...

    Comment


    • #3
      Thank you Andrew. Sorry I thought the imputed data would be enough. The imputation code is
      Code:
      * Missingness
      forvalues i = 2/3 {
          mdesc `SRH_4' Use Rural AgeC Sex ARTever Relationship SchEnrol NecessitiesAll_r HomeTypeInformal if Wave == `i'
          misstable patterns `SRH_4' Use Rural AgeC Sex ARTever Relationship SchEnrol NecessitiesAll_r HomeTypeInformal if Wave == `i'
      }
      
      * The amount of missing data for the key variables at T2 and T3 (for included participants) were, respectively: 13.5% and 6.5% for sex after substance use; 3.8% and 8.6% for inequitable sexual partnership. There is no missing data on mobile phone access and use (neither at T2, nor at T3). The amount of missing data for covariates (for included participants) at baseline was 0.1% for household poverty informal housing and 0% for sex, age, rural residence, relationship status, HIV status and school enrolment
      /*
      Missing-value patterns
           (1 means complete)
      
      * at Wave 2
                    |   Pattern
          Percent   |  1  2  3  4
        ------------+-------------
             85%    |  1  1  1  1
                    |
             11     |  1  1  1  0
              3     |  1  1  0  0
              1     |  1  1  0  1
             <1     |  0  1  1  1
             <1     |  1  0  1  1
        ------------+-------------
            100%    |
      
       Variables are  (1) HomeTypeInformal  (2) NecessitiesAll_r  (3) InequitSex  (4) SexDrunkDrugs
      
      Interpretation: At T2, the dataset contains missing values four variables including household poverty, informal housing, inequitable sexual partnership, and sex after substance use. 85% of data are complete while 15% of the observations contain missing values.
      
      * at Wave 3
                    |   Pattern
          Percent   |  1  2  3  4
        ------------+-------------
             91%    |  1  1  1  1
                    |
              7     |  1  1  0  0
              2     |  1  1  1  0
             <1     |  0  1  1  1
             <1     |  1  0  1  1
        ------------+-------------
            100%    |
      
      Variables are  (1) HomeTypeInformal  (2) NecessitiesAll_r  (3) SexDrunkDrugs  (4) InequitSex
      
      Interpretation: At T3, the dataset contains missing values four variables including household poverty, informal housing, sex after substance use, and inequitable sexual partnership. 91% of data are complete while 9% of the observations contain missing values.
      */
      
      * Now we can determine whether the data are MCAR using the mcartest command.
      
      forvalues i = 2/3 {
          mcartest SexDrunkDrugs InequitSex = i.HomeTypeInformal i.NecessitiesAll_r i.XC i.XM i.Rural AgeC i.Sex i.ARTever i.SchEnrol i.Relationship if Wave == `i', emoutput nolog
      }
      
      * Little's χ² test indicated that data were not missing completely at random (appendix x2)
      
      * Missing data are not MCAR
      
      mi set mlong    // mi set data for imputation
      
      * Reshape data to wide (it makes it easly to use variables from T2 as predictors of missing values)
      
      * Firt drop variables not needed in analysis
      drop SexIntercourseEverProxy q1* XCXM SRH_count HIVRiskAny HIVRisk2Form HIVRiskAll Wave wgt touse-ibInequitSex
      
      mi reshape wide SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
      
      * Identifying which variables in the imputation model have missing information.
      mi register imputed NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0 SexDrunkDrugs1 InequitSex0 InequitSex1
      
      *** Mutiple imputation
      *MI using chained equations/MICE (also known as the fully conditional specification or sequential generalized regression) for binary outcomes.  In simulation studies (Lee & Carlin, 2010; Van Buuren, 2007), MICE has been show to produce estimates that are comparable to MVN method.
      
      * Clear any xtset that is ongoing
      mi xtset, clear
      mi /*mimpt*/ impute chained     (logit) NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0     ///
                          SexDrunkDrugs1 InequitSex0 InequitSex1 = Access0 Access1     ///
                          XC0 XM0 XC1 XM1 Sex ARTever AgeC Rural Relationship         ///
                          SchEnrol, add(10) rseed(20210101) /*skipnonconvergence(5)*/ savetrace(tracedata, replace)
      
      * Bring data back to long format
      mi reshape long SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
      
      * We can check the data and see if there is no problems in our imputation
      summ SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex HomeTypeInformal NecessitiesAll_r
      And the data before imputation is

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More) float(InequitSex XC XM) byte(Sex Rural) double AgeC byte(HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j) long ID
      0 0 0 0 0 0 0 1  -.8304964539007091 1 1 1 0 1 0  10
      0 1 0 . 1 1 0 1  -.8304964539007091 1 1 1 0 1 1  10
      0 1 1 1 0 0 1 0  -.8304964539007091 0 1 1 0 1 0  20
      0 1 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 1  20
      0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0  50
      0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1  50
      . 0 0 0 1 1 1 0  .16950354609929086 0 0 1 0 1 0  60
      0 1 0 0 0 1 1 0  .16950354609929086 0 0 1 0 1 1  60
      0 0 0 0 0 0 0 1  -.8304964539007091 0 1 0 0 1 0  70
      0 0 0 0 1 1 0 1  -.8304964539007091 0 1 0 0 1 1  70
      0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 0  90
      0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 1  90
      0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 0 100
      0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 1 100
      0 0 0 0 0 1 0 0  -2.830496453900709 0 0 1 0 1 0 140
      0 0 0 0 0 0 0 0  -2.830496453900709 0 0 1 0 1 1 140
      0 0 0 0 0 0 1 0  -2.830496453900709 0 0 0 0 1 0 150
      0 0 0 0 0 1 1 0  -2.830496453900709 0 0 0 0 1 1 150
      . 0 0 0 1 1 0 0  -.8304964539007091 0 0 1 1 1 0 170
      1 1 1 . 0 1 0 0  -.8304964539007091 0 0 1 1 1 1 170
      0 0 0 0 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 200
      0 0 0 0 0 1 1 1  -.8304964539007091 0 0 1 0 1 1 200
      . 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 0 210
      0 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 1 210
      0 0 0 0 0 1 1 1 -1.8304964539007091 0 0 1 0 1 0 230
      0 0 0 0 0 1 1 1 -1.8304964539007091 0 0 1 0 1 1 230
      0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0 240
      0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1 240
      0 0 0 0 0 1 1 1  .16950354609929086 0 1 0 0 1 0 280
      0 0 0 0 0 0 1 1  .16950354609929086 0 1 0 0 1 1 280
      0 0 0 0 0 0 0 0 -1.8304964539007091 0 1 1 0 1 0 290
      0 0 0 0 0 0 0 0 -1.8304964539007091 0 1 1 0 1 1 290
      0 0 1 0 0 0 0 0   6.169503546099291 0 0 1 1 1 0 300
      0 0 1 0 0 0 0 0   6.169503546099291 0 0 1 1 1 1 300
      0 0 0 0 0 0 1 0   3.169503546099291 0 0 1 1 1 0 310
      0 0 0 0 0 0 1 0   3.169503546099291 0 0 1 1 1 1 310
      0 0 0 0 0 0 0 0  -.8304964539007091 0 1 0 0 1 0 340
      1 1 0 . 0 0 0 0  -.8304964539007091 0 1 0 0 1 1 340
      . 0 0 0 0 1 1 0   3.169503546099291 0 0 1 0 1 0 360
      0 0 0 0 0 1 1 0   3.169503546099291 0 0 1 0 1 1 360
      0 0 0 0 0 1 1 0  .16950354609929086 0 0 1 0 1 0 390
      0 0 0 0 0 0 1 0  .16950354609929086 0 0 1 0 1 1 390
      0 0 0 0 0 0 1 0 -1.8304964539007091 0 0 1 0 1 0 410
      0 0 0 0 0 1 1 0 -1.8304964539007091 0 0 1 0 1 1 410
      0 0 0 0 0 0 1 1  -3.830496453900709 0 0 0 0 1 0 440
      0 0 0 0 0 1 1 1  -3.830496453900709 0 0 0 0 1 1 440
      1 0 1 1 0 1 0 0  .16950354609929086 0 1 1 0 1 0 450
      . 1 0 . 0 1 0 0  .16950354609929086 0 1 1 0 1 1 450
      0 0 0 0 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 470
      0 0 0 0 0 0 1 1  -.8304964539007091 0 0 1 0 1 1 470
      0 0 0 0 0 0 0 1  -3.830496453900709 1 1 1 0 1 0 480
      0 0 0 0 0 1 0 1  -3.830496453900709 1 1 1 0 1 1 480
      0 0 0 0 0 1 1 0   3.169503546099291 0 0 0 0 1 0 490
      0 1 0 0 0 1 1 0   3.169503546099291 0 0 0 0 1 1 490
      0 0 0 0 0 1 1 0  -.8304964539007091 0 0 0 1 1 0 510
      0 0 0 0 0 1 1 0  -.8304964539007091 0 0 0 1 1 1 510
      0 0 1 0 0 1 1 0  1.1695035460992909 0 0 0 1 1 0 530
      0 0 1 1 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 530
      0 0 1 0 0 0 1 0  1.1695035460992909 0 0 1 0 1 0 540
      0 1 0 1 0 1 1 0  1.1695035460992909 0 0 1 0 1 1 540
      0 0 1 0 0 0 1 1   3.169503546099291 0 1 1 1 1 0 580
      0 1 1 1 0 1 1 1   3.169503546099291 0 1 1 1 1 1 580
      0 0 1 0 0 1 1 1   2.169503546099291 0 0 1 1 1 0 600
      0 0 0 0 0 1 1 1   2.169503546099291 0 0 1 1 1 1 600
      0 0 0 0 0 0 1 0  -.8304964539007091 0 0 1 0 1 0 620
      0 0 0 0 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 620
      0 1 1 0 0 0 1 0  .16950354609929086 0 0 0 1 1 0 650
      0 1 0 0 0 1 1 0  .16950354609929086 0 0 0 1 1 1 650
      1 1 1 0 0 0 1 0  -.8304964539007091 0 0 0 0 1 0 680
      0 0 0 1 0 0 1 0  -.8304964539007091 0 0 0 0 1 1 680
      0 0 0 0 0 0 0 0  -3.830496453900709 0 1 1 0 1 0 690
      . 0 0 . 0 0 0 0  -3.830496453900709 0 1 1 0 1 1 690
      0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 0 700
      0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 1 700
      0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 0 710
      0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 1 710
      0 0 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 0 750
      0 0 0 0 0 1 1 0  -.8304964539007091 0 1 1 0 1 1 750
      0 0 0 0 1 1 1 1   2.169503546099291 0 0 1 1 1 0 780
      0 1 0 0 0 1 1 1   2.169503546099291 0 0 1 1 1 1 780
      . 0 0 0 0 1 0 0  1.1695035460992909 0 0 0 1 1 0 790
      0 1 0 0 0 1 0 0  1.1695035460992909 0 0 0 1 1 1 790
      0 0 0 0 0 1 1 0  -.8304964539007091 0 0 1 0 1 0 800
      0 0 0 0 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 800
      . 0 0 0 0 0 1 0  1.1695035460992909 0 0 0 1 1 0 810
      0 0 0 0 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 810
      . 0 0 0 0 1 1 1  .16950354609929086 0 1 0 0 1 0 830
      0 1 0 0 1 1 1 1  .16950354609929086 0 1 0 0 1 1 830
      0 0 0 0 0 0 1 0 -1.8304964539007091 0 1 0 0 1 0 850
      0 1 0 . 1 1 1 0 -1.8304964539007091 0 1 0 0 1 1 850
      0 0 0 0 0 0 1 1  -2.830496453900709 0 1 0 0 1 0 870
      0 0 0 0 0 0 1 1  -2.830496453900709 0 1 0 0 1 1 870
      0 0 0 0 1 1 1 0  1.1695035460992909 0 0 0 0 1 0 930
      0 0 0 0 0 0 1 0  1.1695035460992909 0 0 0 0 1 1 930
      0 0 0 0 0 0 1 0  -3.830496453900709 0 0 1 0 1 0 940
      0 0 0 0 0 0 1 0  -3.830496453900709 0 0 1 0 1 1 940
      . 0 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 0 950
      0 1 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 1 950
      0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 0 970
      0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 1 970
      end
      label values SexLastUnsafeConsist yes_no
      label values SexPartnerFreq2More yes_no
      label def yes_no 0 "0. No", modify
      label def yes_no 1 "1. Yes", modify
      label values Sex Sex
      label def Sex 0 "Male", modify
      label def Sex 1 "Female", modify
      label values Rural UrbRur
      label def UrbRur 0 "0. Urban", modify
      label def UrbRur 1 "1. Rural", modify
      label values ARTever binary
      label values HomeTypeInformal binary
      label values Relationship binary
      label values SchEnrol binary
      label def binary 0 "0. No", modify
      label def binary 1 "1. Yes", modify
      label values NecessitiesAll_r binary_r
      label def binary_r 0 "0. Yes", modify
      label def binary_r 1 "1. No", modify
      I hope this helps.

      Best,
      Boladé

      Comment


      • #4
        Hi again Andrew, I try to add the post option and still get the same output.

        Comment


        • #5
          When I run the following

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input byte(SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More) float(InequitSex XC XM) byte(Sex Rural) double AgeC byte(HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j) long ID
          0 0 0 0 0 0 0 1  -.8304964539007091 1 1 1 0 1 0  10
          0 1 0 . 1 1 0 1  -.8304964539007091 1 1 1 0 1 1  10
          0 1 1 1 0 0 1 0  -.8304964539007091 0 1 1 0 1 0  20
          0 1 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 1  20
          0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0  50
          0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1  50
          . 0 0 0 1 1 1 0  .16950354609929086 0 0 1 0 1 0  60
          0 1 0 0 0 1 1 0  .16950354609929086 0 0 1 0 1 1  60
          0 0 0 0 0 0 0 1  -.8304964539007091 0 1 0 0 1 0  70
          0 0 0 0 1 1 0 1  -.8304964539007091 0 1 0 0 1 1  70
          0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 0  90
          0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 1  90
          0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 0 100
          0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 1 100
          0 0 0 0 0 1 0 0  -2.830496453900709 0 0 1 0 1 0 140
          0 0 0 0 0 0 0 0  -2.830496453900709 0 0 1 0 1 1 140
          0 0 0 0 0 0 1 0  -2.830496453900709 0 0 0 0 1 0 150
          0 0 0 0 0 1 1 0  -2.830496453900709 0 0 0 0 1 1 150
          . 0 0 0 1 1 0 0  -.8304964539007091 0 0 1 1 1 0 170
          1 1 1 . 0 1 0 0  -.8304964539007091 0 0 1 1 1 1 170
          0 0 0 0 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 200
          0 0 0 0 0 1 1 1  -.8304964539007091 0 0 1 0 1 1 200
          . 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 0 210
          0 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 1 210
          0 0 0 0 0 1 1 1 -1.8304964539007091 0 0 1 0 1 0 230
          0 0 0 0 0 1 1 1 -1.8304964539007091 0 0 1 0 1 1 230
          0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0 240
          0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1 240
          0 0 0 0 0 1 1 1  .16950354609929086 0 1 0 0 1 0 280
          0 0 0 0 0 0 1 1  .16950354609929086 0 1 0 0 1 1 280
          0 0 0 0 0 0 0 0 -1.8304964539007091 0 1 1 0 1 0 290
          0 0 0 0 0 0 0 0 -1.8304964539007091 0 1 1 0 1 1 290
          0 0 1 0 0 0 0 0   6.169503546099291 0 0 1 1 1 0 300
          0 0 1 0 0 0 0 0   6.169503546099291 0 0 1 1 1 1 300
          0 0 0 0 0 0 1 0   3.169503546099291 0 0 1 1 1 0 310
          0 0 0 0 0 0 1 0   3.169503546099291 0 0 1 1 1 1 310
          0 0 0 0 0 0 0 0  -.8304964539007091 0 1 0 0 1 0 340
          1 1 0 . 0 0 0 0  -.8304964539007091 0 1 0 0 1 1 340
          . 0 0 0 0 1 1 0   3.169503546099291 0 0 1 0 1 0 360
          0 0 0 0 0 1 1 0   3.169503546099291 0 0 1 0 1 1 360
          0 0 0 0 0 1 1 0  .16950354609929086 0 0 1 0 1 0 390
          0 0 0 0 0 0 1 0  .16950354609929086 0 0 1 0 1 1 390
          0 0 0 0 0 0 1 0 -1.8304964539007091 0 0 1 0 1 0 410
          0 0 0 0 0 1 1 0 -1.8304964539007091 0 0 1 0 1 1 410
          0 0 0 0 0 0 1 1  -3.830496453900709 0 0 0 0 1 0 440
          0 0 0 0 0 1 1 1  -3.830496453900709 0 0 0 0 1 1 440
          1 0 1 1 0 1 0 0  .16950354609929086 0 1 1 0 1 0 450
          . 1 0 . 0 1 0 0  .16950354609929086 0 1 1 0 1 1 450
          0 0 0 0 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 470
          0 0 0 0 0 0 1 1  -.8304964539007091 0 0 1 0 1 1 470
          0 0 0 0 0 0 0 1  -3.830496453900709 1 1 1 0 1 0 480
          0 0 0 0 0 1 0 1  -3.830496453900709 1 1 1 0 1 1 480
          0 0 0 0 0 1 1 0   3.169503546099291 0 0 0 0 1 0 490
          0 1 0 0 0 1 1 0   3.169503546099291 0 0 0 0 1 1 490
          0 0 0 0 0 1 1 0  -.8304964539007091 0 0 0 1 1 0 510
          0 0 0 0 0 1 1 0  -.8304964539007091 0 0 0 1 1 1 510
          0 0 1 0 0 1 1 0  1.1695035460992909 0 0 0 1 1 0 530
          0 0 1 1 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 530
          0 0 1 0 0 0 1 0  1.1695035460992909 0 0 1 0 1 0 540
          0 1 0 1 0 1 1 0  1.1695035460992909 0 0 1 0 1 1 540
          0 0 1 0 0 0 1 1   3.169503546099291 0 1 1 1 1 0 580
          0 1 1 1 0 1 1 1   3.169503546099291 0 1 1 1 1 1 580
          0 0 1 0 0 1 1 1   2.169503546099291 0 0 1 1 1 0 600
          0 0 0 0 0 1 1 1   2.169503546099291 0 0 1 1 1 1 600
          0 0 0 0 0 0 1 0  -.8304964539007091 0 0 1 0 1 0 620
          0 0 0 0 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 620
          0 1 1 0 0 0 1 0  .16950354609929086 0 0 0 1 1 0 650
          0 1 0 0 0 1 1 0  .16950354609929086 0 0 0 1 1 1 650
          1 1 1 0 0 0 1 0  -.8304964539007091 0 0 0 0 1 0 680
          0 0 0 1 0 0 1 0  -.8304964539007091 0 0 0 0 1 1 680
          0 0 0 0 0 0 0 0  -3.830496453900709 0 1 1 0 1 0 690
          . 0 0 . 0 0 0 0  -3.830496453900709 0 1 1 0 1 1 690
          0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 0 700
          0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 1 700
          0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 0 710
          0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 1 710
          0 0 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 0 750
          0 0 0 0 0 1 1 0  -.8304964539007091 0 1 1 0 1 1 750
          0 0 0 0 1 1 1 1   2.169503546099291 0 0 1 1 1 0 780
          0 1 0 0 0 1 1 1   2.169503546099291 0 0 1 1 1 1 780
          . 0 0 0 0 1 0 0  1.1695035460992909 0 0 0 1 1 0 790
          0 1 0 0 0 1 0 0  1.1695035460992909 0 0 0 1 1 1 790
          0 0 0 0 0 1 1 0  -.8304964539007091 0 0 1 0 1 0 800
          0 0 0 0 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 800
          . 0 0 0 0 0 1 0  1.1695035460992909 0 0 0 1 1 0 810
          0 0 0 0 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 810
          . 0 0 0 0 1 1 1  .16950354609929086 0 1 0 0 1 0 830
          0 1 0 0 1 1 1 1  .16950354609929086 0 1 0 0 1 1 830
          0 0 0 0 0 0 1 0 -1.8304964539007091 0 1 0 0 1 0 850
          0 1 0 . 1 1 1 0 -1.8304964539007091 0 1 0 0 1 1 850
          0 0 0 0 0 0 1 1  -2.830496453900709 0 1 0 0 1 0 870
          0 0 0 0 0 0 1 1  -2.830496453900709 0 1 0 0 1 1 870
          0 0 0 0 1 1 1 0  1.1695035460992909 0 0 0 0 1 0 930
          0 0 0 0 0 0 1 0  1.1695035460992909 0 0 0 0 1 1 930
          0 0 0 0 0 0 1 0  -3.830496453900709 0 0 1 0 1 0 940
          0 0 0 0 0 0 1 0  -3.830496453900709 0 0 1 0 1 1 940
          . 0 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 0 950
          0 1 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 1 950
          0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 0 970
          0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 1 970
          end
          label values SexLastUnsafeConsist yes_no
          label values SexPartnerFreq2More yes_no
          label def yes_no 0 "0. No", modify
          label def yes_no 1 "1. Yes", modify
          label values Sex Sex
          label def Sex 0 "Male", modify
          label def Sex 1 "Female", modify
          label values Rural UrbRur
          label def UrbRur 0 "0. Urban", modify
          label def UrbRur 1 "1. Rural", modify
          label values ARTever binary
          label values HomeTypeInformal binary
          label values Relationship binary
          label values SchEnrol binary
          label def binary 0 "0. No", modify
          label def binary 1 "1. Yes", modify
          label values NecessitiesAll_r binary_r
          label def binary_r 0 "0. Yes", modify
          label def binary_r 1 "1. No", modify
          
          mi set mlong    // mi set data for imputation
          
          * Reshape data to wide (it makes it easly to use variables from T2 as predictors of missing values)
          
          * Firt drop variables not needed in analysis
          drop SexIntercourseEverProxy q1* XCXM SRH_count HIVRiskAny HIVRisk2Form HIVRiskAll Wave wgt touse-ibInequitSex
          
          mi reshape wide SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
          
          * Identifying which variables in the imputation model have missing information.
          mi register imputed NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0 SexDrunkDrugs1 InequitSex0 InequitSex1
          
          *** Mutiple imputation
          *MI using chained equations/MICE (also known as the fully conditional specification or sequential generalized regression) for binary outcomes.  In simulation studies (Lee & Carlin, 2010; Van Buuren, 2007), MICE has been show to produce estimates that are comparable to MVN method.
          
          * Clear any xtset that is ongoing
          mi xtset, clear
          mi /*mimpt*/ impute chained     (logit) NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0     ///
                              SexDrunkDrugs1 InequitSex0 InequitSex1 = Access0 Access1     ///
                              XC0 XM0 XC1 XM1 Sex ARTever AgeC Rural Relationship         ///
                              SchEnrol, add(10) rseed(20210101) /*skipnonconvergence(5)*/ savetrace(tracedata, replace)
          
          * Bring data back to long format
          mi reshape long SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
          
          * We can check the data and see if there is no problems in our imputation
          summ SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex HomeTypeInformal NecessitiesAll_r
          
          local y     SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex
          local x1     XC XM
          local xsex    Rural AgeC HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j
          
          foreach ys of local y {
              forvalues f = 0/1 {
                  mi estimate, saving(`ys'`f', replace) cmdok : melogit `ys' `x1' `xsex' if Sex == `f' || ID:, or cov(un) 
                  estimate store `ys'`f'
              }
          }
          
          coefplot    (SexDrunkDrugs0, label("Boy") msymbol(S) mcolor(black) mfcolor(white))     (SexDrunkDrugs1, label("Girl") msymbol(O) mcolor(black) mfcolor(black))    , bylabel("Sex after substance use") mlabel("{it:p} = " + string(@pval,"%9.3f")) mlabpos(1)                                            ///
                ||    (SexLastUnsafeConsist0)            (SexLastUnsafeConsist1)            , bylabel("Infrequent condom use")                 ///
                ||    (SexPartnerFreq2More0)            (SexPartnerFreq2More1)            , bylabel("Multiple sexual partnership")        ///
                ||     (InequitSex0)                    (InequitSex1)                    , bylabel("Inequitable sexual partnership")        ///
                ||      , eform keep(XC XM) ciopts(lcolor(gs8) lwidth(thick)) xtitle("Adjusted odds ratios (ORs)") xlab(0(1)4,format(%3.0fc)) xline(1, lcolor(gs10)) ylab(1 `" "Health" "content" "use" "(Ref. no access)" "' 2 `" "Social" "media" "use" "(Ref. no access)" "') byopts(xrescale) subtitle(, fcolor(none) lstyle(none)) legend(order(2 "ORs for boys" 4 "ORs for girls" 3 "95% CI") row(1) ring(0) pos(10) region(lstyle(none)))
          I get

          Code:
           
          . * Firt drop variables not needed in analysis
          . drop SexIntercourseEverProxy q1* XCXM SRH_count HIVRiskAny HIVRisk2Form HIVRiskAll Wave wgt touse-ibInequitSex
          variable SexIntercourseEverProxy not found
          r(111);
          
          end of do-file
          Make sure that the problem is reproducible, i.e., the code runs without any errors and replicates the issue. Then we can take it from there.

          Comment


          • #6
            Thank you Andrew for your reply. The imputation code is:
            Code:
            * Little's χ² test indicated that data were not missing completely at random (appendix x2)  
              * Missing data are not MCAR
               
              mi set mlong // mi set data for imputation
               
              mi reshape wide SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
               
              * Identifying which variables in the imputation model have missing information.
              mi register imputed NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0 SexDrunkDrugs1 InequitSex0 InequitSex1
               
              *** Mutiple imputation
              *MI using chained equations/MICE (also known as the fully conditional specification or sequential generalized regression) for binary outcomes.  In simulation studies (Lee & Carlin, 2010; Van Buuren, 2007), MICE has been show to produce estimates that are comparable to MVN method.
               
              * Clear any xtset that is ongoing
              mi xtset, clear
              mi /*mimpt*/ impute chained  (logit) NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0  ///
              SexDrunkDrugs1 InequitSex0 InequitSex1 = Access0 Access1  ///
              XC0 XM0 XC1 XM1 Sex ARTever AgeC Rural Relationship  ///
              SchEnrol, add(10) rseed(20210101) /*skipnonconvergence(5)*/ savetrace(tracedata, replace)
               
              * Bring data back to long format
              mi reshape long SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
               
              * We can check the data and see if there is no problems in our imputation
             summ SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex HomeTypeInformal NecessitiesAll_r
            on the following data:

            Code:
             * Example generated by -dataex-. For more info, type help dataex
              clear
              input byte(SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More) float(InequitSex Use Access XC XM) byte(Sex Rural) double AgeC byte(HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j) long ID
              0 0 0 0 0 0 0 0 0 1  -.8304964539007091 1 1 1 0 1 0  10
              0 1 0 . 2 1 1 1 0 1  -.8304964539007091 1 1 1 0 1 1  10
              0 1 1 1 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 0  20
              0 1 0 0 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 1  20
              0 0 0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0  50
              0 0 0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1  50
              . 0 0 0 2 1 1 1 1 0  .16950354609929086 0 0 1 0 1 0  60
              0 1 0 0 1 1 0 1 1 0  .16950354609929086 0 0 1 0 1 1  60
              0 0 0 0 0 0 0 0 0 1  -.8304964539007091 0 1 0 0 1 0  70
              0 0 0 0 2 1 1 1 0 1  -.8304964539007091 0 1 0 0 1 1  70
              0 0 0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 0  90
              0 0 0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 1  90
              0 0 0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 0 100
              0 0 0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 1 100
              0 0 0 0 1 1 0 1 0 0  -2.830496453900709 0 0 1 0 1 0 140
              0 0 0 0 0 0 0 0 0 0  -2.830496453900709 0 0 1 0 1 1 140
              0 0 0 0 0 0 0 0 1 0  -2.830496453900709 0 0 0 0 1 0 150
              0 0 0 0 1 1 0 1 1 0  -2.830496453900709 0 0 0 0 1 1 150
              . 0 0 0 2 1 1 1 0 0  -.8304964539007091 0 0 1 1 1 0 170
              1 1 1 . 1 1 0 1 0 0  -.8304964539007091 0 0 1 1 1 1 170
              0 0 0 0 1 1 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 200
              0 0 0 0 1 1 0 1 1 1  -.8304964539007091 0 0 1 0 1 1 200
              . 0 0 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 0 210
              0 0 0 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 1 210
              0 0 0 0 1 1 0 1 1 1 -1.8304964539007091 0 0 1 0 1 0 230
              0 0 0 0 1 1 0 1 1 1 -1.8304964539007091 0 0 1 0 1 1 230
              0 0 0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0 240
              0 0 0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1 240
              0 0 0 0 1 1 0 1 1 1  .16950354609929086 0 1 0 0 1 0 280
              0 0 0 0 0 0 0 0 1 1  .16950354609929086 0 1 0 0 1 1 280
              0 0 0 0 0 0 0 0 0 0 -1.8304964539007091 0 1 1 0 1 0 290
              0 0 0 0 1 1 0 0 0 0 -1.8304964539007091 0 1 1 0 1 1 290
              0 0 1 0 0 0 0 0 0 0   6.169503546099291 0 0 1 1 1 0 300
              0 0 1 0 0 0 0 0 0 0   6.169503546099291 0 0 1 1 1 1 300
              0 0 0 0 0 0 0 0 1 0   3.169503546099291 0 0 1 1 1 0 310
              0 0 0 0 1 1 0 0 1 0   3.169503546099291 0 0 1 1 1 1 310
              0 0 0 0 0 0 0 0 0 0  -.8304964539007091 0 1 0 0 1 0 340
              1 1 0 . 0 0 0 0 0 0  -.8304964539007091 0 1 0 0 1 1 340
              . 0 0 0 1 1 0 1 1 0   3.169503546099291 0 0 1 0 1 0 360
              0 0 0 0 1 1 0 1 1 0   3.169503546099291 0 0 1 0 1 1 360
              0 0 0 0 1 1 0 1 1 0  .16950354609929086 0 0 1 0 1 0 390
              0 0 0 0 1 1 0 0 1 0  .16950354609929086 0 0 1 0 1 1 390
              0 0 0 0 1 1 0 0 1 0 -1.8304964539007091 0 0 1 0 1 0 410
              0 0 0 0 1 1 0 1 1 0 -1.8304964539007091 0 0 1 0 1 1 410
              0 0 0 0 1 1 0 0 1 1  -3.830496453900709 0 0 0 0 1 0 440
              0 0 0 0 1 1 0 1 1 1  -3.830496453900709 0 0 0 0 1 1 440
              1 0 1 1 1 1 0 1 0 0  .16950354609929086 0 1 1 0 1 0 450
              . 1 0 . 1 1 0 1 0 0  .16950354609929086 0 1 1 0 1 1 450
              0 0 0 0 1 1 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 470
              0 0 0 0 1 1 0 0 1 1  -.8304964539007091 0 0 1 0 1 1 470
              0 0 0 0 0 0 0 0 0 1  -3.830496453900709 1 1 1 0 1 0 480
              0 0 0 0 1 1 0 1 0 1  -3.830496453900709 1 1 1 0 1 1 480
              0 0 0 0 1 1 0 1 1 0   3.169503546099291 0 0 0 0 1 0 490
              0 1 0 0 1 1 0 1 1 0   3.169503546099291 0 0 0 0 1 1 490
              0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 0 1 1 0 510
              0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 0 1 1 1 510
              0 0 1 0 1 1 0 1 1 0  1.1695035460992909 0 0 0 1 1 0 530
              0 0 1 1 1 1 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 530
              0 0 1 0 0 0 0 0 1 0  1.1695035460992909 0 0 1 0 1 0 540
              0 1 0 1 1 1 0 1 1 0  1.1695035460992909 0 0 1 0 1 1 540
              0 0 1 0 0 0 0 0 1 1   3.169503546099291 0 1 1 1 1 0 580
              0 1 1 1 1 1 0 1 1 1   3.169503546099291 0 1 1 1 1 1 580
              0 0 1 0 1 1 0 1 1 1   2.169503546099291 0 0 1 1 1 0 600
              0 0 0 0 1 1 0 1 1 1   2.169503546099291 0 0 1 1 1 1 600
              0 0 0 0 0 0 0 0 1 0  -.8304964539007091 0 0 1 0 1 0 620
              0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 620
              0 1 1 0 0 0 0 0 1 0  .16950354609929086 0 0 0 1 1 0 650
              0 1 0 0 1 1 0 1 1 0  .16950354609929086 0 0 0 1 1 1 650
              1 1 1 0 0 0 0 0 1 0  -.8304964539007091 0 0 0 0 1 0 680
              0 0 0 1 1 1 0 0 1 0  -.8304964539007091 0 0 0 0 1 1 680
              0 0 0 0 0 0 0 0 0 0  -3.830496453900709 0 1 1 0 1 0 690
              . 0 0 . 0 0 0 0 0 0  -3.830496453900709 0 1 1 0 1 1 690
              0 0 0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 0 700
              0 0 0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 1 700
              0 0 0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 0 710
              0 0 0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 1 710
              0 0 0 0 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 0 750
              0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 1 1 0 1 1 750
              0 0 0 0 2 1 1 1 1 1   2.169503546099291 0 0 1 1 1 0 780
              0 1 0 0 1 1 0 1 1 1   2.169503546099291 0 0 1 1 1 1 780
              . 0 0 0 1 1 0 1 0 0  1.1695035460992909 0 0 0 1 1 0 790
              0 1 0 0 1 1 0 1 0 0  1.1695035460992909 0 0 0 1 1 1 790
              0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 1 0 1 0 800
              0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 800
              . 0 0 0 0 0 0 0 1 0  1.1695035460992909 0 0 0 1 1 0 810
              0 0 0 0 1 1 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 810
              . 0 0 0 1 1 0 1 1 1  .16950354609929086 0 1 0 0 1 0 830
              0 1 0 0 2 1 1 1 1 1  .16950354609929086 0 1 0 0 1 1 830
              0 0 0 0 1 1 0 0 1 0 -1.8304964539007091 0 1 0 0 1 0 850
              0 1 0 . 2 1 1 1 1 0 -1.8304964539007091 0 1 0 0 1 1 850
              0 0 0 0 0 0 0 0 1 1  -2.830496453900709 0 1 0 0 1 0 870
              0 0 0 0 1 1 0 0 1 1  -2.830496453900709 0 1 0 0 1 1 870
              0 0 0 0 2 1 1 1 1 0  1.1695035460992909 0 0 0 0 1 0 930
              0 0 0 0 1 1 0 0 1 0  1.1695035460992909 0 0 0 0 1 1 930
              0 0 0 0 0 0 0 0 1 0  -3.830496453900709 0 0 1 0 1 0 940
              0 0 0 0 1 1 0 0 1 0  -3.830496453900709 0 0 1 0 1 1 940
              . 0 0 0 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 0 950
              0 1 0 0 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 1 950
              0 0 0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 0 970
              0 0 0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 1 970
              end
              label values SexLastUnsafeConsist yes_no
              label values SexPartnerFreq2More yes_no
              label def yes_no 0 "0. No", modify
              label def yes_no 1 "1. Yes", modify
              label values Use UseT3
              label def UseT3 0 "No access", modify
              label def UseT3 1 "Social media", modify
              label def UseT3 2 "Health content", modify
              label values Sex Sex
              label def Sex 0 "Male", modify
              label def Sex 1 "Female", modify
              label values Rural UrbRur
              label def UrbRur 0 "0. Urban", modify
              label def UrbRur 1 "1. Rural", modify
              label values ARTever binary
              label values HomeTypeInformal binary
              label values Relationship binary
              label values SchEnrol binary
              label def binary 0 "0. No", modify
              label def binary 1 "1. Yes", modify
              label values NecessitiesAll_r binary_r
              label def binary_r 0 "0. Yes", modify
              label def binary_r 1 "1. No", modify
            The codes for the graph have not changed:

            Code:
            local y     SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex local x1    XC XM
              local xsex  Rural AgeC HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j
               
              foreach ys of local y {
                  forvalues f = 0/1 {
                      mi estimate, saving(`ys'`f', replace) cmdok : melogit `ys' `x1' `xsex' if Sex == `f' || ID:, or cov(un) 
                      estimate store `ys'`f'
                  }
              }
               
              coefplot    (SexDrunkDrugs0, label("Boy") msymbol(S) mcolor(black) mfcolor(white))      ///
              (SexDrunkDrugs1, label("Girl") msymbol(O) mcolor(black) mfcolor(black)), ///
              bylabel("Sex after substance use")  ///
              mlabel("{it:p} = " + string(@pval,"%9.3f")) mlabpos(1)                      ///
              || (SexLastUnsafeConsist0) (SexLastUnsafeConsist1),  ///
              bylabel("Infrequent condom use")                  ///
              || (SexPartnerFreq2More0)(SexPartnerFreq2More1),  ///
              bylabel("Multiple sexual partnership")         ///
              || (InequitSex0) (InequitSex1),  ///
              bylabel("Inequitable sexual partnership"),         ///
              || eform keep(XC XM) ciopts(lcolor(gs8) lwidth(thick)) ///
              xtitle("Adjusted odds ratios (ORs)") xlab(0(1)4,format(%3.0fc))  ///
              xline(1, lcolor(gs10)) ///
              ylab(1 `" "Health" "content" "use" "(Ref. no access)" "' 2 `" "Social" "media" "use" "(Ref. no access)" "') ///
              byopts(xrescale) subtitle(, fcolor(none) lstyle(none))  ///
              legend(order(2 "ORs for boys" 4 "ORs for girls" 3 "95% CI") row(1) ring(0) pos(10) region(lstyle(none)))

            Comment


            • #7
              Sorry, your data still is not sufficient to run any imputations. Please make sure you are able to reproduce the problem before posting the data and codes.

              Code:
              * Example generated by -dataex-. For more info, type help dataex
                clear
                input byte(SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More) float(InequitSex Use Access XC XM) byte(Sex Rural) double AgeC byte(HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j) long ID
                0 0 0 0 0 0 0 0 0 1  -.8304964539007091 1 1 1 0 1 0  10
                0 1 0 . 2 1 1 1 0 1  -.8304964539007091 1 1 1 0 1 1  10
                0 1 1 1 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 0  20
                0 1 0 0 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 1  20
                0 0 0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0  50
                0 0 0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1  50
                . 0 0 0 2 1 1 1 1 0  .16950354609929086 0 0 1 0 1 0  60
                0 1 0 0 1 1 0 1 1 0  .16950354609929086 0 0 1 0 1 1  60
                0 0 0 0 0 0 0 0 0 1  -.8304964539007091 0 1 0 0 1 0  70
                0 0 0 0 2 1 1 1 0 1  -.8304964539007091 0 1 0 0 1 1  70
                0 0 0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 0  90
                0 0 0 0 0 0 0 0 1 0  -.8304964539007091 1 0 1 0 1 1  90
                0 0 0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 0 100
                0 0 0 0 0 0 0 0 0 1 -1.8304964539007091 0 0 1 0 1 1 100
                0 0 0 0 1 1 0 1 0 0  -2.830496453900709 0 0 1 0 1 0 140
                0 0 0 0 0 0 0 0 0 0  -2.830496453900709 0 0 1 0 1 1 140
                0 0 0 0 0 0 0 0 1 0  -2.830496453900709 0 0 0 0 1 0 150
                0 0 0 0 1 1 0 1 1 0  -2.830496453900709 0 0 0 0 1 1 150
                . 0 0 0 2 1 1 1 0 0  -.8304964539007091 0 0 1 1 1 0 170
                1 1 1 . 1 1 0 1 0 0  -.8304964539007091 0 0 1 1 1 1 170
                0 0 0 0 1 1 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 200
                0 0 0 0 1 1 0 1 1 1  -.8304964539007091 0 0 1 0 1 1 200
                . 0 0 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 0 210
                0 0 0 0 0 0 0 0 0 0  -.8304964539007091 0 0 0 0 1 1 210
                0 0 0 0 1 1 0 1 1 1 -1.8304964539007091 0 0 1 0 1 0 230
                0 0 0 0 1 1 0 1 1 1 -1.8304964539007091 0 0 1 0 1 1 230
                0 0 0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 0 240
                0 0 0 0 0 0 0 0 0 1  -3.830496453900709 0 1 1 0 1 1 240
                0 0 0 0 1 1 0 1 1 1  .16950354609929086 0 1 0 0 1 0 280
                0 0 0 0 0 0 0 0 1 1  .16950354609929086 0 1 0 0 1 1 280
                0 0 0 0 0 0 0 0 0 0 -1.8304964539007091 0 1 1 0 1 0 290
                0 0 0 0 1 1 0 0 0 0 -1.8304964539007091 0 1 1 0 1 1 290
                0 0 1 0 0 0 0 0 0 0   6.169503546099291 0 0 1 1 1 0 300
                0 0 1 0 0 0 0 0 0 0   6.169503546099291 0 0 1 1 1 1 300
                0 0 0 0 0 0 0 0 1 0   3.169503546099291 0 0 1 1 1 0 310
                0 0 0 0 1 1 0 0 1 0   3.169503546099291 0 0 1 1 1 1 310
                0 0 0 0 0 0 0 0 0 0  -.8304964539007091 0 1 0 0 1 0 340
                1 1 0 . 0 0 0 0 0 0  -.8304964539007091 0 1 0 0 1 1 340
                . 0 0 0 1 1 0 1 1 0   3.169503546099291 0 0 1 0 1 0 360
                0 0 0 0 1 1 0 1 1 0   3.169503546099291 0 0 1 0 1 1 360
                0 0 0 0 1 1 0 1 1 0  .16950354609929086 0 0 1 0 1 0 390
                0 0 0 0 1 1 0 0 1 0  .16950354609929086 0 0 1 0 1 1 390
                0 0 0 0 1 1 0 0 1 0 -1.8304964539007091 0 0 1 0 1 0 410
                0 0 0 0 1 1 0 1 1 0 -1.8304964539007091 0 0 1 0 1 1 410
                0 0 0 0 1 1 0 0 1 1  -3.830496453900709 0 0 0 0 1 0 440
                0 0 0 0 1 1 0 1 1 1  -3.830496453900709 0 0 0 0 1 1 440
                1 0 1 1 1 1 0 1 0 0  .16950354609929086 0 1 1 0 1 0 450
                . 1 0 . 1 1 0 1 0 0  .16950354609929086 0 1 1 0 1 1 450
                0 0 0 0 1 1 0 1 1 1  -.8304964539007091 0 0 1 0 1 0 470
                0 0 0 0 1 1 0 0 1 1  -.8304964539007091 0 0 1 0 1 1 470
                0 0 0 0 0 0 0 0 0 1  -3.830496453900709 1 1 1 0 1 0 480
                0 0 0 0 1 1 0 1 0 1  -3.830496453900709 1 1 1 0 1 1 480
                0 0 0 0 1 1 0 1 1 0   3.169503546099291 0 0 0 0 1 0 490
                0 1 0 0 1 1 0 1 1 0   3.169503546099291 0 0 0 0 1 1 490
                0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 0 1 1 0 510
                0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 0 1 1 1 510
                0 0 1 0 1 1 0 1 1 0  1.1695035460992909 0 0 0 1 1 0 530
                0 0 1 1 1 1 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 530
                0 0 1 0 0 0 0 0 1 0  1.1695035460992909 0 0 1 0 1 0 540
                0 1 0 1 1 1 0 1 1 0  1.1695035460992909 0 0 1 0 1 1 540
                0 0 1 0 0 0 0 0 1 1   3.169503546099291 0 1 1 1 1 0 580
                0 1 1 1 1 1 0 1 1 1   3.169503546099291 0 1 1 1 1 1 580
                0 0 1 0 1 1 0 1 1 1   2.169503546099291 0 0 1 1 1 0 600
                0 0 0 0 1 1 0 1 1 1   2.169503546099291 0 0 1 1 1 1 600
                0 0 0 0 0 0 0 0 1 0  -.8304964539007091 0 0 1 0 1 0 620
                0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 620
                0 1 1 0 0 0 0 0 1 0  .16950354609929086 0 0 0 1 1 0 650
                0 1 0 0 1 1 0 1 1 0  .16950354609929086 0 0 0 1 1 1 650
                1 1 1 0 0 0 0 0 1 0  -.8304964539007091 0 0 0 0 1 0 680
                0 0 0 1 1 1 0 0 1 0  -.8304964539007091 0 0 0 0 1 1 680
                0 0 0 0 0 0 0 0 0 0  -3.830496453900709 0 1 1 0 1 0 690
                . 0 0 . 0 0 0 0 0 0  -3.830496453900709 0 1 1 0 1 1 690
                0 0 0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 0 700
                0 0 0 0 0 0 0 0 1 0  -2.830496453900709 0 1 0 0 1 1 700
                0 0 0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 0 710
                0 0 0 0 0 0 0 0 1 0  -3.830496453900709 0 0 0 0 1 1 710
                0 0 0 0 0 0 0 0 1 0  -.8304964539007091 0 1 1 0 1 0 750
                0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 1 1 0 1 1 750
                0 0 0 0 2 1 1 1 1 1   2.169503546099291 0 0 1 1 1 0 780
                0 1 0 0 1 1 0 1 1 1   2.169503546099291 0 0 1 1 1 1 780
                . 0 0 0 1 1 0 1 0 0  1.1695035460992909 0 0 0 1 1 0 790
                0 1 0 0 1 1 0 1 0 0  1.1695035460992909 0 0 0 1 1 1 790
                0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 1 0 1 0 800
                0 0 0 0 1 1 0 1 1 0  -.8304964539007091 0 0 1 0 1 1 800
                . 0 0 0 0 0 0 0 1 0  1.1695035460992909 0 0 0 1 1 0 810
                0 0 0 0 1 1 0 1 1 0  1.1695035460992909 0 0 0 1 1 1 810
                . 0 0 0 1 1 0 1 1 1  .16950354609929086 0 1 0 0 1 0 830
                0 1 0 0 2 1 1 1 1 1  .16950354609929086 0 1 0 0 1 1 830
                0 0 0 0 1 1 0 0 1 0 -1.8304964539007091 0 1 0 0 1 0 850
                0 1 0 . 2 1 1 1 1 0 -1.8304964539007091 0 1 0 0 1 1 850
                0 0 0 0 0 0 0 0 1 1  -2.830496453900709 0 1 0 0 1 0 870
                0 0 0 0 1 1 0 0 1 1  -2.830496453900709 0 1 0 0 1 1 870
                0 0 0 0 2 1 1 1 1 0  1.1695035460992909 0 0 0 0 1 0 930
                0 0 0 0 1 1 0 0 1 0  1.1695035460992909 0 0 0 0 1 1 930
                0 0 0 0 0 0 0 0 1 0  -3.830496453900709 0 0 1 0 1 0 940
                0 0 0 0 1 1 0 0 1 0  -3.830496453900709 0 0 1 0 1 1 940
                . 0 0 0 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 0 950
                0 1 0 0 0 0 0 0 1 0  .16950354609929086 1 0 0 1 1 1 950
                0 0 0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 0 970
                0 0 0 0 0 0 0 0 1 1  -3.830496453900709 0 0 1 0 1 1 970
                end
                label values SexLastUnsafeConsist yes_no
                label values SexPartnerFreq2More yes_no
                label def yes_no 0 "0. No", modify
                label def yes_no 1 "1. Yes", modify
                label values Use UseT3
                label def UseT3 0 "No access", modify
                label def UseT3 1 "Social media", modify
                label def UseT3 2 "Health content", modify
                label values Sex Sex
                label def Sex 0 "Male", modify
                label def Sex 1 "Female", modify
                label values Rural UrbRur
                label def UrbRur 0 "0. Urban", modify
                label def UrbRur 1 "1. Rural", modify
                label values ARTever binary
                label values HomeTypeInformal binary
                label values Relationship binary
                label values SchEnrol binary
                label def binary 0 "0. No", modify
                label def binary 1 "1. Yes", modify
                label values NecessitiesAll_r binary_r
                label def binary_r 0 "0. Yes", modify
                label def binary_r 1 "1. No", modify
              
                  
                 
                mi set mlong // mi set data for imputation
                 
                mi reshape wide SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
                
                
                * Identifying which variables in the imputation model have missing information.
                mi register imputed NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0 SexDrunkDrugs1 InequitSex0 InequitSex1
                 
                *** Mutiple imputation
                *MI using chained equations/MICE (also known as the fully conditional specification or sequential generalized regression) for binary outcomes.  In simulation studies (Lee & Carlin, 2010; Van Buuren, 2007), MICE has been show to produce estimates that are comparable to MVN method.
                 
                * Clear any xtset that is ongoing
                mi xtset, clear
                mi /*mimpt*/ impute chained  (logit) NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0  ///
                SexDrunkDrugs1 InequitSex0 InequitSex1 = Access0 Access1  ///
                XC0 XM0 XC1 XM1 Sex ARTever AgeC Rural Relationship  ///
                SchEnrol, add(10) rseed(20210101) /*skipnonconvergence(5)*/ savetrace(tracedata, replace)
                 
                * Bring data back to long format
                mi reshape long SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
                 
                * We can check the data and see if there is no problems in our imputation
               summ SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex HomeTypeInformal NecessitiesAll_r
                
              local y     SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex local x1    XC XM
                local xsex  Rural AgeC HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j
                 
                foreach ys of local y {
                    forvalues f = 0/1 {
                        mi estimate, saving(`ys'`f', replace) cmdok : melogit `ys' `x1' `xsex' if Sex == `f' || ID:, or cov(un) 
                        estimate store `ys'`f'
                    }
                }
                 
                coefplot    (SexDrunkDrugs0, label("Boy") msymbol(S) mcolor(black) mfcolor(white))      ///
                (SexDrunkDrugs1, label("Girl") msymbol(O) mcolor(black) mfcolor(black)), ///
                bylabel("Sex after substance use")  ///
                mlabel("{it:p} = " + string(@pval,"%9.3f")) mlabpos(1)                      ///
                || (SexLastUnsafeConsist0) (SexLastUnsafeConsist1),  ///
                bylabel("Infrequent condom use")                  ///
                || (SexPartnerFreq2More0)(SexPartnerFreq2More1),  ///
                bylabel("Multiple sexual partnership")         ///
                || (InequitSex0) (InequitSex1),  ///
                bylabel("Inequitable sexual partnership"),         ///
                || eform keep(XC XM) ciopts(lcolor(gs8) lwidth(thick)) ///
                xtitle("Adjusted odds ratios (ORs)") xlab(0(1)4,format(%3.0fc))  ///
                xline(1, lcolor(gs10)) ///
                ylab(1 `" "Health" "content" "use" "(Ref. no access)" "' 2 `" "Social" "media" "use" "(Ref. no access)" "') ///
                byopts(xrescale) subtitle(, fcolor(none) lstyle(none))  ///
                legend(order(2 "ORs for boys" 4 "ORs for girls" 3 "95% CI") row(1) ring(0) pos(10) region(lstyle(none)))
              Res.:

              Code:
              Conditional models:
                  SexDrunkDrugs1: logit SexDrunkDrugs1 i.NecessitiesAll_r i.HomeTypeInformal i.InequitSex0 i.InequitSex1 i.SexDrunkDrugs0 Access0
                                   Access1 XC0 XM0 XC1 XM1 Sex ARTever AgeC Rural Relationship SchEnrol
                     InequitSex1: logit InequitSex1 i.NecessitiesAll_r i.HomeTypeInformal i.InequitSex0 i.SexDrunkDrugs1 i.SexDrunkDrugs0 Access0
                                   Access1 XC0 XM0 XC1 XM1 Sex ARTever AgeC Rural Relationship SchEnrol
                  SexDrunkDrugs0: logit SexDrunkDrugs0 i.NecessitiesAll_r i.HomeTypeInformal i.InequitSex0 i.SexDrunkDrugs1 i.InequitSex1 Access0
                                   Access1 XC0 XM0 XC1 XM1 Sex ARTever AgeC Rural Relationship SchEnrol
              
              Performing chained iterations ...
              error occurred during imputation of SexDrunkDrugs0 SexDrunkDrugs1 InequitSex1 on m = 1
              r(2000);
              
              end of do-file

              Comment


              • #8
                Thanks again Andrew. I realized that I had to increase the data rows for the imputation codes to work. However, Statalist has a limit in the number of characters to post (30000 I think). I will reach out if I find a way.

                Comment


                • #9
                  It's fine to upload the dataset in .dta format in this case.

                  Comment


                  • #10
                    I've used coefplot with MI data before, and this included labeling the dots with stars based on the p-value. I checked using my previous work. Using the mlabel option should work, e.g. using a code fragment like this one did correctly plot the p-value by the dot:

                    Code:
                    coefplot (SexDrunkDrugs0, label("Boy") msymbol(S) mcolor(black) mfcolor(white))... , mlabel(string(@pval,"%9.3f")) mlabpos(1)
                    Bolade, after you post the dta file, you could go back to your own code and simplify it. You've got a bunch of options added right now. Try plotting one model with just the mlabel option and see if it produces the p-value. Like Andrew said, I had used the post option with the original mi estimate: command; without it, I'm surprised that coefplot has anything to plot at all.

                    I've tried parsing the code and I can't see anything that would immediately explain why you're getting p-values plotted in MI, but not in regular estimation. The only possible thing I can see is that you placed the mlabel option with the first model you asked for, then you used the || operator and specified the other models. I have typically put mlabel as a global option after specifying all of the models to coefplot. I'm not sure why that would cause a failure with MI but not without it. Anyway, this is why I suggested you try plotting just one model to see if it changes anything.

                    This is a well-posed question. Thanks for including your complete code and a data extract. I hope we can resolve this. This has got to be resolvable, because I've done pretty much what you've done.
                    Last edited by Weiwen Ng; 24 Aug 2022, 15:24.
                    Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

                    When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

                    Comment


                    • #11
                      Originally posted by Andrew Musau View Post
                      It's fine to upload the dataset in .dta format in this case.
                      Thank you so much, Andrew! I am attaching the dataset.
                      Attached Files

                      Comment


                      • #12
                        Originally posted by Weiwen Ng View Post
                        I've used coefplot with MI data before, and this included labeling the dots with stars based on the p-value. I checked using my previous work. Using the mlabel option should work, e.g. using a code fragment like this one did correctly plot the p-value by the dot:

                        Code:
                        coefplot (SexDrunkDrugs0, label("Boy") msymbol(S) mcolor(black) mfcolor(white))... , mlabel(string(@pval,"%9.3f")) mlabpos(1)
                        Bolade, after you post the dta file, you could go back to your own code and simplify it. You've got a bunch of options added right now. Try plotting one model with just the mlabel option and see if it produces the p-value. Like Andrew said, I had used the post option with the original mi estimate: command; without it, I'm surprised that coefplot has anything to plot at all.

                        I've tried parsing the code and I can't see anything that would immediately explain why you're getting p-values plotted in MI, but not in regular estimation. The only possible thing I can see is that you placed the mlabel option with the first model you asked for, then you used the || operator and specified the other models. I have typically put mlabel as a global option after specifying all of the models to coefplot. I'm not sure why that would cause a failure with MI but not without it. Anyway, this is why I suggested you try plotting just one model to see if it changes anything.

                        This is a well-posed question. Thanks for including your complete code and a data extract. I hope we can resolve this. This has got to be resolvable, because I've done pretty much what you've done.
                        Thank you so much, Weiwen for your reply and your advice. I have tried your suggestion. Yet the same graph. The coefplot with p-value label showing indeed work perfectly with non-imputed data.

                        Comment


                        • #13
                          Thanks for the data example. I have cut out some of your twoway code for purpose of testing if the labels work. As I stated in #2, the issue that I see is that you do not add the -post- option to your mi estimate command. If I do so, I get the p-values printed.

                          Code:
                          use "20220825 sample data.dta", clear
                          
                            mi set mlong // mi set data for imputation
                             
                            mi reshape wide SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
                            
                            
                            * Identifying which variables in the imputation model have missing information.
                            mi register imputed NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0 SexDrunkDrugs1 InequitSex0 InequitSex1
                             
                            *** Mutiple imputation
                            *MI using chained equations/MICE (also known as the fully conditional specification or sequential generalized regression) for binary outcomes.  In simulation studies (Lee & Carlin, 2010; Van Buuren, 2007), MICE has been show to produce estimates that are comparable to MVN method.
                             
                            * Clear any xtset that is ongoing
                            mi xtset, clear
                            mi /*mimpt*/ impute chained  (logit) NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0  ///
                            SexDrunkDrugs1 InequitSex0 InequitSex1 = Access0 Access1  ///
                            XC0 XM0 XC1 XM1 Sex ARTever AgeC Rural Relationship  ///
                            SchEnrol, add(10) rseed(20210101) /*skipnonconvergence(5)*/ savetrace(tracedata, replace)
                             
                            * Bring data back to long format
                            mi reshape long SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
                             
                            * We can check the data and see if there is no problems in our imputation
                           summ SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex HomeTypeInformal NecessitiesAll_r
                            
                          local y     SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex local x1    XC XM
                            local xsex  Rural AgeC HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j
                             
                            foreach ys of local y {
                                forvalues f = 0/1 {
                                    cap noi mi estimate, post saving(`ys'`f', replace) cmdok : melogit `ys' `x1' `xsex' if Sex == `f' || ID:, or cov(un) 
                                    cap noi estimate store `ys'`f'
                                }
                            }
                             
                            *set trace on
                            set scheme s1color
                            cap drop xpos
                            gen xpos = 3.5
                             coefplot    (SexDrunkDrugs0, label("Boy") msymbol(S) mcolor(black) mfcolor(white))      ///
                            (SexDrunkDrugs1, label("Girl") msymbol(O) mcolor(black) mfcolor(black)), ///
                            bylabel("Sex after substance use") mlabel(cond(@pval<0.001, "{it:p} <0.001", "{it:p} =" + string(@pval,"%9.3f"))) ///
                              mlabc(none) addplot(scatter @at xpos, ms(i) mlab(@mlbl) mlabcolor(black) mlabpos(9) mlabgap(-10) mlabsize(small)) ///
                              graphr(margin(r=10)) xsc(r(-10 5))
                          Click image for larger version

Name:	Graph.png
Views:	1
Size:	29.4 KB
ID:	1679202

                          Comment


                          • #14
                            Thanks a stack! I have adapted the code for another outcome
                            Code:
                            local y     SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex
                            local x0    Access
                            local x1     XC XM
                            local x     Rural AgeC Sex HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j
                            local xsex    Rural AgeC HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j
                              foreach ys of local y {
                                  forvalues f = 0/1 {
                                      cap noi mi estimate, post saving(`ys'`f', replace) cmdok : melogit `ys' `x1' `xsex' if Sex == `f' || ID:, or cov(un) 
                                      cap noi estimate store `ys'`f'
                                  }
                              }
                                *set trace on
                              set scheme s1color
                              cap drop xpos
                              gen xpos = 3.5
                            coefplot    (SexLastUnsafeConsist0, label("Boy") msymbol(S) mcolor(black) mfcolor(white))      ///
                              (SexLastUnsafeConsist1, label("Girl") msymbol(O) mcolor(black) mfcolor(black)), ///
                              bylabel("Infrequent condom use") mlabel(cond(@pval<0.001, "{it:p} <0.001", "{it:p} =" + string(@pval,"%9.3f"))) ///
                                mlabc(none) addplot(scatter @at xpos, ms(i) mlab(@mlbl) mlabcolor(black) mlabpos(9) mlabgap(-10) mlabsize(small)) ///
                                graphr(margin(r=10)) xsc(r(-10 5))
                            , and this is get the p-values missing. I got the following (attached) graph.

                            I looked at the regression tables and I can't see what might be the problem.
                            Attached Files
                            Last edited by Bolade Banougnin; 25 Aug 2022, 07:16.

                            Comment


                            • #15
                              Please post images in .PNG format as recommended in the FAQs. I am not able to open the .GPH image created by Stata 17 in Stata 16. That being the case, I ran the code in #14 and three p-values were missing.
                              Click image for larger version

Name:	Graph1.png
Views:	1
Size:	43.2 KB
ID:	1679353




                              On further investigation, I note that these values are not missing in the stored results (highlighted below).

                              Code:
                              . est replay SexLastUnsafeConsist0
                              
                              --------------------------------------------------------------------------------------
                              Model SexLastUnsafeConsist0
                              --------------------------------------------------------------------------------------
                              
                              Mixed-effects logistic regression               Number of obs     =      1,174
                              Group variable:              ID                 Number of groups  =        587
                              
                                                                              Obs per group:
                                                                                            min =          2
                                                                                            avg =        2.0
                                                                                            max =          2
                              
                              Integration method: mvaghermite                 Integration pts.  =          7
                              
                                                                              Wald chi2(.)      =          .
                              Log likelihood =          .                     Prob > chi2       =          .
                              -------------------------------------------------------------------------------------
                              SexLastUnsafeCons~t |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                              --------------------+----------------------------------------------------------------
                                               XC |  -.6959997   .6446828    -1.08   0.280    -1.959555    .5675554
                                               XM |   .3778831   .3858202     0.98   0.327    -.3783106    1.134077
                                            Rural |  -.6248963   .5701184    -1.10   0.273    -1.742308    .4925153
                                             AgeC |   .4053572   .1053465     3.85   0.000     .1988818    .6118326
                                 HomeTypeInformal |  -.5606224   .7523278    -0.75   0.456    -2.035158    .9139131
                                 NecessitiesAll_r |   .5613208    .474195     1.18   0.237    -.3680843    1.490726
                                          ARTever |  -.8648473       .479    -1.81   0.071     -1.80367    .0739755
                                     Relationship |   2.024283   .5228796     3.87   0.000      .999458    3.049108
                                         SchEnrol |  -2.499274   1.336178    -1.87   0.061    -5.118134    .1195862
                                                j |   .8185097   .2963594     2.76   0.006     .2376559    1.399363
                                            _cons |  -2.661133   1.376298    -1.93   0.053    -5.358626    .0363608
                              --------------------+----------------------------------------------------------------
                                    var(_cons[ID])|   8.865928   3.155539                      4.413333    17.81073
                              -------------------------------------------------------------------------------------
                              LR test vs. logistic model: chi2(.) = .                   Prob > chi2 =      .
                              
                              Note: LR test is conservative and provided only for reference.
                              
                              . est replay SexLastUnsafeConsist1
                              
                              --------------------------------------------------------------------------------------
                              Model SexLastUnsafeConsist1
                              --------------------------------------------------------------------------------------
                              
                              Mixed-effects logistic regression               Number of obs     =      1,532
                              Group variable:              ID                 Number of groups  =        766
                              
                                                                              Obs per group:
                                                                                            min =          2
                                                                                            avg =        2.0
                                                                                            max =          2
                              
                              Integration method: mvaghermite                 Integration pts.  =          7
                              
                                                                              Wald chi2(.)      =          .
                              Log likelihood =          .                     Prob > chi2       =          .
                              -------------------------------------------------------------------------------------
                              SexLastUnsafeCons~t |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                              --------------------+----------------------------------------------------------------
                                               XC |  -.6728635   .3554466    -1.89   0.058    -1.369526     .023799
                                               XM |   .3563452    .215419     1.65   0.098    -.0658684    .7785587
                                            Rural |   .2718676    .235966     1.15   0.249    -.1906173    .7343525
                                             AgeC |   .5020056    .061067     8.22   0.000     .3823165    .6216948
                                 HomeTypeInformal |   .1759705   .2726909     0.65   0.519    -.3584938    .7104348
                                 NecessitiesAll_r |  -.0812407   .2505907    -0.32   0.746    -.5723895    .4099081
                                          ARTever |   -.670258   .2284569    -2.93   0.003    -1.118025   -.2224908
                                     Relationship |   1.025744   .2646242     3.88   0.000     .5070905    1.544398
                                         SchEnrol |   .1936175   .3525943     0.55   0.583    -.4974547    .8846896
                                                j |    1.00031   .1776544     5.63   0.000      .652114    1.348506
                                            _cons |    -3.3551   .4931144    -6.80   0.000    -4.321587   -2.388614
                              --------------------+----------------------------------------------------------------
                                    var(_cons[ID])|   2.449458   .6544243                      1.450954    4.135104
                              -------------------------------------------------------------------------------------
                              LR test vs. logistic model: chi2(.) = .                   Prob > chi2 =      .
                              
                              Note: LR test is conservative and provided only for reference.
                              Furthermore, if I asked for the coefficients to be printed, they are available. Since you have 1500+ observations, my workaround involves approximating the p-values using the normal distribution in these cases where they are missing. I can only conclude that this is a bug in coefplot. I would recommend that you send a dataset and some code that reproduces the issue to Ben Jann so that he may have a look at it. The approximation is highlighted in the code below.

                              Code:
                               use "C:\Users\amus\Downloads\20220825 sample data.dta"
                              
                                mi set mlong // mi set data for imputation
                                
                                mi reshape wide SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
                                
                                
                                * Identifying which variables in the imputation model have missing information.
                                mi register imputed NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0 SexDrunkDrugs1 InequitSex0 InequitSex1
                                
                                *** Mutiple imputation
                                *MI using chained equations/MICE (also known as the fully conditional specification or sequential generalized regression) for binary outcomes.  In simulation studies (Lee & Carlin, 2010; Van Buuren, 2007), MICE has been show to produce estimates that are comparable to MVN method.
                                
                                * Clear any xtset that is ongoing
                                mi xtset, clear
                                mi /*mimpt*/ impute chained  (logit) NecessitiesAll_r HomeTypeInformal SexDrunkDrugs0  ///
                                SexDrunkDrugs1 InequitSex0 InequitSex1 = Access0 Access1  ///
                                XC0 XM0 XC1 XM1 Sex ARTever AgeC Rural Relationship  ///
                                SchEnrol, add(10) rseed(20210101) /*skipnonconvergence(5)*/ savetrace(tracedata, replace)
                                
                                * Bring data back to long format
                                mi reshape long SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex Access XC XM Use, i(ID) j(j)
                                
                                * We can check the data and see if there is no problems in our imputation
                               summ SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex HomeTypeInformal NecessitiesAll_r
                              local y     SexDrunkDrugs SexLastUnsafeConsist SexPartnerFreq2More InequitSex
                              local x0    Access
                              local x1     XC XM
                              local x     Rural AgeC Sex HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j
                              local xsex    Rural AgeC HomeTypeInformal NecessitiesAll_r ARTever Relationship SchEnrol j
                                foreach ys of local y {
                                    forvalues f = 0/1 {
                                        cap noi mi estimate, post saving(`ys'`f', replace) cmdok : melogit `ys' `x1' `xsex' if Sex == `f' || ID:, or cov(un)
                                        cap noi estimate store `ys'`f'
                                    }
                                }
                                  *set trace on
                                set scheme s1color
                                cap drop xpos
                                gen xpos = 3.5
                              coefplot (SexLastUnsafeConsist0, label("Boy") msymbol(S) mcolor(black) mfcolor(white))      ///
                                (SexLastUnsafeConsist1, label("Girl") msymbol(O) mcolor(black) mfcolor(black)), ///
                                bylabel("Infrequent condom use") mlabel(cond(@pval<0.001, "{it:p} < 0.001", ///
                                cond(@pval>0.001 &@pval<., "{it:p} = " + string(@pval,"%9.3f"), ///
                                cond(missing(@pval) & 2*normal(@t)<0.001, "{it:p} < 0.001", "{it:p} = " + string(2*normal(@t),"%9.3f")) ))) ///
                                mlabc(none) addplot(scatter @at xpos, ms(i) mlab(@mlbl) mlabcolor(black) ///
                                mlabpos(9) mlabgap(-10) mlabsize(small)) graphr(margin(r=10)) xsc(r(-10 5))
                              Click image for larger version

Name:	Graph2.png
Views:	1
Size:	33.5 KB
ID:	1679354

                              Last edited by Andrew Musau; 26 Aug 2022, 02:50.

                              Comment

                              Working...
                              X