如何在 R 中绘制 SVD/PCA 的距离双标图和相关双标图结果?

如何解决如何在 R 中绘制 SVD/PCA 的距离双标图和相关双标图结果?

我搜索了很长时间,以寻求对距离与相关性双标图的直接解释,以及如何转换 PCA 的标准输出以实现两个双标图的解释。我看到的所有堆栈溢出解释 1 2 3 4 都被数学术语淹没了。如何使用 R 的 prcomp 的输出创建距离双标图和相关双标图?

解决方法

我找到的最好的解释是来自蒙特利尔大学生物科学系 Pierre Legendre 的一些讲座幻灯片 (http://biol09.biol.umontreal.ca/PLcourses/Ordination_section_1.1_PCA_Eng.pdf)。然而,虽然这些幻灯片确实展示了手动绘制距离和相关双标图的方法,但它们没有展示如何根据 prcomp 的结果绘制距离和相关双标图。

所以我研究了一个示例,该示例展示了如何使用 prcomp 的输出使它们等效于上面 pdf 中的示例。我将把它留在这里给像我这样的未来人,他们想知道如何绘制距离与相关双标图以及何时要使用每个图(根据 Pierre Legendre)

set.seed(1)

#Run standard PCA
pca_res <- prcomp(mtcars[,1:7],center = TRUE,scale = TRUE,retx = TRUE)

#To print a distance biplot,simply plot pca_red$x as points and $rotation
#as vectors
library(ggplot2)

arrow_len <- 3 #arbitrary scaling of arrows so they're same mag as PC scores
ggplot(data = as.data.frame(pca_res$x),aes(x = PC1,y = PC2)) +
  geom_point() +
  geom_segment(data = as.data.frame(pca_res$rotation),aes(x = 0,y = 0,yend = arrow_len*PC1,xend = arrow_len*PC2),arrow = arrow(length = unit(0.02,"npc"))) +
  geom_text(data = as.data.frame(pca_res$rotation),mapping = aes(y = arrow_len*PC1,x = arrow_len*PC2,label = row.names(pca_res$rotation)))

#This is equivalent to the following steps:
Y_centered <- scale(mtcars[,scale = TRUE)
Y_eig <- eigen(cov(Y_centered)) 
#Note that Y_eig$vectors == pca_res$rotation ("rotations" or "loadings")
# and Y_eig$values (eigenvalues) == pca_res$sdev**2

#For a distance biplot
U_frame <- Y_eig$vectors
#F is your PC scores,achieved by multiplying your original data by the rotations
F_frame <- Y_centered %*% U_frame

#flipping constants if needed bc PC axis direction is arbitrary
x_flip = -1
y_flip = -1
ggplot(data = as.data.frame(F_frame),aes(x = x_flip*V1,y = y_flip*V2)) +
  geom_point() +
  geom_segment(data = as.data.frame(U_frame),yend = y_flip*arrow_len*V1,xend = x_flip*arrow_len*V2),"npc"))) +
  geom_text(data = as.data.frame(U_frame),mapping = aes(y = y_flip*arrow_len*V1,x = x_flip*arrow_len*V2,label = colnames(Y_centered)))

#To print a correlation biplot,matrix multiply your rotations/loadings
# by the identity matrix times your PCA standard deviations 
# (equivalent to the sqrt of your eigen values)
U_frame_scaling2 <- U_frame %*% diag(Y_eig$values^(0.5))

#And divide your PC scores by your PCA standard deviations
# (equivalent to 1/sqrt(eigen values)
F_frame_scaling2 <- F_frame %*% diag(Y_eig$values^(-0.5))

#Plot
arrow_len <- 1.5 #arbitrary scaling of arrows so they're same mag as PC scores

ggplot(data = as.data.frame(pca_res$x %*% diag(1/pca_res$sdev)),aes(x = V1,y = V2)) +
  geom_point() +
  geom_segment(data = as.data.frame(pca_res$rotation %*% diag(pca_res$sdev)),yend = arrow_len*V1,xend = arrow_len*V2),"npc"))) +
  geom_text(data = as.data.frame(pca_res$rotation %*% diag(pca_res$sdev)),mapping = aes(y = arrow_len*V1,x = arrow_len*V2,label = row.names(pca_res$rotation)))

ggplot(data = as.data.frame(F_frame_scaling2),y = y_flip*V2)) +
  geom_point() +
  geom_segment(data = as.data.frame(U_frame_scaling2),"npc"))) +
  geom_text(data = as.data.frame(U_frame_scaling2),label = colnames(Y_centered)))

至于两者之间的差异(以防上面的 pdf 在某些时候变得不可用):

缩放类型 1:距离双标,当兴趣在 物体相对于彼此的位置。 ——

  • 绘制矩阵 F 来表示对象和 U 来表示变量。

Scaling type 2:correlation biplot,当角度 变量之间的关系是最重要的。 ——

  • 绘制矩阵 G 来表示对象和 Usc2 来表示 变量,其中 G = FΛ–1/2 和 Usc2 = UΛ1/2。

在缩放 1(距离双标图)中,

  • 站点沿每个轴(或主要 分量),等于轴特征值(F的列);
  • 特征向量(U 的列)被规范为长度 = 1;
  • p维排序空间(U的行)中每个物种向量的长度(范数)为1。

在缩放 2(相关双标图)中,

  • 站点沿每个轴(G 的列)具有单位方差;
  • 特征向量(Usc2 的列)被归一化为 长度 = sqrt(特征值);
  • p 维中每个物种向量的范数 排序空间(Usc2 的行)是它的标准差。

在缩放 1(距离双标图)中,

  1. 物体之间的距离近似于它们的欧几里得距离 完整的多维空间。
  2. 在描述符上以直角投影对象近似于 对象沿该描述符的位置。
  3. 由于描述符在全维中的长度相等 空间,描述符在缩减空间中的投影长度 表明它对该空间的形成有多大贡献。
  4. 因此缩放 1 双标图显示哪些变量贡献最大 到几个维度的排序(另见部分:平衡 变量的贡献)。
  5. 描述符轴彼此正交 (90°) 多维空间。这些直角,投影在缩小 空间,不反映变量的相关性。

在缩放 2(相关双标图)中,

  1. 物体之间的距离近似于它们的马氏距离 在完整的多维空间中。
  2. 在描述符上以直角投影对象近似于 对象沿该描述符的位置。
  3. 由于描述符在全维空间中的长度为 sj,因此 描述子 j 在缩减空间中的投影长度是 其标准差 sj 的近似值 .注:sj 为 1 时 变量已标准化。
  4. 双图中描述符之间的角度反映了它们的 相关性。
  5. 当对象之间的距离关系对于 解释,这种类型的双标图是不够的;距离双标 应该使用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res