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