Skip to content

Commit 86854c2

Browse files
RoryuOuYiyunZhuang
andauthored
Feature: GW Correlation (#32)
* Feature: gwcorrelation basic flow * fix bugs in calculation, add test, * edit: rename R file, add telegram * edit: output format * fix: output --------- Co-authored-by: zyy <2711245442@qq.com>
1 parent b8618d2 commit 86854c2

16 files changed

Lines changed: 1219 additions & 5 deletions

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ S3method(fitted,gtdrm)
77
S3method(fitted,gwrm)
88
S3method(fitted,gwrmultiscalem)
99
S3method(plot,gtdrm)
10+
S3method(plot,gwcorrm)
1011
S3method(plot,gwrm)
1112
S3method(plot,gwrmultiscalem)
1213
S3method(plot,modelselcritl)
1314
S3method(predict,gwrm)
1415
S3method(print,gtdrm)
1516
S3method(print,gwavgm)
17+
S3method(print,gwcorrm)
1618
S3method(print,gwrm)
1719
S3method(print,gwrmultiscalem)
1820
S3method(residuals,gtdrm)
@@ -24,6 +26,8 @@ S3method(step,gwrm)
2426
export(gtdr)
2527
export(gtdr_config)
2628
export(gwaverage)
29+
export(gwcorr_config)
30+
export(gwcorrelation)
2731
export(gwr_basic)
2832
export(gwr_multiscale)
2933
export(mgwr_config)
@@ -33,6 +37,7 @@ export(step_view_circle)
3337
export(step_view_diff)
3438
export(step_view_value)
3539
exportClasses(GTDRConfig)
40+
exportClasses(GWCorrConfig)
3641
exportClasses(MGWRConfig)
3742
exportMethods(rep)
3843
exportMethods(rep.int)
@@ -47,6 +52,7 @@ importFrom(graphics,points)
4752
importFrom(graphics,text)
4853
importFrom(methods,new)
4954
importFrom(methods,validObject)
55+
importFrom(sf,st_drop_geometry)
5056
importFrom(stats,coef)
5157
importFrom(stats,fivenum)
5258
importFrom(stats,formula)

R/RcppExports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ gwaverage_fit <- function(x, coords, bw, quantile, adaptive, kernel, longlat, p,
99
.Call(`_GWmodel3_gwaverage_fit`, x, coords, bw, quantile, adaptive, kernel, longlat, p, theta, parallel_type, parallel_arg)
1010
}
1111

12+
gw_correlation_cal <- function(x1, x2, coords, bw, adaptive, kernel, longlat, p, theta, initial_type, optim_bw_criterion, parallel_type, parallel_arg, variable_names, verbose) {
13+
.Call(`_GWmodel3_gw_correlation_cal`, x1, x2, coords, bw, adaptive, kernel, longlat, p, theta, initial_type, optim_bw_criterion, parallel_type, parallel_arg, variable_names, verbose)
14+
}
15+
1216
gwr_basic_fit <- function(x, y, coords, bw, adaptive, kernel, longlat, p, theta, optim_bw_lower, optim_bw_upper, hatmatrix, intercept, parallel_type, parallel_arg, optim_bw, optim_bw_criterion, select_model, select_model_criterion, select_model_threshold, variable_names, verbose) {
1317
.Call(`_GWmodel3_gwr_basic_fit`, x, y, coords, bw, adaptive, kernel, longlat, p, theta, optim_bw_lower, optim_bw_upper, hatmatrix, intercept, parallel_type, parallel_arg, optim_bw, optim_bw_criterion, select_model, select_model_criterion, select_model_threshold, variable_names, verbose)
1418
}

R/class-GWCorrConfig.R

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#' An S4 class to set GW Correlation configurations
2+
#'
3+
#' @slot bw Bandwidth value.
4+
#' @slot adaptive Whether the bandwidth value is adaptive or not.
5+
#' @slot kernel Kernel function used.
6+
#' @slot longlat Whether the coordinates.
7+
#' @slot p Power of the Minkowski distance,
8+
#' default to 2, i.e., Euclidean distance.
9+
#' @slot theta Angle in radian to roate the coordinate system, default to 0.
10+
#' @slot optim_bw Whether optimize bandwidth after selecting models.
11+
#' Avaliable values are `no`, `AIC`, and `CV`.
12+
#' If `no` is specified, the bandwidth specified by argument `bw`
13+
#' is used in calibrating selected models.
14+
#'
15+
#' @exportClass GWCorrConfig
16+
GWCorrConfig <- setClass("GWCorrConfig", slots = c(
17+
bw = "numeric",
18+
adaptive = "logical",
19+
kernel = "character",
20+
longlat = "logical",
21+
p = "numeric",
22+
theta = "numeric",
23+
optim_bw = "character"
24+
), prototype = list(
25+
bw = NA_real_,
26+
adaptive = FALSE,
27+
kernel = "gaussian",
28+
longlat = FALSE,
29+
p = 2.0,
30+
theta = 0.0,
31+
optim_bw = "CV"
32+
))
33+
34+
setValidity("GWCorrConfig", function(object) {
35+
if (is.na(object@bw) && object@optim_bw == "no") {
36+
"@bw cannot be NA when @optim_bw is 'no'"
37+
} else {
38+
TRUE
39+
}
40+
})
41+
42+
#' Replicate MGWR config
43+
#'
44+
#' @param x A \linkS4class{GWCorrConfig} object.
45+
#' @param \dots Additional arguments.
46+
#' @param times Replication times.
47+
#'
48+
#' @return A list of \linkS4class{GWCorrConfig} objects.
49+
#'
50+
#' @examples
51+
#' rep(gwcorr_config(36, TRUE, "bisquare"), 4)
52+
#'
53+
#' @name rep-GWCorrConfig
54+
NULL
55+
56+
#' @rdname rep-GWCorrConfig
57+
#' @export
58+
setMethod(
59+
"rep",
60+
signature(x = "GWCorrConfig"),
61+
definition = function(x, ...) {
62+
mc <- match.call(rep.int)
63+
mc[[1L]] <- as.name("rep.int")
64+
eval(mc)
65+
}
66+
)
67+
68+
#' @rdname rep-GWCorrConfig
69+
#' @export
70+
setMethod(
71+
"rep.int",
72+
signature(x = "GWCorrConfig", times = "numeric"),
73+
definition = function(x, times = 1) {
74+
times <- as.integer(floor(times))
75+
lapply(seq_len(times), function(i) {
76+
gwcorr_config(
77+
bw = x@bw,
78+
adaptive = x@adaptive,
79+
kernel = x@kernel,
80+
longlat = x@longlat,
81+
p = x@p,
82+
theta = x@theta,
83+
optim_bw = x@optim_bw
84+
)
85+
})
86+
}
87+
)
88+
89+
#' Create an instance of GWCorrConfig
90+
#'
91+
#' @param bw Bandwidth value.
92+
#' @param adaptive Whether the bandwidth value is adaptive or not.
93+
#' @param kernel Kernel function used.
94+
#' @param longlat Whether the coordinates.
95+
#' @param p Power of the Minkowski distance,
96+
#' default to 2, i.e., Euclidean distance.
97+
#' @param theta Angle in radian to roate the coordinate system, default to 0.
98+
#' @param optim_bw Whether optimize bandwidth after selecting models.
99+
#' Avaliable values are `no`, `AIC`, and `CV`.
100+
#' If `no` is specified, the bandwidth specified by argument `bw`
101+
#' is used in calibrating selected models.
102+
#'
103+
#' @examples
104+
#' gwcorr_config(36, TRUE, "bisquare", optim_bw = "AIC")
105+
#'
106+
#' @importFrom methods new
107+
#'
108+
#' @export
109+
#'
110+
gwcorr_config <- function(
111+
bw = NA_real_,
112+
adaptive = FALSE,
113+
kernel = c("gaussian", "exp", "bisquare", "tricube", "boxcar"),
114+
longlat = FALSE,
115+
p = 2.0,
116+
theta = 0.0,
117+
optim_bw = c("CV", "AIC", "no")
118+
) {
119+
kernel <- match.arg(kernel)
120+
optim_bw <- match.arg(optim_bw)
121+
if (is.na(bw) && optim_bw == "no") {
122+
stop("Cannot specify a NA value as specified bandwidth!")
123+
}
124+
125+
new("GWCorrConfig",
126+
bw = bw,
127+
adaptive = adaptive,
128+
kernel = kernel,
129+
longlat = longlat,
130+
p = p,
131+
theta = theta,
132+
optim_bw = optim_bw
133+
)
134+
}
135+
136+
gwcorr_bw_criterion_enums <- c(
137+
"CV",
138+
"AIC"
139+
)
140+
141+
gwcorr_initial_enums <- c(
142+
"Null",
143+
"Initial",
144+
"Specified"
145+
)

0 commit comments

Comments
 (0)