Malaria-network-exposure / 04_saom_scoretests.R
04_saom_scoretests.R
Raw

# save score test results from all villages in a single object

score.tests <- matrix(NA, 0, 6)
colnames(score.tests) <- c('Village', 'Effect name', 'Parameter value', 'Chi-square', 'df', 'p')

for (j in 1:10) {
  
  if (!fix.individual & !fix.oplead & !fix.netsize & !fix.netexp & !fix.hhexp) 
    objname <- paste0('saom.v', j, '.rate', rates, '.7full')
  if (!fix.individual & !fix.oplead & !fix.netsize & fix.netexp & !fix.hhexp) 
    objname <- paste0('saom.v', j, '.rate', rates, '.6hhexp')
  if (!fix.individual & !fix.oplead & !fix.netsize & !fix.netexp & fix.hhexp) 
    objname <- paste0('saom.v', j, '.rate', rates, '.5netexp')
  if (!fix.individual & !fix.oplead & !fix.netsize & fix.netexp & fix.hhexp) 
    objname <- paste0('saom.v', j, '.rate', rates, '.4netsize')
  if (!fix.individual & !fix.oplead & fix.netsize & fix.netexp & fix.hhexp) 
    objname <- paste0('saom.v', j, '.rate', rates, '.3oplead')
  if (!fix.individual & fix.oplead & fix.netsize & fix.netexp & fix.hhexp) 
    objname <- paste0('saom.v', j, '.rate', rates, '.2individual')
  if (fix.individual & fix.oplead & fix.netsize & fix.netexp & fix.hhexp) 
    objname <- paste0('saom.v', j, '.rate', rates, '.1baseline')
  
  village.result <- eval(parse(text=objname)) 
  
  which.test <- which(village.result$test)
  if (length(which.test)>0) {
    tostore <- matrix(NA, length(which.test), 6)
    tostore[,1] <- j
    for (i in 1:length(which.test)) {
      test.result <- score.Test(village.result, which.test[i])
      tostore[i,2] <- test.result$efnames
      tostore[i,3] <- village.result$theta[which.test[i]]
      tostore[i,4] <- test.result$chisquare
      tostore[i,5] <- test.result$df
      tostore[i,6] <- test.result$pvalue
    }
    score.tests <- rbind(score.tests, tostore)
  }
}
rm(j, objname, i, village.result, which.test, test.result, tostore)


# rename object according to rate parameter and effect subset fixing
if (!fix.individual & !fix.oplead & !fix.netsize & !fix.netexp & !fix.hhexp) 
  objname <- paste0('saom.scoretests.rate', rates, '.', saom.models[7])
if (!fix.individual & !fix.oplead & !fix.netsize & fix.netexp & !fix.hhexp) 
  objname <- paste0('saom.scoretests.rate', rates, '.', saom.models[6])
if (!fix.individual & !fix.oplead & !fix.netsize & !fix.netexp & fix.hhexp) 
  objname <- paste0('saom.scoretests.rate', rates, '.', saom.models[5])
if (!fix.individual & !fix.oplead & !fix.netsize & fix.netexp & fix.hhexp) 
  objname <- paste0('saom.scoretests.rate', rates, '.', saom.models[4])
if (!fix.individual & !fix.oplead & fix.netsize & fix.netexp & fix.hhexp) 
  objname <- paste0('saom.scoretests.rate', rates, '.', saom.models[3])
if (!fix.individual & fix.oplead & fix.netsize & fix.netexp & fix.hhexp) 
  objname <- paste0('saom.scoretests.rate', rates, '.', saom.models[2])
if (fix.individual & fix.oplead & fix.netsize & fix.netexp & fix.hhexp) 
  objname <- paste0('saom.scoretests.rate', rates, '.', saom.models[1])

assign(objname, score.tests)


# print results to file if requested

if (print.results) write.csv(score.tests, file=paste0(objname, '.csv'))

rm(objname, score.tests)
rm(fix.individual, fix.oplead, fix.netsize, fix.netexp, fix.hhexp)