Skip to content

Commit

Permalink
update rgd to fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
SpatLyu committed Sep 24, 2024
1 parent 569a879 commit 698a028
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 48 deletions.
47 changes: 21 additions & 26 deletions R/rgd.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#' @return A list of the RGD model result.
#' \describe{
#' \item{\code{factor}}{the result of RGD model}
#' \item{\code{disc}}{robust discrete results}
#' }
#' @export
#'
Expand All @@ -32,9 +33,9 @@
#' data('ndvi')
#' g = rgd(NDVIchange ~ ., data = ndvi,
#' discvar = names(ndvi)[-1:-3],
#' cores = 6)
#' discnum = 3:8, cores = 6)
#' }
rgd = \(formula, data, discvar = 3:22, discnum = 10, minsize = 1, cores = 1){
rgd = \(formula, data, discvar = NULL, discnum = 3:22, minsize = 1, cores = 1){
formula = stats::as.formula(formula)
formula.vars = all.vars(formula)
if (inherits(data,'sf')) {data = sf::st_drop_geometry(data)}
Expand All @@ -46,24 +47,22 @@ rgd = \(formula, data, discvar = 3:22, discnum = 10, minsize = 1, cores = 1){
discvar = colnames(data)[-which(colnames(data) == yname)]
}
discdf = dplyr::select(data,dplyr::all_of(c(yname,discvar)))
if (length(discnum)==1) {discnum = rep(discnum,length(discvar))}
g = robust_disc(paste0(yname,'~',paste0(discvar,collapse = '+')),
discdf, discnum, minsize, cores = cores)
discedvar = colnames(data[,-which(colnames(data) %in% discvar)])
newdata = data %>%
dplyr::select(dplyr::all_of(discedvar)) %>%
dplyr::bind_cols(g)
if (length(type) == 1){
res = gd(paste0(yname,' ~ .'),data = newdata,
type = type,alpha = alpha)
} else {
res = vector("list", length(type))
for (i in seq_along(type)){
res[[i]] = gd(paste0(yname,' ~ .'),data = newdata,
type = type[i],alpha = alpha)[[1]]
}
names(res) = type

resqv = vector("list", length(discnum))
resdisc = vector("list", length(discnum))
for (i in seq_along(discnum)) {
g = robust_disc(paste0(yname,'~',paste0(discvar,collapse = '+')),
discdf, discnum[i], minsize, cores = cores)
resdisc[[i]] = g
newdata = data %>%
dplyr::select(dplyr::all_of(discedvar)) %>%
dplyr::bind_cols(g)
resqv[[i]] = gd(paste0(yname,' ~ .'),data = newdata,type = "factor")[[1]]
}
names(resqv) = paste0("discnum_",discnum)
names(resdisc) = paste0("discnum_",discnum)
res = c("factor" = resqv, "disc" = resdisc)
class(res) = "rgd_result"
return(res)
}
Expand All @@ -80,14 +79,10 @@ rgd = \(formula, data, discvar = 3:22, discnum = 10, minsize = 1, cores = 1){
#' @method print rgd_result
#' @export
print.rgd_result = \(x, ...) {
cat(" RGD Model \n")
nx = names(x)
for (i in seq_along(x)){
res = x[i]
class(res) = paste0(nx[i],"_detector")
print(res)
cat("\n")
}
cat("*** Robust Geographical Detector ")
qv = x[[1]]
print(knitr::kable(qv[[length(qv)]],format = "markdown",digits = 12,align = 'c',...))
cat("#### Only display the results corresponding to the maximum number of discretizations.")
}

#' @title plot RGD result
Expand Down
29 changes: 7 additions & 22 deletions man/rgd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 698a028

Please sign in to comment.