了解 dfm_groups 如何在未添加组的情况下工作
Understanding how dfm_groups works with no group added
建立这个问题:
如果我有这个功能:
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% 都包含该单词?
您对 "all" 组的解释是正确的。在 textstat_frequency()
中不指定 groups
的效果是该组将默认为 "all"。在你的函数中,你永远不会在调用这个函数时传递 groups
参数,所以它总是 "all",即使你已经通过内部的 dfm_group()
调用对 dfm 进行了分组你的函数 plot_topterms()
.
此图中某个特征的值为 60 意味着该特征的相对词频(在文档内)的总和为 60。如果您查看 ,您将了解这适用于简单示例。 a
在 text1 中的相对频率为 0.20,在 text2 中为 0.67,因此 textstat_frequency()
将这两者相加为 0.87。你的 60 类似于这个 0.87.
这与文档频率不同,文档频率是某个特征出现(至少一次)的文档数量。如果你想知道特征的文档频率(这是你的解释),那么你应该从 textstat_frequency
return 绘制 docfreq
,而不是 frequency
.
不过我要注意 plot_topterms()
不是 well-designed 函数。
它依赖于几个非函数局部变量,即toRemove
和lemma
。
它不会在 dfm_group()
调用中正确传递 ...
。您应该在函数签名中显式指定一个 groups
参数。
如果我们正在为包设计一个新函数,我们将创建一个新函数 textplot_frequency()
,它从 textstat_frequency()
绘制 return,基本上只实现了 ggplot()
在用户构建了 textstat_frequency
对象后调用。这可以更智能地使用每个 textstat_frequency
对象内置的组变量,以便那些唯一组是 "all" 的对象将把它绘制为一个面。
建立这个问题:
如果我有这个功能:
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% 都包含该单词?
您对 "all" 组的解释是正确的。在 textstat_frequency()
中不指定 groups
的效果是该组将默认为 "all"。在你的函数中,你永远不会在调用这个函数时传递 groups
参数,所以它总是 "all",即使你已经通过内部的 dfm_group()
调用对 dfm 进行了分组你的函数 plot_topterms()
.
此图中某个特征的值为 60 意味着该特征的相对词频(在文档内)的总和为 60。如果您查看 a
在 text1 中的相对频率为 0.20,在 text2 中为 0.67,因此 textstat_frequency()
将这两者相加为 0.87。你的 60 类似于这个 0.87.
这与文档频率不同,文档频率是某个特征出现(至少一次)的文档数量。如果你想知道特征的文档频率(这是你的解释),那么你应该从 textstat_frequency
return 绘制 docfreq
,而不是 frequency
.
不过我要注意 plot_topterms()
不是 well-designed 函数。
它依赖于几个非函数局部变量,即
toRemove
和lemma
。它不会在
dfm_group()
调用中正确传递...
。您应该在函数签名中显式指定一个groups
参数。
如果我们正在为包设计一个新函数,我们将创建一个新函数 textplot_frequency()
,它从 textstat_frequency()
绘制 return,基本上只实现了 ggplot()
在用户构建了 textstat_frequency
对象后调用。这可以更智能地使用每个 textstat_frequency
对象内置的组变量,以便那些唯一组是 "all" 的对象将把它绘制为一个面。