rm(list = ls())
library(zCompositions);library(tidyverse)
setwd("C:/...")
#filename<-"ws_JMP.csv";
filename<-"ws_JMP zero.csv";
ws_JMP<-read.csv(file=filename, header=TRUE, sep = ";", dec = ".");
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#2.0 zPatterns----
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+++++++++++++++++++++++++++++++++++
wu=ws_JMP %>% filter(str_detect(Service, "Water")) %>% filter(str_detect(Setting, "Urban"))
wr=ws_JMP %>% filter(str_detect(Service, "Water")) %>% filter(str_detect(Setting, "Rural"))
su=ws_JMP %>% filter(str_detect(Service, "Sanitation")) %>% filter(str_detect(Setting, "Urban"))
sr=ws_JMP %>% filter(str_detect(Service, "Sanitation")) %>% filter(str_detect(Setting, "Rural"))
#+++++++++++++++++++++++++++++++++++

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#2.0 zPatterns----
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cx=0.8
cl=0.9

pdf(file="C:/.../Figure 2.pdf",
    width=3.5, height=3.5)
#Water-urban - 
wu_z=wu[,11:14]
p.ze1=zPatterns(wu_z,label=0,show.means = TRUE, round.means =1,axis.labels = c("Indicador", "Número de patrón"),
                cex.means=cx,cex.axis = cl,cell.labels=c("Cero","No-cero"),bar.labels=TRUE)
summary(p.ze1)

#Water-rural - 
wr_z=wr[,11:14]
p.ze2=zPatterns(wr_z,label=0,show.means = TRUE, round.means =1,axis.labels = c("Indicador", "Número de patrón"),
                cex.means=cx,cex.axis = cl,cell.labels=c("Cero","No-cero"),bar.labels=TRUE)
summary(p.ze2)

#Sanitation-urban - 
su_z=su[,11:14]
p.ze3=zPatterns(su_z,label=0,show.means = TRUE, round.means =1,axis.labels = c("Indicador", "Número de patrón"),
                cex.means=cx,cex.axis = cl,cell.labels=c("Cero","No-cero"),bar.labels=TRUE)
summary(p.ze3)

#Sanitation-rural - 
sr_z=sr[,11:14]
p.ze4=zPatterns(sr_z,label=0,show.means = TRUE, round.means =1,axis.labels = c("Indicador", "Número de patrón"),
                cex.means=cx,cex.axis = cl,cell.labels=c("Cero","No-cero"),bar.labels=TRUE)
summary(p.ze4)
dev.off()

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#Missing data
rm(list = ls())
filename<-"ws_JMP missing.csv";
ws_JMP<-read.csv(file=filename, header=TRUE, sep = ";", dec = ".");
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#2.0 zPatterns----
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+++++++++++++++++++++++++++++++++++
wu=ws_JMP %>% filter(str_detect(Service, "Water")) %>% filter(str_detect(Setting, "Urban"))
wr=ws_JMP %>% filter(str_detect(Service, "Water")) %>% filter(str_detect(Setting, "Rural"))
su=ws_JMP %>% filter(str_detect(Service, "Sanitation")) %>% filter(str_detect(Setting, "Urban"))
sr=ws_JMP %>% filter(str_detect(Service, "Sanitation")) %>% filter(str_detect(Setting, "Rural"))
#+++++++++++++++++++++++++++++++++++

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#2.0 zPatterns----
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
cx=0.8
cl=0.9
pdf(file="C:/.../Figure 3-missing.pdf",
    width=3.5, height=3.5)
#Water-urban - 
wu_z=wu[,11:14]
p.ze1=zPatterns(wu_z,label=NA,show.means = TRUE, round.means =1,axis.labels = c("Indicador", "Número de patrón"),
                cex.means=cx,cex.axis = cl,cell.labels=c("NA","No-NA"),bar.labels=TRUE)
summary(p.ze1)

#Water-rural - 
wr_z=wr[,11:14]
p.ze2=zPatterns(wr_z,label=NA,show.means = TRUE, round.means =1,axis.labels = c("Indicador", "Número de patrón"),
                cex.means=cx,cex.axis = cl,cell.labels=c("NA","No-NA"),bar.labels=TRUE)
summary(p.ze2)

#Sanitation-urban - 
su_z=su[,11:14]
p.ze3=zPatterns(su_z,label=NA,show.means = TRUE, round.means =1,axis.labels = c("Indicador", "Número de patrón"),
                cex.means=cx,cex.axis = cl,cell.labels=c("NA","No-NA"),bar.labels=TRUE)
summary(p.ze3)

#Sanitation-rural - 
sr_z=sr[,11:14]
p.ze4=zPatterns(sr_z,label=NA,show.means = TRUE, round.means =1,axis.labels = c("Indicador", "Número de patrón"),
                cex.means=cx,cex.axis = cl,cell.labels=c("NA","No-NA"),bar.labels=TRUE)
summary(p.ze4)
dev.off()
