library(haven)
## Warning: пакет 'haven' был собран под R версии 4.5.3
library(FactoMineR)
library(factoextra)
## Warning: пакет 'factoextra' был собран под R версии 4.5.3
## Загрузка требуемого пакета: ggplot2
## Warning: пакет 'ggplot2' был собран под R версии 4.5.3
## Welcome to factoextra!
## Want to learn more? See two factoextra-related books at https://www.datanovia.com/en/product/practical-guide-to-principal-component-methods-in-r/
library(ggplot2)
Загружаем данные
df<-read_sav("religions.sav")
Оставляем только числовые столбцы
numeric_columns <- sapply(df, is.numeric)
df <- df[, numeric_columns]
res.pca <- PCA(df, graph = FALSE)
## Warning in PCA(df, graph = FALSE): Missing values are imputed by the mean of
## the variable: you should use the imputePCA function of the missMDA package
Выполняем анализ главных компонент РСА
print(res.pca)
## **Results for the Principal Component Analysis (PCA)**
## The analysis was performed on 6 individuals, described by 28 variables
## *The results are available in the following objects:
##
## name description
## 1 "$eig" "eigenvalues"
## 2 "$var" "results for the variables"
## 3 "$var$coord" "coord. for the variables"
## 4 "$var$cor" "correlations variables - dimensions"
## 5 "$var$cos2" "cos2 for the variables"
## 6 "$var$contrib" "contributions of the variables"
## 7 "$ind" "results for the individuals"
## 8 "$ind$coord" "coord. for the individuals"
## 9 "$ind$cos2" "cos2 for the individuals"
## 10 "$ind$contrib" "contributions of the individuals"
## 11 "$call" "summary statistics"
## 12 "$call$centre" "mean of the variables"
## 13 "$call$ecart.type" "standard error of the variables"
## 14 "$call$row.w" "weights for the individuals"
## 15 "$call$col.w" "weights for the variables"
Для получения информации о собственных значениях и дисперсии можно использовать следующий код:
eig.val <- get_eigenvalue(res.pca)
eig.val
## eigenvalue variance.percent cumulative.variance.percent
## Dim.1 2.665641e+01 9.520147e+01 95.20147
## Dim.2 6.583989e-01 2.351425e+00 97.55290
## Dim.3 3.853713e-01 1.376326e+00 98.92922
## Dim.4 2.998172e-01 1.070776e+00 100.00000
## Dim.5 8.091662e-29 2.889879e-28 100.00000
fviz_eig(res.pca, addlabels = TRUE, ylim = c(0, 50))
На графике наблюдается резкий спад после второй компоненты. Поэтому оставляем первые две главные компоненты.
var <- get_pca_var(res.pca)
var
## Principal Component Analysis Results for variables
## ===================================================
## Name Description
## 1 "$coord" "Coordinates for the variables"
## 2 "$cor" "Correlations between variables and dimensions"
## 3 "$cos2" "Cos2 for the variables"
## 4 "$contrib" "contributions of the variables"
# Координаты
head(var$coord)
## Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
## d1 0.9959931 0.08667675 0.02201425 0.0005284871 2.615128e-15
## d2 0.9856867 0.15604215 0.05619045 -0.0302516782 -3.766686e-15
## d3 0.9932608 0.10879264 -0.02997603 -0.0264323415 -6.983863e-16
## d4 0.9862426 0.13633891 -0.01250799 -0.0926322853 4.934473e-16
## d5 0.9937955 0.06158202 0.00799803 -0.0922719871 6.349557e-16
## d6 0.9840930 0.10641774 -0.06384742 -0.1271205906 -2.008757e-15
Координаты показывают, насколько сильно каждый дескриптор связан с компонентами.
fviz_pca_var(res.pca, col.var = "black")
На круге видно, что дескрипторы группируются в два противоположных кластера.
# Cos2: качество анализа
head(var$cos2)
## Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
## d1 0.9920022 0.007512859 4.846272e-04 2.792986e-07 6.838897e-30
## d2 0.9715783 0.024349153 3.157367e-03 9.151640e-04 1.418793e-29
## d3 0.9865669 0.011835838 8.985623e-04 6.986687e-04 4.877434e-31
## d4 0.9726745 0.018588299 1.564499e-04 8.580740e-03 2.434903e-31
## d5 0.9876296 0.003792345 6.396848e-05 8.514120e-03 4.031688e-31
## d6 0.9684391 0.011324735 4.076494e-03 1.615964e-02 4.035103e-30
library("corrplot")
## Warning: пакет 'corrplot' был собран под R версии 4.5.3
## corrplot 0.95 loaded
corrplot(var$cos2, is.corr=FALSE)
# Вклады в компоненты
head(var$contrib)
## Dim.1 Dim.2 Dim.3 Dim.4 Dim.5
## d1 3.721439 1.141080 0.12575590 9.315629e-05 8.4517831
## d2 3.644820 3.698237 0.81930512 3.052406e-01 17.5340095
## d3 3.701049 1.797670 0.23316792 2.330315e-01 0.6027729
## d4 3.648933 2.823258 0.04059719 2.861990e+00 0.3009150
## d5 3.705036 0.575995 0.01659918 2.839770e+00 0.4982522
## d6 3.633044 1.720042 1.05780922 5.389832e+00 4.9867424
Вклад показывает, какой процент дисперсии компоненты создан каждым дескриптором.
corrplot(var$contrib, is.corr=FALSE)
fviz_pca_ind(res.pca)
fviz_pca_biplot(res.pca, repel = TRUE,
col.var = "#2E9FDF", # Цвет переменных
col.ind = "#696969" # Цвет наблюдений
)
Биплот позволяет одновременно интерпретировать положение респондентов относительно переменных.