Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation to knnDE function #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions R/knnDE.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#' @title knnDE
#'
#' @param X an n by d matrix of coordinates containing points used int the
#' density estimation, where n is the number of points and d is the dimension
#'
#' @param Grid an m by d matrix of coordinates, where m is the number of points
#' in the grid and d is the dimension
#'
#' @param k a number used as the smoothing parameter
#'
#' @return a vector of length m (number of points in Grid) containing the value
#' of the knn Density Estimator for each point in the grid

knnDE <-
function(X, Grid, k){

if (!is.numeric(X) && !is.data.frame(X)) {
stop("X should be a matrix of coordinates")
}
Expand All @@ -11,10 +24,16 @@ function(X, Grid, k){
stop("k should be a positive integer")
}

# store d, the dimension of X, and n, the number of points in X
d <- ncol(X)
n <- nrow(X)
# find the Euclidean distances of k nearest neighbors from input matrix X and the query matrix
# Grid using the fast nearest neighbor searching algorithm "kd_tree"
r.k <- apply(FNN::knnx.dist(X, Grid, k = k, algorithm = "kd_tree"), 1, max)
# volume of the Euclidean d dimensional unit ball
v.d <- pi^(d/2) /gamma(d/2+1)
# final output incorporates smoothing parameter with volume of unit ball and euclidean k nearest neighbor
# distances to return output vector containing density estimators for each point in the grid
out <- k / (n * v.d * r.k ^ d)
return(out)
}
}