Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number of components display in fviz_mclust_bic #84

Open
hpsprecher opened this issue Feb 10, 2019 · 2 comments
Open

Number of components display in fviz_mclust_bic #84

hpsprecher opened this issue Feb 10, 2019 · 2 comments

Comments

@hpsprecher
Copy link

hpsprecher commented Feb 10, 2019

The function fviz_mclust_bic displays the components in the wrong order when the number of clusters for the mclust models is between single digit and lower double digit (e.g. 2-17). It shows the values for the components in the order 1, 10, 11, 12,...., 2,3,4,...)
M1<- Mclust(iris[,-5], G=seq(2,13), verbose = T)
fviz_mclust_bic(M1)

I've made a small modification (below in bold) on the function and it seems to work now. Please check

fviz_mclust_bic_01<- function (object, model.names = NULL, shape = 19, color = "model", palette = NULL, legend = NULL, main = "Model selection", xlab = "Number of components", ylab = "BIC", ...)
{ if (!inherits(object, "Mclust")) stop("An object of class Mclust is required.")
best_model <- object$modelName
number_of_cluster <- object$G
x <- object$BIC
n <- ncol(x)
dnx <- dimnames(x)
x <- matrix(as.vector(x), ncol = n)
dimnames(x) <- dnx
x <- as.data.frame(x)
if (is.null(model.names)) model.names <- dimnames(x)[[2]]
x <- x[, model.names, drop = FALSE]
x <- cbind.data.frame(cluster = rownames(x), x)
x <- tidyr::gather_(x, key_col = "model", value_col = "BIC", gather_cols = colnames(x)[-1])
x <- x[!is.na(x$BIC), , drop = FALSE]
x$cluster<- factor(x$cluster, levels = dnx[[1]])
x$model <- factor(x$model, levels = dnx[[2]])
if (ggpubr:::.is_col_palette(palette)) palette <- ggpubr:::.get_pal(palette, k = length(model.names))
ggline.opts <- list(data = x, x = "cluster", y = "BIC", group = "model", color = color, shape = shape, palette = palette, main = main, xlab = xlab, ylab = ylab, ...)
p <- do.call(ggpubr::ggline, ggline.opts) + labs(subtitle = paste0("Best model: ", best_model, " | Optimal clusters: n = ", number_of_cluster)) + geom_vline(xintercept = number_of_cluster, linetype = 2, color = "red") + theme(legend.title = element_blank())
if (missing(legend)) p + theme(legend.position = c(0.7, 0.2), legend.direction = "horizontal", legend.key.height = unit(0.5, "line")) + guides(color = guide_legend(nrow = 5, byrow = TRUE)) else p + theme(legend.position = legend)
}

With this modification, it looks like the plot is now displayed in the right order.

fviz_mclust_bic_01(M1)

@hpsprecher
Copy link
Author

any comment on that? are there any plans to update the function in the above mentioned manner?

@kassambara
Copy link
Owner

fixed now, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants