Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#' Classify observations | ||
#' | ||
#' Classify observations according to the maximum a posterior probabilites. | ||
#' | ||
#' @param x Either a \code{matrix} of A) observations where rows corresponds to | ||
#' obsercations and columns to dimensions or B) class probabilities where rows | ||
#' correspond to obsevations and columns to components. | ||
#' @param theta A list of parameters for the full model as described in | ||
#' \code{\link{rtheta}}. If \code{theta} is supplied, \code{x} are assumed to | ||
#' be observations (A). If \code{theta} is missing, \code{x} are assumed to be | ||
#' probabilites (B). | ||
#' | ||
#' @return A integer vector of class numbers with length equal to the number of | ||
#' rows in \code{x}. | ||
#' | ||
#' @examples | ||
#' # Classify using probabilites (usually returned from get.prob) | ||
#' probs <- matrix(runif(75), 25, 3) | ||
#' classify(probs) | ||
#' | ||
#' # Classify using a matrix of observations and theta | ||
#' theta <- rtheta(d = 4, m = 3) | ||
#' u <- SimulateGMCMData(n = 20, theta = theta)$u | ||
#' classify(x = u, theta = theta) | ||
#' @seealso \code{\link{get.prob}} | ||
#' @export | ||
classify <- function(x, theta) { | ||
if (missing(theta)) { | ||
stopifnot(all(0 <= x & x <= 1)) | ||
kappa <- x | ||
} else { | ||
kappa <- get.prob(x, theta) | ||
} | ||
map_class <- apply(kappa, 1, which.max) | ||
return(map_class) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
context("Check classify") | ||
|
||
# Create some test data | ||
d <- 2 | ||
m <- 3 | ||
n <- 50 | ||
|
||
test_that("classify returns expected types and ranges using theta", { | ||
theta <- rtheta(m = m, d = d, method = "EqualEllipsoidal") | ||
u <- SimulateGMCMData(n, theta = theta)$u | ||
out <- classify(x = u, theta = theta) | ||
expect_true(is.numeric(out)) | ||
expect_length(out, n) | ||
expect_lte(max(out), m) | ||
expect_gte(min(out), 1) | ||
}) | ||
|
||
test_that("classify returns expected types and ranges using probabilities", { | ||
prob <- matrix(runif(n*m), n, m) | ||
out <- classify(x = prob) | ||
expect_true(is.numeric(out)) | ||
expect_length(out, n) | ||
expect_lte(max(out), m) | ||
expect_gte(min(out), 1) | ||
}) | ||
|
||
# Test edge cases | ||
|
||
# m = 1 class, d = 2 | ||
d <- 2 | ||
m <- 1 | ||
n <- 25 | ||
theta <- rtheta(m = m, d = d, method = "EqualEllipsoidal") | ||
u <- SimulateGMCMData(n, theta = theta)$u | ||
|
||
test_that("classify returns only one class", { | ||
out <- classify(x = u, theta = theta) | ||
expect_true(is.numeric(out)) | ||
expect_length(out, n) | ||
expect_lte(max(out), m) | ||
expect_gte(min(out), 1) | ||
}) | ||
|