r - R 中的手动主成分分析

标签 r math pca

问题是关于主成分分析的,部分是手工完成的。

免责声明:我的背景不是数学,而且我是第一次使用 R。

给出了 R^3 中的以下五个数据点。其中 xi1-3 是变量,x1 - x5 是观测值。

    | x1 x2 x3 x4 x5 
----------------------
xi1 | -2 -2  0  2  2 
xi2 | -2  2  0 -2  2 
xi3 | -4  0  0  0  4

给出执行主成分分析后的三个主成分向量,如下所示:

Phi1 = (0.41, 0.41, 0.82)T
Phi2 = (-0.71, 0.71, 0.00)T
Phi3 = (0.58, 0.58, -0.58)T

问题如下

1) Calculate the principal component scores zi1, zi2 and zi3 for each of the 5 data points.
2) Calculate the proportion of the variance explained by each principal component.

到目前为止,我已经用以下代码回答了问题 1,其中 Z 代表分数:

A = matrix(
c(-2, -2, 0, 2, 2, -2, 2, 0, -2, 2, -4, 0, 0, 0, 4),
nrow = 3,
ncol = 5,
byrow = TRUE
)

Phi = matrix (
c(0.41, -0.71, 0.58,0.41, 0.71, 0.58, 0.82, 0.00, -0.58),
nrow = 3,
ncol = 3,
byrow = FALSE
)

Z = Phi%*%A

现在我被问题 2 困住了,我得到了公式: enter image description here

但我不确定如何使用 R 命令重新创建公式,有人可以帮助我吗?

最佳答案

#Here is the numerator:
(Phi%*%A)^2%>%rowSums()
[1] 48.4128 16.1312  0.0000

#Here is the denominator:
sum(A^2)
[1] 64

#So the answer is:
(Phi%*%A)^2%>%rowSums()/sum(A^2)
[1] 0.75645 0.25205 0.00000

我们可以使用prcomp+summary进行验证:

summary(prcomp(t(A)))

Importance of components:
                         PC1  PC2 PC3
Standard deviation     3.464 2.00   0
Proportion of Variance 0.750 0.25   0
Cumulative Proportion  0.750 1.00   1

这大致相同,因为您的 $\Phi$ 四舍五入到小数点后两位。

关于r - R 中的手动主成分分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75139647/

相关文章:

r - ggplot2警告:ymin!= 0时,堆栈定义不正确

r - 为ggplot2中的每个方面设置不同的轴限制,不使用尺度= "free"

r - 使用 sub 提取路径的一部分

matlab - opencv和matlab的特征值不一样,为什么?

python - 非线性数据上的 k-NN + 降维

R + 将向量列表合并为单个向量

c++ - 为什么我不能在 C++ Builder 中编译 IsNan

algorithm - 计算函数求和的高效算法

algorithm - 二维形状轮廓识别

matlab - 在 MATLAB 中使用 princomp 进行 PCA(用于人脸识别)