rexpectation-maximization

Binomial Distribution in in R


I applied this code and I get this error over and over again.

library(mixtools)

simulate <- function(lambda=0.3, mu=c(0, 4), sd=c(1, 1), n.obs=10^5) {
    x1 <- rnorm(n.obs, mu[1], sd[1])
    x2 <- rnorm(n.obs, mu[2], sd[2])    
    return(ifelse(runif(n.obs) < lambda, x1, x2)) 
}

x <- simulate()

model <- normalmixEM(x=x, k=2)

Error: object 'C_normpost' not found

Can anyone help me with this?

If on the other hand someone can think of a way to separate two binomial distributions it would be great.


Solution

  • If you have the mixtools package properly installed there should not be an error. I have version 1.1.0 installed which CRAN tells me is the most recent released version. A .C() call to C_normpost is made in three different places in the body of normalmixEM. After that code I see:

    str(model)   # don't type just `model` ... appears to have no print method for it.
    List of 9
     $ x         : num [1:100000] 3.33 3.17 -2.04 3.66 5.11 ...
     $ lambda    : num [1:2] 0.299 0.701
     $ mu        : num [1:2] -0.00253 3.9986
     $ sigma     : num [1:2] 0.994 1.001
     $ loglik    : num -197426
     $ posterior : num [1:100000, 1:2] 1.94e-03 3.66e-03 1.00 5.17e-04 1.42e-06 ...
      ..- attr(*, "dimnames")=List of 2
      .. ..$ : NULL
      .. ..$ : chr [1:2] "comp.1" "comp.2"
     $ all.loglik: num [1:39] -427524 -216959 -208812 -204523 -200531 ...
     $ restarts  : num 0
     $ ft        : chr "normalmixEM"
     - attr(*, "class")= chr "mixEM"
    
    summary(model)
    summary of normalmixEM object:
               comp 1   comp 2
    lambda  0.2992979 0.700702
    mu     -0.0025302 3.998599
    sigma   0.9935503 1.001362
    loglik at estimate:  -197426.2 
    

    So it has done a rather good job of estimating the means (0 and 4) as well as a very good job of estimating the variances of those components (1 and 1). You should probably try to reinstall mixtools in a fresh session and rerun the code.