tests/testthat/test-gd_compute_dist_stats.R

data("gd_GHI_2009_income")
gd_ex1 <- readRDS(test_path("testdata", "gd_ex1.RDS"))
gd_ex3 <- readRDS(test_path("testdata", "gd_ex3.RDS"))

test_that("gd_compute_dist_stats() returns correct results", {

  # Test vs gd_compute_pip_stats()
  df <- gd_GHI_2009_income
  df <- gd_clean_data(df,
    welfare = "welfare",
    population = "weight",
    gd_type = 5,
    quiet = TRUE
  )
  res1 <- gd_compute_pip_stats(
    welfare = df$welfare, population = df$weight,
    povline = 1.9 * 365 / 12, requested_mean = 50,
    default_ppp = 1
  )
  res2 <- gd_compute_dist_stats(
    welfare = df$welfare, population = df$weight,
    mean = 50
  )
  expect_equal(res1$mean, res2$mean)
  expect_equal(res1$median, res2$median)
  expect_equal(res1$gini, res2$gini)
  expect_equal(res1$mld, res2$mld)
  expect_equal(res1$polarization, res2$polarization)
  expect_equal(res1$deciles, res2$deciles)

  # Test against example data
  res <- gd_compute_dist_stats(
    welfare = gd_ex1$welfare, population = gd_ex1$weight,
    mean = 330.5371
  )
  expect_equal(res$mean, 330.5371)
  expect_equal(res$median, 201.4286, tolerance = 1.5e-06)
  expect_equal(res$gini, 0.5135977, tolerance = 1.5e-06)
  expect_equal(res$polarization, 0.4212645, tolerance = 1.5e-06)
  expect_equal(res$deciles,
    tolerance = 8e-05,
    c(
      0.01609, 0.02702, 0.03577, 0.04492, 0.05522,
      0.0675, 0.0832, 0.1056, 0.1452, 0.4195
    )
  )
  skip("mld doesn't match PCN value")
  expect_equal(res$mld, 0.4670367, tolerance = 1.5e-06)
})

# Implemented after fixing issue #120 (CAF 1992)
test_that("gd_compute_gini_lq returns correct results", {
  res <- gd_compute_dist_stats(gd_ex3$welfare, gd_ex3$weight, mean = 1.180057)
  expect_equal(res$gini, 0.61330745577995316)
})

# Added after fixing lorenz selection issue in gd_compute_dist_stats
# Incorrect SSE was used in selection
test_that("gd_compute_dist_stats select correct Lorenz curve", {
  welfare <- c(0.005529081,
               0.035269317,
               0.092114651,
               0.199049919,
               0.358258694,
               0.636638488,
               1.000000000)

  weight <- c(0.0340034,
              0.1381138,
              0.2738274,
              0.4541454,
              0.6462646,
              0.8671867,
              1.0000000)
  res <- gd_compute_dist_stats(welfare = welfare,
                               population = weight,
                               mean = 6.095571)
  expect_equal(res$gini, 0.40235114)
})
PIP-Technical-Team/wbpip documentation built on Nov. 29, 2024, 6:57 a.m.