##
## SDLM data
##
parts <- prep_sar_data2(row = 9, col = 9, quiet = TRUE)
W <- parts$W
x <- sim_sar(w=W, rho=.6)
Wx <- (W %*% x)[,1]
mu <- .5 * x + .25 * Wx
y <- sim_sar(w=W, rho=0.6, mu = mu, type = "SLM")
dat <- cbind(y, x)
# impacts per the above parameters
spill(0.5, 0.25, 0.6, W)
##
## impacts for SDLM
##
fit <- stan_sar(y ~ x, data = dat, sar = parts,
type = "SDLM", iter = 500,
slim = TRUE, quiet = TRUE)
# impacts (posterior distribution)
impax <- impacts(fit)
print(impax)
# plot posterior distributions
og = par(mfrow = c(1, 3),
mar = c(3, 3, 1, 1))
S <- impax$samples[[1]]
hist(S[,1], main = 'Direct')
hist(S[,2], main = 'Indirect')
hist(S[,3], main = 'Total')
par(og)
##
## The approximate method
##
# High rho value requires more K; rho^K must be near zero
Ks <- c(10, 15, 20, 30, 35, 40)
print(cbind(Ks, 0.9^Ks))
# understand sensitivity of results to K when rho is high
spill(0.5, -0.25, 0.9, W, approx = TRUE, K = 10)
spill(0.5, -0.25, 0.9, W, approx = TRUE, K = 20)
spill(0.5, -0.25, 0.9, W, approx = TRUE, K = 30)
spill(0.5, -0.25, 0.9, W, approx = TRUE, K = 50)
# the correct results
spill(0.5, -0.25, 0.9, W, approx = FALSE)
# moderate and low rho values are fine with smaller K
spill(0.5, -0.25, 0.7, W, approx = TRUE, K = 15)
spill(0.5, -0.25, 0.7, W, approx = FALSE)
Run the code above in your browser using DataLab