From bef7dd540344b196282b34d87dd441528d0bc97f Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Thu, 23 Jan 2025 13:55:39 +0800 Subject: [PATCH 1/9] update parallel computing settomh up --- R/spc.R | 32 +++++++++++++++----------------- R/ssh_marginalcontri.R | 8 ++++---- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/R/spc.R b/R/spc.R index 94a352e..a155e0d 100644 --- a/R/spc.R +++ b/R/spc.R @@ -3,13 +3,9 @@ #' @param data A `data.frame`, `tibble` or `sf` object of observation data. #' @param overlay (optional) Spatial overlay method. One of `and`, `or`, `intersection`. #' Default is `and`. -#' @param discnum A numeric vector of discretized classes of columns that need to be discretized. -#' Default all `discvar` use `3:8`. -#' @param minsize (optional) The min size of each discretization group. Default all use `1`. -#' @param strategy (optional) Optimal discretization strategy. When `strategy` is `1L`, choose the highest -#' q-statistics to determinate optimal spatial data discretization parameters. When `strategy` is `2L`, -#' The optimal discrete parameters of spatial data are selected by combining LOESS model. -#' @param increase_rate (optional) The critical increase rate of the number of discretization. Default is `5%`. +#' @param discnum (optional) A vector of number of classes for discretization. Default is `3:8`. +#' @param discmethod (optional) A vector of methods for discretization, default is using +#' `c("sd","equal","geometric","quantile","natural")` by invoking `sdsfun`. #' @param cores (optional) Positive integer (default is 1). When cores are greater than 1, use #' multi-core parallel computing. #' @@ -28,25 +24,27 @@ #' g #' } #' -spc = \(data, overlay = 'and', discnum = 3:8, minsize = 1, - strategy = 2L, increase_rate = 0.05, cores = 1){ +spc = \(data, overlay = 'and', discnum = 3:8, + discmethod = c("sd","equal","geometric","quantile","natural"), + cores = 1){ if (inherits(data,'sf')) { data = sf::st_drop_geometry(data) } xsname = names(data) - calcul_spcv = \(yname,data,overlay,discnum,minsize,strategy,increase_rate,cores){ - rgd_res = gdverse::rgd(paste0(yname," ~ ."), data = data, discnum = discnum, - minsize = minsize, strategy = strategy, - increase_rate = increase_rate, cores = cores) - dti = dplyr::bind_cols(dplyr::select(data,dplyr::all_of(yname)),rgd_res$opt_disc) + calcul_spcv = \(yname,data,overlay,discn,discm,cores){ + opgd_res = gdverse::gd_optunidisc( + paste0(yname," ~ ."), data = data, discnum = discn, + discmethod = discm, cores = cores + ) + dti = dplyr::bind_cols(dplyr::select(data,dplyr::all_of(yname)),opgd_res$disc) sshmcv = cisp::ssh_marginalcontri(paste0(yname," ~ ."), data = dti, overlay = overlay, cores = cores) return(sshmcv$spd) } res = purrr::map_dfr(xsname, - \(.x) calcul_spcv(.x,data = data,overlay = overlay, - discnum = discnum,minsize = minsize,strategy = strategy, - increase_rate = increase_rate, cores = cores) |> + \(.x) calcul_spcv(.x, data = data, overlay = overlay, + discn = discnum, discm = discmethod, + cores = cores) |> dplyr::mutate(yv = .x) |> dplyr::rename(xv = variable, correlation = spd) |> diff --git a/R/ssh_marginalcontri.R b/R/ssh_marginalcontri.R index 07bb2af..c9b9344 100644 --- a/R/ssh_marginalcontri.R +++ b/R/ssh_marginalcontri.R @@ -64,12 +64,12 @@ ssh_marginalcontri = \(formula, data, overlay = 'and', cores = 1){ doclust = FALSE if (cores > 1) { doclust = TRUE - cores = parallel::makeCluster(cores) - on.exit(parallel::stopCluster(cores), add=TRUE) + cl = parallel::makeCluster(cores) + on.exit(parallel::stopCluster(cl), add=TRUE) } if (doclust) { - out_pdv = parallel::parLapply(cores, xs, calcul_pd, + out_pdv = parallel::parLapply(cl, xs, calcul_pd, dti = data, overlay = overlay) out_pdv = tibble::as_tibble(do.call(rbind, out_pdv)) } else { @@ -110,7 +110,7 @@ ssh_marginalcontri = \(formula, data, overlay = 'and', cores = 1){ } if (doclust) { - out_g = parallel::parLapply(cores,xname,calcul_shap) + out_g = parallel::parLapply(cl,xname,calcul_shap) out_g = tibble::as_tibble(do.call(rbind, out_g)) } else { out_g = purrr::map_dfr(xname,calcul_shap) From 6288f85ff5d6c1d19de78b4f246c13c799c9b7c8 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Thu, 23 Jan 2025 14:16:41 +0800 Subject: [PATCH 2/9] update `gdverse` dependence version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7b84ed5..e6201d1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -27,7 +27,7 @@ Depends: Imports: dplyr, forcats, - gdverse (>= 1.3-1), + gdverse (>= 1.3-2), ggplot2, ggraph, igraph, From 39a45209bc5c631aab038206f7d8cfe63a2ebedc Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Thu, 23 Jan 2025 14:54:31 +0800 Subject: [PATCH 3/9] update `spc` docs --- R/spc.R | 1 - man/spc.Rd | 17 ++++------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/R/spc.R b/R/spc.R index a155e0d..286831d 100644 --- a/R/spc.R +++ b/R/spc.R @@ -23,7 +23,6 @@ #' g = spc(sim1, discnum = 3:6, cores = 1) #' g #' } -#' spc = \(data, overlay = 'and', discnum = 3:8, discmethod = c("sd","equal","geometric","quantile","natural"), cores = 1){ diff --git a/man/spc.Rd b/man/spc.Rd index a348c1f..8437eaf 100644 --- a/man/spc.Rd +++ b/man/spc.Rd @@ -8,9 +8,7 @@ spc( data, overlay = "and", discnum = 3:8, - minsize = 1, - strategy = 2L, - increase_rate = 0.05, + discmethod = c("sd", "equal", "geometric", "quantile", "natural"), cores = 1 ) } @@ -20,16 +18,10 @@ spc( \item{overlay}{(optional) Spatial overlay method. One of \code{and}, \code{or}, \code{intersection}. Default is \code{and}.} -\item{discnum}{A numeric vector of discretized classes of columns that need to be discretized. -Default all \code{discvar} use \code{3:8}.} +\item{discnum}{(optional) A vector of number of classes for discretization. Default is \code{3:8}.} -\item{minsize}{(optional) The min size of each discretization group. Default all use \code{1}.} - -\item{strategy}{(optional) Optimal discretization strategy. When \code{strategy} is \code{1L}, choose the highest -q-statistics to determinate optimal spatial data discretization parameters. When \code{strategy} is \code{2L}, -The optimal discrete parameters of spatial data are selected by combining LOESS model.} - -\item{increase_rate}{(optional) The critical increase rate of the number of discretization. Default is \verb{5\%}.} +\item{discmethod}{(optional) A vector of methods for discretization, default is using +\code{c("sd","equal","geometric","quantile","natural")} by invoking \code{sdsfun}.} \item{cores}{(optional) Positive integer (default is 1). When cores are greater than 1, use multi-core parallel computing.} @@ -51,5 +43,4 @@ sim1 = sf::st_as_sf(gdverse::sim,coords = c('lo','la')) g = spc(sim1, discnum = 3:6, cores = 1) g } - } From bc9ec43dd85faa8cf4fe1e6eedbdc80f92ed4149 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Thu, 23 Jan 2025 16:17:04 +0800 Subject: [PATCH 4/9] update `spc` result name --- R/spc.R | 12 ++++++------ man/spc.Rd | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/spc.R b/R/spc.R index 286831d..e4463f3 100644 --- a/R/spc.R +++ b/R/spc.R @@ -11,8 +11,8 @@ #' #' @return A list. #' \describe{ -#' \item{\code{correlation_tbl}}{A tibble with power of spatial pattern correlation} -#' \item{\code{correlation_mat}}{A matrix with power of spatial pattern correlation} +#' \item{\code{cor_tbl}}{A tibble with power of spatial pattern correlation} +#' \item{\code{cor_mat}}{A matrix with power of spatial pattern correlation} #' } #' @export #' @@ -53,7 +53,7 @@ spc = \(data, overlay = 'and', discnum = 3:8, tibble::column_to_rownames(var = 'xv') |> as.matrix() res_mat[is.na(res_mat)] = 1 - res = list("correlation_tbl" = res,"correlation_mat" = res_mat) + res = list("cor_tbl" = res,"cor_mat" = res_mat) class(res) = 'spc_result' return(res) } @@ -64,7 +64,7 @@ spc = \(data, overlay = 'and', discnum = 3:8, #' print.spc_result = \(x, ...) { cat("*** Spatial Pattern Correlation ") - print(knitr::kable(x$correlation_tbl, format = "markdown", digits = 5, align = 'c', ...)) + print(knitr::kable(x$cor_tbl, format = "markdown", digits = 5, align = 'c', ...)) } #' @title plot spc result @@ -75,7 +75,7 @@ plot.spc_result = \(x, style = c("network","matrix"), ...) { style = match.arg(style) switch(style, "network" = { - g = igraph::graph_from_data_frame(x$correlation_tbl, directed = TRUE) + g = igraph::graph_from_data_frame(x$cor_tbl, directed = TRUE) fig_g = ggraph::ggraph(g, layout = "circle") + ggraph::geom_edge_arc(ggplot2::aes(width = abs(correlation), color = correlation), arrow = grid::arrow(type = "closed", length = grid::unit(3, "mm")), @@ -96,7 +96,7 @@ plot.spc_result = \(x, style = c("network","matrix"), ...) { ggplot2::labs(edge_color = "Strength") }, "matrix" = { - g = x$correlation_tbl + g = x$cor_tbl fig_g = ggplot2::ggplot(data = g, ggplot2::aes(x = yv, y = xv, fill = correlation)) + ggplot2::geom_tile(color = "white") + diff --git a/man/spc.Rd b/man/spc.Rd index 8437eaf..25f58a0 100644 --- a/man/spc.Rd +++ b/man/spc.Rd @@ -29,8 +29,8 @@ multi-core parallel computing.} \value{ A list. \describe{ -\item{\code{correlation_tbl}}{A tibble with power of spatial pattern correlation} -\item{\code{correlation_mat}}{A matrix with power of spatial pattern correlation} +\item{\code{cor_tbl}}{A tibble with power of spatial pattern correlation} +\item{\code{cor_mat}}{A matrix with power of spatial pattern correlation} } } \description{ From c772681992c3ecc09a474fba701721cc6521e7c4 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Thu, 23 Jan 2025 16:21:23 +0800 Subject: [PATCH 5/9] update `spc` docs --- R/spc.R | 8 ++++---- man/spc.Rd | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/spc.R b/R/spc.R index e4463f3..9df606e 100644 --- a/R/spc.R +++ b/R/spc.R @@ -6,8 +6,8 @@ #' @param discnum (optional) A vector of number of classes for discretization. Default is `3:8`. #' @param discmethod (optional) A vector of methods for discretization, default is using #' `c("sd","equal","geometric","quantile","natural")` by invoking `sdsfun`. -#' @param cores (optional) Positive integer (default is 1). When cores are greater than 1, use -#' multi-core parallel computing. +#' @param cores (optional) Positive integer (default is 1). When cores are greater than 1, +#' use parallel computing. #' #' @return A list. #' \describe{ @@ -17,9 +17,9 @@ #' @export #' #' @examples -#' \dontrun{ -#' ## The following code needs to configure the Python environment to run: #' sim1 = sf::st_as_sf(gdverse::sim,coords = c('lo','la')) +#' sim1 +#' \donttest{ #' g = spc(sim1, discnum = 3:6, cores = 1) #' g #' } diff --git a/man/spc.Rd b/man/spc.Rd index 25f58a0..e55d25b 100644 --- a/man/spc.Rd +++ b/man/spc.Rd @@ -23,8 +23,8 @@ Default is \code{and}.} \item{discmethod}{(optional) A vector of methods for discretization, default is using \code{c("sd","equal","geometric","quantile","natural")} by invoking \code{sdsfun}.} -\item{cores}{(optional) Positive integer (default is 1). When cores are greater than 1, use -multi-core parallel computing.} +\item{cores}{(optional) Positive integer (default is 1). When cores are greater than 1, +use parallel computing.} } \value{ A list. @@ -37,9 +37,9 @@ A list. spatial pattern correlation } \examples{ -\dontrun{ -## The following code needs to configure the Python environment to run: sim1 = sf::st_as_sf(gdverse::sim,coords = c('lo','la')) +sim1 +\donttest{ g = spc(sim1, discnum = 3:6, cores = 1) g } From 556407d164a09a0f4cef14b466484e6475c76eb0 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Thu, 23 Jan 2025 16:24:19 +0800 Subject: [PATCH 6/9] =?UTF-8?q?Temporarily=20use=20the=20development=20ver?= =?UTF-8?q?sion=20of=20gdverse,=20waiting=20for=20its=20release=20on=20CRA?= =?UTF-8?q?N=E2=80=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DESCRIPTION | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index e6201d1..553d990 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -42,4 +42,6 @@ Imports: Suggests: knitr, rmarkdown +Remotes: + stscl/sdsfun VignetteBuilder: knitr From d72285a1da2d33e6d187532d12fb032204101f44 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Thu, 23 Jan 2025 16:27:34 +0800 Subject: [PATCH 7/9] update docs --- R/spc.R | 3 +-- R/ssh_marginalcontri.R | 6 ++---- man/spc.Rd | 1 + man/ssh_marginalcontri.Rd | 4 ++-- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/R/spc.R b/R/spc.R index 9df606e..cdaac3c 100644 --- a/R/spc.R +++ b/R/spc.R @@ -22,6 +22,7 @@ #' \donttest{ #' g = spc(sim1, discnum = 3:6, cores = 1) #' g +#' plot(g,"matrix") #' } spc = \(data, overlay = 'and', discnum = 3:8, discmethod = c("sd","equal","geometric","quantile","natural"), @@ -61,7 +62,6 @@ spc = \(data, overlay = 'and', discnum = 3:8, #' @title print spc result #' @export #' @noRd -#' print.spc_result = \(x, ...) { cat("*** Spatial Pattern Correlation ") print(knitr::kable(x$cor_tbl, format = "markdown", digits = 5, align = 'c', ...)) @@ -70,7 +70,6 @@ print.spc_result = \(x, ...) { #' @title plot spc result #' @export #' @noRd -#' plot.spc_result = \(x, style = c("network","matrix"), ...) { style = match.arg(style) switch(style, diff --git a/R/ssh_marginalcontri.R b/R/ssh_marginalcontri.R index c9b9344..d0521b4 100644 --- a/R/ssh_marginalcontri.R +++ b/R/ssh_marginalcontri.R @@ -4,8 +4,8 @@ #' @param data A `data.frame`, `tibble` or `sf` object of observation data. #' @param overlay (optional) Spatial overlay method. One of `and`, `or`, `intersection`. #' Default is `and`. -#' @param cores (optional) Positive integer (default is 1). When cores are greater than 1, use -#' multi-core parallel computing. +#' @param cores (optional) Positive integer (default is 1). When cores are greater than 1, +#' use parallel computing. #' #' @return A list. #' \describe{ @@ -166,7 +166,6 @@ ssh_marginalcontri = \(formula, data, overlay = 'and', cores = 1){ #' @title print ssh_marginalcontri result #' @export #' @noRd -#' print.sshmc_result = \(x, ...) { cat("*** SSH Marginal Contributions ") print(knitr::kable(x$spd, format = "markdown", digits = 12, align = 'c', ...)) @@ -175,7 +174,6 @@ print.sshmc_result = \(x, ...) { #' @title plot ssh_marginalcontri result #' @export #' @noRd -#' plot.sshmc_result = \(x, low_color = "#6600CC", high_color = "#FFCC33", ...){ g = x$determination diff --git a/man/spc.Rd b/man/spc.Rd index e55d25b..86353c4 100644 --- a/man/spc.Rd +++ b/man/spc.Rd @@ -42,5 +42,6 @@ sim1 \donttest{ g = spc(sim1, discnum = 3:6, cores = 1) g +plot(g,"matrix") } } diff --git a/man/ssh_marginalcontri.Rd b/man/ssh_marginalcontri.Rd index c1237ab..1355de0 100644 --- a/man/ssh_marginalcontri.Rd +++ b/man/ssh_marginalcontri.Rd @@ -14,8 +14,8 @@ ssh_marginalcontri(formula, data, overlay = "and", cores = 1) \item{overlay}{(optional) Spatial overlay method. One of \code{and}, \code{or}, \code{intersection}. Default is \code{and}.} -\item{cores}{(optional) Positive integer (default is 1). When cores are greater than 1, use -multi-core parallel computing.} +\item{cores}{(optional) Positive integer (default is 1). When cores are greater than 1, +use parallel computing.} } \value{ A list. From 3093556ad135dfb43029f3116a44b5f7ba82abc2 Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Thu, 23 Jan 2025 16:31:57 +0800 Subject: [PATCH 8/9] update examples --- R/ssh_marginalcontri.R | 1 + man/ssh_marginalcontri.Rd | 1 + 2 files changed, 2 insertions(+) diff --git a/R/ssh_marginalcontri.R b/R/ssh_marginalcontri.R index d0521b4..b8fc78e 100644 --- a/R/ssh_marginalcontri.R +++ b/R/ssh_marginalcontri.R @@ -19,6 +19,7 @@ #' NTDs1 = sf::st_as_sf(gdverse::NTDs, coords = c('X','Y')) #' g = ssh_marginalcontri(incidence ~ ., data = NTDs1, cores = 1) #' g +#' plot(g) #' ssh_marginalcontri = \(formula, data, overlay = 'and', cores = 1){ formula = stats::as.formula(formula) diff --git a/man/ssh_marginalcontri.Rd b/man/ssh_marginalcontri.Rd index 1355de0..9f086ab 100644 --- a/man/ssh_marginalcontri.Rd +++ b/man/ssh_marginalcontri.Rd @@ -32,5 +32,6 @@ spatial association marginal contributions derived from spatial stratified heter NTDs1 = sf::st_as_sf(gdverse::NTDs, coords = c('X','Y')) g = ssh_marginalcontri(incidence ~ ., data = NTDs1, cores = 1) g +plot(g) } From 467205a70e09685389c93347cbe65a358f06c41f Mon Sep 17 00:00:00 2001 From: SpatLyu Date: Thu, 23 Jan 2025 16:38:50 +0800 Subject: [PATCH 9/9] fix installation error --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 553d990..57c4759 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -43,5 +43,5 @@ Suggests: knitr, rmarkdown Remotes: - stscl/sdsfun + stscl/gdverse VignetteBuilder: knitr