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:

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:

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., y ~ x1 + x2).

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:

  • 'tds_mgwr' (default): Top-Down Scale MGWR (Stage 1 only).

  • 'atds_mgwr': Adaptive Top-Down Scale MGWR (Stage 1 + Stage 2 boosting).

  • 'atds_gwr': Univariate Adaptive Top-Down Scale GWR.

kernels

A character string or vector specifying the kernel type (e.g., 'triangle', 'bisq', 'gauss'). Default is 'triangle'.

fixed_vars

A character vector indicating the names of variables with spatially stationary (fixed) coefficients. Default is NULL.

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:

nns

Integer. Number of bandwidth steps in the decreasing sequence (default 30).

get_AIC

Logical. If TRUE, computes AICc (slower). Default FALSE (except for 'atds_mgwr').

init_model

Character. Initial model type to start backfitting: 'OLS' (default), 'GWR', 'GTWR', or 'known'.

ncore

Integer. Number of cores for parallelization. Default 1.

tol

Numeric. Convergence tolerance. Default 0.001.

nrounds

Integer. Number of boosting rounds for Stage 2 (only for 'atds_mgwr'). Default 3.

control

A named list of standard control arguments passed to the internal MGWRSAR calls:

adaptive

Logical or Vector. TRUE for adaptive bandwidth (nearest neighbors), FALSE for fixed distance. Can be a vector of length 2 for space/time.

Type

Character. Spatial weighting type: 'GD' (Spatial only) or 'GDT' (Space-Time).

NN

Integer. Maximum number of neighbors for distance matrix computation (truncation). Default is nrow(data).

Details

The TDS algorithm works in two stages:

  1. **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.

  2. **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 mgwrsar-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 mgwrsar-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., y ~ x1 + x2).

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 NULL (all coefficients are varying).

kernels

A character vector specifying the kernel types for spatial and temporal components (e.g., c("gauss", "gauss")).

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 MGWRSAR function (e.g., adaptive, NN, Z).

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 (x, y) and optional temporal (t) coordinates.

Details

Depending on the dimensionality of the input, uniqueness is enforced as follows:

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

Betav

matrix. The estimated varying coefficients, dimension (n, kv).

Betac

numeric. The estimated constant coefficients, length kc.

Model

character. The type of model (e.g., "GWR", "MGWR", "SAR", "tds_mgwr").

fixed_vars

character. A vector with names of spatially constant covariates.

Y

numeric. The dependent variable.

XC

matrix. The explanatory variables with constant coefficients.

XV

matrix. The explanatory variables with varying coefficients.

X

matrix. All explanatory variables.

W

Matrix. The spatial weight matrix for spatial dependence (row-standardized).

isgcv

logical. Indicates if Leave-One-Out Cross-Validation (LOOCV) has been computed.

edf

numeric. The estimated effective degrees of freedom.

formula

formula. The model formula.

data

data.frame. The dataframe used for computation.

Method

character. The estimation technique for spatial dependence models ('2SLS' or 'B2SLS'). Default is '2SLS'.

coords

matrix. The spatial coordinates of observations.

H

numeric. The bandwidth vector (spatial).

Ht

numeric. The bandwidth vector (temporal), if applicable.

kernels

character. The kernel type(s) used (e.g., 'gauss', 'bisq').

adaptive

logical. Indicates if an adaptive kernel (nearest neighbors) was used.

Type

character. The type of Generalized Kernel Product ('GD' for spatial, 'GDT' for spatio-temporal).

TP

numeric. Indices of target points (if a subset was used).

SSRtp

numeric. The residual sum of squares calculated only on target points.

SSR

numeric. The total residual sum of squares.

residuals

numeric. The vector of residuals.

fit

numeric. The vector of fitted values.

pred

numeric. The vector of predicted values (out-of-sample).

sev

matrix. Local standard errors of varying coefficients.

se

numeric. Standard errors of constant coefficients.

tS

numeric. The trace of the Hat matrix (effective number of parameters).

Shat

matrix. The Hat matrix (or approximation).

R_k

list. List of partial Hat matrices by covariate (for MGWR inference).

h_w

numeric. The bandwidth value used for computing the spatial weight matrix W. Default is 0.

kernel_w

character. The kernel type used for computing W. Default is NULL.

RMSE

numeric. Root Mean Square Error (on training data).

RMSEtp

numeric. Root Mean Square Error computed on target points.

CV

numeric. Leave-One-Out Cross-Validation score.

AIC

numeric. Akaike Information Criterion.

AICc

numeric. Corrected Akaike Information Criterion.

AICctp

numeric. Corrected AIC for target points.

BIC

numeric. Bayesian Information Criterion.

R2

numeric. R-squared.

R2_adj

numeric. Adjusted R-squared.

get_ts

logical. Indicates if the trace of the Hat matrix (Tr(S)) was stored.

NN

numeric. The maximum number of neighbors used for weight computation (truncation parameter).

ncore

numeric. Number of CPU cores used.

mycall

call. The original function call.

ctime

numeric. Computation time in seconds.

HRMSE

matrix. History of RMSE values (for iterative algorithms like backfitting).

HBETA

list. History of estimated Beta coefficients at each iteration.

loglik

numeric. Log-likelihood value.

G

list. List containing neighboring indices and distances (knn object).

V

numeric. Sequence of spatial bandwidths tested (for TDS algorithms).

Vt

numeric. Sequence of temporal bandwidths tested (for TDS algorithms).

Z

numeric. Temporal or auxiliary variable for GDT kernel type.

TS

numeric. Diagonal elements of the Hat Matrix.

alpha

numeric. Ratio parameter for GDT kernels (balancing space and time).

HM

matrix. Matrix of optimal bandwidths per covariate (for TDS).

HKmin

numeric. Minimum allowed bandwidth per covariate (for TDS).

HKMIN

list. List of minimum bandwidths per covariate for spatio-temporal models (TDS).

isolated_idx

numeric. Indices of observations without sufficient neighbors.

my_crs

ANY. 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., y ~ x1 + x2).

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 'bisq' (default), 'gauss', 'triangle', 'tricube', 'rectangle'.

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:

init

Character. The type of model used for initialization. Options are 'GWR' (default) or 'lm' (OLS).

maxiter

Integer. Maximum number of backfitting iterations. Default is 20.

tolerance

Numeric. Convergence threshold based on the change in RMSE or bandwidths. Default is 1e-6.

nstable

Integer. Number of consecutive iterations where bandwidths must remain stable to declare convergence. Default is 6.

H0

Numeric vector. Optional initial bandwidths for each covariate. If NULL, they are initialized via GWR or global search.

get_AIC

Logical. If TRUE, calculates the corrected Akaike Information Criterion (AICc) at the end. Default is FALSE.

verbose

Logical. If TRUE, prints progress information during backfitting. Default is FALSE.

Components for control:

adaptive

Logical. If TRUE (default), uses an adaptive bandwidth (k-nearest neighbors). If FALSE, uses a fixed distance bandwidth.

Type

Character. The type of spatial weighting. 'GD' (Geographical Distance, default) or 'GDT' (Geo-Temporal).

NN

Integer. Maximum number of neighbors for matrix truncation (speeds up computation). Default is nrow(data).

ncore

Integer. Number of cores to use for parallel computation.

isgcv

Logical. If TRUE, computes Leave-One-Out Cross-Validation scores. Default is FALSE.

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 get_AIC = TRUE).

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 mgwrsar.

y

missing (not used).

type

Character. The type of output to plot. Options are:

  • 'coef': Spatially varying coefficients (default).

  • 't_coef': t-statistics of the coefficients (visualized with a significance threshold of 1.96).

  • 'residuals': Model residuals.

  • 'fitted': Fitted values.

var

Character. The name of the variable (covariate) to plot. Required if type is 'coef' or 't_coef'.

crs

Numeric or character. The Coordinate Reference System (e.g., EPSG code) of the coordinates. If NULL, the function attempts to retrieve the CRS from x@my_crs.

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 NULL, a default title is automatically generated.

show_legend

Logical. Whether to display the legend. Default is TRUE.

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

MGWRSAR


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 mgwrsar-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 mgwrsar-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., y ~ x1 + x2).

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 NULL (all coefficients are varying).

kernels

A character vector specifying the kernel types for spatial and temporal components (e.g., c("gauss", "gauss")).

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 MGWRSAR function (e.g., adaptive, NN, Z).

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 NULL for spatial-only models.

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 parallel::detectCores() - 1.

n_rounds

An integer specifying the number of grid search rounds (zooming steps). Default is 3.

refine

Logical. If TRUE, a final optimization step using golden section search is performed around the best candidate found. Default is FALSE.

verbose

Logical. If TRUE, prints progress messages to the console.

show_progress

Logical. If TRUE, displays a progress bar during computation.

tol

A numeric vector of length 2 (or 1) specifying the tolerance for spatial and temporal bandwidths. If NULL, it is calculated automatically based on the range.

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 refine = TRUE).

best_model

The final mgwrsar model object fitted with the optimal bandwidths.

ctime

The total computation time.

See Also

MGWRSAR


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 'FT2017', 'Gao2021', or 'GG2024' (default).

constant

Vector. Indices or names of explanatory variables that should have constant (spatially stationary) coefficients. Default is NULL (all coefficients vary).

nuls

Vector. Indices of explanatory variables that should have a null effect (coefficient = 0). Default is NULL.

lambda

Numeric. The spatial autoregressive parameter (rho) if a SAR process is desired. Default is NULL (no spatial autocorrelation).

NN

Integer. The number of nearest neighbors used to construct the spatial weight matrix W if lambda is provided. Default is 4.

config_beta

Character. Configuration of the spatial pattern for Beta coefficients (e.g., 'default').

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 'normal' (default), 'unif', or 'Chi2'.

Value

A named list containing:

mydata

A data frame with the simulated response variable y and explanatory variables.

coords

A matrix of spatial coordinates.

Betav

A matrix of the true spatially varying coefficients.

W

The spatial weight matrix (if lambda is 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 mgwrsar-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