#' @title Nearest neighbor(NN) test for multivariate equal distribution
#' @description Hypothesis testing problems can use permutation test. For equal distribution tests, nntest can faciliate multivariate tests.
#' @usage nntest(x,y)
#' @param x sample data from a specific distribution
#' @param y sample data from another different distribution
#' @details user should be noted that x and y must have compatible number of dimension
#' @references Statistic computing with R. Maria L. Rizzo
#' @examples
#' \dontrun{
#' mat=matrix(0,10,1)
#' for(i in 1:10){
#' x=rnorm(100)
#' y=rnorm(100)*(1+i/10)
#' mat[i]=nn.test(x,y)
#' }
#' plot(mat,ylim=c(0,0.5),main="different variance")
#' }
#' @export
nntest=function(x, y){
z <- c(x, y)
o <- rep(0, length(z))
z <- as.data.frame(cbind(z, o))
Tn3 <- function(z, ix, sizes) {
n1 <- sizes[1]
n2 <- sizes[2]
n <- n1 + n2
z <- z[ix, ]
o <- rep(0, NROW(z))
z <- as.data.frame(cbind(z, o))
NN <- RANN::nn2(z, k=3)
block1 <- NN$nn.idx[1:n1, ]
block2 <- NN$nn.idx[(n1+1):n, ]
i1 <- sum(block1 < n1 + .5)
i2 <- sum(block2 > n1 + .5)
return((i1 + i2) / (3 * n))
}
N <- c(length(x), length(y))
boot.obj <- boot::boot(data = z, statistic = Tn3, sim = "permutation", R = 999, sizes = N)
tb <- c(boot.obj$t, boot.obj$t0)
mean(tb >= boot.obj$t0)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.