tests/testthat/test-spGini.R

test_that("spGini decomposition components sum to the global Gini", {
  set.seed(1)
  d <- random.test.data(nrows = 5, ncols = 5)
  Coords <- cbind(d$X + runif(25)/10, d$Y + runif(25)/10)
  g <- spGini(Coords, 4, d$dep, WType = "Binary")
  g <- unlist(g)
  expect_equal(unname(g[1]), unname(g[2] + g[3]), tolerance = 1e-10)
  expect_true(g[1] >= 0 && g[1] <= 1)
})

test_that("spGini.w matches spGini for the same binary weights", {
  set.seed(2)
  d <- random.test.data(nrows = 5, ncols = 5)
  Coords <- cbind(d$X + runif(25)/10, d$Y + runif(25)/10)
  g <- unlist(spGini(Coords, 4, d$dep, WType = "Binary"))
  # spGini's adaptive kernel includes the observation itself,
  # so bandwidth bw corresponds to bw-1 nearest neighbours in w.matrix
  w <- w.matrix(Coords, 3, WType = "Binary", family = "adaptive")
  g.w <- spGini.w(d$dep, w)
  expect_equal(unname(g[1:3]), unname(g.w[1:3]), tolerance = 1e-10)
})

Try the lctools package in your browser

Any scripts or data that you put into this service are public.

lctools documentation built on July 9, 2026, 9:07 a.m.