tests/testthat/test-Tajima_D.R

pacman::p_load("testthat","popgen.stats")

context("Tajima_D Function")

#### Test for R version

test_that("theta_t computed correctly",{

  set.seed(2019)
  seq <-matrix(sample(0:1, size = 9, replace = TRUE), nc = 3)
  #3 pairwise diff, 3 pairs
  test<-theta_t(seq)
  n=dim(seq)[1]
  expect_equal(test,4/choose(n,2))

  set.seed(2019)
  seq <-matrix(sample(0:1, size = 16, replace = TRUE), nc = 4)
  n=dim(seq)[1]
  #14 pairwise difference, 6 pairs
  test=theta_t(seq)
  print(test)
  expect_equal(theta_t(seq) ,14/choose(n,2))

  set.seed(2019)
  seq  <- matrix(sample(0:1, size = 100, replace = TRUE), nr = 25)
  n=dim(seq)[1]
  y  <- sum(as.matrix((dist(seq, method = "manhattan"))))/2/choose(n, 2)
  expect_equal(theta_t(seq),y)
})

test_that("theta_w computed correctly",{

  ##test case 1
  set.seed(1707)
  seq <-matrix(sample(0:1, size = 9, replace = TRUE), nc = 3)
  seg=dim(seq)[2]
  sample_size=dim(seq)[1]

  normalise=0 #initialisation of normalisation constant
  for (i in 1:(sample_size-1)){
    normalise=normalise+1/i
  }
  expect_equal(theta_w(seq),seg/normalise)


  ##test case 2
  set.seed(1707)
  seq <-matrix(sample(0:1, size = 100, replace = TRUE), nc = 20)
  seg=dim(seq)[2]
  sample_size=dim(seq)[1]


  normalise=0 #initialisation of normalisation constant
  for (i in 1:(sample_size-1)){
    normalise=normalise+1/i
  }
  expect_equal(theta_w(seq),seg/normalise)

})

######################### End test for R version
deponent-verb/popgen.stats documentation built on Nov. 4, 2019, 10:26 a.m.