Skip to content

Commit

Permalink
Enforce sum constraint re issue #18 in as.theta.
Browse files Browse the repository at this point in the history
  • Loading branch information
AEBilgrau committed Dec 29, 2018
1 parent bb49e50 commit 014a3bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
16 changes: 12 additions & 4 deletions R/as.theta.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' Means on matrix form are as assumed to be \code{d} by \code{m}. I.e.
#' rows correspond to the dimensions and columns to components, or the mean vectors
#' as column vectors.
#' Finally, the sum constriant of 1 for the mixture proportions is enforced.
#'
#' @param x A theta-like object that can be coerced.
#' @return A theta object. See \code{\link{rtheta}}.
Expand All @@ -29,10 +30,11 @@
#' theta <- as.theta(x)
#' print(theta)
#'
#' x2 <- unname(list(
#' pie = c(0.5, 0.5),
#' mu = simplify2array(list(comp1=rep(0,d), comp2=rep(1,d))),
#' sigma = simplify2array(list(comp1=diag(d), comp2=diag(d)))
#' x2 <- unname(list( # Unnamed
#' # missing m and d
#' pie = c(1, 1), # Does not sum to 1
#' mu = simplify2array(list(comp1=rep(0,d), comp2=rep(1,d))), # matrix, not a list
#' sigma = simplify2array(list(comp1=diag(d), comp2=diag(d))) # array, not a list
#' ))
#' theta2 <- as.theta(x2)
#' print(theta2)
Expand Down Expand Up @@ -80,6 +82,12 @@ as.theta <- function(x) {
names = dimnames(x[[5]])[[3]])
}

# Enforce sum contraint of pie to 1
if (!isTRUE(all.equal(sum(x$pie), 1))) {
x$pie <- x$pie/sum(x$pie)
warning("x$pie rescaled to enforce sum constraint of 1")
}

if (is.theta(x)) {
return(x)
} else {
Expand Down
10 changes: 6 additions & 4 deletions man/as.theta.Rd

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

11 changes: 7 additions & 4 deletions tests/testthat/test-as.theta.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ test_that("Check that as.theta works as intended", {

# Coercion tests
d <- 3
x2 <- unname(list(
pie = c(0.5, 0.5),
mu = simplify2array(list(comp1 = rep(0,d), comp2 = rep(1,d))),
sigma = simplify2array(list(comp1 = diag(d), comp2 = diag(d)))
x2 <- unname(list( # Unnamed
# missing m and d
pie = c(1, 1), # Does not sum to 1
mu = simplify2array(list(comp1 = rep(0,d),
comp2 = rep(1,d))), # matrix, not a list
sigma = simplify2array(list(comp1 = diag(d),
comp2 = diag(d))) # array, not a list
))

test_that("Check that as.theta works for 'matrix means'", {
Expand Down

0 comments on commit 014a3bc

Please sign in to comment.