### !! 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 <- clustlearn::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(clustlearn::db1, 2)
### Example 2
# test(clustlearn::db2, 2)
### Example 3
test(clustlearn::db3, 3)
### Example 4
test(clustlearn::db4, 3)
### Example 5
test(clustlearn::db5, 3)
### Example 6
# test(clustlearn::db6, 3)
### Example 7 (with explanations, no plots)
cl <- clustlearn::gaussian_mixture(
clustlearn::db5[1:20, ],
3,
details = TRUE,
waiting = FALSE
)
Run the code above in your browser using DataLab