Run model on cluster

this script runs a model on our scientific computing cluster

library(dplyr); library(brms)
setwd("/usr/users/rarslan/updated_data/")

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

model_formula = children ~ paternalage + birth_cohort + male + maternalage.factor + paternalage.mean + paternal_loss + maternal_loss + older_siblings + nr.siblings + last_born + (1 | idParents)

if (dataset == "swed") {
    load("swed1.rdata")
    model_prior = c(set_prior("normal(0,5)", class = "b"),
                                    set_prior("student_t(3, 0, 5)", class = "sd"))
    model_family = poisson()
} else {
    load(paste0(dataset, ".rdata"))
    model_prior = c(set_prior("normal(0,5)", class = "b"),
                                    set_prior("normal(0,5)", class = "b", nlpar = "hu"),
                                    set_prior("student_t(3, 0, 5)", class = "sd"))
    model_formula_hu = update(model_formula,  hu ~ . )
    model_formula = bf(model_formula, model_formula_hu)
    model_family = hurdle_poisson()
}

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

model = brm( model_formula,
                         prior = model_prior, 
                         family = model_family, data = model_data, 
                         chains = 6, iter = 800, warmup = 300, cores = 6, ranef = TRUE)

summary(model)

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