Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions R/geodetector.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#'
factor_detector = \(y,x,confintv = FALSE,alpha = 0.95){
x = gdverse::all2int(x)
gdf = tibble::tibble(x = x, y = y) %>%

Check notice on line 27 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L27

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::group_by(x) %>%

Check notice on line 28 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L28

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::filter(dplyr::n() > 1) %>%

Check notice on line 29 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L29

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::ungroup() %>%

Check notice on line 30 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L30

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::mutate(x = factor(x))
x = gdf$x
y = gdf$y
Expand Down Expand Up @@ -75,6 +75,9 @@
#' \item{\code{Variable2 Q-statistics}}{Q-statistics for variable2}
#' \item{\code{Variable1 and Variable2 interact Q-statistics}}{Q-statistics for variable1 and variable2 interact}
#' \item{\code{Interaction}}{the interact result type}
#' \item{\code{Variable1 P-value}}{P-value of the Q-statistic for Variable1}
#' \item{\code{Variable2 P-value}}{P-value of the Q-statistic for Variable2}
#' \item{\code{Variable1 and Variable2 interact P-value}}{P-value of the Q-statistic for variable1 and variable2 interact}
#' }
#' @export
#'
Expand All @@ -85,25 +88,30 @@
#'
interaction_detector = \(y,x1,x2){
x12 = paste0(x1,x2,'_')
qv1 = gdverse::factor_detector(y,x1)[[1]]
qv2 = gdverse::factor_detector(y,x2)[[1]]
qv12 = gdverse::factor_detector(y,x12)[[1]]
g1 = gdverse::factor_detector(y,x1)
g2 = gdverse::factor_detector(y,x2)
g12 = gdverse::factor_detector(y,x12)

qv1 = g1[[1]]
qv2 = g2[[1]]
qv12 = g12[[1]]

if (qv12 < min(qv1, qv2)) {
interaction = c("Weaken, nonlinear")
interaction = "Weaken, nonlinear"
} else if (qv12 >= min(qv1, qv2) & qv12 <= max(qv1, qv2)) {
interaction = c("Weaken, uni-")
interaction = "Weaken, uni-"
} else if (qv12 > max(qv1, qv2) & (qv12 < qv1 + qv2)) {
interaction = c("Enhance, bi-")
interaction = "Enhance, bi-"
} else if (qv12 == qv1 + qv2) {
interaction = c("Independent")
interaction = "Independent"
} else {
interaction = c("Enhance, nonlinear")
interaction = "Enhance, nonlinear"
}
interd = list(qv1,qv2,qv12,interaction)
interd = list(qv1,qv2,qv12,interaction,g1[[2]],g2[[2]],g12[[2]])
names(interd) = c("Variable1 Q-statistics","Variable2 Q-statistics",
"Variable1 and Variable2 interact Q-statistics",
"Interaction")
"Interaction", "Variable1 P-value","Variable2 P-value",
"Variable1 and Variable2 interact P-value")
return(interd)
}

Expand All @@ -130,8 +138,8 @@
x1 = purrr::map_chr(seq_along(paradf), \(i) paradf[[i]][1])
x2 = purrr::map_chr(seq_along(paradf), \(i) paradf[[i]][2])
paradf = tibble::tibble(zone1st = paste0('zone',x1),
zone2nd = paste0('zone',x2))

Check notice on line 141 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L141

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)

Check notice on line 142 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L142

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
twounit_risk_detector = \(n1,n2,cutoff = 0.95){
y1 = dplyr::filter(gdf, x == n1) %>% dplyr::pull(y)
y2 = dplyr::filter(gdf, x == n2) %>% dplyr::pull(y)
Expand All @@ -148,7 +156,7 @@
risk = factor(risk,levels = c("Yes", "No"), labels = c("Yes", "No"))
riskd = list(tt$statistic,tt$parameter,tt$p.value,risk)
names(riskd) = c("T-statistic","Degree-freedom","P-value","Risk")
return(riskd)

Check notice on line 159 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L159

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
}

rd = purrr::map2_dfr(x1,x2,twounit_risk_detector,cutoff = alpha) %>%
Expand Down Expand Up @@ -248,21 +256,21 @@
explanatory = data[, formulaname[[2]]]

switch(type,
"factor" = {

Check notice on line 259 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L259

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
res = purrr::map_dfr(names(explanatory),

Check notice on line 260 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L260

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
\(i) gdverse::factor_detector(response,

Check notice on line 261 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L261

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
data[,i,drop = TRUE])) %>%
dplyr::mutate(variable = names(explanatory)) %>%
dplyr::select(variable,dplyr::everything()) %>%
dplyr::arrange(dplyr::desc(`Q-statistic`))
res = list("factor" = res)
class(res) = "factor_detector"

Check notice on line 267 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L267

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
},
"interaction" = {
res = utils::combn(names(explanatory), 2, simplify = FALSE) %>%

Check notice on line 270 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L270

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
purrr::map_dfr(\(i) gdverse::interaction_detector(response,

Check notice on line 271 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L271

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
data[,i[1],drop = TRUE],
data[,i[2],drop = TRUE]) %>%

Check notice on line 273 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L273

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
tibble::as_tibble() %>%
dplyr::mutate(variable1 = i[1],
variable2 = i[2]) %>%
Expand All @@ -272,22 +280,22 @@
class(res) = "interaction_detector"
},
"risk" = {
res = purrr::map_dfr(names(explanatory),

Check notice on line 283 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L283

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
\(i) gdverse::risk_detector(response,

Check notice on line 284 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L284

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
data[,i,drop = TRUE],
alpha) %>%
dplyr::mutate(variable = i) %>%
dplyr::select(variable,zone1st,zone2nd,Risk,
dplyr::everything()))
res = list("risk" = res)
class(res) = "risk_detector"

Check notice on line 291 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L291

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
},
"ecological" = {
res = utils::combn(names(explanatory), 2, simplify = FALSE) %>%
purrr::map_dfr(\(i) gdverse::ecological_detector(response,

Check notice on line 295 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L295

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
data[,i[1],drop = TRUE],

Check notice on line 296 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L296

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
data[,i[2],drop = TRUE],
alpha) %>%

Check notice on line 298 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L298

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
tibble::as_tibble() %>%
dplyr::mutate(variable1 = i[1],
variable2 = i[2]) %>%
Expand Down Expand Up @@ -328,10 +336,10 @@
#' @return Formatted string output
#' @export
#'
print.interaction_detector = \(x, ...) {

Check notice on line 339 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L339

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
cat(" Interaction Detector ")
IntersectionSymbol = rawToChar(as.raw(c(0x20, 0xE2, 0x88, 0xA9, 0x20)))
x = x$interaction %>%

Check notice on line 342 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L342

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::mutate(`Interactive variable` = paste0(variable1,
IntersectionSymbol,
variable2)) %>%
Expand All @@ -351,13 +359,13 @@
#' @return Formatted string output
#' @export
#'
print.risk_detector = \(x, ...) {

Check notice on line 362 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L362

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
cat(" Risk Detector \n")

Check notice on line 363 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L363

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
x = dplyr::select(x$risk,variable,zone1st,zone2nd,Risk)
xvar = x %>%
dplyr::count(variable) %>%

Check notice on line 366 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L366

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::pull(variable)

Check notice on line 367 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L367

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
rd2mat = \(x,zonevar){

Check notice on line 368 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L368

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
matt = x %>%
dplyr::filter(variable == zonevar) %>%
dplyr::select(-variable) %>%
Expand Down Expand Up @@ -387,12 +395,12 @@
#'
print.ecological_detector = \(x, ...) {
cat(" Ecological Detector ")
x = dplyr::select(x$ecological,

Check notice on line 398 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L398

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::all_of(c('variable1','variable2','Ecological')))
ed2mat = \(x){
matt = x %>%
tidyr::pivot_wider(names_from = "variable2",

Check notice on line 402 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L402

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
values_from = "Ecological")

Check notice on line 403 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L403

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
matname = matt$variable1
matt = matt %>%
dplyr::select(-variable1) %>%
Expand All @@ -418,19 +426,19 @@
#'
#' @return A ggplot2 layer.
#' @export
#'

Check notice on line 429 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L429

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
plot.factor_detector = \(x, slicenum = 2, alpha = 0.95,

Check notice on line 430 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L430

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
keep = TRUE, qlabelsize = 3.88, ...) {

Check notice on line 431 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L431

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
g = x$factor %>%

Check notice on line 432 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L432

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::select(variable, qv = `Q-statistic`,pv = `P-value`) %>%

Check notice on line 433 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L433

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::filter(!is.na(qv)) %>%
dplyr::mutate(variable = forcats::fct_reorder(variable, qv, .desc = TRUE)) %>%
dplyr::mutate(variable_col = c("first",rep("others",times = nrow(.)-1))) %>%
dplyr::mutate(qv_text = paste0(sprintf("%4.2f", qv * 100), "%"))

Check notice on line 437 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L437

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)

if (!keep) {
g = g %>%
dplyr::mutate(significance = dplyr::if_else(pv <= 1 - alpha,

Check notice on line 441 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L441

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
'Significant',
'Not Significant',
NA)) %>%
Expand Down Expand Up @@ -469,15 +477,15 @@
#' @param ... (optional) Other arguments passed to `ggplot2::theme()`.
#'
#' @return A ggplot2 layer
#' @export

Check notice on line 480 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L480

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
#'
plot.interaction_detector = \(x,alpha = 1,...){
g = x$interaction %>%
dplyr::select(interactv = `Variable1 and Variable2 interact Q-statistics`,
dplyr::everything())

Check notice on line 485 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L485

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
gv1 = dplyr::count(g,variable1)

Check notice on line 486 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L486

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
gv2 = dplyr::count(g,variable2)
gv = gv1 %>%

Check notice on line 488 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L488

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::left_join(gv2,by = 'n') %>%
dplyr::arrange(dplyr::desc(n))
g = g %>%
Expand Down Expand Up @@ -534,7 +542,7 @@
panel.grid = ggplot2::element_blank(), ...) +
ggplot2::annotate("text", x = Inf, y = -Inf, label = gname,
vjust = -1.75, hjust = 1.25,
color = "#ff0000", family = "serif")

Check notice on line 545 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L545

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
return(fig_rd)
}
g = dplyr::select(x$risk,variable,zone1st,zone2nd,Risk) %>%
Expand Down Expand Up @@ -564,10 +572,10 @@
#'
plot.ecological_detector = \(x, ...) {
g = dplyr::select(x$ecological,
dplyr::all_of(c('variable1','variable2','Ecological')))

Check notice on line 575 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L575

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
gv1 = dplyr::count(g,variable1)

Check notice on line 576 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L576

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
gv2 = dplyr::count(g,variable2)
gv = gv1 %>%

Check notice on line 578 in R/geodetector.R

View check run for this annotation

codefactor.io / CodeFactor

R/geodetector.R#L578

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::left_join(gv2,by = 'n') %>%
dplyr::arrange(dplyr::desc(n))
g = g %>%
Expand Down
10 changes: 5 additions & 5 deletions R/gozh.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,22 @@
}
interact_type = \(qv1,qv2,qv12){
if (qv12 < min(qv1, qv2)) {
interaction = c("Weaken, nonlinear")
interaction = "Weaken, nonlinear"
} else if (qv12 >= min(qv1, qv2) & qv12 <= max(qv1, qv2)) {
interaction = c("Weaken, uni-")
interaction = "Weaken, uni-"
} else if (qv12 > max(qv1, qv2) & (qv12 < qv1 + qv2)) {
interaction = c("Enhance, bi-")
interaction = "Enhance, bi-"
} else if (qv12 == qv1 + qv2) {
interaction = c("Independent")
interaction = "Independent"
} else {
interaction = c("Enhance, nonlinear")
interaction = "Enhance, nonlinear"
}
return(interaction)
}

suppressMessages({newdata = xname %>%

Check notice on line 185 in R/gozh.R

View check run for this annotation

codefactor.io / CodeFactor

R/gozh.R#L185

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
purrr::map_dfc(calcul_rpartdisc)%>%

Check notice on line 186 in R/gozh.R

View check run for this annotation

codefactor.io / CodeFactor

R/gozh.R#L186

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
purrr::set_names(xname) %>%

Check notice on line 187 in R/gozh.R

View check run for this annotation

codefactor.io / CodeFactor

R/gozh.R#L187

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::bind_cols(dplyr::select(dti,
dplyr::all_of(yname)),
.)})
Expand Down Expand Up @@ -212,7 +212,7 @@
"Variable1 and Variable2 interact Q-statistics" = qv12,
"variable1" = variable1, "variable2" = variable2,
"Interaction" = purrr::pmap_chr(list(qv1 = qv1,qv2 = qv2,qv12 = qv12),
interact_type)) %>%

Check notice on line 215 in R/gozh.R

View check run for this annotation

codefactor.io / CodeFactor

R/gozh.R#L215

Use the |> pipe operator instead of the %>% pipe operator. (pipe_consistency_linter)
dplyr::select(variable1,variable2,Interaction,
dplyr::everything())
res = list("interaction" = res)
Expand Down
3 changes: 3 additions & 0 deletions man/interaction_detector.Rd

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