Malaria-network-exposure / 02_descriptives.R
02_descriptives.R
Raw
### SET WORKING DIRECTORY

setwd(workdir)


### LOAD REQUIRED PACKAGES

library(lattice)


### DISTRIBUTIONS OF ALL VARIABLES BY VILLAGE, WITH OUTPUT

for (i in 1:10) {

  data <- eval(parse(text=paste0('v', i, '.', sheet.names[2])))
  
  pdf(paste0('distributions_village', i, '.pdf'))
  
  par(mfrow=c(3,3))
  for (j in 3) barplot(table(data[,j]), main=colnames(data)[j], ylim=c(0,nrow(data)), col='grey')
  for (j in 4) hist(data[,j], main=colnames(data)[j], ylim=c(0,nrow(data)), col='grey')
  for (j in 5:16) barplot(table(data[,j]), main=colnames(data)[j], ylim=c(0,nrow(data)), col='grey')
  for (j in 17:20) hist(data[,j], main=colnames(data)[j], ylim=c(0,nrow(data)), col='grey')
  for (j in c(21:24, 26:35)) barplot(table(data[,j]), main=colnames(data)[j], ylim=c(0,nrow(data)), col='grey')
  for (j in 36:43) hist(data[,j], main=colnames(data)[j], ylim=c(0,nrow(data)), col='grey')
  par(mfrow=c(1,1))
  
  dev.off()
  
}
rm(data, i, j)


### CORRELATION TABLES BY DEPENDENT VARIABLE BY VILLAGE, WITH OUTPUT

for (i in 1:10) {
  
  data <- eval(parse(text=paste0('v', i, '.', sheet.names[2])))
  
  pdf(paste0('correlations_village', i, '.pdf'))
  lattice.options(axis.padding=list(factor=0.5))
  for (j in 9:16) {
    print(levelplot(t(cor(data[,c(j,3:8,17:20,23:24,26:27,j+19,j+27)],
                          use='pairwise.complete.obs')),
                    xlab=NULL,
                    ylab=NULL,
                    main=list(paste0('Correlations, dep.var.: ', colnames(data)[j]), 
                              font=1, cex=1.5),
                    scales=list(x=list(rot=45), cex=0.6, tck=c(0,0), axs="i"),
                    col.regions=colorRampPalette(c('blue', 'green'), bias=T)(100),
                    par.settings=list(layout.heights=list(top.padding=0, bottom.padding=-1),
                                      layout.widths=list(left.padding=0, right.padding=0))))
  }
  dev.off()
  
}
rm(data, i, j)


### NETWORK EXPOSURE MEANS AND MEDIANS BY PREVENTIVE MEASURE AND VILLAGE, WITH OUTPUT

exposure.indiv  <- v.attributes[,36:43]
v.numbers <- rep(1:10, table(v.attributes[,2])[village.names])
exposure.indiv.mean <- round(aggregate(exposure.indiv, by=list(v.numbers), FUN=mean), 2)
exposure.indiv.median <- round(aggregate(exposure.indiv, by=list(v.numbers), FUN=median), 2)

towrite <- cbind(exposure.indiv.mean[,2], exposure.indiv.median[,2])
for (i in 3:9) {
  towrite <- cbind(towrite, exposure.indiv.mean[,i])
  towrite <- cbind(towrite, exposure.indiv.median[,i])
}
colnames(towrite) <- paste(c('mean:', 'median:'), rep(colnames(exposure.indiv), each=2))

write.csv(towrite, file='exposure_individual.csv')

rm(exposure.indiv, exposure.indiv.mean, exposure.indiv.median, v.numbers, towrite, i)


### HOUSEHOLD EXPOSURE MEANS AND MEDIANS BY PREVENTIVE MEASURE AND VILLAGE, WITH OUTPUT

exposure.hh  <- v.attributes[,28:35]
v.numbers <- rep(1:10, table(v.attributes[,2])[village.names])
exposure.hh.mean <- round(aggregate(exposure.hh, by=list(v.numbers), FUN=mean, na.rm=T), 2)
exposure.hh.median <- round(aggregate(exposure.hh, by=list(v.numbers), FUN=median, na.rm=T), 2)

towrite <- cbind(exposure.hh.mean[,2], exposure.hh.median[,2])
for (i in 3:9) {
  towrite <- cbind(towrite, exposure.hh.mean[,i])
  towrite <- cbind(towrite, exposure.hh.median[,i])
}
colnames(towrite) <- paste(c('mean:', 'median:'), rep(colnames(exposure.hh), each=2))

write.csv(towrite, file='exposure_household.csv')

rm(exposure.hh, exposure.hh.mean, exposure.hh.median, v.numbers, towrite, i)


### NETWORK OUTDEGREE OUTSIDE VILLAGE MEANS AND SD BY VILLAGE, WITH OUTPUT

v.numbers <- rep(1:10, table(v.attributes[,2])[village.names])
outdeg.outvill.mean <- round(aggregate(v.attributes[,19], by=list(v.numbers), 
                                       FUN=mean, na.rm=T), 2)
outdeg.outvill.sd <- round(aggregate(v.attributes[,19], by=list(v.numbers), 
                                     FUN=sd, na.rm=T), 2)
towrite <- cbind(outdeg.outvill.mean[,2], outdeg.outvill.sd[,2])
colnames(towrite) <- c('mean', 'sd')

write.csv(towrite, file='talk_outdegree_outofvillage.csv')

rm(outdeg.outvill.mean, outdeg.outvill.sd, v.numbers, towrite)


### NETWORK OUTDEGREE TO NON-RESPONDENTS MEANS AND SD BY VILLAGE, WITH OUTPUT

v.numbers <- rep(1:10, table(v.attributes[,2])[village.names])
outdeg.nonresp.mean <- round(aggregate(v.attributes[,18], by=list(v.numbers), 
                                       FUN=mean, na.rm=T), 2)
outdeg.nonresp.sd <- round(aggregate(v.attributes[,18], by=list(v.numbers), 
                                     FUN=sd, na.rm=T), 2)
towrite <- cbind(outdeg.nonresp.mean[,2], outdeg.nonresp.sd[,2])
colnames(towrite) <- c('mean', 'sd')

write.csv(towrite, file='talk_outdegree_tononrespondents.csv')

rm(outdeg.nonresp.mean, outdeg.nonresp.sd, v.numbers, towrite)