## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----load---------------------------------------------------------------------
library(slxr)
data(defense_burden)

names(defense_burden)
dim(defense_burden$data)
dim(defense_burden$W_contig)

## ----single-w-----------------------------------------------------------------
W_contig <- slx_weights(style = "custom",
                        matrix = defense_burden$W_contig,
                        row_standardize = FALSE)

fit <- slx(ch_milex ~ milex_tm1 + log_pop_tm1 + civilwar_tm1 +
                      total_wars_tm1 + alliance_us +
                      ch_milex_us + ch_milex_ussr,
           data = defense_burden$data,
           W = W_contig,
           lag = "total_wars_tm1")

summary(fit)

## ----single-w-effects---------------------------------------------------------
slx_effects(fit)

## ----multi-w------------------------------------------------------------------
W_alliance <- slx_weights(style = "custom",
                          matrix = defense_burden$W_alliance,
                          row_standardize = FALSE)
W_defense  <- slx_weights(style = "custom",
                          matrix = defense_burden$W_defense,
                          row_standardize = FALSE)

fit_multi <- slx(
  ch_milex ~ milex_tm1 + log_pop_tm1 + civilwar_tm1 +
             total_wars_tm1 + alliance_us +
             ch_milex_us + ch_milex_ussr,
  data = defense_burden$data,
  spatial = list(
    civilwar_tm1   = W_contig,
    total_wars_tm1 = list(contig = W_contig, alliance = W_alliance),
    milex_tm1      = list(contig = W_contig, defense  = W_defense)
  )
)

slx_effects(fit_multi)

## ----plot, fig.width = 7, fig.height = 4--------------------------------------
library(ggplot2)
slx_plot_effects(fit_multi, types = c("indirect", "total"))

## ----broom, eval = FALSE------------------------------------------------------
# tidy(fit_multi)
# glance(fit_multi)
# 
# modelsummary::modelsummary(
#   list("Contiguity only" = fit, "Multi-W" = fit_multi),
#   statistic = "conf.int"
# )

