【问题标题】:Understanding how dfm_groups works with no group added了解 dfm_groups 如何在未添加组的情况下工作
【发布时间】:2019-07-10 18:58:16
【问题描述】:

基于这个问题:Interpretation of dfm_weight(scheme='prop') with groups (quanteda)

如果我有这个功能:

     plot_topterms = function(data,text_field,n,...){

  corp=corpus(data,text_field = text_field) %>% 
    dfm(remove_numbers=T,remove_punct=T,remove=c(stopwords('english')),ngrams=1:2) %>%
    dfm_weight(scheme ='prop') %>% 
    dfm_group(groups=...) %>% 
    dfm_replace(pattern=as.character(lemma$first),replacement = as.character(lemma$X1)) %>% 
    dfm_remove(pattern = c(paste0("^", stopwords("english"), "_"), paste0("_", stopwords("english"), "$")), valuetype = "regex") %>% 
    dfm_remove(toRemove)
  freq_weight <- textstat_frequency(corp, n = n)

  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) +
    #scale_y_continuous(labels = scales::percent)+
    theme(text = element_text(size=20))+
    labs(x = NULL, y = "Relative frequency")
}

而且我没有传入分组变量,所以我执行以下操作:

plot_topterms(df,textField,n=10)

我得到一个组变量等于all 的输出。这应该相当于甚至没有正确的 dfm_group 行?如果是这样的话,如果我对单词fun 的相对频率为 60,这是否意味着 60% 的所有文档都包含该单词?

【问题讨论】:

    标签: r quanteda


    【解决方案1】:

    您对“all”组的解释是正确的。在textstat_frequency() 中不指定groups 的效果是该组将默认为“全部”。在您的函数中,您永远不会在对该函数的调用中传递groups 参数,因此即使您已经通过函数plot_topterms() 中的dfm_group() 调用对dfm 进行了分组,它也将始终为“全部”。

    此图中某个特征的值为 60 意味着该特征的相对词频(在文档内)的总和为 60。如果您查看 the question you reference above,您将看到它是如何用于简单的例子。 a 在 text1 中的相对频率在 text2 中是 0.20 和 0.67,所以 textstat_frequency() 将这两者相加为 0.87。你的 60 相当于这个 0.87。

    与文档频率相同,后者是出现某个特征的文档数(至少一次)。如果您想知道特征的文档频率(这是您的解释),那么您应该从textstat_frequency 返回绘制docfreq,而不是frequency

    不过我会注意到 plot_topterms() 不是一个设计良好的函数。

    • 它依赖于几个非函数局部变量,即toRemovelemma

    • 它不会在dfm_group() 调用中正确传递...。您应该在函数签名中明确指定 groups 参数。

    如果我们正在为包设计一个新函数,我们将创建一个新函数textplot_frequency(),它绘制从textstat_frequency() 的返回值,基本上只在用户构建textstat_frequency 对象后实现ggplot() 调用.这可以更智能地使用每个 textstat_frequency 对象中内置的组变量,以便那些唯一组为“all”的对象将其绘制为单个方面。

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 2011-05-12
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多