Since the data set used in Chapter 11 is proprietary (see https://dhsprogram.com/ for details), we are not able to provide it. In Section 1 we give the coding and output of the analysis in Chapter 11; in Section 2 we provide reproducible code, using a data set that is in the public domain, that mimics the analysis in Chapter 11.
plot_mu <-function(model , colour =TRUE, main, both =TRUE) { col <-ifelse(colour,"_col", "") col_ <-ifelse(colour,1, 0) pred_mu_women <-pred_mu_func(csex =1, model = model)[[2]] pred_mu_men <-pred_mu_func(csex =0, model = model)[[2]] main <-ifelse(substitute(model) =="m_ig",TeX(r"((a) Predicted $\mu$ with $IG(0.001,0.001)$)"),ifelse(substitute(model) =="m_sd",TeX(r"((b) Predicted $\mu$ with $SD(0.009)$)"),ifelse(substitute(model) =="m_hc",TeX(r"((c) Predicted $\mu$ with $\it{C}^{+}(0.01)$)"),TeX(r"((d) Predicted $\mu$ with $\it{U}(0.27)$)"))))plot(pred_mu_women$cage,pred_mu_women$mean,type="l",col=rgb(red =0, green =0, blue =0),cex.lab=1,cex.main=1,cex.axis=1, main = main,ylab=TeX(r"($\widehat{\mu}(Z_{wa}\,|\,x)$)"),xlab="child's age", ylim =c(-1.5, 0))lines(pred_mu_women$cage,pred_mu_women$uq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_mu_women$cage,pred_mu_women$lq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))polygon(c(pred_mu_women$cage,rev(pred_mu_women$cage)),c(pred_mu_women$mean,rev(pred_mu_women$uq)),border=NA,col=ifelse(colour,rgb(red = col_, green =0, blue =0,alpha =0.5),rgb(127,127,127, maxColorValue =255)))polygon(c(pred_mu_women$cage,rev(pred_mu_women$cage)),c(pred_mu_women$mean,rev(pred_mu_women$lq)),border=NA,col=ifelse(colour,rgb(red = col_, green =0, blue =0,alpha =0.5),rgb(127,127,127, maxColorValue =255)))if (both) {lines(pred_mu_men$cage,pred_mu_men$uq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_mu_men$cage,pred_mu_men$lq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))polygon(c(pred_mu_men$cage,rev(pred_mu_men$cage)),c(pred_mu_men$mean,rev(pred_mu_men$uq)),border=NA,col=ifelse(colour, rgb(red =0, green =0, blue = col_,alpha =0.3),rgb(195,195,195, maxColorValue =255)))polygon(c(pred_mu_men$cage,rev(pred_mu_men$cage)),c(pred_mu_men$mean,rev(pred_mu_men$lq)),border=NA,col=ifelse(colour, rgb(red =0, green =0, blue = col_,alpha =0.3),rgb(195,195,195, maxColorValue =255)))lines(pred_mu_men$cage,pred_mu_men$mean,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.5))lines(pred_mu_women$cage,pred_mu_women$mean,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))legend("topright",cex =1, legend=c("Girls", "Boys"),fill=c(rgb(red = col_, green =0, blue =0,alpha =0.5),rgb(red =0, green =0, blue = col_,alpha =0.3)), bty="n")} else {lines(pred_mu_women$cage,pred_mu_women$mean,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))}}par(mar=c(4.4, 5.6, 3.1, 1.1), mfrow =c(2,2))plot_mu(model = m_ig , colour = T, main ="(a)")plot_mu(model = m_sd , colour = T, main ="(b)")plot_mu(model = m_hc , colour = T, main ="(c)")plot_mu(model = m_u , colour = T, main ="(d)")
Regression Effects
Compute and plot simultaneous CI
#### simultaneous confidence bands; men -------------pred <-pred_mu_func(model = m2)[[1]]pred_mu_men$lq_sim <- pred_mu_men$lqpred_mu_men$uq_sim <- pred_mu_men$uqtemp <- predn =0while((n/ncol(temp))<0.95){ pred_mu_men$uq_sim <- pred_mu_men$mean +1.001*(abs(pred_mu_men$uq_sim - pred_mu_men$mean)) pred_mu_men$lq_sim <- pred_mu_men$mean -1.001*(abs(pred_mu_men$lq_sim - pred_mu_men$mean)) n =0for (x in1:ncol(temp)){if (all(temp[,x] <= pred_mu_men$uq_sim) &all(temp[,x] >= pred_mu_men$lq_sim)){ n <- n+1 } }}#### simultaneous confidence bands; women -------------pred <-pred_mu_func(model = m2,csex =1)[[1]]pred_mu_women$lq_sim <- pred_mu_women$lqpred_mu_women$uq_sim <- pred_mu_women$uqtemp <- predn =0while((n/ncol(temp))<0.95){ pred_mu_women$uq_sim <- pred_mu_women$mean +1.001*(abs(pred_mu_women$uq_sim - pred_mu_women$mean)) pred_mu_women$lq_sim <- pred_mu_women$mean -1.001*(abs(pred_mu_women$lq_sim - pred_mu_women$mean)) n =0for (x in1:ncol(temp)){if (all(temp[,x] <= pred_mu_women$uq_sim) &all(temp[,x] >= pred_mu_women$lq_sim)){ n <- n+1 } }}par(mar=c(4.4, 5.6, 3.1, 1.1), mfrow =c(1,2))plot(pred_mu_women$cage,pred_mu_women$mean,type="l",col=rgb(red =0, green =0, blue =0),cex.lab=1,cex.main=1,cex.axis=1,main =expression("(a) Predicted"~ mu ~"- Girls") ,ylab=TeX(r"($\widehat{\mu}(Z_{wa}\,|\,x)$)"),xlab="child's age", ylim =c(-1.5, 0))lines(pred_mu_women$cage,pred_mu_women$uq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_mu_women$cage,pred_mu_women$lq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_mu_women$cage,pred_mu_women$uq_sim,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_mu_women$cage,pred_mu_women$lq_sim,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))polygon(c(pred_mu_women$cage,rev(pred_mu_women$cage)),c(pred_mu_women$mean,rev(pred_mu_women$uq)),border=NA,col=rgb(red =1, green =0, blue =0,alpha =0.5))polygon(c(pred_mu_women$cage,rev(pred_mu_women$cage)),c(pred_mu_women$mean,rev(pred_mu_women$lq)),border=NA,col=rgb(red =1, green =0, blue =0,alpha =0.5))polygon(c(pred_mu_women$cage,rev(pred_mu_women$cage)),c(pred_mu_women$uq_sim,rev(pred_mu_women$uq)),border=NA,col=rgb(red =1, green =0, blue =0,alpha =0.2))polygon(c(pred_mu_women$cage,rev(pred_mu_women$cage)),c(pred_mu_women$lq_sim,rev(pred_mu_women$lq)),border=NA,col=rgb(red =1, green =0, blue =0,alpha =0.2))legend("topright",cex =1, legend=c("Pointwise CI", "Simultaneous CI"),fill=c(rgb(red =1, green =0, blue =0,alpha =0.5),rgb(red =1, green =0, blue =0,alpha =0.2)), bty="n")plot(pred_mu_men$cage,pred_mu_men$mean,type="l",col=rgb(red =0, green =0, blue =0),cex.lab=1,cex.main=1,cex.axis=1,main =expression("(b) Predicted"~ mu ~"- Boys") ,ylab="",xlab="child's age", ylim =c(-1.5, 0))lines(pred_mu_men$cage,pred_mu_men$uq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_mu_men$cage,pred_mu_men$lq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_mu_men$cage,pred_mu_men$uq_sim,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_mu_men$cage,pred_mu_men$lq_sim,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))polygon(c(pred_mu_men$cage,rev(pred_mu_men$cage)),c(pred_mu_men$mean,rev(pred_mu_men$uq)),border=NA,col=rgb(red =0, green =0, blue =1,alpha =0.3))polygon(c(pred_mu_men$cage,rev(pred_mu_men$cage)),c(pred_mu_men$mean,rev(pred_mu_men$lq)),border=NA,col=rgb(red =0, green =0, blue =1,alpha =0.3))polygon(c(pred_mu_men$cage,rev(pred_mu_men$cage)),c(pred_mu_men$uq_sim,rev(pred_mu_men$uq)),border=NA,col=rgb(red =0, green =0, blue =1,alpha =0.1))polygon(c(pred_mu_men$cage,rev(pred_mu_men$cage)),c(pred_mu_men$lq_sim,rev(pred_mu_men$lq)),border=NA,col=rgb(red =0, green =0, blue =1,alpha =0.1))legend("topright",cex =1, legend=c("Pointwise CI", "Simultaneous CI"),fill=c(rgb(red =0, green =0, blue =1,alpha =0.3),rgb(red =0, green =0, blue =1,alpha =0.1)), bty="n")
par(mar=c(4.4, 5.6, 3.1, 1.1))plot(pred_sigma_women$cage,pred_sigma_women$mean,type="l",col=rgb(red =0, green =0, blue =0),cex.lab=1,cex.main=1,cex.axis=1,main =expression("Predicted"~ sigma),ylab=TeX(r"($\widehat{\sigma}(Z_{wa}\,|\,x)$)"),xlab="child's age",ylim =c(0.8,1.4))lines(pred_sigma_women$cage,pred_sigma_women$uq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_sigma_women$cage,pred_sigma_women$lq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))polygon(c(pred_sigma_women$cage,rev(pred_sigma_women$cage)),c(pred_sigma_women$mean,rev(pred_sigma_women$uq)),border=NA,col=rgb(red =1, green =0, blue =0,alpha =0.5))polygon(c(pred_sigma_women$cage,rev(pred_sigma_women$cage)),c(pred_sigma_women$mean,rev(pred_sigma_women$lq)),border=NA,col=rgb(red =1, green =0, blue =0,alpha =0.5))lines(pred_sigma_men$cage,pred_sigma_men$mean,type="l",col=rgb(red =0, green =0, blue =0))lines(pred_sigma_men$cage,pred_sigma_men$uq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_sigma_men$cage,pred_sigma_men$lq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))polygon(c(pred_sigma_men$cage,rev(pred_sigma_men$cage)),c(pred_sigma_men$mean,rev(pred_sigma_men$uq)),border=NA,col=rgb(red =0, green =0, blue =1,alpha =0.5))polygon(c(pred_sigma_men$cage,rev(pred_sigma_men$cage)),c(pred_sigma_men$mean,rev(pred_sigma_men$lq)),border=NA,col=rgb(red =0, green =0, blue =1,alpha =0.5))legend("topright",cex =0.75, legend=c("Girls", "Boys"),fill=c(rgb(red =1, green =0, blue =0,alpha =0.5),rgb(red =0, green =0, blue =1,alpha =0.5)), bty="n")
par(mar=c(4.4, 5.6, 3.1, 1.1))plot(pred_sigma$cage,pred_sigma$mean,type="l",col=rgb(red =0, green =0, blue =0),cex.lab=1,cex.main=1,cex.axis=1,main =expression("Predicted"~ sigma),ylab=TeX(r"($\widehat{\sigma}(Z_{wa}\,|\,x)$)"),xlab="child's age",ylim =c(1.2, 1.8))lines(pred_sigma$cage,pred_sigma$uq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))lines(pred_sigma$cage,pred_sigma$lq,type="l",col=rgb(red =0, green =0, blue =0,alpha =0.7))polygon(c(pred_sigma$cage,rev(pred_sigma$cage)),c(pred_sigma$mean,rev(pred_sigma$uq)),border=NA,col=rgb(red =0, green =0, blue =1,alpha =0.5))polygon(c(pred_sigma$cage,rev(pred_sigma$cage)),c(pred_sigma$mean,rev(pred_sigma$lq)),border=NA,col=rgb(red =0, green =0, blue =1,alpha =0.5))