1

I am new to gaussian mixture models and I am trying to learn how to use normalmixEM in R. I have read the documentation and in order to learn more about how it works I am trying to fit a mixture model to a fake data set that I created, however I get the following error and I am not sure why:

"Error: NA/NaN/Inf in foreign function call (arg 5)"

Code:

S=(t(t(runif(5,0,1))))
Initialprops=S/sum(S)
values=matrix(0L, nrow = 10000, ncol =1)
means=matrix(0L, nrow = 10000, ncol =1)
sigma=matrix(0L, nrow = 10000, ncol =1)
for (i in (1:10000))
    {values[i,1]=runif(1,0,1)
     means [i,1]=i
    sigma[i,1]=(2^(i-1))
    }
normalmixEM(values,Initialprops,means,sigma,5)

For the fake data set I just used a set of 10000 random numbers from a (0,1) uniform distribution. I used a set of 5 normalized random numbers for the initial mixing proportions. For the means I used the sequence 1, 2, 3, ... and for the sigma I used the sequence 1, 2, 4, 8 ...

Thank you!

  • Did you want to fit a mixture of 5 Gaussian distributions? If so, then your `means` and `sigma` vectors should be of length 5 (just like your `Initialprops` vector). I suggest you re-write your definition of the `means` and `sigma` vectors, but if you do something like `normalmixEM(values,Initialprops,means[1:5,1],sigma[1:5,1],5)`, the function will work properly. Also, in the R help, it is said for `k` that `Initial value ignored unless mu and sigma are both NULL.`. Therefore, your `k = 5` argument is not used here. – jav Sep 26 '16 at 01:13
  • Hello! Thanks for your help. I was trying to fit the data of the 10000 numbers to a k=5 (and thus why i had 5 initial mixing proportions). Do you happen to know how would I then fit the 10000 data points to a k=5? – Daniel López Sep 26 '16 at 19:19
  • That makes sense now. If you have no information beforehand, you could just do `normalmixEM(values, k = 5)`. If you have an idea of the initial proportions, you could do instead `normalmixEM(values,Initialprops, k = 5)`. Finally, if you have an idea of the initial means and sigma, then you would do `normalmixEM(values,Initialprops,means,sigma)`. However, your `means` and `sigma` vector should each be of length `k = 5` (you have one mean and one sd for each normal distribution). Let me know if this doesn't make sense and I will explain more. – jav Sep 26 '16 at 21:58
  • I think I understand now! So just to be sure, lets say we have the five initial proportions, the first mean and first sigma would be the means and standard dev of the group of values that lie in that first base group with the first initial mixing proportion yes? – Daniel López Sep 26 '16 at 22:40
  • That is correct :). – jav Sep 26 '16 at 22:55
  • 1
    THANK YOU SO MUCH YOU ROCK! – Daniel López Sep 26 '16 at 23:04

0 Answers0