# # simple.S # # demo program for gaussian mixture (2-dim) # # Splus code of the EM algorithm for mixture models # copyright by Shotaro Akaho # AIST Neuroscience Research Instutute, Japan # Notice: # 1. Only noncommercial use is allowed # 2. Attach this header in the case of redistribution # 3. If you find any bugs, it is strongly recommended to inform them to me. # device init (if you want to use another device, modify the following) motif() # initialize a model (2 gaussian) g <- list() for(i in 1:2) { g[[i]] <- gauss(rnorm(2), diag(rnorm(2)^2)) # initialization parameter shold be modified appropriately } g <- mixture(g) # sample generator (Num_of_samples x 2 matrix) # this part could be replaced by your data x <- matrix(rnorm(100, c(0, 1)), ncol=2) x <- rbind(matrix(rnorm(100, c(1, 0)), ncol=2)) # run the EM algorithm with printing the mean log likelihood for(iter in 1:10) { g <- estimate(g, x) print(like.em(g, x)) } # plot the result plot.em(g, x)