Run Bernoulli model with survival to 15 on cluster

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(survive1y == 1) %>% 
    filter(byear < uptobyear) %>%   
    select(survive1y, surviveR, 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(    surviveR ~ 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 = 1500, warmup = 1000, cores = 6, ranef = FALSE)

summary(model)

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