Home > Blockchain >  floating-point error fligner.test r function?
floating-point error fligner.test r function?

Time:08-24

I have noticed that using the statistical test fligner.test from the r stat package provides different results with a simple transformation, even though this shouldn't be the case.

Here an example (the difference for the original dataset is much more dramatic):

g  <- factor(rep(1:2, each=6))
x1 <- c(2,2,6,6,1,4,5,3,5,6,5,5)
x2 <- (x1-1)/5 #> cor(x1,x2) [1] 1
fligner.test(x1,g) # chi-squared = 4.2794, df = 1, p-value = 0.03858
fligner.test(x2,g) # chi-squared = 4.8148, df = 1, p-value = 0.02822

Looking at the function code, I have noticed that the median centering might be causing the issue:

x1 <- x1 - tapply(x1,g,median)[g]
x2 <- x2 - tapply(x2,g,median)[g]
unique(abs(x1)) # 1 3 2 0
unique(abs(x2)) # 0.2 0.6 0.4 0.2 0.0 <- repeated 0.2

Is this a known issue, and how should this inconsistency be resolved?

CodePudding user response:

I think your analysis is correct here. In your example the problem ultimately occurs because (0.8 - 0.6) == 0.2 is FALSE unless rounded to 15 decimal places. You should file a bug report, since this is avoidable.

If you are desperate in the meantime, you can adapt stats:::fligner.test.default by applying a tiny bit of rounding at the median centering stage to remove floating point inequalities:

fligner <- function (x, g, ...) 
{
  if (is.list(x)) {
    if (length(x) < 2L) 
      stop("'x' must be a list with at least 2 elements")
    DNAME <- deparse1(substitute(x))
    x <- lapply(x, function(u) u <- u[complete.cases(u)])
    k <- length(x)
    l <- lengths(x)
    if (any(l == 0)) 
      stop("all groups must contain data")
    g <- factor(rep(1:k, l))
    x <- unlist(x)
  }
  else {
    if (length(x) != length(g)) 
      stop("'x' and 'g' must have the same length")
    DNAME <- paste(deparse1(substitute(x)), "and", 
                   deparse1(substitute(g)))
    OK <- complete.cases(x, g)
    x <- x[OK]
    g <- g[OK]
    g <- factor(g)
    k <- nlevels(g)
    if (k < 2) 
      stop("all observations are in the same group")
  }
  n <- length(x)
  if (n < 2) 
    stop("not enough observations")
  x <- round(x - tapply(x, g, median)[g], 15)
  a <- qnorm((1   rank(abs(x))/(n   1))/2)
  a <- a - mean(a)
  v <- sum(a^2)/(n - 1)
  a <- split(a, g)
  STATISTIC <- sum(lengths(a) * vapply(a, mean, 0)^2)/v
  PARAMETER <- k - 1
  PVAL <- pchisq(STATISTIC, PARAMETER, lower.tail = FALSE)
  names(STATISTIC) <- "Fligner-Killeen:med chi-squared"
  names(PARAMETER) <- "df"
  METHOD <- "Fligner-Killeen test of homogeneity of variances"
  RVAL <- list(statistic = STATISTIC, parameter = PARAMETER, 
               p.value = PVAL, method = METHOD, data.name = DNAME)
  class(RVAL) <- "htest"
  return(RVAL)
}

This now returns the correct result for both your vectors:

fligner(x1,g)
#> 
#> Fligner-Killeen test of homogeneity of variances
#> 
#> data:  x1 and g
#> Fligner-Killeen:med chi-squared = 4.2794, df = 1, p-value = 0.03858

fligner(x2,g) 
#> 
#> Fligner-Killeen test of homogeneity of variances
#> 
#> data:  x2 and g
#> Fligner-Killeen:med chi-squared = 4.2794, df = 1, p-value = 0.03858
  • Related