PCA原理及实现-R

PCA分析与解释

PCA是一种无参数的数据降维方法，常用的机器学习算法一种，这篇文章主要从PCA应用于解释两个方面介绍。关于PCA原理，详情这里

`PCA主要是，在数据处理与分析时候，一个数据集有很多变量（维度），譬如患者肿瘤发生，有吸烟，饮食等生活史，和临床生化检查等，这些变量间相互关联，纳入模型，影响效率与模型构建。所以PCA就是从众多变量中找出特征值，能够代表这些变量，来解释最终结果。`

PCA实例

``````#####
library(factoextra)
library(tidyverse)

# Split Data into Training and Testing in R
sample_size = floor(0.8*nrow(iris))
set.seed(777)

# randomly split data in r
picked = sample(seq_len(nrow(iris)),size = sample_size)
df1 =iris[picked,] %>% as.tbl()
df2 =iris[-picked,] %>% as.tbl()

## 1.pca
res.pca <- prcomp(df1[,-5], scale = T) # 标准化

``````

``````## 2.the percentage of variances explained by each principal component.
fviz_eig(res.pca)

``````
image.png

``````## 3.Individuals with a similar profile are grouped together.
fviz_pca_ind(res.pca,
col.ind = "cos2", # Color by the quality of representation
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE     # Avoid text overlapping
)

``````
image.png

``````## 3.Individuals with a similar profile are grouped together.

fviz_pca_var(res.pca, col.var = "contrib",
gradient.cols = c("white", "blue", "red"),
ggtheme = theme_minimal())

fviz_pca_biplot(res.pca, label = "var", habillage=df1\$Species,
ggtheme = theme_minimal())

cor(df1[,-5])
Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length    1.0000000  -0.1183494    0.8625614   0.8117941
Sepal.Width    -0.1183494   1.0000000   -0.4480079  -0.3872041
Petal.Length    0.8625614  -0.4480079    1.0000000   0.9633883
Petal.Width     0.8117941  -0.3872041    0.9633883   1.0000000
``````
image.png

``````## 4.Graph of Coordinate
fviz_pca_ind(res.pca,
col.ind = df1\$Species, # color by groups
palette = c("#00AFBB",  "#FC4E07","black"),
addEllipses = TRUE, # Concentration ellipses
ellipse.type = "confidence",
ellipse.level=0.95,
legend.title = "Groups",
ggtheme = theme_minimal())

## 5. Coordinate of groups
coord.groups <- res.ind\$coord %>%
as_data_frame() %>%
select(Dim.1, Dim.2) %>%
mutate(Species = df1\$Species) %>%
group_by(Species) %>%
summarise(
Dim.1 = mean(Dim.1),
Dim.2 = mean(Dim.2))

coord.groups

# A tibble: 3 x 3
Species     Dim.1  Dim.2
<fct>       <dbl>  <dbl>
1 setosa      2.10   0.278
2 versicolor -0.570 -0.529
3 virginica  -1.82   0.248
``````
image.png

PCA预测

``````### predict
#ind.sup=veteran %>% select(-trt) %>% as.tbl() %>% slice(101:136)
ind.sup.coord <- predict(res.pca, newdata = df2)
ind.sup.coord[, 1:4]

# df1
coord1 = res.ind\$coord %>%
as_data_frame() %>%
select(Dim.1, Dim.2) %>%
mutate(Species = df1\$Species) %>% set_names("PC1","PC2","Species")

# df2
coord2 =ind.sup.coord[, 1:2] %>% as_data_frame() %>%
mutate(Species = df2\$Species)

# plot
coord1 %>% bind_rows(coord2) %>%
ggplot()+
geom_point(aes(PC1,PC2,group=Species,color=Species),shape=0,size=3)+
geom_point(data=coord2,aes(PC1,PC2),fill=0.1)
``````
image.png

``````library(factoextra)
# Eigenvalues
eig.val <- get_eigenvalue(res.pca)
eig.val

# Results for Variables
res.var <- get_pca_var(res.pca)
res.var\$coord          # Coordinates
res.var\$contrib        # Contributions to the PCs
res.var\$cos2           # Quality of representation
# Results for individuals
res.ind <- get_pca_ind(res.pca)
res.ind\$coord          # Coordinates
res.ind\$contrib        # Contributions to the PCs
res.ind\$cos2           # Quality of representation
``````