Nothing
#' Multivariate normal mixture model clustering
#'
#' Implement Gibbs sampling for MVN model with no spatial random effects
#'
#' @param Y An n x g matrix of gene expression values. n is the number of cell spots and g is the number of features.
#' @param K The number of mixture components to fit.
#' @param nsim Number of total MCMC iterations to run.
#' @param burn Number of MCMC iterations to discard as burn in. The number of saved samples is nsim - burn.
#' @param z_init Optional initialized allocation vector. Randomly initialized if NULL.
#'
#' @return a list of posterior samples
#' @export
#' @importFrom utils setTxtProgressBar txtProgressBar
#' @importFrom mvtnorm rmvnorm
#' @importFrom stats cov kmeans
#' @importFrom MCMCpack rdirichlet
#' @examples
#' \donttest{
#' n <- 100 # number of observations
#' g <- 3 # number of features
#' K <- 3 # number of clusters (mixture components)
#' pi <- rep(1/K,K) # cluster membership probability
#' z <- sample(1:K, size = n, replace = TRUE, prob = pi) # cluster indicators
#' z <- remap_canonical2(z)
#'
#' # Cluster Specific Parameters
#' # cluster specific means
#' Mu <- list(
#' Mu1 = rnorm(g,-5,1),
#' Mu2 = rnorm(g,0,1),
#' Mu3 = rnorm(g,5,1)
#' )
#' # cluster specific variance-covariance
#' S <- matrix(1,nrow = g,ncol = g) # covariance matrix
#' diag(S) <- 1.5
#' Sig <- list(
#' Sig1 = S,
#' Sig2 = S,
#' Sig3 = S
#' )
#'
#' Y <- matrix(0, nrow = n, ncol = g)
#' for(i in 1:n)
#' {
#' Y[i,] <- mvtnorm::rmvnorm(1,mean = Mu[[z[i]]],sigma = Sig[[z[i]]])
#' }
#'
#' # fit model
#' fit1 <- fit_mvn(Y,3,10,0)}
fit_mvn <- function(Y,
K,
nsim = 2000,
burn = 1000,
z_init = NULL)
{
# parameters
n <- nrow(Y) # number of observations
p <- ncol(Y) # number of features
pi <- rep(1/K,K) # cluster membership probability
if(is.null(z_init)) # initialize z
{
fit_kmeans <- kmeans(Y,centers = K)
z_init <- fit_kmeans$cluster
z <- z_init
}
else # user provided initialization
{
z <- z_init
pi <- table(z)/n
}
# priors - shared across clusters
mu0 <- colMeans(Y)
L0 <- S0 <- diag(p)
nu0 <- 2
a0 <- rep(4,K) # prior parameter vector for pi1,...,piK
# cluster specific sample stats
Sigma <- list(0)
Ybar <- list(0)
for(k in 1:K)
{
Sigma[[k]] <- stats::cov(Y[z == k,])
Ybar[[k]] <- colMeans(Y[z == k,])
}
# Intermediate MCMC vars
Ln <- list(0)
mn <- list(0)
mun <- list(0)
Sn <- list(0)
# Empty sample storage
MU <- SIGMA <- vector("list",K)
n_save <- nsim - burn
Z <- matrix(0,nrow = n_save,ncol = n)
for(k in 1:K)
{
MU[[k]] <- matrix(0,nrow = n_save,ncol = p)
SIGMA[[k]] <- matrix(0,nrow = n_save,ncol = p*p)
}
start.time <- proc.time()
message(paste("Started MCMC of",nsim))
pb <- txtProgressBar(min = 0, max = nsim, style = 3)
for(i in 1:nsim)
{
### Update cluster - specific parameters
for(k in 1:K)
{
### update cluster specific sample stats
nk <- sum(z == k)
Ybar[[k]] <- colMeans(Y[z == k,])
### update mu - cluster specific
Ln[[k]] <- solve(solve(L0) + nk*solve(Sigma[[k]]))
mn[[k]] <- Ln[[k]] %*% (solve(L0) %*% mu0 + nk*solve(Sigma[[k]]) %*% Ybar[[k]])
mun[[k]] <- mvrnormArma(1 ,mn[[k]],Ln[[k]])
### update Sigma - cluster specific
Sn[[k]] <- S0 + (t(Y[z == k,]) - c(mun[[k]])) %*% t(t(Y[z == k,]) - c(mun[[k]]))
Sigma[[k]] <- solve(rwishart(nu0+nk, solve(Sn[[k]])))
}
### Update cluster indicators
z <- update_z(z,Y,mun,Sigma,pi,1:K)
# remap to address label switching
z <- remap_canonical2(z)
n.z <- as.vector(unname(table(z))) # gives the number of members currently in each class
# Update pi1,...,piK
pi <- MCMCpack::rdirichlet(1,a0 + n.z)
## save results
if(i > burn)
{
iter <- i - burn
for(k in 1:K)
{
MU[[k]][iter,] <- mun[[k]]
SIGMA[[k]][iter,] <- c(Sigma[[k]])
}
Z[iter,] <- z
}
setTxtProgressBar(pb, i)
}
close(pb)
run.time<-proc.time()-start.time
message(paste("Finished MCMC after",run.time[1],"seconds"))
z_map <- apply(Z, 2, get_map)
ret_list <- list(Y = Y,
W = NULL,
coords_df = NULL,
MU = MU,
XI = NULL,
SIGMA = SIGMA,
DELTA = NULL,
K = K,
Z = Z,
z = z_map,
z_init = z_init)
return(ret_list)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.