Chuỗi bài: R
r
73 dòng
· Cập nhật 2026-02-03
Fractional_Logit_PCA.r
R/Fractional_Logit_PCA.r
## --------------------------------------------------------
## 1. Load packages
## --------------------------------------------------------
library(readxl)
library(ggplot2)
## --------------------------------------------------------
## 2. Read Excel data
## --------------------------------------------------------
df <- read_excel("Fractional Logit.xlsx")
## Keep only numeric columns
df_num <- df[sapply(df, is.numeric)]
## --------------------------------------------------------
## 3. Run PCA (standardized)
## --------------------------------------------------------
pca <- prcomp(df_num, center = TRUE, scale. = TRUE)
## --------------------------------------------------------
## 4. Eigenvalues (Standard deviations^2)
## --------------------------------------------------------
eigenvalues <- pca$sdev^2
eigenvalues
## Explained variance ratio
explained_var <- eigenvalues / sum(eigenvalues)
cumulative_var <- cumsum(explained_var)
explained_df <- data.frame(
PC = paste0("PC", 1:length(eigenvalues)),
Eigenvalue = eigenvalues,
Explained_Variance = explained_var,
Cumulative = cumulative_var
)
print(explained_df)
## --------------------------------------------------------
## 5. Scree Plot
## --------------------------------------------------------
ggplot(explained_df, aes(x = seq_along(Eigenvalue), y = Eigenvalue)) +
geom_point(color = "orange", size = 3) +
geom_line(color = "orange", size = 1) +
xlab("Principal Component") +
ylab("Eigenvalue") +
ggtitle("Scree Plot") +
theme_minimal()
## --------------------------------------------------------
## 6. PCA Loadings (variable contributions)
## --------------------------------------------------------
loadings <- pca$rotation
print(loadings)
## If you want top loadings per PC:
top_loadings <- apply(loadings, 2, function(x) x[order(abs(x), decreasing = TRUE)][1:10])
print(top_loadings)
## --------------------------------------------------------
## 7. PCA scores (each row's PC1, PC2, PC3 ...)
## --------------------------------------------------------
scores <- pca$x
head(scores)
## Save scores to CSV
write.csv(scores, "PCA_scores.csv")
## Save loadings to CSV
write.csv(loadings, "PCA_loadings.csv")
## Save explained variance table
write.csv(explained_df, "PCA_explained_variance.csv")
Bài viết liên quan
R
r
Cập nhật 2026-02-03
Fractional_Logit.r
Fractional_Logit.r — r source code from the R learning materials (R/Fractional_Logit.r).
Đọc bài viết →
R
r
Cập nhật 2026-02-03
01-probability-distributions.R
01-probability-distributions.R — r source code from the R learning materials (R/advanced/01-probability-distributions.R).
Đọc bài viết →
R
r
Cập nhật 2026-02-03
02-hypothesis-testing.R
02-hypothesis-testing.R — r source code from the R learning materials (R/advanced/02-hypothesis-testing.R).
Đọc bài viết →
R
r
Cập nhật 2026-02-03
03-linear-regression.R
03-linear-regression.R — r source code from the R learning materials (R/advanced/03-linear-regression.R).
Đọc bài viết →
R
r
Cập nhật 2026-02-03
04-advanced-anova.R
04-advanced-anova.R — r source code from the R learning materials (R/advanced/04-advanced-anova.R).
Đọc bài viết →
R
r
Cập nhật 2026-02-03
05-model-selection.R
05-model-selection.R — r source code from the R learning materials (R/advanced/05-model-selection.R).
Đọc bài viết →