Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the centroid of PCA

Tags:

r

I have a Principal component called pca. I want to find the centroid using all the components (center of galactic space) and find the distance of each sample Sample from this centre. How can I do this in R?

pca<-structure(list(Sample = c("1", "2", "4", "5", "6"), PCA.1 = c(0.00338, 
-0.020373, -0.019842, -0.019161, -0.019594), PCA.2 = c(0.00047, 
-0.010116, -0.011532, -0.011582, -0.013245), PCA.3 = c(-0.008787, 
0.001412, 0.003751, 0.00371, 0.004242), PCA.4 = c(0.011242, 0.000882, 
-0.003662, -0.002206, -0.002449), PCA.5 = c(0.055873, -0.022664, 
-0.014058, -0.024757, -0.020033), PCA.6 = c(-0.001511, 0.006226, 
-0.005417, 0.000522, -0.003114), PCA.7 = c(-0.056734, -0.007418, 
-0.01043, -0.006961, -0.006006), PCA.8 = c(0.005189, 0.008031, 
-0.002979, 0.000743, 0.006276), PCA.9 = c(0.008169, -0.000265, 
0.010893, 0.003233, 0.007316)), .Names = c("Sample", "PCA.1", 
"PCA.2", "PCA.3", "PCA.4", "PCA.5", "PCA.6", "PCA.7", "PCA.8", 
"PCA.9"), row.names = c(NA, 5L), class = "data.frame")
like image 714
MAPK Avatar asked Dec 28 '25 20:12

MAPK


1 Answers

Assuming you are looking for Euclidean distance, you can find the mean for each variable and you have a centroid. Using simple maths, distance between any point and the centroid is square root of sums of squared differences of n-dimensions (I hope I got that right, see the formula in the link I provide above).

centroid <- sapply(pca[, -1], mean)
pt <- pca[, -1]

sqrt(apply((pt - centroid)^2, MARGIN = 1, sum))

         1          2          3          4          5 
0.08777085 0.03572868 0.04321890 0.04162779 0.02065304 
like image 168
Roman Luštrik Avatar answered Dec 31 '25 13:12

Roman Luštrik