【问题标题】:Different results from base R cor() function than similarity() function in recommenderlab package?基本 R cor() 函数的结果与Recommenderlab 包中的similarity() 函数的结果不同吗?
【发布时间】:2019-06-19 14:12:00
【问题描述】:

谁能解释为什么这两个相关矩阵返回不同的结果?

library(recommenderlab)
data(MovieLense)
cor_mat <- as( similarity(MovieLense, method = "pearson", which = "items"), "matrix" )
cor_mat_base <- suppressWarnings( cor(as(MovieLense, "matrix"), use = "pairwise.complete.obs") )
print( cor_mat[1:5, 1:5] )
print( cor_mat_base[1:5, 1:5] )

【问题讨论】:

  • 您为什么希望它们返回相同的结果? cor_mat 是否也在使用完整的观察?
  • @NelsonGon 我不确定我是否理解这个问题。我会假设这两个函数将仅使用两个值都不为 NA 的配对值(因为我不知道如何运行相关性。)无论我提供给“use =”的哪个非 NA 生成参数,我都会得到相同的结果结果。除非我误会了?

标签: r recommender-systems recommenderlab


【解决方案1】:

dissimilarity() = 1 - pmax(cor(), 0) R 基本函数。此外,指定method 以使它们使用相同的值也很重要:

library("recommenderlab")
data(MovieLense)
cor_mat <- as( dissimilarity(MovieLense, method = "pearson", 
                          which = "items"), "matrix" )
cor_mat_base <- suppressWarnings( cor(as(MovieLense, "matrix"), method = "pearson"
                                      , use = "pairwise.complete.obs") )
print( cor_mat[1:5, 1:5] )
print(1- cor_mat_base[1:5, 1:5] )

> print( cor_mat[1:5, 1:5] )
                  Toy Story (1995) GoldenEye (1995) Four Rooms (1995) Get Shorty (1995) Copycat (1995)
Toy Story (1995)         0.0000000        0.7782159         0.8242057         0.8968647      0.6135248
GoldenEye (1995)         0.7782159        0.0000000         0.7694644         0.7554443      0.7824406
Four Rooms (1995)        0.8242057        0.7694644         0.0000000         1.0000000      0.8153877
Get Shorty (1995)        0.8968647        0.7554443         1.0000000         0.0000000      1.0000000
Copycat (1995)           0.6135248        0.7824406         0.8153877         1.0000000      0.0000000
> print(1- cor_mat_base[1:5, 1:5] )
                  Toy Story (1995) GoldenEye (1995) Four Rooms (1995) Get Shorty (1995) Copycat (1995)
Toy Story (1995)         0.0000000        0.7782159         0.8242057         0.8968647      0.6135248
GoldenEye (1995)         0.7782159        0.0000000         0.7694644         0.7554443      0.7824406
Four Rooms (1995)        0.8242057        0.7694644         0.0000000         1.2019687      0.8153877
Get Shorty (1995)        0.8968647        0.7554443         1.2019687         0.0000000      1.2373503
Copycat (1995)           0.6135248        0.7824406         0.8153877         1.2373503      0.0000000

为了更好地理解它,请检查两个包的详细信息:)。

操作/编辑: 需要指出的是,即使1-dissimilaritycor 之间也有一些值略有不同,cor 大于 1。这是因为 dissimilarity() 将下限设置为 0(即,不返回负数),并且还执行cor() 可以返回大于1 的值。https://www.rdocumentation.org/packages/stats/versions/3.6.0/topics/cor 他们只指定

For r &lt;- cor(*, use = "all.obs"), it is now guaranteed that all(abs(r) &lt;= 1).

这应该被评估。

【讨论】:

  • 我认为在cor 中,pearson 是默认方法。
  • 很好奇,为什么要切换相似性和不相似性?它们真的是一回事吗?
  • @CarlesSansFuentes 非常感谢!切换到 dissimilarity() 函数,然后从 1 中减去它。我仍然不太明白相似性()当时在做什么。帮助详细信息说“相似性是根据测量值使用 s=1/(1+d) 或 s=1-d 从差异计算得出的。对于 Pearson,我们使用 1 - 正相关。”那么对于 Pearson 来说,similarity() 和 1-dissimilarity() 不应该是相同的吗?
  • 嗯,这个解释得很详细,涉及到相似性和不相似性的定义。检查 R 中的?dissimilarity。在详细信息中,它说`根据度量,使用 s=1/(1+d) 或 s=1-d 从差异计算相似性。对于 Pearson,我们使用 1 - 正相关。`
  • @CarlesSansFuentes 我刚刚看到您的编辑,但我认为它不正确。你减去 1 - cor 和 cor 可以是一个负数,这就是为什么你得到一个大于 1 的数字。你应该减去 1 - dissimilarity 然后你会得到相似的答案(虽然不完全相同,因为它看起来 dissimilarity()将下限设置为 0,并且不允许负数。)
猜你喜欢
  • 2014-05-02
  • 2020-03-02
  • 1970-01-01
  • 2015-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
相关资源
最近更新 更多