rm(list=ls())
#Number of data
n <- 2000
set.seed(4321)
d <- 2
ncl <- 4
# Sample data
library(truncnorm)
sdev <- array(dim=c(d,d,ncl))
#xi <- matrix(nrow=d, ncol=ncl, c(-1.5, 1.5, 1.5, 1.5, 2, -2.5, -2.5, -3))
#xi <- matrix(nrow=d, ncol=ncl, c(-0.5, 0, 0.5, 0, 0.5, -1, -1, 1))
xi <- matrix(nrow=d, ncol=ncl, c(-0.2, 0.5, 2.4, 0.4, 0.6, -1.3, -0.9, -2.7))
psi <- matrix(nrow=d, ncol=4, c(0.3, -0.7, -0.8, 0, 0.3, -0.7, 0.2, 0.9))
nu <- c(100,25,8,5)
p <- c(0.15, 0.05, 0.5, 0.3) # frequence des clusters
sdev[, ,1] <- matrix(nrow=d, ncol=d, c(0.3, 0, 0, 0.3))
sdev[, ,2] <- matrix(nrow=d, ncol=d, c(0.1, 0, 0, 0.3))
sdev[, ,3] <- matrix(nrow=d, ncol=d, c(0.3, 0.15, 0.15, 0.3))
sdev[, ,4] <- .3*diag(2)
c <- rep(0,n)
w <- rep(1,n)
z <- matrix(0, nrow=d, ncol=n)
for(k in 1:n){
c[k] = which(rmultinom(n=1, size=1, prob=p)!=0)
w[k] <- rgamma(1, shape=nu[c[k]]/2, rate=nu[c[k]]/2)
z[,k] <- xi[, c[k]] + psi[, c[k]]*rtruncnorm(n=1, a=0, b=Inf, mean=0, sd=1/sqrt(w[k])) +
(sdev[, , c[k]]/sqrt(w[k]))%*%matrix(rnorm(d, mean = 0, sd = 1), nrow=d, ncol=1)
#cat(k, "/", n, " observations simulated\\n", sep="")
}
# Set parameters of G0
hyperG0 <- list()
hyperG0[["b_xi"]] <- rowMeans(z)
hyperG0[["b_psi"]] <- rep(0,d)
hyperG0[["kappa"]] <- 0.001
hyperG0[["D_xi"]] <- 100
hyperG0[["D_psi"]] <- 100
hyperG0[["nu"]] <- d+1
hyperG0[["lambda"]] <- diag(apply(z,MARGIN=1, FUN=var))/3
# hyperprior on the Scale parameter of DPM
a <- 0.0001
b <- 0.0001
# do some plots
doPlot <- TRUE
nbclust_init <- 30
## Data
########
library(ggplot2)
p <- (ggplot(data.frame("X"=z[1,], "Y"=z[2,]), aes(x=X, y=Y))
+ geom_point()
#+ ggtitle("Simple example in 2d data")
+xlab("D1")
+ylab("D2")
+theme_bw())
p #pdf(height=8.5, width=8.5)
c2plot <- factor(c)
levels(c2plot) <- c("4", "1", "3", "2")
pp <- (ggplot(data.frame("X"=z[1,], "Y"=z[2,], "Cluster"=as.character(c2plot)))
+ geom_point(aes(x=X, y=Y, colour=Cluster, fill=Cluster))
#+ ggtitle("Slightly overlapping skew-normal simulation\\n")
+ xlab("D1")
+ ylab("D2")
+ theme_bw()
+ scale_colour_discrete(guide=guide_legend(override.aes = list(size = 6, shape=22))))
pp #pdf(height=7, width=7.5)
## alpha priors plots
#####################
prioralpha <- data.frame("alpha"=rgamma(n=5000, shape=a, scale=1/b),
"distribution" =factor(rep("prior",5000),
levels=c("prior", "posterior")))
p <- (ggplot(prioralpha, aes(x=alpha))
+ geom_histogram(aes(y=..density..),
colour="black", fill="white")
+ geom_density(alpha=.2, fill="red")
+ ggtitle(paste("Prior distribution on alpha: Gamma(", a,
",", b, ")\n", sep=""))
)
p
# Gibbs sampler for Dirichlet Process Mixtures
##############################################
MCMCsample_st <- DPMGibbsSkewT(z, hyperG0, a, b, N=2000,
doPlot, nbclust_init, plotevery=100,
gg.add=list(theme_bw(),
guides(shape=guide_legend(override.aes = list(fill="grey45")))),
diagVar=FALSE)
s <- summary(MCMCsample_st, burnin = 900, thin=2, lossFn = "Binder")
print(s)
plot(s, hm=TRUE) #pdf(height=8.5, width=10.5) #png(height=700, width=720)
plot_ConvDPM(MCMCsample_st, from=2)
#cluster_est_binder(MCMCsample_st$mcmc_partitions[900:1000])
postalpha <- data.frame("alpha"=s$alpha,
"distribution" = factor(rep("posterior",length(s$alpha)),
levels=c("prior", "posterior")))
postK <- data.frame("K"=sapply(lapply(postalpha$alpha, "["),
function(x){sum(x/(x+0:(n-1)))}))
p <- (ggplot(postalpha, aes(x=alpha))
+ geom_histogram(aes(y=..density..), binwidth=.1,
colour="black", fill="white")
+ geom_density(alpha=.2, fill="blue")
+ ggtitle("Posterior distribution of alpha\n")
# Ignore NA values for mean
# Overlay with transparent density plot
+ geom_vline(aes(xintercept=mean(alpha, na.rm=TRUE)),
color="red", linetype="dashed", size=1)
)
p
p <- (ggplot(postK, aes(x=K))
+ geom_histogram(aes(y=..density..),
colour="black", fill="white")
+ geom_density(alpha=.2, fill="blue")
+ ggtitle("Posterior distribution of predicted K\n")
# Ignore NA values for mean
# Overlay with transparent density plot
+ geom_vline(aes(xintercept=mean(K, na.rm=TRUE)),
color="red", linetype="dashed", size=1)
#+ scale_x_continuous(breaks=c(0:6)*2, minor_breaks=c(0:6)*2+1)
+ scale_x_continuous(breaks=c(1:12))
)
p
p <- (ggplot(drop=FALSE, alpha=.6)
+ geom_density(aes(x=alpha, fill=distribution),
color=NA, alpha=.6,
data=postalpha)
+ geom_density(aes(x=alpha, fill=distribution),
color=NA, alpha=.6,
data=prioralpha)
+ ggtitle("Prior and posterior distributions of alpha\n")
+ scale_fill_discrete(drop=FALSE)
+ theme_bw()
)
p
postK <- data.frame("K"=sapply(lapply(s$mcmc_partitions, unique), length))
(ggplot(postK, aes(x=K))
+ geom_histogram(aes(y=..density..),
colour="black", fill="grey45", binwidth=1)
+ ggtitle("Posterior distribution of K\n")
# Ignore NA values for mean
# Overlay with transparent density plot
+ geom_vline(aes(xintercept=mean(K, na.rm=TRUE)),
color="red", linetype="dashed", size=1)
+ theme_bw()
+ scale_x_continuous(breaks=c(3:11))
+ xlim(3,11)
)
# k-means
plot(x=z[1,], y=z[2,], col=kmeans(t(z), centers=4)$cluster,
xlab = "d = 1", ylab= "d = 2", main="k-means with K=4 clusters")
KM <- kmeans(t(z), centers=4)
KMclust <- factor(KM$cluster)
levels(KMclust) <- c("2", "4", "1", "3")
dataKM <- data.frame("X"=z[1,], "Y"=z[2,],
"Cluster"=as.character(KMclust))
dataCenters <- data.frame("X"=KM$centers[,1],
"Y"=KM$centers[,2],
"Cluster"=c("2", "4", "1", "3"))
p <- (ggplot(dataKM)
+ geom_point(aes(x=X, y=Y, col=Cluster))
+ geom_point(aes(x=X, y=Y, fill=Cluster, order=Cluster),
data=dataCenters, shape=22, size=5)
+ scale_colour_discrete(name="Cluster", guide=guide_legend(override.aes=list(size=6,
shape=22)))
+ ggtitle("K-means with K=4 clusters\n")
+ theme_bw()
)
pRun the code above in your browser using DataLab