The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

hgwrr

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)
#> Loading required package: sf
#> Warning: package 'sf' was built under R version 4.2.3
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE

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(multisampling)
m_df <- hgwr(
  formula = y ~ L(g1 + g2) + x1 + (z1 | group),
  data = multisampling$data,
  coords = multisampling$coords
)

bw and kernel

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

  • A integer value representing the number of nearest neighbours.
  • "CV" letting the algorithm select one.

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_sf
#> Hierarchical and geographically weighted regression model
#> =========================================================
#> Formula: Price ~ L(d.Water + d.Commercial) + BuildingArea + (Floor.High |  
#>     group)
#>  Method: Back-fitting and Maximum likelihood
#>    Data: wuhan.hp
#> 
#> Global Fixed Effects
#> -------------------
#>  Intercept  BuildingArea 
#>  10.008232     -0.020732 
#> 
#> Local Fixed Effects
#> -------------------
#> Bandwidth: 299 (nearest neighbours)
#>   Coefficient           Min  1st Quartile     Median  3rd Quartile         Max 
#>     Intercept     -2.956253     -0.125587   0.143338      0.405493    3.572753 
#>       d.Water   -157.647881    -14.198803  -2.423598      6.753992  169.499432 
#>  d.Commercial  -1229.561779     -9.364932  -1.297365     13.221426  355.622525 
#> 
#> Random Effects
#> --------------
#>    Groups        Name  Std.Dev.      Corr 
#>     group   Intercept  0.093518           
#>            Floor.High  0.084110  0.051649 
#>  Residual              0.081659           
#> 
#> Other Information
#> -----------------
#> Number of Obs: 19599
#>        Groups: group , 776

And the summary() method shows some diagnostic information.

summary(m_sf)
#> Hierarchical and geographically weighted regression model
#> =========================================================
#> Formula: Price ~ L(d.Water + d.Commercial) + BuildingArea + (Floor.High |  
#>     group)
#>  Method: Back-fitting and Maximum likelihood
#>    Data: wuhan.hp
#> 
#> Parameter estimates
#> -------------------
#> Fixed effects:
#>                  Estimated    Sd. Err       t.val   Pr(>|t|)      
#>     Intercept  10.00823152  0.1393714  71.8097949  0.0000000  *** 
#>  BuildingArea  -0.02073159  0.0290205   0.7143773  0.4750026      
#> 
#> Local fixed effects:
#>                         Min  1st Quartile     Median  3rd Quartile         Max 
#>     Intercept     -2.956253     -0.125587   0.143338      0.405493    3.572753 
#>       d.Water   -157.647881    -14.198803  -2.423598      6.753992  169.499432 
#>  d.Commercial  -1229.561779     -9.364932  -1.297365     13.221426  355.622525 
#> 
#> Diagnostics
#> -----------
#>  rsquared       0.921337 
#>    logLik   47101.028127 
#>       AIC  -93562.186582 
#> 
#> Scaled residuals
#> ----------------
#>        Min         1Q     Median        3Q       Max 
#>  -0.507137  -0.045648  -0.002332  0.041843  0.859992 
#> 
#> Other Information
#> -----------------
#> Number of Obs: 19599
#>        Groups: group , 776

Some other methods are provided.

head(coef(m_sf))
#>   Intercept    d.Water d.Commercial BuildingArea   Floor.High
#> 1  10.03093 -0.6959796    -5.542168  -0.02073159 -0.009766081
#> 2   9.95649 -1.2036938    -4.599027  -0.02073159  0.032570342
#> 3  10.19179 -2.6167090    -6.640576  -0.02073159 -0.008295003
#> 4  10.32102 -2.3586815    -7.352020  -0.02073159  0.025201766
#> 5  10.00403 -1.6968718    -4.245220  -0.02073159  0.019346672
#> 6  10.36764 -4.1144536    -6.600430  -0.02073159  0.072659049
head(fitted(m_sf))
#> [1] 9.494168 9.492037 9.494307 9.494246 9.505008 9.495766
head(residuals(m_sf))
#> [1] -0.03564015 -0.13629818 -0.05890533  0.07927795  0.15277073  0.16673234

Further reading

Model comparison

Mathematical basis

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

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.