I was trying 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 wanted to show the p-values labels on the graphs.
I used your coefplot command to plot these from multiply imputed data. I am posting the codes here. Please, note that I am showing coefplot on one of the four outcomes here.
Code:
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 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 * Regressions 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' } } * Plot graphs *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))
Code:
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 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
* Regressions
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'
}
}
* Plot graphs
*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(-abs(@t))<0.001, "{it:p} < 0.001", "{it:p} = " + string(2*normal(-abs(@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))
Thank you again Andrew Musau
0 Response to Using the coefplot command to plot odds ratios with p-values in multiple imputation estimations – potential bug to fix!
Post a Comment