# NOT RUN {
# parameters
data(coords_df_sim)
coords_df <- coords_df_sim[,1:2]
z <- remap_canonical2(coords_df_sim$z)
n <- nrow(coords_df) # number of observations
g <- 3 # number of features
K <- length(unique(coords_df_sim$z)) # number of clusters (mixture components)
pi <- table(z)/length(z) # cluster membership probability
# Cluster Specific Parameters
# cluster specific means
Mu <- list(
Mu1 = rnorm(g,-2,1),
Mu2 = rnorm(g,-1,1),
Mu3 = rnorm(g,1,1),
Mu4 = rnorm(g,2,1)
)
# cluster specific variance-covariance
S <- matrix(0.5,nrow = g,ncol = g) # y covariance matrix
diag(S) <- 1
Sig <- list(
Sig1 = S,
Sig2 = S,
Sig3 = S,
Sig4 = 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]]])
}
# sometimes helps to initialize using heuristic like kmeans
fitk <- stats::kmeans(Y,4)
z_km <- remap_canonical2(fitk$cluster)
# fit model
# use more iterations in practice
fit1 <- fit_mvn_smooth(Y,coords_df,4,2,10,0,z_km)
# }
Run the code above in your browser using DataLab