r-spatial / spdep

Spatial Dependence: Weighting Schemes and Statistics
https://r-spatial.github.io/spdep/
116 stars 26 forks source link

Consider a p-value extraction function for boot objects #119

Closed JosiahParry closed 1 year ago

JosiahParry commented 1 year ago

moran_bv() now returns a boot object. It isn't immediately clear how to interpret that object. I can be useful to have a helper to extract the simulated p-value.

JosiahParry commented 1 year ago

e.g.

data(boston, package = "spData")
x <- boston.c$CRIM
y <- boston.c$NOX
listw <- spdep::nb2listw(boston.soi)
set.seed(1)
res_xy <- spdep::moran_bv(x, y, listw, nsim=499)

p_sim <- function(x) {
  alternative <- "two.sided"
  nsim <- x[["R"]]

  obs <- x[["t0"]]
  replicates <- x[["t"]]

  gt <- (sum(obs >= replicates) + 1 )/ (nsim + 1)
  lt <- (sum(obs <= replicates) + 1 )/ (nsim + 1)
  tt <- pmin(gt, lt)

  switch(
    alternative,
    two.sided = tt,
    less = lt,
    greater = gt
  )
}

p_sim(res_xy)
#> [1] 0.002
JosiahParry commented 1 year ago

well, with the alternative as an argument and not hard coded, but you get my point :)

rsbivand commented 1 year ago

I'll take a look, it may take a little while. Are there any such extractors in the boot package? boot.ci() or norm.ci() perhaps?