## use (mu, sigma)
ex <- norMix(mu = c(-1,2,5), sigma = c(1, 1/sqrt(2), sqrt(3)))
tools::assertWarning(verbose=TRUE,
## *deprecated* (using 'sig2' will *NOT* work in future!)
ex. <- norMix(mu = c(-1,2,5), sig2 = c(1, 0.5, 3))
)
stopifnot(all.equal(ex, ex.))
plot(ex, col="gray", p.norm=FALSE)
x <- rnorMix(100, ex)
lines(density(x))
rug(x)
## EM estimation may fail depending on random sample
ex1 <- norMixEM(x, 3, trace=2) #-> warning (sometimes)
ex1
plot(ex1)
## initialization by cut() into intervals of equal length:
ex2 <- norMixEM(x, cut(x, 3))
ex2
## initialization by kmeans():
k3 <- kmeans(x, 3)$cluster
ex3 <- norMixEM(x, k3)
ex3
## Now, MLE instead of EM:
exM <- norMixMLE(x, k3, tol = 1e-12, trace=4)
exM
## real data
data(faithful)
plot(density(faithful$waiting, bw = "SJ"), ylim=c(0,0.044))
rug(faithful$waiting)
(nmF <- norMixEM(faithful$waiting, 2))
lines(nmF, col=2)
## are three components better?
nmF3 <- norMixEM(faithful$waiting, 3, maxiter = 200)
lines(nmF3, col="forestgreen")
Run the code above in your browser using DataLab