【问题标题】:Interpretation of dfm_weight(scheme='prop') with groups (quanteda)用组解释 dfm_weight(scheme='prop') (quanteda)
【发布时间】:2019-07-02 16:25:51
【问题描述】:

我正在使用 dfm_weight 查看不同的权重选项。如果我选择 scheme = 'prop' 并将 textstat_frequency 按location 分组,那么每个组中一个词的正确解释是什么?

假设在纽约,career 是 0.6,而在波士顿,team 是 4.0,我该如何解释这些数字?

    corp=corpus(df,text_field = "What are the areas that need the most improvement at our company?") %>% 
  dfm(remove_numbers=T,remove_punct=T,remove=c(toRemove,stopwords('english')),ngrams=1:2) %>%
  dfm_weight('prop') %>% 
  dfm_replace(pattern=as.character(lemma$first),replacement = as.character(lemma$X1)) %>% 
  dfm_remove(pattern = c(paste0("^", stopwords("english"), "_"), paste0("_", stopwords("english"), "$")), valuetype = "regex")
freq_weight <- textstat_frequency(corp, n = 10, groups = c("location"))


ggplot(data = freq_weight, aes(x = nrow(freq_weight):1, y = frequency)) +
  geom_bar(stat='identity')+
  facet_wrap(~ group, scales = "free") +
  coord_flip() +
  scale_x_continuous(breaks = nrow(freq_weight):1,
                     labels = freq_weight$feature) +
  labs(x = NULL, y = "Relative frequency")

【问题讨论】:

    标签: r quanteda


    【解决方案1】:

    正确的解释是,这是文档中原始术语比例的总和,但按组求和。这不是一个很自然的解释,因为它对比例求和,而在求和之前,您不知道该比例基于多少项(以绝对频率计)。

    quanteda a discussion 之后我们启用了它(但请用户注意)。

    library("quanteda")
    #> Package version: 1.4.3
    corp <- corpus(c("a b b c c", 
                     "a a b", 
                     "b b c",
                     "c c c d"),
                   docvars = data.frame(grp = c(1, 1, 2, 2)))
    dfmat <- dfm(corp) %>%
        dfm_weight(scheme = "prop")
    dfmat
    #> Document-feature matrix of: 4 documents, 4 features (43.8% sparse).
    #> 4 x 4 sparse Matrix of class "dfm"
    #>        features
    #> docs            a         b         c    d
    #>   text1 0.2000000 0.4000000 0.4000000 0   
    #>   text2 0.6666667 0.3333333 0         0   
    #>   text3 0         0.6666667 0.3333333 0   
    #>   text4 0         0         0.7500000 0.25
    

    现在我们可以比较有组和没有组的textstat_frequency()。 (两者都没有太大意义。)

    # sum across the corpus
    textstat_frequency(dfmat, groups = NULL)
    #>   feature frequency rank docfreq group
    #> 1       c 1.4833333    1       3   all
    #> 2       b 1.4000000    2       3   all
    #> 3       a 0.8666667    3       2   all
    #> 4       d 0.2500000    4       1   all
    
    # sum across groups
    textstat_frequency(dfmat, groups = "grp")
    #>   feature frequency rank docfreq group
    #> 1       a 0.8666667    1       2     1
    #> 2       b 0.7333333    2       2     1
    #> 3       c 0.4000000    3       1     1
    #> 4       c 1.0833333    1       2     2
    #> 5       b 0.6666667    2       1     2
    #> 6       d 0.2500000    3       1     2
    

    如果您想要的是分组后的相对词频,那么您可以先对 dfm 进行分组,然后对其加权,如下所示:

    dfmat2 <- dfm(corp) %>%
        dfm_group(groups = "grp") %>%
        dfm_weight(scheme = "prop")
    
    textstat_frequency(dfmat2, groups = "grp")
    #>   feature frequency rank docfreq group
    #> 1       a 0.3750000    1       1     1
    #> 2       b 0.3750000    1       1     1
    #> 3       c 0.2500000    3       1     1
    #> 4       c 0.5714286    1       1     2
    #> 5       b 0.2857143    2       1     2
    #> 6       d 0.1428571    3       1     2
    

    现在,组内的词频总和为 1.0,使它们的解释更加自然,因为它们是根据分组计数而不是分组比例计算的。

    【讨论】:

    • 啊,好吧,这很有道理。因此,如果有人问他们应该如何阅读 6 或 0.4,那么与所有组相比,该术语在组中出现的次数是多少,还是我读错了?
    • 我还发现,如果我在分组之前对它们进行加权(你说的方式不准确,我同意),我会得到更具描述性的顶级术语。如果我分组然后权重,我会得到“现在”、“喜欢”、“好”等术语,但反过来我会得到可以讲述更好故事的术语(“领导力”、“clear_communication”、等)我也不知道该怎么做。有什么指导吗?谢谢!
    • group -> weight -> frequency 显示组内出现频率最高的术语。 weight -> group -> frequency 显示组内相对权重的总和,因此得分最高的单词将是每个文档中出现频率最高的单词,即高比例单词。确切的解释将完全取决于您的上下文和文档的性质(和长度)。
    猜你喜欢
    • 1970-01-01
    • 2020-11-08
    • 2017-02-26
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2014-05-20
    • 2012-12-19
    • 2013-01-07
    相关资源
    最近更新 更多