Run Bernoulli model with infant survival on cluster

# including only those who survived infancy
library(dplyr); library(brms)
setwd("/usr/users/rarslan/updated_data/")

args = commandArgs()
dataset = args[6]
uptobyear = args[7]

load(paste0(dataset, ".rdata"))

model_data = get(paste0(dataset, ".1")) %>% tbl_df %>% 
    filter(byear < uptobyear) %>%   
    select(survive1y, birth_cohort, male, maternalage.factor, paternalage.mean, paternalage, paternal_loss, maternal_loss, older_siblings, nr.siblings, last_born, idParents) %>% 
    na.omit()

if (dataset == "swed_subset_survival") {
    # switched the factor reference from later than 45 to unclear, because censoring means parental deaths later than age 45 are recorded as unclear/unknown
    model_data$maternal_loss = relevel(model_data$maternal_loss, ref = "unclear")
    model_data$paternal_loss = relevel(model_data$paternal_loss, ref = "unclear")
    dataset = "swed"
}

model = brm(    survive1y ~ paternalage + birth_cohort + male + maternalage.factor + paternalage.mean + paternal_loss + maternal_loss + older_siblings + nr.siblings + last_born + (1 | idParents),
        prior = c(set_prior("normal(0,5)", class = "b"), 
                            set_prior("student_t(3, 0, 5)", class = "sd")), 
        family = bernoulli(link = "cauchit"), data = model_data, 
        chains = 6, iter = 1000, warmup = 500, cores = 6, ranef = FALSE)

summary(model)

saveRDS(model,file = paste0("coefs/", dataset, "/e1_survive1y.rds"))