1

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 1 0 1 1 0 0 1 0 1 0

The result for the second iteration:

[1] 0 1 0 1 0 1 0 0 1 0 1 0 0 1 0 1 1 0 1 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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.

kjetil b halvorsen
  • 63,378
  • 26
  • 142
  • 467
Jas
  • 11
  • 3
  • I (partly) understand why this question was migrated, but it might be difficult for people, who are not familiar with `R`, to understand your problem. Sharing the formulas with proper notation would be a better idea. If there is a problem in them, people can point out easily. In this format, it still looks like a question of implementation using `R`. – T.E.G. Jan 01 '17 at 20:37

0 Answers0