| Title: | Simulate Spatial Data Generation Processes |
|---|---|
| Description: | Provides functionality for simulating data generation processes across various spatial regression models, conceptually aligned with the 'dgp' module of the 'Python' library 'spreg' <https://pysal.org/spreg/api.html#dgp>. |
| Authors: | Wenbo Lyu [aut, cre] (ORCID: <https://orcid.org/0009-0002-6003-3800>), Josiah Parry [aut] (ORCID: <https://orcid.org/0000-0001-9910-865X>), Edzer Pebesma [ctb] (ORCID: <https://orcid.org/0000-0001-8049-7069>) |
| Maintainer: | Wenbo Lyu <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.0 |
| Built: | 2026-10-18 21:34:59 UTC |
| Source: | https://github.com/josiahparry/spdgp |
Simulate an error term
make_error( n = 10, mu = 0, var = 1, method = c("normal", "laplace", "cauchy", "lognormal") )make_error( n = 10, mu = 0, var = 1, method = c("normal", "laplace", "cauchy", "lognormal") )
n |
the number of values to simulate. |
mu |
the sample average. |
var |
the sample variance. The |
method |
must be one of |
"normal": fit with rnorm()
"laplace": fit with smoothmest::rdoublex()
"cauchy": fit with rcauchy()
"lognormal": fit with rlnorm()
A numeric vector
Creates a square grid with ncol and nrow dimensions.
make_square_grid(nrow, ncol = nrow)make_square_grid(nrow, ncol = nrow)
nrow |
the number of rows in the grid. |
ncol |
defaults to |
An sfc object by sf package.
make_square_grid(3, 2)make_square_grid(3, 2)
Given a dataframe of numeric values and a spatial weights matrix, calculate the spatial lag of each variable.
make_wx(x, listw, order = NULL)make_wx(x, listw, order = NULL)
x |
a |
listw |
a |
order |
unused. |
A data.frame of the spatially lagged variables.
listw <- sim_grid_listw(10, 10) x_vars <- make_x(100, mu = c(0.5, 1.2), var = c(1, 0.5)) res <- make_wx(x_vars, listw) head(res)listw <- sim_grid_listw(10, 10) x_vars <- make_x(100, mu = c(0.5, 1.2), var = c(1, 0.5)) res <- make_wx(x_vars, listw) head(res)
This function computes the contribution of spatially lagged X variables based on
provided coefficients. The function takes the spatially lagged variables (wx, see make_wx())
and multiplies them by their corresponding regression coefficients (gamma), returning
the predicted influence of the spatial lags. Only spatial lags are considered;
the original X variables are not included in this calculation.
make_wxg(wx, gamma)make_wxg(wx, gamma)
wx |
a matrix of spatially lagged x variables. |
gamma |
a vector of coefficients for the spatially lagged x variables. Its length must match the number of columns in wx. |
A numeric vector
grid <- make_square_grid(5) listw <- spdep::nb2listw(spdep::poly2nb(grid)) x <- make_x(25, c(0,1), c(1,4)) wx <- make_wx(x, listw) gamma <- c(1.75, 0.4) make_wxg(wx, gamma)grid <- make_square_grid(5) listw <- spdep::nb2listw(spdep::poly2nb(grid)) x <- make_x(25, c(0,1), c(1,4)) wx <- make_wx(x, listw) gamma <- c(1.75, 0.4) make_wxg(wx, gamma)
Simulates independent variables.
make_x_bivariate(n = 5, mu = 1, cor = 0.25, var = c(1, 1)) make_x_uniform(n = 5, var = 1) make_x_normal(n = 5, mu = 0, var = 1) make_x( n = 5, mu = 0, var = 1, cor = 0, method = c("uniform", "normal", "bivnormal") )make_x_bivariate(n = 5, mu = 1, cor = 0.25, var = c(1, 1)) make_x_uniform(n = 5, var = 1) make_x_normal(n = 5, mu = 0, var = 1) make_x( n = 5, mu = 0, var = 1, cor = 0, method = c("uniform", "normal", "bivnormal") )
n |
the number of values to simulate. |
mu |
the sample average. |
cor |
correlation between bivariate normal |
var |
the sample variance. The |
method |
must be one of |
A data.frame of the simulated independent variables.
make_x(10, mu = c(0.5, 1.2), var = c(1, 0.5))make_x(10, mu = c(0.5, 1.2), var = c(1, 0.5))
This function calculates predicted x values based on regression coefficients.
The results of this function can be passed to other sim_*() functions.
make_xb(x, beta)make_xb(x, beta)
x |
a |
beta |
a vector of the beta coefficients for each of the variables. There must be
|
A numeric vector
x <- make_x(25, c(0,1), c(1,4)) betas <- c(1, 1.5, -2) make_xb(x, betas)x <- make_x(25, c(0,1), c(1,4)) betas <- c(1, 1.5, -2) make_xb(x, betas)
Simulate the Spatial Durbin Model
sim_durbin(u, xb, wxg, listw, rho = 0.5)sim_durbin(u, xb, wxg, listw, rho = 0.5)
u |
an error vector |
xb |
predicted x values as calculated by |
wxg |
predicted spatial lag effect as calculated by |
listw |
a |
rho |
the spatial autoregressive coefficient for the spatially lagged dependent variable. |
A numeric vector
ncol <- 20 n <- ncol^2 listw <- sim_grid_listw(ncol, ncol) # Create spatial weights for a grid u <- make_error(n) # Simulate random errors x <- make_x( n, mu = c(0.25, 5), var = c(1, 0.75), method = "normal" ) # Generate x variables # create xb with intercept = 1, beta1 = 2, beta2 = -3 xb <- make_xb(x, c(1, 2, -3)) wx <- make_wx(x, listw) wxg <- make_wxg(wx, c(-2, 1.5)) y <- sim_durbin(u, xb, wxg, listw, rho = 0.5) # combine data df <- cbind(y = y, x) # fit SDM spatialreg::lagsarlm(y ~ ., df, listw, Durbin = TRUE)ncol <- 20 n <- ncol^2 listw <- sim_grid_listw(ncol, ncol) # Create spatial weights for a grid u <- make_error(n) # Simulate random errors x <- make_x( n, mu = c(0.25, 5), var = c(1, 0.75), method = "normal" ) # Generate x variables # create xb with intercept = 1, beta1 = 2, beta2 = -3 xb <- make_xb(x, c(1, 2, -3)) wx <- make_wx(x, listw) wxg <- make_wxg(wx, c(-2, 1.5)) y <- sim_durbin(u, xb, wxg, listw, rho = 0.5) # combine data df <- cbind(y = y, x) # fit SDM spatialreg::lagsarlm(y ~ ., df, listw, Durbin = TRUE)
This function generates a pure spatial error process, which is useful when you only want to simulate the error structure without including any deterministic part (i.e., no xb term). This can be used to analyze or simulate the behavior of spatially dependent errors in isolation.
sim_error(u, listw, lambda = 0.5, model = c("sar", "ma"))sim_error(u, listw, lambda = 0.5, model = c("sar", "ma"))
u |
an error vector |
listw |
a |
lambda |
a value value between -1 and 1. The spatial autoregressive coefficient for the error term. |
model |
default |
A numeric vector
listw <- sim_grid_listw(5) u <- make_error(25) sim_error(u, listw)listw <- sim_grid_listw(5) u <- make_error(25) sim_error(u, listw)
Simulate General Nested Model
sim_gns(u, xb, wxg, listw, rho = 0.5, lambda = 0.2, model = c("sar", "ma"))sim_gns(u, xb, wxg, listw, rho = 0.5, lambda = 0.2, model = c("sar", "ma"))
u |
an error vector |
xb |
predicted x values as calculated by |
wxg |
predicted spatial lag effect as calculated by |
listw |
a |
rho |
the spatial autoregressive coefficient for the spatially lagged dependent variable. |
lambda |
a value value between -1 and 1. The spatial autoregressive coefficient for the error term. |
model |
default |
A numeric vector
Create a spatial weights matrix based on a square grid structure.
sim_grid_listw(nrow, ncol = nrow, style = "W", type = c("queen", "rook"))sim_grid_listw(nrow, ncol = nrow, style = "W", type = c("queen", "rook"))
nrow |
the number of rows in the grid. |
ncol |
defaults to |
style |
the spatial weights style. Defaults to row standardized. See |
type |
default |
A listw object by spdep package.
sim_grid_listw(10, 5)sim_grid_listw(10, 5)
Simiulate Matrix Exponential Spatial Lag Model
sim_mess(u, xb, listw, rho = 0.5)sim_mess(u, xb, listw, rho = 0.5)
u |
an error vector |
xb |
predicted x values as calculated by |
listw |
a |
rho |
the spatial autoregressive coefficient for the spatially lagged dependent variable. |
A numeric vector
Simulate a y variable for an Ordinary Least Squares (OLS) regression.
sim_ols(u, xb)sim_ols(u, xb)
u |
an error vector |
xb |
predicted x values as calculated by |
A numeric vector
u <- make_error(50, method = "normal") x <- make_x(50) xb <- make_xb(x, c(1,2)) y <- sim_ols(u, xb) lm(y ~ x[[1]])u <- make_error(50, method = "normal") x <- make_x(50) xb <- make_xb(x, c(1,2)) y <- sim_ols(u, xb) lm(y ~ x[[1]])
Simulate y for a SAR model.
sim_sar(u, xb, listw, rho = 0.5)sim_sar(u, xb, listw, rho = 0.5)
u |
an error vector |
xb |
predicted x values as calculated by |
listw |
a |
rho |
the spatial autoregressive coefficient for the spatially lagged dependent variable. |
A numeric vector
ncol <- 20 n <- ncol^2 listw <- sim_grid_listw(ncol, ncol) # Create spatial weights for a grid u <- make_error(n) # Simulate random errors x <- make_x( n, mu = c(0.25, 5), var = c(1, 0.75), method = "normal" ) # Generate x variables # create xb with intercept = 1, beta1 = 2, beta2 = -3 xb <- make_xb(x, c(1, 2, -3)) y <- sim_sar(u, xb, listw) # combine data df <- cbind(y = y, x) # fit SAR model # Note lambda, x_1, and x_2 estimates. spatialreg::stsls(y ~ ., df, listw)ncol <- 20 n <- ncol^2 listw <- sim_grid_listw(ncol, ncol) # Create spatial weights for a grid u <- make_error(n) # Simulate random errors x <- make_x( n, mu = c(0.25, 5), var = c(1, 0.75), method = "normal" ) # Generate x variables # create xb with intercept = 1, beta1 = 2, beta2 = -3 xb <- make_xb(x, c(1, 2, -3)) y <- sim_sar(u, xb, listw) # combine data df <- cbind(y = y, x) # fit SAR model # Note lambda, x_1, and x_2 estimates. spatialreg::stsls(y ~ ., df, listw)
Generate y values for the "combo" / SARAR / SAC model.
sim_sarar(u, xb, listw, rho = 0.5, lambda = 0.2, model = c("sar", "ma"))sim_sarar(u, xb, listw, rho = 0.5, lambda = 0.2, model = c("sar", "ma"))
u |
an error vector |
xb |
predicted x values as calculated by |
listw |
a |
rho |
the spatial autoregressive coefficient for the spatially lagged dependent variable. |
lambda |
a value value between -1 and 1. The spatial autoregressive coefficient for the error term. |
model |
default |
A numeric vector
Simulate the y values for an SEM model.
sim_sem(u, xb, listw, lambda = 0.5, model = c("sar", "ma"))sim_sem(u, xb, listw, lambda = 0.5, model = c("sar", "ma"))
u |
an error vector |
xb |
predicted x values as calculated by |
listw |
a |
lambda |
a value value between -1 and 1. The spatial autoregressive coefficient for the error term. |
model |
default |
A numeric vector
ncol <- 10 n <- ncol^2 listw <- sim_grid_listw(ncol, ncol) # Create spatial weights for a grid u <- make_error(n) # Simulate random errors x <- make_x( n, mu = c(0.25, 5), var = c(1, 0.75), method = "normal" ) # Generate x variables # create xb with intercept = 1, beta1 = 2, beta2 = -3 xb <- make_xb(x, c(1, 2, -3)) y <- sim_sem(u, xb, listw) # combine data df <- cbind(y = y, x) # fit SEM model # Note lambda, x_1, and x_2 estimates. spatialreg::errorsarlm(y ~ ., df, listw)ncol <- 10 n <- ncol^2 listw <- sim_grid_listw(ncol, ncol) # Create spatial weights for a grid u <- make_error(n) # Simulate random errors x <- make_x( n, mu = c(0.25, 5), var = c(1, 0.75), method = "normal" ) # Generate x variables # create xb with intercept = 1, beta1 = 2, beta2 = -3 xb <- make_xb(x, c(1, 2, -3)) y <- sim_sem(u, xb, listw) # combine data df <- cbind(y = y, x) # fit SEM model # Note lambda, x_1, and x_2 estimates. spatialreg::errorsarlm(y ~ ., df, listw)
This function simulates the y values of an SLX model, where the dependent variable is influenced by both the original and spatially lagged x variables.
sim_slx(u, xb, wxg)sim_slx(u, xb, wxg)
u |
an error vector |
xb |
predicted x values as calculated by |
wxg |
predicted spatial lag effect as calculated by |
A numeric vector
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 modelncol <- 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
Simulate Spatially Lagged X Error Model
sim_slx_error(u, xb, wxg, listw, lambda = 0.5, model = c("sar", "ma"))sim_slx_error(u, xb, wxg, listw, lambda = 0.5, model = c("sar", "ma"))
u |
an error vector |
xb |
predicted x values as calculated by |
wxg |
predicted spatial lag effect as calculated by |
listw |
a |
lambda |
a value value between -1 and 1. The spatial autoregressive coefficient for the error term. |
model |
default |
A numeric vector