| Title: | Generalized Spatial Autoregresive Models for Mean and Variance |
|---|---|
| Description: | Modeling spatial dependencies in dependent variables, extending traditional spatial regression approaches. It allows for the joint modeling of both the mean and the variance of the dependent variable, incorporating semiparametric effects in both models. Based on generalized additive models (GAM), the package enables the inclusion of non-parametric terms while maintaining the classical theoretical framework of spatial regression. Additionally, it implements the Generalized Spatial Autoregression (GSAR) model, which extends classical methods like logistic Spatial Autoregresive Models (SAR), probit Spatial Autoregresive Models (SAR), and Poisson Spatial Autoregresive Models (SAR), offering greater flexibility in modeling spatial dependencies and significantly improving computational efficiency and the statistical properties of the estimators. Related work includes: a) J.D. Toloza-Delgado, Melo O.O., Cruz N.A. (2024). "Joint spatial modeling of mean and non-homogeneous variance combining semiparametric SAR and GAMLSS models for hedonic prices". <doi:10.1016/j.spasta.2024.100864>. b) Cruz, N. A., Toloza-Delgado, J. D., Melo, O. O. (2024). "Generalized spatial autoregressive model". <doi:10.48550/arXiv.2412.00945>. |
| Authors: | Nelson Alirio Cruz Gutierrez [aut, cre, cph], Oscar Orlando Melo [aut], Jurgen Toloza-Delgado [aut] |
| Maintainer: | Nelson Alirio Cruz Gutierrez <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.3.0 |
| Built: | 2026-05-16 08:58:26 UTC |
| Source: | https://github.com/cruzalirio/spatemr |
'GSCIMC' estimates generalized estimating equations (GEE) incorporating spatial autoregressive (SAR) components. It extends GEE models to account for spatial dependence in the response variable.
GSCIMC( formula, family = gaussian(), weights = NULL, data, W, start = NULL, toler = 1e-04, maxit = 200, trace = FALSE, eps = 1e-06 )GSCIMC( formula, family = gaussian(), weights = NULL, data, W, start = NULL, toler = 1e-04, maxit = 200, trace = FALSE, eps = 1e-06 )
formula |
A formula specifying the model structure (response ~ predictors). |
family |
A description of the error distribution and link function. Default is 'gaussian()'. |
weights |
Optional vector of prior weights. Must be positive. |
data |
A data frame containing the variables in the model. |
W |
A spatial weights matrix defining the spatial dependence structure. |
start |
Optional starting values for parameter estimation. |
toler |
Convergence tolerance for iterative optimization. Default is '1e-05'. |
maxit |
Maximum number of iterations for model fitting. Default is '50'. |
trace |
Logical; if 'TRUE', prints iteration details. Default is 'FALSE'. |
eps |
Minimun value for variance diference of zero |
The function estimates a spatially autoregressive GEE model by iteratively updating the spatial dependence parameter ('rho') and regression coefficients ('beta'). The estimation follows a quasi-likelihood approach using iterative weighted least squares (IWLS).
The function supports common GLM families ('gaussian', 'binomial', 'poisson', 'Gamma', 'inverse.gaussian') and their quasi-likelihood equivalents.
A list of class '"GSCIMC"' containing:
coefficients |
Estimated regression coefficients. |
rho |
Estimated spatial autoregressive parameter. |
fitted.values |
Predicted values from the model. |
linear.predictors |
Linear predictor values ('X * beta'). |
prior.weights |
Weights used in estimation. |
y |
Observed response values. |
formula |
Model formula. |
call |
Function call used to fit the model. |
data |
Data used in the model. |
converged |
Logical indicating whether the algorithm converged. |
logLik |
Quasi-log-likelihood of the fitted model. |
deviance |
Residual deviance. |
df.residual |
Residual degrees of freedom. |
phi |
Dispersion parameter estimate. |
R |
Robust Variance Estimation. |
CIC |
Corrected Information Criterion. |
RJC |
Robust Jackknife Correction. |
https://doi.org/10.48550/arXiv.2412.00945
Cruz, N. A., Toloza-Delgado, J. D., & Melo, O. O. (2024). Generalized spatial autoregressive model. arXiv preprint arXiv:2412.00945.
library(spdep) library(sp) data(meuse) sp::coordinates(meuse) <- ~x+y W <- spdep::nb2mat(knn2nb(knearneigh(meuse, k=5)), style="W") fit <- GSCIMC(cadmium ~ dist + elev, family=poisson(), data=meuse, W=W) summary_SAR(fit)library(spdep) library(sp) data(meuse) sp::coordinates(meuse) <- ~x+y W <- spdep::nb2mat(knn2nb(knearneigh(meuse, k=5)), style="W") fit <- GSCIMC(cadmium ~ dist + elev, family=poisson(), data=meuse, W=W) summary_SAR(fit)
This function fits a hurdle model using GSCIMC, consisting of: (1) A logit model for zero vs. non-zero responses. (2) A truncated Poisson model for positive counts.
Hurdle_GSCIMC( formula, data, W, weights = NULL, toler = 1e-05, maxit = 200, trace = FALSE )Hurdle_GSCIMC( formula, data, W, weights = NULL, toler = 1e-05, maxit = 200, trace = FALSE )
formula |
A formula specifying the model. |
data |
The dataset. |
W |
The spatial weight matrix. |
weights |
Optional weights. |
toler |
Convergence tolerance. |
maxit |
Maximum number of iterations. |
trace |
Logical. If TRUE, prints progress. |
A list containing the logit and Poisson-truncated models.
set.seed(123) n <- 100 x <- rnorm(n) y <- rpois(n, lambda = exp(0.5 * x)) y[rbinom(n, 1, 1/(1+exp(-0.5*x)))] <- 0 # Introduce zeros W <- matrix(rbinom(n^2,1,0.2), n, n) # Example spatial weight matrix diag(W) <- 0 rtot <- rowSums(W) W <- W/ifelse(rtot==0, 0.1, rtot) model <- Hurdle_GSCIMC(y ~ x, data = data.frame(y, x), W = W) summary_SAR(model$logit_model) summary_SAR(model$poisson_truncated_model)set.seed(123) n <- 100 x <- rnorm(n) y <- rpois(n, lambda = exp(0.5 * x)) y[rbinom(n, 1, 1/(1+exp(-0.5*x)))] <- 0 # Introduce zeros W <- matrix(rbinom(n^2,1,0.2), n, n) # Example spatial weight matrix diag(W) <- 0 rtot <- rowSums(W) W <- W/ifelse(rtot==0, 0.1, rtot) model <- Hurdle_GSCIMC(y ~ x, data = data.frame(y, x), W = W) summary_SAR(model$logit_model) summary_SAR(model$poisson_truncated_model)
This method prints a formatted summary of a 'summary.GSCIMC' object, including details of the model coefficients, rho, dispersion, and other statistics.
## S3 method for class 'summary.GSCIMC' print(x, ...)## S3 method for class 'summary.GSCIMC' print(x, ...)
x |
An object of class 'summary.GSCIMC'. |
... |
Additional arguments (currently unused). |
Print a summary for the specified Generalized Spatial Autoregresive Model class.
This method prints a formatted summary of a 'summary.SARARgamlss' object, including details of the GAMLSS model, spatial parameters (rho and lambda), and Wald tests.
## S3 method for class 'summary.SARARgamlss' print(x, ...)## S3 method for class 'summary.SARARgamlss' print(x, ...)
x |
An object of class 'summary.SARARgamlss'. |
... |
Additional arguments (currently unused). |
Print a summary for the specified GAMLSS model.
This function defines a truncated Poisson family for use in Generalized Linear Models (GLMs), where zero values are not allowed. It modifies the Poisson likelihood by excluding zero-count observations.
ptfamily(link = "log")ptfamily(link = "log")
link |
Character string or a link-glm object specifying the link function. Accepted values are "log", "identity", and "sqrt". |
An object of class "family" that can be used in glm().
set.seed(123) y <- rpois(100, lambda = 3) y <- y[y > 0] # Truncate zeros x <- rnorm(length(y)) model <- glm(y ~ x, family = ptfamily()) summary(model)set.seed(123) y <- rpois(100, lambda = 3) y <- y[y > 0] # Truncate zeros x <- rnorm(length(y)) model <- glm(y ~ x, family = ptfamily()) summary(model)
This function estimates a Spatial Autoregressive Generalized Additive Model for Location Scale
(SARARgamlss) using GAMLSS. The model includes both spatial dependencies and the possibility of
non-parametric terms in the formulas for the mean and variance. The function supports SAR, SARAR,
and SEM model types and performs the estimation through an iterative process that updates spatial
dependence parameters. The variance of the spatial parameters and
is estimated using the inverse of the Hessian matrix from the optimization.
SARARgamlss( formula, sigma.formula = ~1, W1 = diag(0, nrow(data)), W2 = diag(0, nrow(data)), data, tol = 1e-04, maxiter = 20, type = c("SAR", "SARAR", "SEM"), weights = NULL )SARARgamlss( formula, sigma.formula = ~1, W1 = diag(0, nrow(data)), W2 = diag(0, nrow(data)), data, tol = 1e-04, maxiter = 20, type = c("SAR", "SARAR", "SEM"), weights = NULL )
formula |
A formula specifying the mean structure of the model (response ~ explanatory variables). |
sigma.formula |
A formula specifying the variance structure of the model (default: ~1). |
W1 |
A spatial weights matrix for the SAR term (default: identity matrix). |
W2 |
A spatial weights matrix for the SARAR term (default: identity matrix). |
data |
A data.frame containing the variables used in the model. |
tol |
Convergence tolerance (default: 1E-4). |
maxiter |
Maximum number of iterations for optimization (default: 20). |
type |
The type of spatial model to fit: one of "SAR", "SARAR", or "SEM". |
weights |
Optional weights for the observations (default: NULL). |
A fitted GAMLSS model object with spatial autoregressive terms. The model object also includes
the variance of the spatial parameters and
Toloza-Delgado, J. D., Melo, O. O., & Cruz, N. A. Joint spatial modeling of mean and non-homogeneous variance combining semiparametric SAR and GAMLSS models for hedonic prices. Spatial Statistics, 65, 100864 (2025) @source https://doi.org/10.1016/j.spasta.2024.100864
library(spdep) library(gamlss) data(oldcol) # Create spatial weight matrices W1 and W2 W1 <- spdep::nb2mat(COL.nb, style = "W") W2 <- W1 # In this case, assume the same spatial weights for both # Fit a SARARgamlss model result <- SARARgamlss(formula = CRIME ~ INC + cs(HOVAL), sigma.formula = ~ INC + pb(HOVAL), W1 = W1, W2 = W2,data = COL.OLD, tol = 1E-4, maxiter = 20, type = "SARAR") summary_SAR(result) gamlss::term.plot(result$gamlss, what="mu")library(spdep) library(gamlss) data(oldcol) # Create spatial weight matrices W1 and W2 W1 <- spdep::nb2mat(COL.nb, style = "W") W2 <- W1 # In this case, assume the same spatial weights for both # Fit a SARARgamlss model result <- SARARgamlss(formula = CRIME ~ INC + cs(HOVAL), sigma.formula = ~ INC + pb(HOVAL), W1 = W1, W2 = W2,data = COL.OLD, tol = 1E-4, maxiter = 20, type = "SARAR") summary_SAR(result) gamlss::term.plot(result$gamlss, what="mu")
This function generates a summary for objects of class 'SARARgamlss' or 'GSCIMC'. It combines the summary outputs for both models, including GAMLSS model details, spatial parameters (rho and lambda), and Wald tests.
summary_SAR(object)summary_SAR(object)
object |
An object of class 'SARARgamlss' or 'GSCIMC'. |
A list containing the summary for the specified model class.
library(spdep) library(gamlss) data(oldcol) W1 <- spdep::nb2mat(COL.nb, style = "W") W2 <- W1 # In this case, assume the same spatial weights for both # Fit a SARARgamlss model result_sarar <- SARARgamlss(formula = CRIME ~ INC + HOVAL, sigma.formula = ~ INC + pb(HOVAL), W1 = W1, W2 = W2, data = COL.OLD, type="SAR") summary_SAR(result_sarar) # Example for GSCIMC model result_GSCIMC <- GSCIMC(formula = CRIME ~ INC + HOVAL, data = COL.OLD, W = W1) summary_SAR(result_GSCIMC)library(spdep) library(gamlss) data(oldcol) W1 <- spdep::nb2mat(COL.nb, style = "W") W2 <- W1 # In this case, assume the same spatial weights for both # Fit a SARARgamlss model result_sarar <- SARARgamlss(formula = CRIME ~ INC + HOVAL, sigma.formula = ~ INC + pb(HOVAL), W1 = W1, W2 = W2, data = COL.OLD, type="SAR") summary_SAR(result_sarar) # Example for GSCIMC model result_GSCIMC <- GSCIMC(formula = CRIME ~ INC + HOVAL, data = COL.OLD, W = W1) summary_SAR(result_GSCIMC)
This function calculates the inverse of the variance of the spatial autoregressive parameter
in a generalized spatial autoregressive (GSAR) or GEE-SAR model.
The calculation is based on the quasi-likelihood derivatives with respect to
for different exponential family distributions.
var_rho_inv(A, W, X, beta, family, y, offs = NULL, weights = NULL, phi = 1)var_rho_inv(A, W, X, beta, family, y, offs = NULL, weights = NULL, phi = 1)
A |
Matrix. The spatial transformation matrix |
W |
Matrix. Row-standardized spatial weights matrix |
X |
Matrix. Design matrix of covariates, dimension |
beta |
Numeric vector. Current estimates of regression coefficients |
family |
GLM family object. The response distribution family (e.g., 'gaussian()', 'poisson()', 'binomial()', 'Gamma()', 'Negative Binomial()'). |
y |
response variable |
offs |
Numeric vector. Optional offset vector, length |
weights |
Numeric vector. Observation weights |
phi |
Numeric. Dispersion parameter, used for 'gaussian', 'Gamma', or 'Negative Binomial' families. Default is 1. |
The function computes first and second derivatives of the mean with respect to
, and then applies the appropriate formula for the inverse variance based on the
selected family. This generalizes the quasi-likelihood derivations for spatially correlated
generalized linear models.
For binomial families with large , it is recommended to truncate within
to avoid numerical instability.
Numeric. The inverse of the variance of ().
## Not run: library(Matrix) n <- 10 W <- Matrix(0,n,n) diag(W[-1,]) <- 1 X <- matrix(rnorm(n*2), n, 2) beta <- c(0.5, -0.2) rho <- 0.3 A <- Diagonal(n) - rho*W family <- binomial() weights <- rep(1,n) var_rho_inv(A, W, X, beta, family, weights) ## End(Not run)## Not run: library(Matrix) n <- 10 W <- Matrix(0,n,n) diag(W[-1,]) <- 1 X <- matrix(rnorm(n*2), n, 2) beta <- c(0.5, -0.2) rho <- 0.3 A <- Diagonal(n) - rho*W family <- binomial() weights <- rep(1,n) var_rho_inv(A, W, X, beta, family, weights) ## End(Not run)