【问题标题】:How to plot distance biplot and correlation biplot results of SVD/PCA in R?如何在 R 中绘制 SVD/PCA 的距离双图和相关双图结果?
【发布时间】:2021-05-02 16:56:35
【问题描述】:

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

【问题讨论】:

  • 糟糕,我知道我看到了自我回答,但不知道这样做的预期方式。我已相应调整,谢谢!

标签: r pca svd prcomp biplot


【解决方案1】:

我发现最好的解释是来自蒙特利尔大学生物科学系的 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[, 1:7], center = TRUE, 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),
               aes(x = 0, y = 0, yend = y_flip*arrow_len*V1, xend = x_flip*arrow_len*V2),
               arrow = arrow(length = unit(0.02, "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)),
               aes(x = 0, y = 0, yend = arrow_len*V1, xend = arrow_len*V2),
               arrow = arrow(length = unit(0.02, "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), aes(x = x_flip*V1, y = y_flip*V2)) +
  geom_point() +
  geom_segment(data = as.data.frame(U_frame_scaling2),
               aes(x = 0, y = 0, yend = y_flip*arrow_len*V1, xend = x_flip*arrow_len*V2),
               arrow = arrow(length = unit(0.02, "npc"))) +
  geom_text(data = as.data.frame(U_frame_scaling2),
            mapping = aes(y = y_flip*arrow_len*V1, x = x_flip*arrow_len*V2,
                          label = colnames(Y_centered)))

至于两者之间的区别(以防上面的pdf在某些时候不可用):

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

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

缩放类型2:相关双标图,当角度 变量之间的关系是最重要的。 ——

  • 绘制矩阵 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. 由于描述符在全维中具有相等的长度 1 空间,描述符在缩减空间中的投影长度 表明它对该空间的形成有多大贡献。
  4. 因此,缩放 1 双图显示哪些变量贡献最大 到几个维度的排序(另见部分:平衡 变量的贡献)。
  5. 描述符轴相互正交 (90°) 多维空间。这些直角,投影在减少 空间,不反映变量的相关性。

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

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

【讨论】:

    猜你喜欢
    • 2018-10-26
    • 2017-01-06
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 2019-03-09
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多