Run Poisson model on cluster

including only those who ever married, controlling number of spouses interacted with sex

library(dplyr); library(brms)
setwd("/usr/users/rarslan/updated_data/")
args = commandArgs()
dataset = args[6]
uptobyear = args[7]

if (dataset == "swed") {
    load("swed1.rdata")
} else {
    load(paste0(dataset, ".rdata"))
}

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


model = brm(    children ~ paternalage + birth_cohort + spouses * 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 = poisson(), data = model_data, 
                         chains = 6, iter = 1500, warmup = 500, cores = 6, ranef = FALSE)

summary(model)

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