我一直在用findAssoc()
使用文本挖掘( tm
包)但意识到我的数据集似乎有些不对劲。
我的数据集是保存在一列 csv 文件中的 1500 个开放式答案。
所以我这样称呼数据集并使用典型的tm_map
使其成为语料库。
library(tm)
Q29 <- read.csv("favoritegame2.csv")
corpus <- Corpus(VectorSource(Q29$Q29))
corpus <- tm_map(corpus, tolower)
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeNumbers)
corpus<- tm_map(corpus, removeWords, stopwords("english"))
dtm<- DocumentTermMatrix(corpus)
findAssocs(dtm, "like", .2)
> cousin fill ....
0.28 0.20
一季度。当我找到与
like
相关的条款时,我没有看到输出 like = 1
作为输出的一部分。然而,dtm.df <-as.data.frame(inspect(dtm))
该数据框由 1500 个 obs 组成。 1689 个变量..(或者是因为数据保存在一行 csv 文件中?)
Q2。即使
cousin
和 fill
当目标词like
出现一次出现一次,分数就这样不同了。他们不应该是一样的吗?我试图找到
findAssoc()
的数学公式但还没有成功。任何建议都非常感谢!
最佳答案
我想没有人回答过你的最后一个问题。
I'm trying to find the math of findAssoc() but no success yet. Any advice is highly appreciated!
findAssoc() 的数学基于 R 的 stats 包中的标准函数 cor()。给定两个数值向量,cor() 计算它们的协方差除以两个标准差。
因此,给定包含术语“word1”和“word2”的 DocumentTermMatrix dtm,使得 findAssocs(dtm, “word1”, 0) 返回值为 x 的“word2”,即“word1”和“word2”的术语向量的相关性是 x。
一个冗长的例子
> data <- c("", "word1", "word1 word2","word1 word2 word3","word1 word2 word3 word4","word1 word2 word3 word4 word5")
> dtm <- DocumentTermMatrix(VCorpus(VectorSource(data)))
> as.matrix(dtm)
Terms
Docs word1 word2 word3 word4 word5
1 0 0 0 0 0
2 1 0 0 0 0
3 1 1 0 0 0
4 1 1 1 0 0
5 1 1 1 1 0
6 1 1 1 1 1
> findAssocs(dtm, "word1", 0)
$word1
word2 word3 word4 word5
0.63 0.45 0.32 0.20
> cor(as.matrix(dtm)[,"word1"], as.matrix(dtm)[,"word2"])
[1] 0.6324555
> cor(as.matrix(dtm)[,"word1"], as.matrix(dtm)[,"word3"])
[1] 0.4472136
以此类推第 4 和第 5 个词。
另见 http://r.789695.n4.nabble.com/findAssocs-tt3845751.html#a4637248
关于r - tm::findAssocs 的数学这个函数是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14267199/