library(tidyverse)
library(targets)
library(broom)
library(broom.mixed)
library(tidybayes)
library(glue)
library(brms)
library(scales)
library(kableExtra)
library(modelsummary)
library(lubridate)
library(here)

# Generated via random.org
set.seed(9936)

# Load models
withr::with_dir(here::here(), {
  source(tar_read(plot_funs))
  
  # Load big list of models
  tar_load(model_df)
  
  # Load actual model objects
  # This works, but doesn't put the models in the dependency network for some reason
  # tar_load(model_df$model)
  
  # This also works but again doesn't register correctly as dependencies
  # tar_load(starts_with("m_clpriv"))
  
  # So we do it this manual way
  tar_load(c(m_pts_baseline, m_pts_total, m_pts_total_new, 
             m_pts_advocacy, m_pts_entry, m_pts_funding, 
             m_pts_v2csreprss, 
             m_pts_baseline_rewb, m_pts_total_rewb, 
             m_pts_advocacy_rewb, m_pts_entry_rewb, m_pts_funding_rewb, 
             m_pts_v2csreprss_rewb, 
             m_clphy_baseline, m_clphy_total, m_clphy_total_new, 
             m_clphy_advocacy, m_clphy_entry, m_clphy_funding, 
             m_clphy_v2csreprss, 
             m_clphy_baseline_rewb, m_clphy_total_rewb, 
             m_clphy_advocacy_rewb, m_clphy_entry_rewb, m_clphy_funding_rewb, 
             m_clphy_v2csreprss_rewb, 
             m_clpriv_baseline, m_clpriv_total, m_clpriv_total_new, 
             m_clpriv_advocacy, m_clpriv_entry, m_clpriv_funding, 
             m_clpriv_v2csreprss, 
             m_clpriv_baseline_rewb, m_clpriv_total_rewb, 
             m_clpriv_advocacy_rewb, m_clpriv_entry_rewb, m_clpriv_funding_rewb, 
             m_clpriv_v2csreprss_rewb, 
             m_lhr_baseline, m_lhr_total, m_lhr_total_new, 
             m_lhr_advocacy, m_lhr_entry, m_lhr_funding, 
             m_lhr_v2csreprss, 
             m_pts_baseline_train, m_pts_total_train, 
             m_pts_advocacy_train, m_pts_entry_train, m_pts_funding_train, 
             m_pts_v2csreprss_train, 
             m_pts_baseline_rewb_train, m_pts_total_rewb_train, 
             m_pts_advocacy_rewb_train, m_pts_entry_rewb_train, m_pts_funding_rewb_train, 
             m_pts_v2csreprss_rewb_train, 
             m_clphy_baseline_train, m_clphy_total_train, 
             m_clphy_advocacy_train, m_clphy_entry_train, m_clphy_funding_train, 
             m_clphy_v2csreprss_train, 
             m_clphy_baseline_rewb_train, m_clphy_total_rewb_train, 
             m_clphy_advocacy_rewb_train, m_clphy_entry_rewb_train, m_clphy_funding_rewb_train, 
             m_clphy_v2csreprss_rewb_train, 
             m_clpriv_baseline_train, m_clpriv_total_train, 
             m_clpriv_advocacy_train, m_clpriv_entry_train, m_clpriv_funding_train, 
             m_clpriv_v2csreprss_train, 
             m_clpriv_baseline_rewb_train, m_clpriv_total_rewb_train, 
             m_clpriv_advocacy_rewb_train, m_clpriv_entry_rewb_train, m_clpriv_funding_rewb_train, 
             m_clpriv_v2csreprss_rewb_train))
})
computer <- "Work"

if (computer == "Work") {
  computer_details <- "2019 MacBook Pro with 16 cores and 32 GB of RAM, using Stan through brms through cmdstanr"
} else {
  computer_details <- "2016 MacBook Pro with 4 cores and 16 GB of RAM, using Stan through brms through cmdstanr"
}

We ran these models on a 2019 MacBook Pro with 16 cores and 32 GB of RAM, using Stan through brms through cmdstanr.

 

Model run times

Models for E1 (civil society laws)

Full data

models_e1 <- model_df %>% 
  filter(!str_detect(model, "v2csreprss")) %>% 
  mutate(actual_model = model %>% map(~eval(rlang::sym(.)))) %>% 
  mutate(across(c(outcome_var, explan_var, re, family), ~fct_inorder(., ordered = TRUE)))

model_time_e1 <- models_e1 %>% 
  filter(training == "Full data") %>% 
  mutate(duration = map(actual_model, ~rstan::get_elapsed_time(.$fit)),
         duration = map(duration, ~rownames_to_column(as_tibble(.)))) %>% 
  select(-actual_model) %>% 
  unnest(duration) %>% 
  mutate(model = glue("<code>{model}</code>")) %>% 
  group_by(Model = model, Outcome = outcome_var, `Main predictor` = explan_var, 
           `Random effects` = re, `Family` = family) %>% 
  summarize(`Total time (i.e. longest chain)` = as.duration(max(warmup + sample))) %>%
  ungroup() %>% 
  arrange(Outcome, `Main predictor`, `Random effects`)

total_row_e1 <- tibble(Outcome = "Total", 
                       `Total time (i.e. longest chain)` = 
                         as.duration(sum(model_time_e1$`Total time (i.e. longest chain)`)))

model_time_e1 <- model_time_e1 %>% 
  bind_rows(total_row_e1)

model_time_e1 %>% 
  select(-Outcome) %>% 
  kbl(escape = FALSE) %>% 
  pack_rows(index = table(fct_inorder(model_time_e1$Outcome))) %>% 
  kable_styling()
Model Main predictor Random effects Family Total time (i.e. longest chain)
Political terror
m_pts_baseline Baseline RE Ordered logit 409.918s (~6.83 minutes)
m_pts_baseline_rewb Baseline REWB Ordered logit 345.805s (~5.76 minutes)
m_pts_total Total legal barriers RE Ordered logit 433.103s (~7.22 minutes)
m_pts_total_rewb Total legal barriers REWB Ordered logit 4342.29s (~1.21 hours)
m_pts_total_new New legal barriers RE Ordered logit 285.252s (~4.75 minutes)
m_pts_advocacy Barriers to advocacy RE Ordered logit 428.914s (~7.15 minutes)
m_pts_advocacy_rewb Barriers to advocacy REWB Ordered logit 1552.281s (~25.87 minutes)
m_pts_entry Barriers to entry RE Ordered logit 475.187s (~7.92 minutes)
m_pts_entry_rewb Barriers to entry REWB Ordered logit 2428.61s (~40.48 minutes)
m_pts_funding Barriers to funding RE Ordered logit 402.648s (~6.71 minutes)
m_pts_funding_rewb Barriers to funding REWB Ordered logit 1985.206s (~33.09 minutes)
Physical violence
m_clphy_baseline Baseline RE OLS 53.131s
m_clphy_baseline_rewb Baseline REWB OLS 51.631s
m_clphy_total Total legal barriers RE OLS 68.392s (~1.14 minutes)
m_clphy_total_rewb Total legal barriers REWB OLS 137.281s (~2.29 minutes)
m_clphy_total_new New legal barriers RE OLS 42.051s
m_clphy_advocacy Barriers to advocacy RE OLS 50.4s
m_clphy_advocacy_rewb Barriers to advocacy REWB OLS 114.655s (~1.91 minutes)
m_clphy_entry Barriers to entry RE OLS 47.712s
m_clphy_entry_rewb Barriers to entry REWB OLS 127.251s (~2.12 minutes)
m_clphy_funding Barriers to funding RE OLS 47.802s
m_clphy_funding_rewb Barriers to funding REWB OLS 125.555s (~2.09 minutes)
Private civil liberties
m_clpriv_baseline Baseline RE OLS 48.242s
m_clpriv_baseline_rewb Baseline REWB OLS 47.843s
m_clpriv_total Total legal barriers RE OLS 66.66s (~1.11 minutes)
m_clpriv_total_rewb Total legal barriers REWB OLS 145.336s (~2.42 minutes)
m_clpriv_total_new New legal barriers RE OLS 38.383s
m_clpriv_advocacy Barriers to advocacy RE OLS 58.229s
m_clpriv_advocacy_rewb Barriers to advocacy REWB OLS 125.432s (~2.09 minutes)
m_clpriv_entry Barriers to entry RE OLS 57.738s
m_clpriv_entry_rewb Barriers to entry REWB OLS 134.677s (~2.24 minutes)
m_clpriv_funding Barriers to funding RE OLS 58.683s
m_clpriv_funding_rewb Barriers to funding REWB OLS 151.048s (~2.52 minutes)
Latent human rights
m_lhr_baseline Baseline RE OLS 28.22s
m_lhr_total Total legal barriers RE OLS 41.405s
m_lhr_total_new New legal barriers RE OLS 37.269s
m_lhr_advocacy Barriers to advocacy RE OLS 46.507s
m_lhr_entry Barriers to entry RE OLS 43.971s
m_lhr_funding Barriers to funding RE OLS 41.249s
Total
15125.967s (~4.2 hours)

Training data

model_time_e1 <- models_e1 %>% 
  filter(training == "Training") %>% 
  mutate(duration = map(actual_model, ~rstan::get_elapsed_time(.$fit)),
         duration = map(duration, ~rownames_to_column(as_tibble(.)))) %>% 
  select(-actual_model) %>% 
  unnest(duration) %>% 
  mutate(model = glue("<code>{model}</code>")) %>% 
  group_by(Model = model, Outcome = outcome_var, `Main predictor` = explan_var, 
           `Random effects` = re, `Family` = family) %>% 
  summarize(`Total time (i.e. longest chain)` = as.duration(max(warmup + sample))) %>%
  ungroup() %>% 
  arrange(Outcome, `Main predictor`, `Random effects`)

total_row_e1 <- tibble(Outcome = "Total", 
                       `Total time (i.e. longest chain)` = 
                         as.duration(sum(model_time_e1$`Total time (i.e. longest chain)`)))

model_time_e1 <- model_time_e1 %>% 
  bind_rows(total_row_e1)

model_time_e1 %>% 
  select(-Outcome) %>% 
  kbl(escape = FALSE) %>% 
  pack_rows(index = table(fct_inorder(model_time_e1$Outcome))) %>% 
  kable_styling()
Model Main predictor Random effects Family Total time (i.e. longest chain)
Political terror
m_pts_baseline_train Baseline RE Ordered logit 231.438s (~3.86 minutes)
m_pts_baseline_rewb_train Baseline REWB Ordered logit 405.144s (~6.75 minutes)
m_pts_total_train Total legal barriers RE Ordered logit 270.754s (~4.51 minutes)
m_pts_total_rewb_train Total legal barriers REWB Ordered logit 3516.09s (~58.6 minutes)
m_pts_advocacy_train Barriers to advocacy RE Ordered logit 251.297s (~4.19 minutes)
m_pts_advocacy_rewb_train Barriers to advocacy REWB Ordered logit 1420.875s (~23.68 minutes)
m_pts_entry_train Barriers to entry RE Ordered logit 399.496s (~6.66 minutes)
m_pts_entry_rewb_train Barriers to entry REWB Ordered logit 2211.18s (~36.85 minutes)
m_pts_funding_train Barriers to funding RE Ordered logit 409.446s (~6.82 minutes)
m_pts_funding_rewb_train Barriers to funding REWB Ordered logit 2371.87s (~39.53 minutes)
Physical violence
m_clphy_baseline_train Baseline RE OLS 38.541s
m_clphy_baseline_rewb_train Baseline REWB OLS 35.182s
m_clphy_total_train Total legal barriers RE OLS 54.07s
m_clphy_total_rewb_train Total legal barriers REWB OLS 104.707s (~1.75 minutes)
m_clphy_advocacy_train Barriers to advocacy RE OLS 31.266s
m_clphy_advocacy_rewb_train Barriers to advocacy REWB OLS 104.991s (~1.75 minutes)
m_clphy_entry_train Barriers to entry RE OLS 42.59s
m_clphy_entry_rewb_train Barriers to entry REWB OLS 94.811s (~1.58 minutes)
m_clphy_funding_train Barriers to funding RE OLS 32.453s
m_clphy_funding_rewb_train Barriers to funding REWB OLS 128.664s (~2.14 minutes)
Private civil liberties
m_clpriv_baseline_train Baseline RE OLS 41.352s
m_clpriv_baseline_rewb_train Baseline REWB OLS 41.835s
m_clpriv_total_train Total legal barriers RE OLS 59.934s
m_clpriv_total_rewb_train Total legal barriers REWB OLS 133.318s (~2.22 minutes)
m_clpriv_advocacy_train Barriers to advocacy RE OLS 39.844s
m_clpriv_advocacy_rewb_train Barriers to advocacy REWB OLS 102.351s (~1.71 minutes)
m_clpriv_entry_train Barriers to entry RE OLS 39.159s
m_clpriv_entry_rewb_train Barriers to entry REWB OLS 125.315s (~2.09 minutes)
m_clpriv_funding_train Barriers to funding RE OLS 38.165s
m_clpriv_funding_rewb_train Barriers to funding REWB OLS 100.147s (~1.67 minutes)
Total
12876.285s (~3.58 hours)

Models for E2 (civil society environment)

Full data

models_e2 <- model_df %>% 
  filter(str_detect(model, "baseline") | str_detect(model, "v2csreprss")) %>% 
  mutate(actual_model = model %>% map(~eval(rlang::sym(.)))) %>% 
  mutate(across(c(outcome_var, explan_var, re, family), ~fct_inorder(., ordered = TRUE)))

model_time_e2 <- models_e2 %>% 
  filter(training == "Full data") %>% 
  mutate(duration = map(actual_model, ~rstan::get_elapsed_time(.$fit)),
         duration = map(duration, ~rownames_to_column(as_tibble(.)))) %>% 
  select(-actual_model) %>% 
  unnest(duration) %>% 
  mutate(model = glue("<code>{model}</code>")) %>% 
  group_by(Model = model, Outcome = outcome_var, `Main predictor` = explan_var, 
           `Random effects` = re, `Family` = family) %>% 
  summarize(`Total time (i.e. longest chain)` = as.duration(max(warmup + sample))) %>%
  ungroup() %>% 
  arrange(Outcome, `Main predictor`, `Random effects`)

total_row_e2 <- tibble(Outcome = "Total", 
                       `Total time (i.e. longest chain)` = 
                         as.duration(sum(model_time_e2$`Total time (i.e. longest chain)`)))

model_time_e2 <- model_time_e2 %>% 
  bind_rows(total_row_e2)

model_time_e2 %>% 
  select(-Outcome) %>% 
  kbl(escape = FALSE) %>% 
  pack_rows(index = table(fct_inorder(model_time_e2$Outcome))) %>% 
  kable_styling()
Model Main predictor Random effects Family Total time (i.e. longest chain)
Political terror
m_pts_baseline Baseline RE Ordered logit 409.918s (~6.83 minutes)
m_pts_baseline_rewb Baseline REWB Ordered logit 345.805s (~5.76 minutes)
m_pts_v2csreprss Civil society repression RE Ordered logit 424.237s (~7.07 minutes)
m_pts_v2csreprss_rewb Civil society repression REWB Ordered logit 4393.15s (~1.22 hours)
Physical violence
m_clphy_baseline Baseline RE OLS 53.131s
m_clphy_baseline_rewb Baseline REWB OLS 51.631s
m_clphy_v2csreprss Civil society repression RE OLS 67.857s (~1.13 minutes)
m_clphy_v2csreprss_rewb Civil society repression REWB OLS 161.387s (~2.69 minutes)
Private civil liberties
m_clpriv_baseline Baseline RE OLS 48.242s
m_clpriv_baseline_rewb Baseline REWB OLS 47.843s
m_clpriv_v2csreprss Civil society repression RE OLS 48.277s
m_clpriv_v2csreprss_rewb Civil society repression REWB OLS 173.8s (~2.9 minutes)
Latent human rights
m_lhr_baseline Baseline RE OLS 28.22s
m_lhr_v2csreprss Civil society repression RE OLS 46.927s
Total
6300.425s (~1.75 hours)

Training data

model_time_e2 <- models_e2 %>% 
  filter(training == "Training") %>% 
  mutate(duration = map(actual_model, ~rstan::get_elapsed_time(.$fit)),
         duration = map(duration, ~rownames_to_column(as_tibble(.)))) %>% 
  select(-actual_model) %>% 
  unnest(duration) %>% 
  mutate(model = glue("<code>{model}</code>")) %>% 
  group_by(Model = model, Outcome = outcome_var, `Main predictor` = explan_var, 
           `Random effects` = re, `Family` = family) %>% 
  summarize(`Total time (i.e. longest chain)` = as.duration(max(warmup + sample))) %>%
  ungroup() %>% 
  arrange(Outcome, `Main predictor`, `Random effects`)

total_row_e2 <- tibble(Outcome = "Total", 
                       `Total time (i.e. longest chain)` = 
                         as.duration(sum(model_time_e2$`Total time (i.e. longest chain)`)))

model_time_e2 <- model_time_e2 %>% 
  bind_rows(total_row_e2)

model_time_e2 %>% 
  select(-Outcome) %>% 
  kbl(escape = FALSE) %>% 
  pack_rows(index = table(fct_inorder(model_time_e2$Outcome))) %>% 
  kable_styling()
Model Main predictor Random effects Family Total time (i.e. longest chain)
Political terror
m_pts_baseline_train Baseline RE Ordered logit 231.438s (~3.86 minutes)
m_pts_baseline_rewb_train Baseline REWB Ordered logit 405.144s (~6.75 minutes)
m_pts_v2csreprss_train Civil society repression RE Ordered logit 479.087s (~7.98 minutes)
m_pts_v2csreprss_rewb_train Civil society repression REWB Ordered logit 2043.508s (~34.06 minutes)
Physical violence
m_clphy_baseline_train Baseline RE OLS 38.541s
m_clphy_baseline_rewb_train Baseline REWB OLS 35.182s
m_clphy_v2csreprss_train Civil society repression RE OLS 51.804s
m_clphy_v2csreprss_rewb_train Civil society repression REWB OLS 151.724s (~2.53 minutes)
Private civil liberties
m_clpriv_baseline_train Baseline RE OLS 41.352s
m_clpriv_baseline_rewb_train Baseline REWB OLS 41.835s
m_clpriv_v2csreprss_train Civil society repression RE OLS 47.212s
m_clpriv_v2csreprss_rewb_train Civil society repression REWB OLS 137.284s (~2.29 minutes)
Total
3704.111s (~1.03 hours)

 

Actual code

All the models are run with a targets pipeline with dataset-specific functions that live in these files:

  • R/models_pts.R (for PTS-based models)
  • R/models_clphy.R (for V-Dem physical violence-based models)
  • R/models_clpriv.R (for V-Dem private civil liberties-based models)
  • R/models_lhr.R (for latent human rights-based models)

To keep the analysis relatively self contained here in the analysis notebook, and to make it so there’s no need to hunt through the GitHub repository to find the actual code, here’s that code:

R/models_pts.R

# Settings ----------------------------------------------------------------

# Run this inside each model function instead of outside so that future workers
# use these options internally
pts_setup <- function() {
  options(worker_options)

  # Settings
  CHAINS <- 4
  ITER <- 2000
  WARMUP <- 1000
  BAYES_SEED <- 2009  # From random.org

  # Priors
  priors_vague <- c(set_prior("normal(0, 10)", class = "Intercept"),
                    set_prior("normal(0, 3)", class = "b"),
                    set_prior("cauchy(0, 1)", class = "sd"))

  return(list(chains = CHAINS, iter = ITER, warmup = WARMUP, seed = BAYES_SEED,
              priors_vague = priors_vague))
}


# Regular models ----------------------------------------------------------

f_pts_baseline <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ PTS_factor +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_total <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ barriers_total + barriers_total_lag1 +
         PTS_factor +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_total_new <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ barriers_total_new + barriers_total_new_lag1 +
         PTS_factor +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_advocacy <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ advocacy + advocacy_lag1 +
         PTS_factor +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_entry <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ entry + entry_lag1 +
         PTS_factor +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_funding <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ funding + funding_lag1 +
         PTS_factor +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_v2csreprss <- function(dat) {
  pts_settings <- pts_setup()

  model <- brm(
    bf(PTS_factor_lead1 ~ v2csreprss + v2csreprss_lag1 +
         PTS_factor +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}


# REWB models -------------------------------------------------------------

f_pts_baseline_rewb <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ PTS_factor +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_total_rewb <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ barriers_total_within + barriers_total_between +
         barriers_total_lag1_within + barriers_total_lag1_between +
         PTS_factor +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_advocacy_rewb <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ advocacy_within + advocacy_between +
         advocacy_lag1_within + advocacy_lag1_between +
         PTS_factor +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_entry_rewb <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ entry_within + entry_between +
         entry_lag1_within + entry_lag1_between +
         PTS_factor +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_funding_rewb <- function(dat) {
  pts_settings <- pts_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(PTS_factor_lead1 ~ funding_within + funding_between +
         funding_lag1_within + funding_lag1_between +
         PTS_factor +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

f_pts_v2csreprss_rewb <- function(dat) {
  pts_settings <- pts_setup()

  model <- brm(
    bf(PTS_factor_lead1 ~ v2csreprss_within + v2csreprss_between +
         v2csreprss_lag1_within + v2csreprss_lag1_between +
         PTS_factor +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = cumulative(),
    prior = pts_settings$priors_vague,
    data = dat,
    chains = pts_settings$chains, iter = pts_settings$iter,
    warmup = pts_settings$warmup, seed = pts_settings$seed)

  return(model)
}

R/models_clphy.R

# Settings ----------------------------------------------------------------

clphy_setup <- function() {
  options(worker_options)

  # Settings
  CHAINS <- 4
  ITER <- 2000
  WARMUP <- 1000
  BAYES_SEED <- 4045  # From random.org

  # Priors
  priors_vague <- c(set_prior("normal(0, 10)", class = "Intercept"),
                    set_prior("normal(0, 3)", class = "b"),
                    set_prior("cauchy(0, 1)", class = "sd"))

  return(list(chains = CHAINS, iter = ITER, warmup = WARMUP, seed = BAYES_SEED,
              priors_vague = priors_vague))
}


# Regular models ----------------------------------------------------------

f_clphy_baseline <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ v2x_clphy +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_total <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ barriers_total + barriers_total_lag1 +
         v2x_clphy +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_total_new <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ barriers_total_new + barriers_total_new_lag1 +
         v2x_clphy +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_advocacy <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ advocacy + advocacy_lag1 +
         v2x_clphy +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_entry <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ entry + entry_lag1 +
         v2x_clphy +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_funding <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ funding + funding_lag1 +
         v2x_clphy +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_v2csreprss <- function(dat) {
  clphy_settings <- clphy_setup()

  model <- brm(
    bf(v2x_clphy_lead1 ~ v2csreprss + v2csreprss_lag1 +
         v2x_clphy +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}


# REWB models -------------------------------------------------------------

f_clphy_baseline_rewb <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ v2x_clphy +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_total_rewb <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ barriers_total_within + barriers_total_between +
         barriers_total_lag1_within + barriers_total_lag1_between +
         v2x_clphy +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_advocacy_rewb <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ advocacy_within + advocacy_between +
         advocacy_lag1_within + advocacy_lag1_between +
         v2x_clphy +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_entry_rewb <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ entry_within + entry_between +
         entry_lag1_within + entry_lag1_between +
         v2x_clphy +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_funding_rewb <- function(dat) {
  clphy_settings <- clphy_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clphy_lead1 ~ funding_within + funding_between +
         funding_lag1_within + funding_lag1_between +
         v2x_clphy +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

f_clphy_v2csreprss_rewb <- function(dat) {
  clphy_settings <- clphy_setup()

  model <- brm(
    bf(v2x_clphy_lead1 ~ v2csreprss_within + v2csreprss_between +
         v2csreprss_lag1_within + v2csreprss_lag1_between +
         v2x_clphy +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clphy_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clphy_settings$chains, iter = clphy_settings$iter,
    warmup = clphy_settings$warmup, seed = clphy_settings$seed)

  return(model)
}

R/models_clpriv.R

# Settings ----------------------------------------------------------------

clpriv_setup <- function() {
  options(worker_options)

  # Settings
  CHAINS <- 4
  ITER <- 2000
  WARMUP <- 1000
  BAYES_SEED <- 9324  # From random.org

  # Priors
  priors_vague <- c(set_prior("normal(0, 10)", class = "Intercept"),
                    set_prior("normal(0, 3)", class = "b"),
                    set_prior("cauchy(0, 1)", class = "sd"))

  return(list(chains = CHAINS, iter = ITER, warmup = WARMUP, seed = BAYES_SEED,
              priors_vague = priors_vague))
}


# Regular models ----------------------------------------------------------

f_clpriv_baseline <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ v2x_clpriv +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_total <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ barriers_total + barriers_total_lag1 +
         v2x_clpriv +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_total_new <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ barriers_total_new + barriers_total_new_lag1 +
         v2x_clpriv +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_advocacy <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ advocacy + advocacy_lag1 +
         v2x_clpriv +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_entry <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ entry + entry_lag1 +
         v2x_clpriv +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_funding <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ funding + funding_lag1 +
         v2x_clpriv +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_v2csreprss <- function(dat) {
  clpriv_settings <- clpriv_setup()

  model <- brm(
    bf(v2x_clpriv_lead1 ~ v2csreprss + v2csreprss_lag1 +
         v2x_clpriv +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}


# REWB models -------------------------------------------------------------

f_clpriv_baseline_rewb <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ v2x_clpriv +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_total_rewb <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ barriers_total_within + barriers_total_between +
         barriers_total_lag1_within + barriers_total_lag1_between +
         v2x_clpriv +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_advocacy_rewb <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ advocacy_within + advocacy_between +
         advocacy_lag1_within + advocacy_lag1_between +
         v2x_clpriv +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_entry_rewb <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ entry_within + entry_between +
         entry_lag1_within + entry_lag1_between +
         v2x_clpriv +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_funding_rewb <- function(dat) {
  clpriv_settings <- clpriv_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(v2x_clpriv_lead1 ~ funding_within + funding_between +
         funding_lag1_within + funding_lag1_between +
         v2x_clpriv +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

f_clpriv_v2csreprss_rewb <- function(dat) {
  clpriv_settings <- clpriv_setup()

  model <- brm(
    bf(v2x_clpriv_lead1 ~ v2csreprss_within + v2csreprss_between +
         v2csreprss_lag1_within + v2csreprss_lag1_between +
         v2x_clpriv +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = clpriv_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = clpriv_settings$chains, iter = clpriv_settings$iter,
    warmup = clpriv_settings$warmup, seed = clpriv_settings$seed)

  return(model)
}

R/models_lhr.R

# Settings ----------------------------------------------------------------

lhr_setup <- function() {
  options(worker_options)

  # Settings
  CHAINS <- 4
  ITER <- 2000
  WARMUP <- 1000
  BAYES_SEED <- 4045  # From random.org

  # Priors
  priors_vague <- c(set_prior("normal(0, 10)", class = "Intercept"),
                    set_prior("normal(0, 3)", class = "b"),
                    set_prior("cauchy(0, 1)", class = "sd"))

  return(list(chains = CHAINS, iter = ITER, warmup = WARMUP, seed = BAYES_SEED,
              priors_vague = priors_vague))
}


# Regular models ----------------------------------------------------------

f_lhr_baseline <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ latent_hr_mean +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_total <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ barriers_total + barriers_total_lag1 +
         latent_hr_mean +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_total_new <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ barriers_total_new + barriers_total_new_lag1 +
         latent_hr_mean +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_advocacy <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ advocacy + advocacy_lag1 +
         latent_hr_mean +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_entry <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ entry + entry_lag1 +
         latent_hr_mean +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_funding <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ funding + funding_lag1 +
         latent_hr_mean +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_v2csreprss <- function(dat) {
  lhr_settings <- lhr_setup()

  model <- brm(
    bf(latent_hr_mean_lead1 ~ v2csreprss + v2csreprss_lag1 +
         latent_hr_mean +
         v2x_polyarchy +
         gdpcap_log +
         un_trade_pct_gdp +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}


# REWB models -------------------------------------------------------------

f_lhr_baseline_rewb <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ latent_hr_mean +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_total_rewb <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ barriers_total_within + barriers_total_between +
         barriers_total_lag1_within + barriers_total_lag1_between +
         latent_hr_mean +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_advocacy_rewb <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ advocacy_within + advocacy_between +
         advocacy_lag1_within + advocacy_lag1_between +
         latent_hr_mean +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_entry_rewb <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ entry_within + entry_between +
         entry_lag1_within + entry_lag1_between +
         latent_hr_mean +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_funding_rewb <- function(dat) {
  lhr_settings <- lhr_setup()

  dat <- dat %>% filter(laws)

  model <- brm(
    bf(latent_hr_mean_lead1 ~ funding_within + funding_between +
         funding_lag1_within + funding_lag1_between +
         latent_hr_mean +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}

f_lhr_v2csreprss_rewb <- function(dat) {
  lhr_settings <- lhr_setup()

  model <- brm(
    bf(latent_hr_mean_lead1 ~ v2csreprss_within + v2csreprss_between +
         v2csreprss_lag1_within + v2csreprss_lag1_between +
         latent_hr_mean +
         v2x_polyarchy_within + v2x_polyarchy_between +
         gdpcap_log_within + gdpcap_log_between +
         un_trade_pct_gdp_within + un_trade_pct_gdp_between +
         armed_conflict +
         (1 | gwcode)
    ),
    family = gaussian(),
    prior = lhr_settings$priors_vague,
    control = list(adapt_delta = 0.9),
    data = dat,
    chains = lhr_settings$chains, iter = lhr_settings$iter,
    warmup = lhr_settings$warmup, seed = lhr_settings$seed)

  return(model)
}
LS0tCnRpdGxlOiAiTW9kZWwgZGV0YWlscyBhbmQgZGlhZ25vc3RpY3MiCmF1dGhvcjogIlN1cGFybmEgQ2hhdWRocnkgYW5kIEFuZHJldyBIZWlzcyIKZGF0ZTogIkxhc3QgcnVuOiBgciBmb3JtYXQoU3lzLnRpbWUoKSwgJyVGJylgIgpvdXRwdXQ6IAogIGh0bWxfZG9jdW1lbnQ6CiAgICBjb2RlX2ZvbGRpbmc6IGhpZGUKZWRpdG9yX29wdGlvbnM6IAogIGNodW5rX291dHB1dF90eXBlOiBjb25zb2xlCi0tLQoKYGBge3Igc2V0dXAsIGluY2x1ZGU9RkFMU0V9CmxpYnJhcnkoa25pdHIpCmxpYnJhcnkoa2FibGVFeHRyYSkKa25pdF9wcmludC5kYXRhLmZyYW1lIDwtIGZ1bmN0aW9uKHgsIC4uLikgewogIHJlcyA8LSBwYXN0ZShjKCcnLCAnJywga2FibGVfc3R5bGluZyhrYWJsZSh4LCBib29rdGFicyA9IFRSVUUpKSksIGNvbGxhcHNlID0gJ1xuJykKICBhc2lzX291dHB1dChyZXMpCn0KCnJlZ2lzdGVyUzNtZXRob2QoImtuaXRfcHJpbnQiLCAiZGF0YS5mcmFtZSIsIGtuaXRfcHJpbnQuZGF0YS5mcmFtZSkKcmVnaXN0ZXJTM21ldGhvZCgia25pdF9wcmludCIsICJncm91cGVkX2RmIiwga25pdF9wcmludC5kYXRhLmZyYW1lKQoKa25pdHI6Om9wdHNfY2h1bmskc2V0KGZpZy5yZXRpbmEgPSAzLA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