rm(list=ls())
library(Hmisc)
library(data.table)
library(rstanarm)
library(rstan)

setwd('~/research/gTiger/Article_citsci/open_code')
source('settings/parameters.r')
source('settings/paths.r')
source('settings/functions.r')

setwd(path_to_data)


# municipalities
ovi_muni_daily_probs = readRDS('ovi_muni_daily_probs.Rds')
names(ovi_muni_daily_probs)[1] = "ovi_prob"
tiga_muni_daily_probs = readRDS('muni_daily_probs.Rds')
comp_muni_daily_probs = merge(ovi_muni_daily_probs, tiga_muni_daily_probs, by=c("PROVMUN", "sea_days"), all=F)
comp_muni_daily_probs = comp_muni_daily_probs[(!is.na(ovi_prob)) & (!is.na(tiga_prob))]

saveRDS(comp_muni_daily_probs, file="comp_muni_daily_probs.Rds")


# Tigacells
tiga_daily_probs = readRDS('untracked/M_preds_dt.Rds')
tiga_daily_probs_M0a = readRDS('untracked/M_preds_dt_mosquito_alert_model_M0a.Rds')

tiga_tigacell_daily_probs = cbind(tiga_daily_probs[ , list(tiga_prob=apply(.SD,1, mean, na.rm=TRUE)), , .SDcols=2:1001], tiga_daily_probs[, c("rn", "sea_days"), with=F])
names(tiga_tigacell_daily_probs)[2] = "TigacellID"

tiga_tigacell_daily_probs_M0a = cbind(tiga_daily_probs_M0a[ , list(tiga_prob_M0a=apply(.SD,1, mean, na.rm=TRUE)), , .SDcols=2:1001], tiga_daily_probs_M0a[, c("rn", "sea_days"), with=F])
names(tiga_tigacell_daily_probs_M0a)[2] = "TigacellID"


ovi_daily_probs = readRDS('untracked/O_preds_dt.Rds')
ovi_tigacell_daily_probs = cbind(ovi_daily_probs[ , list(ovi_prob=apply(.SD,1, mean, na.rm=TRUE)), , .SDcols=2:1001], ovi_daily_probs[, c("rn", "sea_days"), with=F])
names(ovi_tigacell_daily_probs)[2] = "TigacellID"

comp_tigacell_daily_probs = merge(tiga_tigacell_daily_probs, ovi_tigacell_daily_probs, by=c("TigacellID", "sea_days"), all=F)
comp_tigacell_daily_probs = merge(tiga_tigacell_daily_probs_M0a, comp_tigacell_daily_probs, by=c("TigacellID", "sea_days"), all=F)


comp_tigacell_daily_probs = comp_tigacell_daily_probs[(!is.na(ovi_prob) & (!is.na(tiga_prob)))]

saveRDS(comp_tigacell_daily_probs, file="comp_tigacell_daily_probs.Rds")

# parameters
M = readRDS('untracked/mosquito_alert_model.Rds')
saveRDS(coef(M), 'mosquito_alert_model_result_coefs.Rds')
saveRDS(se(M), 'mosquito_alert_model_result_se.Rds')

M = readRDS('untracked/mosquito_alert_model_M0a.Rds')
saveRDS(coef(M), 'mosquito_alert_model_result_coefs_M0a.Rds')
saveRDS(se(M), 'mosquito_alert_model_result_se_M0a.Rds')


M = readRDS('untracked/mosquito_alert_model_M0b.Rds')
saveRDS(coef(M), 'mosquito_alert_model_result_coefs_M0b.Rds')
saveRDS(se(M), 'mosquito_alert_model_result_se_M0b.Rds')


M = readRDS('untracked/mosquito_alert_model_M0c.Rds')
saveRDS(coef(M), 'mosquito_alert_model_result_coefs_M0c.Rds')
saveRDS(se(M), 'mosquito_alert_model_result_se_M0c.Rds')

M = readRDS('untracked/mosquito_alert_model_M0d.Rds')
saveRDS(coef(M), 'mosquito_alert_model_result_coefs_M0d.Rds')
saveRDS(se(M), 'mosquito_alert_model_result_se_M0d.Rds')

M = readRDS('untracked/mosquito_alert_model_M0e.Rds')
saveRDS(coef(M), 'mosquito_alert_model_result_coefs_M0e.Rds')
saveRDS(se(M), 'mosquito_alert_model_result_se_M0e.Rds')



O = readRDS('untracked/ovitrap_model.Rds')
saveRDS(coef(O), 'ovitrap_model_result_ovi_tigacell_coefs.Rds')
saveRDS(se(O), 'ovitrap_model_result_ovi_tigacell_se.Rds')

O = readRDS('untracked/ovitrap_model_M0a.Rds')
saveRDS(coef(O), 'ovitrap_model_result_ovi_tigacell_coefs_M0a.Rds')
saveRDS(se(O), 'ovitrap_model_result_ovi_tigacell_se_M0a.Rds')

O = readRDS('untracked/ovitrap_model_M0b.Rds')
saveRDS(coef(O), 'ovitrap_model_result_ovi_tigacell_coefs_M0b.Rds')
saveRDS(se(O), 'ovitrap_model_result_ovi_tigacell_se_M0b.Rds')

O = readRDS('untracked/ovitrap_model_M0c.Rds')
saveRDS(coef(O), 'ovitrap_model_result_ovi_tigacell_coefs_M0c.Rds')
saveRDS(se(O), 'ovitrap_model_result_ovi_tigacell_se_M0c.Rds')

O = readRDS('untracked/ovitrap_model_M0d.Rds')
saveRDS(coef(O), 'ovitrap_model_result_ovi_tigacell_coefs_M0d.Rds')
saveRDS(se(O), 'ovitrap_model_result_ovi_tigacell_se_M0d.Rds')

O = readRDS('untracked/ovitrap_model_M0e.Rds')
saveRDS(coef(O), 'ovitrap_model_result_ovi_tigacell_coefs_M0e.Rds')
saveRDS(se(O), 'ovitrap_model_result_ovi_tigacell_se_M0e.Rds')


