1

The excerpt below is from "Permutation, Parametric and Bootstrap Tests of Hypotheses", Third Ed. by Phillip Good (pages 58-61), section 3.7.2..

I am trying to implement this permutation test in R (see further below) to compare two variances. I am thinking now about how to calculate the p-value, and whether the test allows for different alternative hypothesis (greater, less, two-sided) and I am not sure on how to proceed.

Could you shed some light on this and perhaps give me some criticism about the code? Many thanks!

enter image description here

# Aly's non-parametric, permutation test of equality of variances
# From "Permutation, Parametric and Bootstrap Tests of Hypotheses", Third Ed. 
# by Phillip Good (pages 58-61), section 3.7.2.

# Implementation of delta statistic as defined by formula in page 60
# x_{i}, order statistics
# z = x_{i+1} - x_{i}, differences between successive order statistics
aly_delta_statistic <- function(z) {
  z_length <- length(z)
  m <- z_length + 1
  i <- 1:z_length
  sum(i*(m-i)*z)
}

aly_test_statistic <- function(sample1, sample2 = NULL, nperm = 1) {

  # compute statistic based on one sample only: sample1
  if(is.null(sample2)) {
    sample1 <- sort(sample1)
    z <- diff(sample1)
    return(aly_delta_statistic(z))
  }

  # statistic based on randomization of the two samples
  else {
    m1 <- length(sample1)
    m2 <- length(sample2)
    # allocate a vector to save the statistic delta
    statistic <- vector(mode = "numeric", length = nperm)
    for(j in 1:nperm) {
      # 1st stage resampling (performed only if samples sizes are different)
      # larger sample is resized to the size of the smaller
      if(m2 > m1) {
         sample2 <- sort(sample(sample2, m1))
         m <- m1
      } else {
         sample1 <- sort(sample(sample1, m2))
         m <- m2
      }
      # z-values: z1 in column 1 and z2 in column 2.
      z_two_samples <- matrix(c(diff(sample1), diff(sample2)), ncol = 2)
      # 2nd stage resampling
      z <- apply(z_two_samples, 1, sample, 1)
      statistic[j] <- aly_delta_statistic(z)
    }
    return(statistic)
  }
}
Ramiro Magno
  • 3,085
  • 15
  • 30
  • Do you have the rights to share this book content? – pat-s Jan 17 '17 at 21:46
  • 1
    I have the book. Still, I would expect that sharing such a small snippet in this context would fall under fair use. – Ramiro Magno Jan 17 '17 at 22:19
  • 1
    This part of your question: "*I am thinking now about how to calculate the p-value, and whether the test allows for different alternative hypothesis (greater, less, two-sided) and I am not sure on how to proceed*" is more of a stats question (and probably belongs on stats.stackexchange) but the part about critiquing your code would be [off topic](http://stats.stackexchange.com/help/on-topic) there. It may make more sense to ask two questions (the first about how the test works) - and once you've got that properly understood, *then* worry about good implementation in a second question here. – Glen_b Jan 18 '17 at 05:00

0 Answers0