rhierarchical-clusteringtopic-modelingdendrogramdendextend

Error in .rect_dendrogram(dend, k = k, palette = rect_border, rect_fill = rect_fill, : k must be between 2 and 97


I am trying to estimate a cluster dendrogram in R for a structural topic model I produced with 98 topics.

I first ran the following which worked well:

res.hc <- eclust(scale(out_corr$cor), "hclust", nboot = 500)

I then attempting to visualize the dendrogram using the following syntax:

fviz_dend(res.hc, rect = TRUE)

Here, I received the following error: Error in .rect_dendrogram(dend, k = k, palette = rect_border, rect_fill = rect_fill, : k must be between 2 and 97

Is this because the number of topics in my model is 98? If so, is there a way to still visualize the dendrogram without reducing my topics to 97?

Thank you!


Solution

  • The following steps helped to resolve the issue:

    1. estimate cluster dendrogram
    res.hc <- eclust(scale(out_corr$cor), "hclust", nboot = 500)
    
    1. install dendextend
    install.packages("dendextend")
    library(dendextend)
    
    1. install dplyr
    install.packages("dplyr")
    library(dplyr)
    
    1. save cluster estimate as a dendrogram
    dend<-as.dendrogram(res.hc)
    
    1. color in cluster levels
    par(mar=c(1,1,1,7))
    dend %>%
      set("labels_col", value = c("skyblue", "red", "grey", "blue"), k=4) %>%
      set("branches_k_color", value = c("skyblue", "red", "grey", "blue"), k = 4) %>%
      plot(horiz=FALSE, axes=FALSE)
    abline(v = 350, lty = 2)