R:使用prcomp(,center = T,scale = T)与缩放数据然后使用prcomp()之间的差异

时间:2019-01-30 07:47:12

标签: r pca

我正在对由...产生的基因矩阵进行PCA

data.matrix <- matrix(nrow=1000,ncol=10)
colnames(data.matrix) <- c(paste("wt",1:5,seq=""),paste("ko",1:5,seq=""))
rownames(data.matrix) <- c(paste("gene",1:1000,seq=""))
for(i in 1:1000){
  wt.value <- rpois(5,lambda = sample(x=10:1000,size = 1))
  ko.value <- rpois(5,lambda = sample(x=10:1000,size = 1))
  data.matrix[i,] <- c(wt.value,ko.value)
}
head(data.matrix)

但是,当我使用不同的方法进行PCA时,结果似乎有所不同。 首先,我使用prcomp()函数并将参数center和scale设置为TRUE

pca <- prcomp(t(data.matrix),scale = TRUE,center = TRUE) 

它产生

的结果
> head(pca$x)
            PC1        PC2        PC3        PC4         PC5        PC6        PC7        PC8
wt 1  -28.53655  0.8854383  3.1131238 -5.9954480  2.25035104 -5.7410346 -3.7012447 -0.3814312
wt 2  -28.52902 -5.9444648 -1.4076822  5.0848454  1.95499941  1.2085364 -2.9876925 -2.6067478
wt 3  -28.20923 -4.9139767  3.0002804  0.9265671 -0.20113099  0.8996807  3.8002186  5.5054392
wt 4  -28.34473  3.9979801 -8.6897406  1.0091133  0.09485369 -1.0152221  1.9143473 -1.4684064
wt 5  -28.11383  6.0484765  3.9915348 -0.9655781 -4.15302287  4.6649855  0.9518471 -0.9617939
ko 1   28.79886  1.7334621 -0.2196764  3.3399867 -3.15128297  0.8190534 -6.6076358  3.0583754
             PC9          PC10
wt 1  -0.3407986  2.007856e-14
wt 2   3.8783848 -1.415274e-14
wt 3  -3.2851629  1.525950e-14
wt 4  -3.0906947 -1.086024e-14
wt 5   2.8361550  9.615572e-15
ko 1  -2.7765555  3.098303e-14

然后我尝试在使用prcomp之前缩放数据,如下所示。

data.matrix1 <- base::scale(data.matrix,scale = T,center = T)
pca1 <- prcomp(t(data.matrix1),scale = F,center = F)

结果是

> head(pca1$x)
            PC1       PC2         PC3          PC4         PC5         PC6        PC7
wt 1  -22.55748  22.02238  0.65748920 -1.840202787  0.62029220 -0.02719390  0.5086453
wt 2  -22.59268  21.99110 -0.06182498  0.501444120 -1.40202773 -1.40378996  0.1613028
wt 3  -22.57925  22.01502 -0.39997569 -0.059653681 -0.08420866 -0.06076671 -1.0856140
wt 4  -22.62006  21.95731 -1.17116782  1.041911324  1.45442072  0.09886287  0.7055064
wt 5  -22.60121  21.98247  0.96933394  0.352323617 -0.59038988  1.39983077 -0.2900812
ko 1  -22.68592 -21.90240  0.61933659  0.002144164 -0.14879010 -0.34936597  1.5217331
             PC8        PC9        PC10
wt 1   0.5642123 -0.4199931 -0.46607294
wt 2   0.3137377 -0.7366336  0.32207940
wt 3   0.5216304  1.7000837 -0.03079762
wt 4  -0.2032306 -0.2901303  0.06174784
wt 5  -1.1912112 -0.2566693  0.12432282
ko 1  -0.4532521  1.1513345  0.50913300

两种方法有什么区别?

1 个答案:

答案 0 :(得分:0)

在缩放之前必须先转置:

t_data.matrix1 <- scale(t(data.matrix),scale = T,center = T)
pca1 <- prcomp(t_data.matrix1,scale = F,center = F)

all.equal(pca, pca1)
#[1] TRUE