| Type: | Package |
| Title: | GWR, Mixed GWR with Spatial Autocorrelation and Multiscale GWR (Top-Down Scale Approaches) |
| Version: | 1.3.1 |
| Date: | 2026-01-19 |
| Maintainer: | Ghislain Geniaux <ghislain.geniaux@inrae.fr> |
| Description: | Provides methods for Geographically Weighted Regression with spatial autocorrelation (Geniaux and Martinetti 2017) <doi:10.1016/j.regsciurbeco.2017.04.001>. Implements Multiscale Geographically Weighted Regression with Top-Down Scale approaches (Geniaux 2026) <doi:10.1007/s10109-025-00481-4>. |
| License: | GPL-2 | GPL-3 [expanded from: GPL (≥ 2)] |
| Depends: | R (≥ 3.5.0), sp, Matrix |
| Imports: | Rcpp, ggplot2, sf, knitr, methods, doParallel, foreach, nabor, mapview, rlang, dplyr, gridExtra, grid, mboost, mgcv, caret, stringr, SMUT,plotly, RhpcBLASctl, magrittr, lifecycle |
| Suggests: | rmarkdown |
| VignetteBuilder: | knitr |
| LinkingTo: | Rcpp, RcppEigen, RcppArmadillo |
| Config/notCompileAttributes: | true |
| RoxygenNote: | 7.3.2 |
| NeedsCompilation: | yes |
| Encoding: | UTF-8 |
| SystemRequirements: | C++17 |
| Packaged: | 2026-01-21 14:10:07 UTC; geniaux |
| Author: | Ghislain Geniaux [aut, cre], Davide Martinetti [aut], César Martinez [aut] |
| Repository: | CRAN |
| Date/Publication: | 2026-01-21 14:50:02 UTC |
Estimation of linear and local linear model with spatial autocorrelation model (mgwrsar).
Description
MGWRSAR is is a wrapper function for estimating linear and local linear models with spatial autocorrelation (SAR models with spatially varying coefficients).
Usage
MGWRSAR(formula, data, coords, fixed_vars = NULL, kernels, H,
Model = "GWR", control = list())
Arguments
formula |
a formula. |
data |
a dataframe or a spatial dataframe (sp package). |
coords |
default NULL, a dataframe or a matrix with coordinates, not required if data is a spatial dataframe. |
fixed_vars |
a vector with the names of spatiallay constant coefficient for mixed model. All other variables present in formula are supposed to be spatially varying. If empty or NULL (default), all variables in formula are supposed to be spatially varying. |
kernels |
A vector containing the kernel types. Possible types: rectangle ("rectangle"), bisquare ("bisq"), tricube ("tcub"), epanechnikov ("epane"), gaussian ("gauss")) . |
H |
vector containing the bandwidth parameters for the kernel functions. |
Model |
character containing the type of model: Possible values are "OLS", "SAR", "GWR" (default), "MGWR" , "MGWRSAR_0_0_kv","MGWRSAR_1_0_kv", "MGWRSAR_0_kc_kv", "MGWRSAR_1_kc_kv", "MGWRSAR_1_kc_0". See Details for more explanation. |
control |
list of extra control arguments for MGWRSAR wrapper - see Details below |
Details
- Z
A matrix of variables for genralized kernel product, default NULL.
- W
A row-standardized spatial weight matrix for Spatial Aurocorrelation, default NULL.
- Type
Verbose mode, default FALSE.
- adaptive
A vector of boolean to choose adaptive version for each kernel.
- kernel_w
The type of kernel for computing W, default NULL.
- h_w
The bandwidth value for computing W, default 0.
- Method
Estimation method for computing the models with Spatial Dependence. '2SLS' or 'B2SLS', default '2SLS'.
- TP
Avector of target points, default NULL.
- ncore
Number of CPU core for parallel computation, default 1
- isgcv
If TRUE, compute a LOOCV criteria, default FALSE.
- maxknn
When n >NmaxDist, only the maxknn first neighbours are used for distance compution, default 500.
- NmaxDist
When n >NmaxDist only the maxknn first neighbours are used for distance compution, default 5000
- verbose
Verbose mode, default FALSE.
Value
MGWRSAR returns an object of class mgwrsar with at least the following components:
- Betav
matrix of coefficients of dim(n,kv) x kv.
- Betac
vector of coefficients of length kc.
- Model
The sum of square residuals.
- Y
The dependent variable.
- XC
The explanatory variables with constant coefficients.
- XV
The explanatory variables with varying coefficients.
- X
The explanatory variables.
- W
The spatial weight matrix for spatial dependence.
- isgcv
if gcv has been computed.
- edf
The estimated degrees of freedom.
- formula
The formula.
- data
The dataframe used for computation.
- Method
The type of model.
- coords
The spatial coordinates of observations.
- H
The bandwidth vector.
- fixed_vars
The names of constant coefficients.
- kernels
The kernel vector.
- SSR
The sum of square residuals.
- residuals
The vector of residuals.
- fit
the vector of fitted values.
- sev
local standard error of parameters.
- get_ts
Boolean, if trace of hat matrix Tr(S) should be stored.
- NN
Maximum number of neighbors for weights computation
MGWRSAR is is a wrapper function for estimating linear and local linear model
with spatial autocorrelation that allows to estimate the following models :
y=\beta_c X_c+\,\epsilon_i (OLS)
y=\beta_v(u_i,v_i) X_v+\,\epsilon_i (GWR)
y=\beta_c X_c+\beta_v(u_i,v_i) X_v+\,\epsilon_i (MGWR)
y=\lambda Wy+\beta_c X_c+\,\epsilon_i (MGWR-SAR(0,k,0))
y=\lambda Wy+\beta_v(u_i,v_i)X_v+\,\epsilon_i (MGWR-SAR(0,0,k))
y=\lambda Wy+\beta_c X_c+\beta_v(u_i,v_i)X_v+\,\epsilon_i (MGWR-SAR(0,k_c,k_v))
y=\lambda(u_i,v_i) Wy+\beta_c X_c+\,\epsilon_i (MGWR-SAR(1,k,0))
y=\lambda(u_i,v_i)Wy+\beta_v(u_i,v_i)X_v+\,\epsilon_i (MGWR-SAR(1,0,k))
y=\lambda(u_i,v_i)Wy+\beta_cX_c+\beta_v(u_i,v_i)X_v+\,\epsilon_i (MGWR-SAR(1,k_c,k_v))
When model imply spatial autocorrelation, a row normalized spatial weight matrix must be provided. 2SLS and Best 2SLS method can be used. When model imply local regression, a bandwidth and a kernel type must be provided. Optimal bandwidth can be estimated using goldens_search_bandwid function. When model imply mixed local regression, the names of stationary covariates must be provided.
#' In addition to the ability of considering spatial autocorrelation in GWR/MGWR like models, MGWRSAR function introduces several useful technics for estimating local regression with space coordinates:
it uses RCCP and RCCPeigen code that speed up computation and allows parallel computings;
it allows to drop out variables with not enough local variance in local regression, which allows to consider dummies in GWR/MGWR framework without trouble.
it allows to drop out local outliers in local regression.
it allows to consider additional variable for kernel, including time (asymetric kernel) and categorical variables (see Li and Racine 2010). Experimental version.
References
Geniaux, G. and Martinetti, D. (2017). A new method for dealing simultaneously with spatial autocorrelation and spatial heterogeneity in regression models. Regional Science and Urban Economics. (https://doi.org/10.1016/j.regsciurbeco.2017.04.001)
McMillen, D. and Soppelsa, M. E. (2015). A conditionally parametric probit model of microdata land use in chicago. Journal of Regional Science, 55(3):391-415.
Loader, C. (1999). Local regression and likelihood, volume 47. springer New York.
Franke, R. and Nielson, G. (1980). Smooth interpolation of large sets of scattered data. International journal for numerical methods in engineering, 15(11):1691-1704.
See Also
golden_search_bandwidth, summary, plot, predict, kernel_matW
Examples
library(mgwrsar)
## loading data example
data(mydata)
coords=as.matrix(mydata[,c("x","y")])
## Creating a spatial weight matrix (sparce dgCMatrix)
## of 4 nearest neighbors with 0 in diagonal
W=kernel_matW(H=4,kernels='rectangle',coords=coords,NN=4,adaptive=TRUE,
diagnull=TRUE)
mgwrsar_0_kc_kv<-MGWRSAR(formula = 'Y_mgwrsar_0_kc_kv~X1+X2+X3', data = mydata,
coords=coords, fixed_vars='X2',kernels=c('gauss'),H=20, Model = 'MGWRSAR_0_kc_kv',
control=list(SE=FALSE,adaptive=TRUE,W=W))
summary(mgwrsar_0_kc_kv)
Top-Down Scale (TDS) and Adaptive Top-Down Scale (ATDS) Estimation for MGWR
Description
This function implements the "Top-Down Scale" (TDS) methodology for estimating Multiscale Geographically Weighted Regression (MGWR) models. Unlike classical backfitting approaches that fully optimize bandwidths at each iteration, TDS uses a pre-defined sequence of decreasing bandwidths to efficiently identify the optimal spatial scale for each covariate.
The function supports two main algorithms:
-
'tds_mgwr': A backfitting algorithm that selects a unique optimal bandwidth for each covariate from a decreasing sequence.
-
'atds_mgwr': Extends 'tds_mgwr' with a second "boosting" stage (Adaptive TDS). It refines estimates by allowing bandwidths to vary locally, capturing complex spatial patterns (e.g., simultaneous large-scale trends and local variations).
Usage
TDS_MGWR(formula, data, coords, Model = 'tds_mgwr',
kernels = 'gauss', fixed_vars = NULL, Ht = NULL,
control_tds = list(nns = 25, get_AIC = FALSE, init_model = "OLS"),
control = list(adaptive = TRUE))
Arguments
formula |
A formula object specifying the model (e.g., |
data |
A data frame containing the variables in the model. |
coords |
A matrix or data frame of coordinates (2 columns for spatial). |
Model |
A character string specifying the algorithm. Options:
|
kernels |
A character string or vector specifying the kernel type (e.g., |
fixed_vars |
A character vector indicating the names of variables with spatially stationary (fixed) coefficients. Default is |
Ht |
Numeric. Optional bandwidth for the second dimension (time) if using spatio-temporal models (Type 'GDT'). |
control_tds |
A named list of control parameters specific to the TDS algorithm:
|
control |
A named list of standard control arguments passed to the internal
|
Details
The TDS algorithm works in two stages:
**Stage 1 (Backfitting):** Starts with a global model (OLS) or a simple GWR. It iteratively updates the bandwidth for each covariate by testing values from a decreasing sequence. This avoids the "yo-yo" effect of standard backfitting and converges faster.
**Stage 2 (Boosting - atds_mgwr only):** Uses the residuals from Stage 1 to iteratively refine coefficients. This stage allows the effective bandwidth to adapt locally, improving accuracy for covariates with spatially heterogeneous scales.
Value
An object of class mgwrsar containing:
Betav |
Matrix of spatially varying coefficients. |
H |
Vector of optimal bandwidths found for each covariate. |
RMSE |
Root Mean Square Error of the final model. |
AICc |
Corrected Akaike Information Criterion (if requested). |
fitted.values |
Vector of fitted values. |
residuals |
Vector of residuals. |
References
Geniaux, G. (2024). Top-Down Scale Approaches for Multiscale GWR with Locally Adaptive Bandwidths. Springer Nature.
See Also
MGWRSAR, golden_search_bandwidth
atds_gwr Top-Down Scaling approach of GWR
Description
This function performs a Geographically Weighted Regression (GWR) using a top-down scaling approach, adjusting GWR coefficients with a progressively decreasing bandwidth as long as the AICc criterion improves.
Usage
atds_gwr(formula,data,coords,kernels='triangle',fixed_vars=NULL,
control_tds=list(nns=30),control=list(adaptive=TRUE,verbose=FALSE))
Arguments
formula |
a formula. |
data |
a dataframe. |
coords |
default NULL, a dataframe or a matrix with coordinates |
kernels |
A vector containing the kernel types. Possible types: triangle ("triangle"), bisquare ("bisq"), tricube ("tcub"), epanechnikov ("epane"). |
fixed_vars |
a vector with the names of spatiallay constant coefficient for mixed model. All other variables present in formula are supposed to be spatially varying. If empty or NULL (default), all variables in formula are supposed to be spatially varying. |
control_tds |
list of extra control arguments for TDS_MGWR model - see TDS_MGWR Help |
control |
list of extra control arguments for MGWRSAR wrapper - see MGWRSAR Help |
See Also
TDS_MGWR, gwr_multiscale, MGWRSAR, golden_search_bandwidth.
coef for mgwrsar model
Description
coef for mgwrsar model
Usage
## S4 method for signature 'mgwrsar'
coef(object, ...)
Arguments
object |
A model of class |
... |
coef parameters forwarded. |
Value
A named list with a matrix of varying coefficients and a vector or non varying coefficients.
Search of a suitable set of target points. find_TP is a wrapper function that identifies a set of target points based on spatial smoothed OLS residuals.
Description
Search of a suitable set of target points. find_TP is a wrapper function that identifies a set of target points based on spatial smoothed OLS residuals.
Usage
find_TP(formula, data,coords,kt,ks=16,Wtp=NULL,type='residuals',
model_residuals=NULL,verbose=0,prev_TP=NULL,nTP=NULL)
Arguments
formula |
a formula |
data |
a dataframe or a spatial dataframe (SP package) |
coords |
a dataframe or a matrix with coordinates, not required if data is a spatial dataframe |
kt |
the minimum number of first neighbors with lower (resp.higer) absolute value of the smoothed residuals. |
ks |
the number of first neighbors for computing the smoothed residuals, default 16. |
Wtp |
a precomputed matrix of weights, default NULL. |
type |
method for choosing TP, could be 'residuals', ' kdtree','random', default 'residuals' |
model_residuals |
(optional) a vector of residuals. |
verbose |
verbose mode, default FALSE. |
prev_TP |
index of already used TP (version length(kt)>1), default NULL. |
nTP |
numbeer of target points for random choice of target points, default NULL. |
Details
find_TP is a wrapper function that identifies a set of target points, based on spatial smoothed residuals by default. If no vector of residuals are provided, OLS residuals are computed. The function first computes the smooth of model residuals using a Shepard's kernel with ks neighbors (default 16). Then it identifies local maxima (resp. minima) that fits the requirement of having at least kt neighbors with lower (resp.higer) absolute value of the smoothed residuals. As kt increases the number of target points decreases.
Value
find_TP returns an index vector of Target Points set.
Examples
library(mgwrsar)
## loading data example
data(mydata)
coords=as.matrix(mydata[,c("x","y")])
TP=find_TP(formula = 'Y_gwr~X1+X2+X3', data =mydata,coords=coords,kt=6,
type='residuals')
# only 60 targets points are used
length(TP)
model_GWR_tp<-MGWRSAR(formula = 'Y_gwr~X1+X2+X3', data = mydata,
coords=coords, fixed_vars=NULL,kernels=c('gauss'), H=0.03, Model = 'GWR',
control=list(SE=TRUE,TP=TP,ks=12))
summary(model_GWR_tp@Betav)
fitted for mgwrsar model
Description
fitted for mgwrsar model
Usage
## S4 method for signature 'mgwrsar'
fitted(object, ...)
Arguments
object |
A model of class |
... |
fitted parameters forwarded. |
Value
A vector of fitted values.
Optimization of 2D Bandwidths (Spatial and Temporal) using Golden Section Search
Description
This function optimizes spatial and temporal bandwidths simultaneously using
a 2D Golden Section Search approach. It is typically used internally by
search_bandwidths when refine = TRUE for
spatio-temporal models (Type 'GDT').
Usage
golden_search_2d_bandwidth(formula, data, coords, fixed_vars, kernels, Model,
control, lower.bound.space, upper.bound.space,
lower.bound.time, upper.bound.time,
tolerance_s = 1e-06, tolerance_t = 1e-06,
max_iter = 10)
Arguments
formula |
A formula object. |
data |
A data frame containing the variables. |
coords |
A matrix of coordinates (spatial indices). |
fixed_vars |
Vector of names of variables with fixed coefficients. |
kernels |
Vector of kernel types (e.g., c('gauss', 'gauss')). |
Model |
Character string specifying the model type (e.g., 'GWR'). |
control |
List of control parameters. |
lower.bound.space |
Numeric. Lower bound for the spatial bandwidth search. |
upper.bound.space |
Numeric. Upper bound for the spatial bandwidth search. |
lower.bound.time |
Numeric. Lower bound for the temporal bandwidth search. |
upper.bound.time |
Numeric. Upper bound for the temporal bandwidth search. |
tolerance_s |
Numeric. Convergence tolerance for the spatial dimension. Default is 1e-6. |
tolerance_t |
Numeric. Convergence tolerance for the temporal dimension. Default is 1e-6. |
max_iter |
Integer. Maximum number of iterations for the optimization loop. Default is 10. |
Value
A list containing:
minimum |
Vector of optimized bandwidths c(h_spatial, h_temporal). |
objective |
Final AICc value. |
model |
The fitted mgwrsar model object. |
See Also
golden_search_bandwidth, search_bandwidths
Golden search bandwidth (deprecated)
Description
'golden_search_bandwidth()' is deprecated. Please use [search_bandwidths()], which provides a unified interface for spatial and spatio-temporal bandwidth search and can run golden-search refinement internally.
This wrapper preserves backward compatibility by translating the historical arguments ('lower.bound', 'upper.bound', 'Ht') to the new interface.
Usage
golden_search_bandwidth(
formula,
Ht = NULL,
data,
coords,
fixed_vars = NULL,
kernels,
Model = "GWR",
control,
lower.bound,
upper.bound,
tolerance = 1e-06,
ncore = 1,
show_progress = TRUE
)
Arguments
formula |
A formula object specifying the model (e.g., |
Ht |
Optional temporal bandwidth (used only for legacy calls). If not 'NULL', it is mapped to 'ht_range = c(Ht, Ht)' (i.e., fixed temporal bandwidth) when 'n_rounds = 0'. |
data |
A data frame containing the variables in the model. |
coords |
A matrix or data frame of coordinates (2 columns for spatial, 1 for temporal, or more for GDT). |
fixed_vars |
A character vector indicating the names of variables with spatially stationary (fixed) coefficients.
Default is |
kernels |
A character vector specifying the kernel types for spatial and temporal components
(e.g., |
Model |
A character string specifying the model type. Options include "GWR", "MGWR", "OLS", "SAR", etc. Default is "GWR". |
control |
A named list of extra control arguments passed to the |
lower.bound, upper.bound |
Numeric bounds for the spatial bandwidth search; mapped to 'hs_range'. |
tolerance |
Numeric tolerance; mapped to 'tol' when relevant. |
ncore |
Number of cores to use. |
show_progress |
Logical; forwarded to [search_bandwidths()]. |
Value
A list returned by [search_bandwidths()] (see its Value section).
int_prems to be documented
Description
int_prems to be documented
Usage
int_prems(X)
Arguments
X |
to be documented |
Value
to be documented
kernel_matW A function that returns a sparse weight matrix based computed with a specified kernel (gauss,bisq,tcub,epane,rectangle,triangle) considering coordinates provides in S and a given bandwidth. If NN<nrow(S) only NN firts neighbours are considered. If Type!='GD' then S should have additional columns and several kernels and bandwidths should be be specified by the user.
Description
kernel_matW A function that returns a sparse weight matrix based computed with a specified kernel (gauss,bisq,tcub,epane,rectangle,triangle) considering coordinates provides in S and a given bandwidth. If NN<nrow(S) only NN firts neighbours are considered. If Type!='GD' then S should have additional columns and several kernels and bandwidths should be be specified by the user.
Usage
kernel_matW(H,kernels,coords,NN,TP=NULL,Type='GD',adaptive=FALSE,
diagnull=TRUE,alpha=1,dists=NULL,indexG=NULL,extrapol=FALSE,QP=NULL,K=0)
Arguments
H |
A vector of bandwidths |
kernels |
A vector of kernel types |
coords |
A matrix with variables used in kernel (reference) |
NN |
Number of spatial Neighbours for kernels computations |
TP |
A vector with index of target points |
Type |
Type of Genelarized kernel product ('GD' only spatial,'GDC' spatial + a categorical variable,'GDX' spatial + a continuous variable, 'GDT' spatial + a time index, and other combinations 'GDXXC','GDTX',...) |
adaptive |
A vector of boolean to choose adaptive version for each kernel |
diagnull |
Zero on diagonal, default FALSE |
alpha |
TO BE DOCUMENTED |
dists |
TO BE DOCUMENTED |
indexG |
TO BE DOCUMENTED |
extrapol |
TO BE DOCUMENTED |
QP |
A matrix with variables used in kernel (neighbors), default NULL (if NULL coord_j=coord_i) |
K |
TO BE DOCUMENTED |
Value
A sparse Matrix of weights (dgCMatrix).
Examples
library(mgwrsar)
## loading data example
data(mydata)
coords=as.matrix(mydata[,c("x","y")])
## Creating a spatial weight matrix (sparce dgCMatrix) of 4 nearest neighbors with 0 in diagonal
W=kernel_matW(H=4,kernels='rectangle',coords=coords,NN=4,adaptive=TRUE,diagnull=TRUE)
Ensure Uniqueness of Coordinates or Values in 1D, 2D, or 3D Structures
Description
This function enforces uniqueness in numeric data structures of dimension one, two, or three by applying minimal perturbations to duplicated values. It is primarily intended to avoid degeneracy issues (e.g., duplicated coordinates or time stamps) in spatial and spatio-temporal modeling.
Usage
make_unique_by_structure(M)
Arguments
M |
A numeric vector, matrix, or data frame with 1, 2, or 3 columns.
For matrices or data frames, columns are interpreted as spatial
( |
Details
Depending on the dimensionality of the input, uniqueness is enforced as follows:
-
1D: duplicated values are slightly perturbed to ensure uniqueness;
-
2D: duplicated
(x, y)coordinate pairs are resolved by perturbing the second coordinate; -
3D: duplicated
(x, y)pairs and duplicated temporal coordinates are handled separately, ensuring uniqueness in both space and time.
The perturbation magnitude is proportional to machine precision and the scale of the input data, ensuring numerical stability while preserving the original structure as closely as possible.
This function is designed as a low-level utility for preprocessing spatial or spatio-temporal inputs prior to kernel-based local regression or distance computations, where duplicated locations or timestamps may cause numerical singularities.
Value
If M is a vector, a numeric vector of the same length is returned.
If M is a matrix or data frame, a numeric matrix with the same
dimensions is returned, with standardized column names:
"t" (1D), "x", "y" (2D), or "x", "y", "t" (3D).
Class of mgwrsar Model.
Description
A S4 class to represent the results of MGWRSAR, TDS-MGWR and related spatial models.
Slots
Betavmatrix. The estimated varying coefficients, dimension (n, kv).
Betacnumeric. The estimated constant coefficients, length kc.
Modelcharacter. The type of model (e.g., "GWR", "MGWR", "SAR", "tds_mgwr").
fixed_varscharacter. A vector with names of spatially constant covariates.
Ynumeric. The dependent variable.
XCmatrix. The explanatory variables with constant coefficients.
XVmatrix. The explanatory variables with varying coefficients.
Xmatrix. All explanatory variables.
WMatrix. The spatial weight matrix for spatial dependence (row-standardized).
isgcvlogical. Indicates if Leave-One-Out Cross-Validation (LOOCV) has been computed.
edfnumeric. The estimated effective degrees of freedom.
formulaformula. The model formula.
datadata.frame. The dataframe used for computation.
Methodcharacter. The estimation technique for spatial dependence models ('2SLS' or 'B2SLS'). Default is '2SLS'.
coordsmatrix. The spatial coordinates of observations.
Hnumeric. The bandwidth vector (spatial).
Htnumeric. The bandwidth vector (temporal), if applicable.
kernelscharacter. The kernel type(s) used (e.g., 'gauss', 'bisq').
adaptivelogical. Indicates if an adaptive kernel (nearest neighbors) was used.
Typecharacter. The type of Generalized Kernel Product ('GD' for spatial, 'GDT' for spatio-temporal).
TPnumeric. Indices of target points (if a subset was used).
SSRtpnumeric. The residual sum of squares calculated only on target points.
SSRnumeric. The total residual sum of squares.
residualsnumeric. The vector of residuals.
fitnumeric. The vector of fitted values.
prednumeric. The vector of predicted values (out-of-sample).
sevmatrix. Local standard errors of varying coefficients.
senumeric. Standard errors of constant coefficients.
tSnumeric. The trace of the Hat matrix (effective number of parameters).
Shatmatrix. The Hat matrix (or approximation).
R_klist. List of partial Hat matrices by covariate (for MGWR inference).
h_wnumeric. The bandwidth value used for computing the spatial weight matrix W. Default is 0.
kernel_wcharacter. The kernel type used for computing W. Default is NULL.
RMSEnumeric. Root Mean Square Error (on training data).
RMSEtpnumeric. Root Mean Square Error computed on target points.
CVnumeric. Leave-One-Out Cross-Validation score.
AICnumeric. Akaike Information Criterion.
AICcnumeric. Corrected Akaike Information Criterion.
AICctpnumeric. Corrected AIC for target points.
BICnumeric. Bayesian Information Criterion.
R2numeric. R-squared.
R2_adjnumeric. Adjusted R-squared.
get_tslogical. Indicates if the trace of the Hat matrix (Tr(S)) was stored.
NNnumeric. The maximum number of neighbors used for weight computation (truncation parameter).
ncorenumeric. Number of CPU cores used.
mycallcall. The original function call.
ctimenumeric. Computation time in seconds.
HRMSEmatrix. History of RMSE values (for iterative algorithms like backfitting).
HBETAlist. History of estimated Beta coefficients at each iteration.
logliknumeric. Log-likelihood value.
Glist. List containing neighboring indices and distances (knn object).
Vnumeric. Sequence of spatial bandwidths tested (for TDS algorithms).
Vtnumeric. Sequence of temporal bandwidths tested (for TDS algorithms).
Znumeric. Temporal or auxiliary variable for GDT kernel type.
TSnumeric. Diagonal elements of the Hat Matrix.
alphanumeric. Ratio parameter for GDT kernels (balancing space and time).
HMmatrix. Matrix of optimal bandwidths per covariate (for TDS).
HKminnumeric. Minimum allowed bandwidth per covariate (for TDS).
HKMINlist. List of minimum bandwidths per covariate for spatio-temporal models (TDS).
isolated_idxnumeric. Indices of observations without sufficient neighbors.
my_crsANY. Coordinate Reference System (CRS) information.
Internal Functions for mgwrsar Package
Description
Internal functions used by the mgwrsar package. These functions are exported for technical reasons but are not intended to be called directly by the typical user.
Usage
INST_C(A, B, C, D)
PhWY_C(A, B, C, D)
Proj_C(A, B)
QRcpp2_C(A, B, C)
Sl_C(A, B, C, D)
gwr_beta_pivotal_qrp_cpp(X, y, XV, indexG, Wd, TP, get_ts = FALSE, get_s = FALSE,
get_Rk = FALSE, get_se = FALSE)
gwr_beta_univar_cpp(y, x, XV, indexG, Wd, TP, get_ts = FALSE, get_s = FALSE)
mgwr_beta_pivotal_qrp_mixed_cpp(XV, y, XC, indexG, Wd, TP, get_ts = FALSE,
get_s = FALSE, get_Rk = FALSE, get_se = FALSE)
ApproxiW(W, TP, n)
mod(Y, X, W, X_s, Y_s, S, type, b2sls, get_ts, get_s)
get_index_mahalanobis_dual_rcpp(DS, DTM)
Arguments
A, B, C, D, X, y, XV, XC, indexG, Wd, TP, get_ts, get_s, get_Rk, get_se, x, W, n, X_s, Y_s, S, type, b2sls, DS, DTM |
Internal arguments. |
A bootstrap test for Betas for mgwrsar class model.
Description
A bootstrap test for Betas for mgwrsar class model.
Usage
mgwrsar_bootstrap_test(x0,x1,B=100,ncore=1,type='standard',eps='H1',
df='H1',focal='median',D=NULL,verbose=FALSE)
Arguments
x0 |
The H0 mgwrsar model |
x1 |
The H1 mgwrsar model |
B |
number of bootstrap repetitions, default 100 |
ncore |
number of cores |
type |
type of bootstap : 'wild','Rademacher','spatial' or 'standard' (default) |
eps |
Hypothesis under wich residuals are simulated, 'H0' or 'H1' (default) |
df |
Hypothesis under wich degree of freedom is estimated. |
focal |
see sample_stat help |
D |
A matrix of distance |
verbose |
default FAlSE |
Value
The value of the statictics test and a p ratio.
See Also
mgwrsar_bootstrap_test_all
A bootstrap test for testing nullity of all Betas for mgwrsar class model,
Description
A bootstrap test for testing nullity of all Betas for mgwrsar class model,
Usage
mgwrsar_bootstrap_test_all(model,B=100,ncore=1,type='standard')
Arguments
model |
A mgwrsar model |
B |
number of bootstrap replications, default 100 |
ncore |
number of cores for parallelization. |
type |
type of boostrap ('spatial','wild','random') |
Value
a matrix with statistical test values and p ratios
See Also
mgwrsar_bootstrap_test
Multiscale Geographically Weighted Regression (MGWR)
Description
This function estimates a Multiscale Geographically Weighted Regression (MGWR) model based on the proposition of Fotheringham et al. (2017). Unlike standard GWR where a single bandwidth is used for all covariates, MGWR allows for covariate-specific bandwidths. It uses a backfitting algorithm to iteratively estimate the optimal bandwidth and coefficients for each explanatory variable.
Usage
multiscale_gwr(formula, data, coords, kernels = 'bisq',
control_mgwr = list(), control = list())
Arguments
formula |
A formula object specifying the model (e.g., |
data |
A data frame containing the variables in the model. |
coords |
A matrix or data frame of coordinates (2 columns for spatial, 3 for spatio-temporal). |
kernels |
A character string specifying the kernel type.
Options include |
control_mgwr |
A named list of control parameters specific to the MGWR backfitting algorithm. See 'Details' for available components. |
control |
A named list of standard control arguments passed to the internal GWR estimation steps. See 'Details' for available components. |
Details
Components for control_mgwr:
initCharacter. The type of model used for initialization. Options are
'GWR'(default) or'lm'(OLS).maxiterInteger. Maximum number of backfitting iterations. Default is 20.
toleranceNumeric. Convergence threshold based on the change in RMSE or bandwidths. Default is
1e-6.nstableInteger. Number of consecutive iterations where bandwidths must remain stable to declare convergence. Default is 6.
H0Numeric vector. Optional initial bandwidths for each covariate. If
NULL, they are initialized via GWR or global search.get_AICLogical. If
TRUE, calculates the corrected Akaike Information Criterion (AICc) at the end. Default isFALSE.verboseLogical. If
TRUE, prints progress information during backfitting. Default isFALSE.
Components for control:
adaptiveLogical. If
TRUE(default), uses an adaptive bandwidth (k-nearest neighbors). IfFALSE, uses a fixed distance bandwidth.TypeCharacter. The type of spatial weighting.
'GD'(Geographical Distance, default) or'GDT'(Geo-Temporal).NNInteger. Maximum number of neighbors for matrix truncation (speeds up computation). Default is
nrow(data).ncoreInteger. Number of cores to use for parallel computation.
isgcvLogical. If
TRUE, computes Leave-One-Out Cross-Validation scores. Default isFALSE.
Value
An object of class mgwrsar containing:
Betav |
Matrix of estimated spatially varying coefficients. |
H |
Vector of final optimal bandwidths for each covariate. |
RMSE |
Root Mean Square Error of the final model. |
residuals |
Vector of residuals. |
fitted.values |
Vector of fitted values. |
AICc |
Corrected AIC (if |
R2 |
R-squared of the model. |
References
Fotheringham, A. S., Yang, W., & Kang, W. (2017). Multiscale geographically weighted regression (MGWR). Annals of the American Association of Geographers, 107(6), 1247-1265.
See Also
MGWRSAR, TDS_MGWR, golden_search_bandwidth
mydata is a simulated data set of a mgwrsar model
Description
mydata is a simulated data set of a mgwrsar model
Format
A data frames with 1000 rows 22 variables and a matrix of coordinates with two columns
Author(s)
Ghislain Geniaux and Davide Martinetti ghislain.geniaux@inrae.fr
References
doi:10.1016/j.regsciurbeco.2017.04.001
mydataf is a Simple Feature object with real estate data in south of France.
Description
mydataf is a Simple Feature object with real estate data in south of France.
Format
A sf object with 1403 rows, 5 columns
Author(s)
Ghislain Geniaux ghislain.geniaux@inrea.fr
References
https://www.data.gouv.fr/fr/datasets/demandes-de-valeurs-foncieres/
normW row normalization of dgCMatrix
Description
normW row normalization of dgCMatrix
Usage
normW(x)
Arguments
x |
A dgCMatrix class matrix |
Value
A row normalized dgCMatrix
Plot method for mgwrsar model
Description
Maps the results of an mgwrsar model using an interactive Plotly map. Supports plotting coefficients, t-statistics, residuals, and fitted values. Automatically switches between a geographic map (if CRS is present) and a scatter plot.
Visualizes the results of an MGWRSAR model (coefficients, t-statistics, residuals, or fitted values)
using interactive plots via the plotly package.
If a Coordinate Reference System (CRS) is provided (either via the crs argument or stored in the model object),
the function generates an interactive map. Otherwise, it generates a standard scatter plot of the values
against the coordinates.
Usage
## S4 method for signature 'mgwrsar,missing'
plot(
x,
y,
type = "coef",
var = NULL,
crs = NULL,
mypalette = "RdYlGn",
opacity = 0.8,
size = 5,
title = NULL,
show_legend = TRUE,
n_time_steps = 10,
...
)
## S3 method for class 'mgwrsar'
plot(
x,
type = "coef",
var = NULL,
crs = NULL,
mypalette = "RdYlGn",
size = 5,
opacity = 0.8,
title = NULL,
show_legend = TRUE,
n_time_steps = 10,
...
)
Arguments
x |
An object of class |
y |
missing (not used). |
type |
Character. The type of output to plot. Options are:
|
var |
Character. The name of the variable (covariate) to plot. Required if |
crs |
Numeric or character. The Coordinate Reference System (e.g., EPSG code) of the coordinates.
If |
mypalette |
Character. The color palette to use for the points (e.g., "RdYlGn", "Viridis"). Default is "RdYlGn". |
opacity |
Numeric. The opacity of the markers, between 0 and 1. Default is 0.8. |
size |
Numeric. The size of the markers. Default is 5. |
title |
Character. A custom title for the plot. If |
show_legend |
Logical. Whether to display the legend. Default is |
n_time_steps |
time step for spatio-temporal model (default 10). |
... |
Additional arguments passed to the internal plot function. |
Value
An interactive Plotly object (Mapbox or Scatter).
A plotly object representing the interactive plot (or map).
See Also
plot_effect plot_effect is a function that plots the effect of a variable X_k with spatially varying coefficient, i.e X_k * Beta_k(u_i,v_i) for comparing the magnitude of effects of between variables.
Description
plot_effect plot_effect is a function that plots the effect of a variable X_k with spatially varying coefficient, i.e X_k * Beta_k(u_i,v_i) for comparing the magnitude of effects of between variables.
Usage
plot_effect(model,sampling=TRUE,nsample=2000,nsample_max=5000,title='')
Arguments
model |
a model of mgwrsar class with some spatially varying coefficients. |
sampling |
Bolean, if nrow(model@Betav)> nsample_max a sample of size nsample is randomly selected, default TRUE. |
nsample |
integer, size of the sample if sampling is TRUE, default 2000. |
nsample_max |
integer, size max to engage sampling if sampling is TRUE, default 5000. |
title |
a title for the plot. |
Examples
library(mgwrsar)
## loading data example
data(mydata)
coords=as.matrix(mydata[,c("x","y")])
## Creating a spatial weight matrix (sparce dgCMatrix)
## of 8 nearest neighbors with 0 in diagonal
model_GWR0<-MGWRSAR(formula = 'Y_gwr~X1+X2+X3', data = mydata,coords=coords,
fixed_vars=NULL,kernels=c('gauss'),H=0.13, Model = 'GWR',control=list(SE=TRUE))
plot_effect(model_GWR0)
predict method for mgwrsar model
Description
predict method for mgwrsar model
Usage
## S4 method for signature 'mgwrsar'
predict(
object,
newdata,
newdata_coords,
W = NULL,
type = "BPN",
h_w = 100,
kernel_w = "rectangle",
maxobs = 4000,
beta_proj = FALSE,
method_pred = "TP",
k_extra = 8,
exposant = 8,
...
)
Arguments
object |
A model of class |
newdata |
a matrix or data.frame of new data. |
newdata_coords |
a matrix of new coordinates, and eventually other variables if a General Kernel Product is used. |
W |
the spatial weight matrix for models with spatial autocorrelation. |
type |
Type for BLUP estimator, default "BPN". If NULL use predictions without spatial bias correction. |
h_w |
A bandwidth value for the spatial weight matrix |
kernel_w |
kernel type for the spatial weight matrix. Possible types: rectangle ("rectangle"), bisquare ("bisq"), tricube ("tcub"), epanechnikov ("epane"), gaussian ("gauss")) . |
maxobs |
maximum number of observations for exact calculation of solve(I- rho*W), default maxobs=4000. |
beta_proj |
A boolean, if TRUE the function then return a two elements list(Y_predicted,Beta_proj_out) |
method_pred |
If method_pred = 'TP' (default) prediction is done by recomputing a MGWRSAR model with new-data as target points, else if method_pred in ('tWtp_model','model','shepard') a matrix for projecting estimated betas is used (see details). |
k_extra |
number of neighboors for local parameter extrapolation if shepard kernel is used, default 8. |
exposant |
shapenig parameter for tds_mgtwr model, default 6. |
... |
predict parameters forwarded. |
Details
if method_pred ='tWtp_model', the weighting matrix for prediction is based on the expected weights of outsample data if they were had been added to insample data to estimate the corresponding MGWRSAR (see Geniaux 2022 for further detail), if method_pred ='shepard'a shepard kernel with k_extra neighbours (default 8) is used and if method_pred='kernel_model' the same kernel and number of neighbors as for computing the MGWRSAR model is used.
Value
A vector of predictions if beta_proj is FALSE or a list with a vector named Y_predicted and a matrix named Beta_proj_out.
A vector of predictions.
reord_D
Description
Reorders the elements of a distance matrix row by row according to a corresponding index matrix.
Usage
reord_D(M, idxG)
Arguments
M |
A numeric matrix to be reordered. |
idxG |
An integer matrix of the same number of rows as 'M', where each row contains column indices indicating the new ordering for that row. |
Value
A reordered numeric matrix with the same number of rows as 'M' and columns equal to the maximum index in 'idxG'.
reord_M
Description
Reorders the elements of a matrix row by row according to a corresponding index matrix. Each row of the index matrix specifies the new column order for the corresponding row of the input matrix.
Usage
reord_M_R(M, idxG)
Arguments
M |
A numeric matrix to be reordered. |
idxG |
An integer matrix of the same number of rows as 'M', where each row contains column indices indicating the new ordering for that row. |
Details
This function performs a scatter-like reordering: for each row 'i', the values of 'M[i, ]' are placed in the positions specified by 'idxG[i, ]'. It is particularly useful for spatial or neighborhood-based rearrangements where each observation has its own local indexing of neighbors.
Value
A reordered numeric matrix with the same number of rows as 'M' and columns equal to the maximum index in 'idxG'.
Examples
M <- matrix(1:9, nrow = 3, byrow = TRUE)
idxG <- matrix(c(3, 1, 2, 1, 2, 3, 2, 3, 1), nrow = 3, byrow = TRUE)
reord_M_R(M, idxG)
residuals for mgwrsar model
Description
residuals for mgwrsar model
Usage
## S4 method for signature 'mgwrsar'
residuals(object, ...)
Arguments
object |
A model of class |
... |
residuals parameters forwarded. |
Value
A vector of residuals.
Bandwidth Selection via Multi-round Grid Search based on AICc
Description
This function selects the optimal bandwidths (spatial and/or temporal) for GWR or MGWR models by minimizing the Corrected Akaike Information Criterion (AICc). It uses a multi-round grid search approach (coarse-to-fine) to efficiently narrow down the optimal parameter space before optionally applying a golden section search for final refinement.
Usage
search_bandwidths(
formula,
data,
coords,
fixed_vars = NULL,
kernels = c("gauss", "gauss"),
Model = "GWR",
control = list(),
hs_range = NULL,
ht_range = NULL,
n_seq = 10,
ncore = 1,
n_rounds = 0,
refine = TRUE,
verbose = FALSE,
show_progress = FALSE,
tol = NULL,
parallel_method = "auto"
)
Arguments
formula |
A formula object specifying the model (e.g., |
data |
A data frame containing the variables in the model. |
coords |
A matrix or data frame of coordinates (2 columns for spatial, 1 for temporal, or more for GDT). |
fixed_vars |
A character vector indicating the names of variables with spatially stationary (fixed) coefficients.
Default is |
kernels |
A character vector specifying the kernel types for spatial and temporal components
(e.g., |
Model |
A character string specifying the model type. Options include "GWR", "MGWR", "OLS", "SAR", etc. Default is "GWR". |
control |
A named list of extra control arguments passed to the |
hs_range |
A numeric vector of length 2 defining the lower and upper bounds for the spatial bandwidth search. |
ht_range |
A numeric vector of length 2 defining the lower and upper bounds for the temporal bandwidth search.
Set to |
n_seq |
An integer specifying the number of bandwidth candidates to test per dimension in each round. |
ncore |
An integer specifying the number of CPU cores to use for parallel processing.
Default is |
n_rounds |
An integer specifying the number of grid search rounds (zooming steps). Default is 3. |
refine |
Logical. If |
verbose |
Logical. If |
show_progress |
Logical. If |
tol |
A numeric vector of length 2 (or 1) specifying the
tolerance for spatial and temporal bandwidths. If |
parallel_method |
Parallelization method ("auto", "fork", "socket"); "auto" selects the best available backend depending on the OS, other values run sequentially. |
Details
The function performs a grid search over n_rounds. In the first round, it tests n_seq
candidates linearly or geometrically spaced within the provided ranges. In subsequent rounds,
the search range is narrowed around the best candidate from the previous round.
Value
A list containing:
mode |
The mode of optimization ("spatial_only" or "spatio-temporal"). |
results |
A list of data frames containing the results (bandwidths and AICc) for each round. |
best |
A data frame row corresponding to the best parameter combination found. |
refined |
The result of the refinement step (if |
best_model |
The final |
ctime |
The total computation time. |
See Also
Simulate Data Generating Processes (DGP) for Multiscale GWR
Description
The simu_multiscale function generates synthetic datasets with spatially varying coefficients
based on Data Generating Processes (DGP) proposed in the literature. It supports
formulations from Fotheringham et al. (2017), Gao et al. (2021), and Geniaux (2024).
It also allows for the introduction of spatial autocorrelation (SAR) in the response variable.
Usage
simu_multiscale(
n = 1000,
myseed = 1,
type = "GG2024",
constant = NULL,
nuls = NULL,
lambda = NULL,
NN = 4,
config_beta = "default",
config_snr = 0.7,
config_eps = "normal"
)
Arguments
n |
Integer. The number of observations to simulate. Default is 1000. |
myseed |
Integer. Random seed for reproducibility. Default is 1. |
type |
Character. The type of DGP to use. Options are |
constant |
Vector. Indices or names of explanatory variables that should have constant (spatially stationary) coefficients. Default is |
nuls |
Vector. Indices of explanatory variables that should have a null effect (coefficient = 0). Default is |
lambda |
Numeric. The spatial autoregressive parameter (rho) if a SAR process is desired. Default is |
NN |
Integer. The number of nearest neighbors used to construct the spatial weight matrix |
config_beta |
Character. Configuration of the spatial pattern for Beta coefficients (e.g., |
config_snr |
Numeric. The desired Signal-to-Noise Ratio (SNR). Default is 0.7. |
config_eps |
Character. The distribution of the error term. Options are |
Value
A named list containing:
- mydata
A data frame with the simulated response variable
yand explanatory variables.- coords
A matrix of spatial coordinates.
- Betav
A matrix of the true spatially varying coefficients.
- W
The spatial weight matrix (if
lambdais not NULL).
References
Fotheringham, A. S., Yang, W., & Kang, W. (2017). Multiscale geographically weighted regression (MGWR). Annals of the American Association of Geographers, 107(6), 1247-1265.
Gao, S., Mei, C. L., Xu, Q. X., & Wang, N. (2021). Non-iterative multiscale estimation for spatial autoregressive geographically weighted regression models. Entropy, 25(2), 320.
Geniaux, G. (2025). Top-Down Scale Approaches for Multiscale GWR with Locally Adaptive Bandwidths. Springer Nature, 2021 LATEX template.
Examples
library(mgwrsar)
library(ggplot2)
library(gridExtra)
library(grid)
# Simulate data using Geniaux (2024) DGP
simu <- simu_multiscale(n = 1000, type = 'GG2024', config_snr = 0.7)
mydata <- simu$mydata
coords <- simu$coords
# Visualizing the spatial patterns of the coefficients
p1 <- ggplot(mydata, aes(x, y, col = Beta1)) + geom_point() + scale_color_viridis_c()
p2 <- ggplot(mydata, aes(x, y, col = Beta2)) + geom_point() + scale_color_viridis_c()
p3 <- ggplot(mydata, aes(x, y, col = Beta3)) + geom_point() + scale_color_viridis_c()
p4 <- ggplot(mydata, aes(x, y, col = Beta4)) + geom_point() + scale_color_viridis_c()
grid.arrange(p1, p2, p3, p4, nrow = 2, ncol = 2,
top = textGrob("DGP Geniaux (2024)", gp = gpar(fontsize = 20, font = 3)))
summary for mgwrsar model
Description
summary for mgwrsar model
Usage
## S4 method for signature 'mgwrsar'
summary(object, ...)
Arguments
object |
A model of class |
... |
summary parameters forwarded. |
Value
A summary object.
summary_Matrix to be documented
Description
summary_Matrix to be documented
Usage
summary_Matrix(object, ...)
Arguments
object |
to be documented |
... |
to be documented |
Value
to be documented