[英]R Cluster Analysis
我正在遵循以下https://rstudio-pubs-static.s3.amazonaws.com/31867_8236987cf0a8444e962ccd2aec46d9c3.html中列出的代碼
library(cluster)
d <- dist(t(dtmss), method="euclidian")
fit <- hclust(d=d, method="ward")
fit
plot.new()
plot(fit, hang=-1)
groups <- cutree(fit, k=5)
rect.hclust(fit, k=5, border="red")
如何在每個群集中打印單詞? 樹狀圖非常局促,完全不可讀。
謝謝!
編輯:
對於輸入,請考慮任何帶有名為“ Comment”的列的csv文件。 每個觀察(50行)都有文本注釋。
然后,我使用上面鏈接中的代碼:
library(tm)
input = read.csv("FILEPATH/InputFile.csv")
summary(input)
comments <- Corpus(VectorSource(input$Comment))
data <- tm_map(comments, removePunctuation)
data <- tm_map(data, removeNumbers)
data <- tm_map(data, tolower)
data <- tm_map(data, removeWords, stopwords("english"))
data <- tm_map(data, PlainTextDocument)
dtm <- DocumentTermMatrix(data)
freq <- colSums(as.matrix(dtm))
ord <- order(freq)
findFreqTerms(dtm, lowfreq = 10)
freq <- sort(colSums(as.matrix(dtm)), decreasing = TRUE)
head(freq, 30)
dtms <- removeSparseTerms(dtm, 0.1)
inspect(dtms)
library(cluster)
d <- dist(t(dtms), method="euclidian")
fit <- hclust(d=d, method="ward")
fit
plot(fit, hang=-1)
plot.new()
plot(fit, hang=-1)
groups <- cutree(fit, k=5)
rect.hclust(fit, k=5, border="red")
我希望這是足夠的信息。
再次感謝。
您可以從組中獲取觀察所在的集群,然后基於它們對數據進行子集化:
t(dtms)[groups==1]
應該打印出集群1的成員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.