AndriSignorell / DescTools

Tools for Descriptive Statistics and Exploratory Data Analysis
http://andrisignorell.github.io/DescTools/
82 stars 18 forks source link

Bootstrapping Gini Index breaks in spatial covariance simulation #107

Open seandalby opened 1 year ago

seandalby commented 1 year ago

Hi - I get the following error when I run the attached code:

Error in if (const(t, min(1e-08, mean(t, na.rm = TRUE)/1e+06))) { : missing value where TRUE/FALSE needed

I couldn't find out why this is happening in the source code, but I know the data I'm providing doesn't have NA's, negative values, or Inf's - any thoughts as to why?

gini_sim_boot_break.txt

GegznaV commented 1 year ago

@seandalby, your example is not fully reproducible. Where does parDist() come from? Is I from parallelDit? Reprex from the file you provided with library(parallelDist) added:

library(DescTools)
library(parallelDist)
set.seed(1234)

# Distance Matrix:
cols <- 20
rows <- 20
X <- rep(1:cols, each = rows)
Y <- rep(1:rows, cols)
coords <- data.frame(X, Y)
coords_mat <- as.matrix(coords)
dim <- cols * rows
dist <- as.matrix(parDist(x = coords_mat, method = "euclidean"))

# This is the spatial covariance matrix:
matern_2 <- (1 / 2) * ((dist / 2)^2) * besselK(dist / 2, 2)
matern_2[is.nan(matern_2)] <- 1

# Random draw:
samp <- exp(mvtnorm::rmvnorm(1, rep(0, dim),
  sigma = matern_2, checkSymmetry = FALSE
))

sum(is.na(samp))
#> [1] 0
sum(samp <= 0)
#> [1] 0
sum(samp == Inf)
#> [1] 0

# What's breaking?
boot_temp <- DescTools::Gini(samp, conf.level = 0.95, R = 500, type = "perc")
#> Error in if (const(t, min(1e-08, mean(t, na.rm = TRUE)/1e+06))) {: missing value where TRUE/FALSE needed

Created on 2023-03-29 with reprex v2.0.2

seandalby commented 1 year ago

My mistake - it's in the package "parallelDist" - you'll need to install that first.

GegznaV commented 1 year ago

Input should be vector, not matrix (from Gini() documentation): image

boot_temp <- DescTools::Gini(as.vector(samp), conf.level = 0.95, R = 500, type = "perc")
boot_temp
#>      gini    lwr.ci    upr.ci 
#> 0.6613123 0.6110096 0.7044767 
GegznaV commented 1 year ago

@AndriSignorell, should the data type/R class of the input object be asserted here to have a more user-friendly error message?

Or should the input be coerced into a vector which may also lead to an incorrect result in case of inappropriate input?

seandalby commented 1 year ago

Shoot, ok yeah it works on my end converting it to a vector - it was a 1 x 400 matrix before - thanks so much for the help! And yeah I guess I'd second a more friendly error message - but the bootstrap procedure here is great and I appreciate the package!