So I find myself needing to apply global Moran's I across ~20 variables (as in ~20 instances of univariate autocorrelation for each variable, not attempted multivariate spatial autocorrelation). I'm using R and the sf + spdep packages.
Where data_lisa
is a sf object structured:
id | var_a | var_b | ... | var_n | geometry
...and lw
the spatial weights list created with:
lw <- nb2listw(neighbours = poly2nb(data_lisa,
queen = TRUE),
style = "W",
zero.policy = TRUE)
Using spdep, I can apply global Moran's I for a single variable as:
moran.mc(data_lisa$var_a,
listw = lw,
nsim = 999,
zero.policy = TRUE)
...and receive all the expected results.
So I'm looking for help in how to programmatically apply this function across all of my variables.
The result of moran.mc
is a list object which I suspect is where I'm encountering the greatest issues, as I don't have much experience interacting with lists.
Ideally the output would look something like this.
variable | moran_stat | pval |
---|---|---|
var_a | 0.064 | 0.042 |
var_b | 0.322 | 0.001 |
var_c | 0.183 | 0.001 |
How can I do this?