ncol <- 20
n <- ncol^2
listw <- sim_grid_listw(ncol, ncol) # Create spatial weights for a grid
u <- make_error(n, method = "normal") # Simulate random errors
x <- make_x(n, method = "uniform") # Generate x variables
xb <- make_xb(x, c(1, 2)) # Calculate xb using the original x and coefficients
wx <- make_wx(x, listw) # Generate spatially lagged x variables
wxg <- make_wxg(wx, 0.5) # Calculate the effect of the spatial lags
y <- sim_slx(u, xb, wxg) # Simulate the SLX model outcome
df <- data.frame(y, x)
spatialreg::lmSLX(y ~ ., data = df, listw = listw) # Estimate the SLX model
Run the code above in your browser using DataLab