R/sstep.R

Defines functions sstep

Documented in sstep

#' S-step of EM algorithm
#' @description Given the posterior probability, generate a matrix to assign
#' each individual to a class. The assignment is randomly sampled based on the posterior probability.
#' @param postpr (`matrix()`) \cr
#' The matrix of the posterior probability
sstep <- function(postpr) {
  assign_func <- function(postpr) {
    vec <- rmultinom(seq_len(length(postpr)), size = 1, prob = postpr)
    return(vec)
  }
  y <- apply(postpr, 1, assign_func)
  if (ncol(postpr) == 1) {
    return(matrix(y, ncol = 1))
  } else {
    return(t(y))
  }
}

Try the em package in your browser

Any scripts or data that you put into this service are public.

em documentation built on Jan. 11, 2023, 9:07 a.m.