### SHOULD BE RUN ON CLUSTER ###

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

ncores = 4

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

setwd(path_to_data)

rstan_options(auto_write = TRUE)
options(mc.cores = ncores)

D = readRDS('mosquito_alert_model_dataset.Rds')

D = data.frame(D)

D$trials = 1

M = stan_glmer(cbind(any_reliable_reports, trials-any_reliable_reports)~SE_adj_log_std2 + sea_days_std2 + sea_days_std2_sq + (1 | TigacellID),data=D, family=binomial(link = "logit"), prior = student_t(df = 1, location=0, scale=2.5), prior_intercept = student_t(df = 1, location=0, scale=2.5), chains = ncores, cores = ncores)

summary(M)

saveRDS(M, 'untracked/mosquito_alert_model_M0d.Rds')

M_loo = loo(M)

saveRDS(M_loo, 'untracked/mosquito_alert_model_M0d_loo.Rds')


