hgwrr

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

The package hgwrr is used to calibrate Hierarchical and Geographically Weighted Regression (HGWR) model on spatial data. It requires the spatial hierarchical structure in the data; i.e., samples are grouped by their locations. All the variables are either in the group level or sample level. For the group-level variables, they can have fixed effects (globally constant) or spatially weighted effects (varying with the location). For the sample-level variables, they can have fixed effects or random effects (varying among groups). We note the fixed effects as $\beta$, the group-level spatially weighted (GLSW) effects as $\gamma$, and sample-level random (SLR) effects as $\mu$. The HGWR model consists of these three kinds of effects and estimates the three kinds of effects considering the spatial heterogeneity.

library(hgwrr)

Usage

Model calibration

To calibrate a HGWR model, use the function hgwr().

hgwr(
  formula, data, ..., bw = "CV",
  kernel = c("gaussian", "bisquared"),
  alpha = 0.01, eps_iter = 1e-6, eps_gradient = 1e-6,
  max_iters = 1e6, max_retries = 1e6,
  ml_type = c("D_Only", "D_Beta"), verbose = 0
)

The following is explanation of some important parameters.

formula

This parameter specifies the model form. Recall that the three kinds of effects are GLSW, fixed, and SLR effects. They are specified in different parts of the formula.

response ~ L(GLSW) + fixed + (SLR | group)

In the formula, L() is used to mark some effects as GLSW effects, and ( | group) is used to set the SLR effects and grouping indicator. Only group-level variables can have GLSW effects.

data

sf objects

From version 0.3-1, this parameter supports sf objects. In this case, no further arguments in ... are required. Here is an example.

data(wuhan.hp)
m_sf <- hgwr(
  formula = Price ~ L(d.Water + d.Commercial) + BuildingArea + (Floor.High | group),
  data = wuhan.hp,
  bw = 299
)

data.frame objects

If the data is a normal data.frame object, an extra argument coords is required to specify the coordinates of each group. Note that the row order of coords needs to match that of the group variable. Here is an example.

data(mulsam.test)
m_df <- hgwr(
  formula = y ~ L(g1 + g2) + x1 + (z1 | group),
  data = mulsam.test$data,
  coords = mulsam.test$coords
)

bw and kernel

Argument bw is the bandwidth used to estimate GLSW effects. It can be either of the following options:

Argument kernel is the kernel function used to estimate GLSW effects. Currently, there are only two choices: "gaussian" and "bisquared".

Results

The output of returned object of hgwr() shows the estimates of the effects.

m_df

And the summary() method shows some diagnostic information.

summary(m_df)

The significance level of spatial heterogeneity in GLSW effects can be tested with the following codes.

summary(m_df, test_hetero = T)

Some other methods are provided.

head(coef(m_df))
head(fitted(m_df))
head(residuals(m_df))

Further reading

Model comparison

Mathematical basis

The following papers shows more details about the mathematical basis about the HGWR model.



Try the hgwrr package in your browser

Any scripts or data that you put into this service are public.

hgwrr documentation built on April 4, 2025, 3:57 a.m.