A Hierarchical Linear Model (HLM) with group-level geographically weighted effects.
Usage
# S3 method for class 'hgwrm'
spatial_hetero_test(
x,
round = 99,
statistic = stat_glsw,
parallel = FALSE,
verbose = 0,
...
)
hgwr(
formula,
data,
...,
bw = "CV",
kernel = c("gaussian", "bisquared"),
alpha = 0.01,
eps_iter = 1e-06,
eps_gradient = 1e-06,
max_iters = 1e+06,
max_retries = 1e+06,
ml_type = c("D_Only", "D_Beta"),
f_test = FALSE,
verbose = 0
)
# S3 method for class 'sf'
hgwr(
formula,
data,
...,
bw = "CV",
kernel = c("gaussian", "bisquared"),
alpha = 0.01,
eps_iter = 1e-06,
eps_gradient = 1e-06,
max_iters = 1e+06,
max_retries = 1e+06,
ml_type = c("D_Only", "D_Beta"),
f_test = FALSE,
verbose = 0
)
# S3 method for class 'data.frame'
hgwr(
formula,
data,
...,
coords,
bw = "CV",
kernel = c("gaussian", "bisquared"),
alpha = 0.01,
eps_iter = 1e-06,
eps_gradient = 1e-06,
max_iters = 1e+06,
max_retries = 1e+06,
ml_type = c("D_Only", "D_Beta"),
f_test = FALSE,
verbose = 0
)
hgwr_fit(
formula,
data,
coords,
bw = c("CV", "AIC"),
kernel = c("gaussian", "bisquared"),
alpha = 0.01,
eps_iter = 1e-06,
eps_gradient = 1e-06,
max_iters = 1e+06,
max_retries = 1e+06,
ml_type = c("D_Only", "D_Beta"),
f_test = FALSE,
verbose = 0
)
Arguments
- x
An
hgwrm
object- round
The number of times to sampling from model.
- statistic
A function used to calculate the statistics on the original data and bootstrapped data. Default to the variance of standardlised GLSW estimates.
- parallel
If TRUE, use
furrr
package to parallel.- verbose
An integer value. Determine the log level. Possible values are:
- 0
no log is printed.
- 1
only logs in back-fitting are printed.
- 2
all logs are printed.
- ...
Further arguments for the specified type of
data
.- formula
A formula. Its structure is similar to
lmer
function in lme4 package. Models can be specified with the following form:response ~ L(glsw) + fixed + (random | group)
For more information, please see the
formula
subsection in details.- data
The data.
- bw
A numeric value. It is the value of bandwidth or
"CV"
. In this stage this function only support adaptive bandwidth. And its unit must be the number of nearest neighbours. If"CV"
is specified, the algorithm will automatically select an optimized bandwidth value.- kernel
A character value. It specify which kernel function is used in GWR part. Possible values are
gaussian
Gaussian kernel function \(k(d)=\exp\left(-\frac{d^2}{b^2}\right)\)
bisquared
Bi-squared kernel function. If \(d<b\) then \(k(d)=\left(1-\frac{d^2}{b^2}\right)^2\) else \(k(d)=0\)
- alpha
A numeric value. It is the size of the first trial step in maximum likelihood algorithm.
- eps_iter
A numeric value. Terminate threshold of back-fitting.
- eps_gradient
A numeric value. Terminate threshold of maximum likelihood algorithm.
- max_iters
An integer value. The maximum of iteration.
- max_retries
An integer value. If the algorithm tends to be diverge, it stops automatically after trying max_retires times.
- ml_type
An integer value. Represent which maximum likelihood algorithm is used. Possible values are:
D_Only
Only \(D\) is specified by maximum likelihood.
D_Beta
Both \(D\) and \(beta\) is specified by maximum likelihood.
- f_test
A logical value. Determine whether to do F test on GLSW effects. If
f_test=TURE
, there will be af_test
item in the returned object showing the F test for each GLSW effect.- coords
A 2-column matrix. It consists of coordinates for each group.
Value
A list describing the model with following fields.
gamma
Coefficients of group-level spatially weighted effects.
beta
Coefficients of fixed effects.
mu
Coefficients of sample-level random effects.
D
Variance-covariance matrix of sample-level random effects.
sigma
Variance of errors.
effects
A list including names of all effects.
call
Calling of this function.
frame
The DataFrame object sent to this call.
frame.parsed
Variables extracted from the data.
groups
Unique group labels extracted from the data.
f_test
A list of F test for GLSW effects. Only exists when
f_test=TRUE
. Each item contains the F value, degrees of freedom in the numerator, degrees of freedom in the denominator, and \(p\) value of \(F>F_\alpha\).
Details
Effect Specification in Formula
In the HGWR model, there are three types of effects specified by the
formula
argument:
- Group-level spatially weighted (GLSW, aka. local fixed) effects
Effects wrapped by functional symbol
L
.- Sample-level random (SLR) effects
Effects specified outside the functional symbol
L
but to the left of symbol|
.- Fixed effects
Other effects
For example, the following formula in the example of this function below is written as
y ~ L(g1 + g2) + x1 + (z1 | group)
where g1
and g2
are GLSW effects,
x1
is the fixed effects,
and z1
is the SLR effects grouped by the group indicator group
.
Note that SLR effects can only be specified once!
Functions
spatial_hetero_test(hgwrm)
: Test the spatial heterogeneity with bootstrapping.hgwr_fit()
: Fit a HGWR model
Examples
data(mulsam.test)
hgwr(
formula = y ~ L(g1 + g2) + x1 + (z1 | group),
data = mulsam.test$data,
coords = mulsam.test$coords,
bw = 10
)
#> Hierarchical and geographically weighted regression model
#> =========================================================
#> Formula: y ~ L(g1 + g2) + x1 + (z1 | group)
#> Method: Back-fitting and Maximum likelihood
#> Data: mulsam.test$data
#>
#> Fixed Effects
#> -------------
#> Intercept x1
#> 1.852190 1.967644
#>
#> Group-level Spatially Weighted Effects
#> --------------------------------------
#> Bandwidth: 10 (nearest neighbours)
#>
#> Coefficient estimates:
#> Coefficient Min 1st Quartile Median 3rd Quartile Max
#> Intercept -0.549094 -0.439522 -0.151433 -0.024133 0.178044
#> g1 0.909293 1.253143 1.692616 1.927313 2.310056
#> g2 1.083410 1.279953 1.415744 1.594576 1.693768
#>
#> Sample-level Random Effects
#> ---------------------------
#> Groups Name Std.Dev. Corr
#> group Intercept 1.033171
#> z1 1.033171 0.000000
#> Residual 1.033171
#>
#> Other Information
#> -----------------
#> Number of Obs: 873
#> Groups: group , 25
mod_Ftest <- hgwr(
formula = y ~ L(g1 + g2) + x1 + (z1 | group),
data = mulsam.test$data,
coords = mulsam.test$coords,
bw = 10
)
summary(mod_Ftest)
#> Hierarchical and geographically weighted regression model
#> =========================================================
#> Formula: y ~ L(g1 + g2) + x1 + (z1 | group)
#> Method: Back-fitting and Maximum likelihood
#> Data: mulsam.test$data
#>
#> Parameter Estimates
#> -------------------
#> Fixed effects:
#> Estimated Sd. Err t.val Pr(>|t|)
#> Intercept 1.852190 0.203079 9.120541 0.000000 ***
#> x1 1.967644 0.033827 58.168539 0.000000 ***
#>
#> Bandwidth: 10 (nearest neighbours)
#>
#> GLSW effects:
#> Mean Est. Mean Sd. *** ** * .
#> Intercept -0.208441 0.247059 0.0% 0.0% 4.0% 24.0%
#> g1 1.631474 1.795246 0.0% 0.0% 0.0% 0.0%
#> g2 1.430116 1.476570 0.0% 0.0% 0.0% 0.0%
#>
#> SLR effects:
#> Groups Name Mean Std.Dev. Corr
#> group Intercept 0.000000 1.033171
#> z1 1.869539 1.033171 0.000000
#> Residual 0.079964 1.033171
#>
#>
#> Diagnostics
#> -----------
#> rsquared 0.905207
#> logLik NaN
#> AIC NaN
#>
#> Scaled Residuals
#> ----------------
#> Min 1Q Median 3Q Max
#> -3.416380 -0.584726 0.092501 0.725766 3.028003
#>
#> Other Information
#> -----------------
#> Number of Obs: 873
#> Groups: group , 25