tests/testthat/test-distances.R

test_that("Euclidean distances are correctly calculated", {
  
  euclid <- round(as.matrix(dist(x100[, c("b1", "b2")])), 1)
  
  b.out <- block(x100, 
                 id.vars = "id", 
                 block.vars = c("b1", "b2"), 
                 distance = "euclidean")
  
  bl.dists <- round(b.out$blocks[[1]]["Distance"], 1)
  bl.ids <- createBlockIDs(b.out, data = x100, id.var = "id")
  
  euclid.dists <- NULL
  
  for(i in 1:(nrow(x100) / 2)){
    which.r <- which(bl.ids %in% i)[1]
    which.c <- which(bl.ids %in% i)[2]
    euclid.dists <- append(euclid.dists, euclid[which.r, which.c])
  }
  
  expect_identical(unlist(unname(bl.dists)), euclid.dists)
})

Try the blockTools package in your browser

Any scripts or data that you put into this service are public.

blockTools documentation built on Feb. 16, 2023, 7:14 p.m.