From 4d329c23a50f7d88b8e7c28543c2a285fe3f1649 Mon Sep 17 00:00:00 2001 From: benholmgren <44175897+benholmgren@users.noreply.github.com> Date: Thu, 6 Dec 2018 11:04:49 -0700 Subject: [PATCH 1/3] Added documentation to knnDE function Briefly explained parameters and return value with roxygen style comments, then added documentation to walk through code --- R/knnDE.R | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/R/knnDE.R b/R/knnDE.R index 5f57519..1e64309 100644 --- a/R/knnDE.R +++ b/R/knnDE.R @@ -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) -} \ No newline at end of file +} From ff3a754fe81934074615436d0e3875724c90e5e9 Mon Sep 17 00:00:00 2001 From: benholmgren <44175897+benholmgren@users.noreply.github.com> Date: Mon, 25 Feb 2019 10:35:58 -0700 Subject: [PATCH 2/3] Revised comments for knnDE Streamlined comments --- R/knnDE.R | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/R/knnDE.R b/R/knnDE.R index 1e64309..b186dd1 100644 --- a/R/knnDE.R +++ b/R/knnDE.R @@ -9,31 +9,26 @@ 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) + # store d, the dimension of X, and n, the 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") + # 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) - # 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 + # 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) } From 03c10c500ee0dd71fcfb38dece7048954f54919a Mon Sep 17 00:00:00 2001 From: benholmgren <44175897+benholmgren@users.noreply.github.com> Date: Tue, 26 Feb 2019 12:45:40 -0700 Subject: [PATCH 3/3] Added title to knnDE roxygen roxygen needs a title to make documentation reliably --- R/knnDE.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/knnDE.R b/R/knnDE.R index b186dd1..d2e5c65 100644 --- a/R/knnDE.R +++ b/R/knnDE.R @@ -1,8 +1,13 @@ +#' @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