[R-sig-ME] Latent class in Gaussian Mixture model

jas ni jasnie111 at gmail.com
Sat Dec 31 12:07:14 CET 2016


Hi Guys,

I would like to get any advice on the latent class in the mixture model.
But i wish to do latent code by hand without relying on the existing R
package. This is my snippet code to do the finite mixture:

no<-nrows(myData.obs)
prob1 = pi1*dmvnorm(myData.obs,m1,Sigma1);   # probabilities of sample
points under model 1
prob2 = pi2*dmvnorm(myData.obs,m2,Sigma2);   # same for model 2
Z<-rbinom(no,1,prob1/(prob1 + prob2 ))

pi1<-rbeta(1,sum(Z)+1/2,no-sum(Z)+1/2)if (pi1>1/2) {
  pi1<-1-pi1
  Z<-1-Z}

I want to generate the Z values which is able to classify the data points
into the particular Gaussian component such as 0 for the first Gaussian and
1 for the second Gaussian component with the given mean and sigma. However,
if i run the code with 30 number of iterations, only the first and second
iteration giving some changes on the data classification. The next
iteration is remain unchanged which is means that the data point are not
properly assigned to the particular Gaussian component.The results for
first until third iteration for Z are shown below.

The result for the first iteration:

[1] 1 1 0 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1 0 0 1 0
1 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0 0 0 1 0[61] 0 1 0 0 1 0
1 0 0 1 0 0 0 1 0 0 0 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 0
1 0 0 1 0 1 0 1 0 1 0 0 1 0 0 1 0 1 0[121] 1 0 1 0 1 0 1 0 1 0 0 1 0 0
0 1 0 1 0 1 0 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 1 0 0 0 0 0 1 0
0 1 0 0 0 1 0 0 1 0 1[181] 0 1 0 0 0 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 1 0
1 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 1 0 1 0 1 1 0 0 1 0 1 0 0 0 0 1
0 1 0[241] 1 0 0 0 0 0 0 0 1 0 0 0 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 0 1 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0
0 0 0 1 0 1 0 1 1 0 0 1[241] 0 1 0 1 0 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 0
0 1 0 1 1 0 0 1 0 1 0

What i want to do is, the Z values for latent class should be changed /
updated based on the current probability prob1 and prob2 in each iteration.

I would feel grateful if you could give me any advice or comment to improve
the solution.

Thank you in advance

- Jas

	[[alternative HTML version deleted]]



More information about the R-sig-mixed-models mailing list