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 1 commit
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,20 +1,39 @@
#' @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){


# ensure input X is a matrix of numbers
if (!is.numeric(X) && !is.data.frame(X)) {
stop("X should be a matrix of coordinates")
}
# ensure input Grid is a matrix of numbers
if (!is.numeric(Grid) && !is.data.frame(Grid)) {
stop("Grid should be a matrix of coordinates")
}
# ensure that k is a positive integer
if (!is.numeric(k) || length(k) != 1 || k < 1) {
stop("k should be a positive integer")
}

# store the number of rows and columns in input matrix X
# (d being the dimension and n being number of points in X)
d <- ncol(X)
n <- nrow(X)
# find the Euclidean distances from k nearest neighbors using input matrix X, the query matrix
# Grid, a maximum number n of nearest neighbors to search, and the 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)
# function in its final form, dividing smoothing parameter k by the number of points in X multiplied
# by the volume of unit ball and the Euclidean distances from k nearest neighbors to the power of d dimensions
out <- k / (n * v.d * r.k ^ d)
return(out)
}
}