### !! This algorithm is very slow, so we'll only test it on some datasets !!
### Helper functions
dmnorm <- function(x, mu, sigma) {
k <- ncol(sigma)
x <- as.matrix(x)
diff <- t(t(x) - mu)
num <- exp(-1 / 2 * diag(diff %*% solve(sigma) %*% t(diff)))
den <- sqrt(((2 * pi)^k) * det(sigma))
num / den
}
test <- function(db, k) {
print(cl <- gaussian_mixture(db, k, 100))
x <- seq(min(db[, 1]), max(db[, 1]), length.out = 100)
y <- seq(min(db[, 2]), max(db[, 2]), length.out = 100)
plot(db, col = cl$cluster, asp = 1, pch = 20)
for (i in seq_len(k)) {
m <- cl$mu[i, ]
s <- cl$sigma[i, , ]
f <- function(x, y) cl$lambda[i] * dmnorm(cbind(x, y), m, s)
z <- outer(x, y, f)
contour(x, y, z, col = i, add = TRUE)
}
}
### Example 1
test(db1[1:250,], 2)
### Example 2 (with explanations, no plots)
cl <- gaussian_mixture(
db5[1:10, ],
3,
learn = TRUE,
waiting = FALSE
)
Run the code above in your browser using DataLab