r-spatial / spdep

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

Can't access parameters of localG object #132

Closed dkczk closed 10 months ago

dkczk commented 1 year ago

I used the localG_perm() function which unlike localmoran_perm() doesn't return a dataframe but instead a localG num object. I can't access the parameters ("Gi", "E.Gi", "Var.Gi", "StdDev.Gi", "Pr(z != E(Gi))", "Pr(z != E(Gi)) Sim", "Pr(folded) Sim", "Skewness", "Kurtosis") from that object. I tried $, @ and the ordinary number and name indexing and nothing returns the specified parameter. How can I extract e.g. "Pr(z != E(Gi))" from a localG object?

Example:

library("terra")
library("spdep")

r <- terra::rast(
    ncols = 100,
    nrows = 100,
    vals = rnorm(100 * 100)
)

nb <- spdep::cell2nb(
    terra::nrow(r),
    terra::ncol(r),
    type = "queen",
    torus = TRUE
    )

r_auto <- spdep::localG_perm(
    as.vector(terra::values(r)),
    spdep::nb2listw(nb, style = "B"),
    nsim = 100,
    zero.policy = TRUE
)

Ubuntu 22.04.2 spdep 1.2-8 R 4.3.1

rsbivand commented 1 year ago

The Value section of the help page ?localG says:

For conditional permutation, the returned value is the same as for 'localG()', and the simulated standard deviate is returned as column '"StdDev.Gi"' in 'attr(., "internals")'.

so:

> str(r_auto)
 'localG' num [1:10000] -0.847 -1.281 -0.491 -1.378 -1.128 ...
 - attr(*, "internals")= num [1:10000, 1:9] -0.0389 -0.0572 -0.0211 -0.0613 -0.0506 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:9] "Gi" "E.Gi" "Var.Gi" "StdDev.Gi" ...
 - attr(*, "cluster")= Factor w/ 2 levels "Low","High": 2 1 1 1 1 1 1 1 2 1 ...
 - attr(*, "gstari")= logi FALSE
 - attr(*, "call")= language spdep::localG_perm(x = as.vector(terra::values(r)), listw = spdep::nb2listw(nb,      style = "B"), nsim = 100, ze| __truncated__

The online help pages are stale, I'll update them.

dkczk commented 1 year ago

Thank you very much! Is there a reason why localG_perm() is returned like that and not as dataframe?

rsbivand commented 1 year ago

Yes, G is returned as a standard variate as in its original literature. Inference is a matter of choice for the analyst - the results needed are returned, but the analyst has to make the choices, see i.a. https://doi.org/10.1111/gean.12319 and https://r-spatial.org/book/15-Measures.html.