Skip to content

Instantly share code, notes, and snippets.

@sjsrey
Last active September 1, 2017 00:59
Show Gist options
  • Save sjsrey/6dc3a2c52a851625a5fa8951fbda3776 to your computer and use it in GitHub Desktop.
Save sjsrey/6dc3a2c52a851625a5fa8951fbda3776 to your computer and use it in GitHub Desktop.
Using reticulate to test pysal against spdep
library(reticulate)
use_condaenv("pysal2")
pysal <- import("pysal")
np <- import("numpy")
pth <- pysal$examples$get_path("stl_hom.txt")
f <- pysal$open(pth)
col <- f$by_col("HR8893")
y <- np$array(col)
w <- pysal$open(pysal$examples$get_path("stl.gal"))$read()
mi <- pysal$Moran(y, w, two_tailed=FALSE)
df <- read.csv(pth, skip=1)
library(spdep)
nb <- read.gal(pysal$examples$get_path("stl.gal"))
miR <- moran.test(df$HR8893, nb2listw(nb, style="W"))
all.equal(mi$I, unname(miR$estimate[1]))
all.equal(mi$EI, unname(miR$estimate[2]))
all.equal(mi$VI_rand, unname(miR$estimate[3]), scale=1)
all.equal(mi$z_rand, unname(miR$statistic), scale=1)
miN <- moran.test(df$HR8893, nb2listw(nb, style="W"), randomisation=FALSE)
all.equal(mi$VI_norm, unname(miN$estimate[3]), scale=1)
all.equal(mi$z_norm, unname(miN$statistic), scale=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment