dfm_weight(scheme='prop') 与组的解释 (quanteda)

Interpretation of dfm_weight(scheme='prop') with groups (quanteda)

我正在使用 dfm_weight 查看不同的加权选项。如果我 select scheme = 'prop' 并且我按 location 对 textstat_frequency 进行分组,每个组中的单词的正确解释是什么?

假设在纽约,术语 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")

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

quanteda < 1.4 不允许这样做,但在 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,这使得它们的解释更加自然,因为它们是根据分组计数而不是分组比例计算的。