r - 基本 R cor() 函数的结果与Recommenderlab 包中的similarity() 函数的结果不同?
问题描述
谁能解释为什么这两个相关矩阵返回不同的结果?
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] )
解决方案
R基dissimilarity() = 1 - pmax(cor(), 0)
函数。此外,重要的是要指定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
要很好地理解它,请检查两个包的详细信息:)。
OP/编辑:
重要的是要指出,偶数1-dissimilarity
和之间有一些值略有不同cor
,cor
大于 1。这是因为dissimilarity()
将下限设置为 0(即不返回负数),并且也在做cor()
可能返回大于 1 的值。https://www.rdocumentation.org/packages/stats/versions/3.6.0/topics/cor 他们只指定
For r <- cor(*, use = "all.obs"), it is now guaranteed that all(abs(r) <= 1).
这应该被评估。
推荐阅读
- java - 如何知道在 android 中使用的权限列表?
- java - 设置 Google AppEngine Jetty 版本
- c++ - 无法获得菜单选择以运行功能。C++
- python - 为什么 ImportError:没有名为“cv2”的模块错误仅适用于 python3?
- node.js - Npm 安装失败。我克隆了其他 React-router 演示(具有权限功能)。但它在 npm install 上失败了
- docker - Docker 登录到 Docker Desktop for Windows 下 docker stack 中的 nexus 存储库
- remote-access - 通过本地设备远程访问服务器
- laravel - 区块链 API/Web 套接字仍然可用吗?
- css - 如何对齐复选框
- react-native - 模块 HMRClient 不是注册的可调用模块(调用设置)