Skip to content

Commit

Permalink
Merge 366c8aa into 8205fd7
Browse files Browse the repository at this point in the history
  • Loading branch information
AEBilgrau committed May 31, 2019
2 parents 8205fd7 + 366c8aa commit 7515e9a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -9,6 +9,7 @@ export(SimulateGMMData)
export(Uhat)
export(as.theta)
export(choose.theta)
export(classify)
export(dmvnormal)
export(fit.full.GMCM)
export(fit.general.GMCM)
Expand Down
36 changes: 36 additions & 0 deletions R/classify.R
@@ -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)
}
3 changes: 2 additions & 1 deletion inst/NEWS.Rd
Expand Up @@ -6,9 +6,10 @@
\section{Changes in version 1.4}{
\itemize{
\item Added a shiny application for the package. Available at \href{https://gmcm.shinyapps.io/GMCM/}{online} or via \code{GMCM::runGMCM()} for a local instance.
\item Added \code{classify()} function to ease classification after fitting a general GMCM.
}
}

\section{Changes in version 1.3.2 (2018-03-12)}{
\itemize{
\item Hotfix due to new default RNG after R version 3.5.3.
Expand Down
38 changes: 38 additions & 0 deletions man/classify.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions tests/testthat/test-classify.R
@@ -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)
})

0 comments on commit 7515e9a

Please sign in to comment.