Load Necessary Libraries
if (!requireNamespace("kableExtra", quietly = TRUE))
install.packages("kableExtra", repos = "https://cran.rstudio.com/")
library(knitr)
library(kableExtra)
Create Data Frame
data <- data.frame(
Age = c(30.83, 58.67, 24.50, 27.83, 20.17),
Debt = c(0.000, 4.460, 0.500, 1.540, 5.625),
YearsEmployed = c(1.25, 3.04, 1.50, 3.75, 1.71),
CreditScore = c(1, 6, 0, 5, 0),
Income = c(0, 560, 824, 3, 0)
)
kable(data) %>% kable_styling()
|
Age
|
Debt
|
YearsEmployed
|
CreditScore
|
Income
|
|
30.83
|
0.000
|
1.25
|
1
|
0
|
|
58.67
|
4.460
|
3.04
|
6
|
560
|
|
24.50
|
0.500
|
1.50
|
0
|
824
|
|
27.83
|
1.540
|
3.75
|
5
|
3
|
|
20.17
|
5.625
|
1.71
|
0
|
0
|
Convert Data to Matrix
numeric_data <- as.matrix(data)
# Hapus kolom dengan varians nol
numeric_data <- numeric_data[, apply(numeric_data, 2, var) > 0]
Compute Eigenvalues and Eigenvectors
cor_matrix <- cor(numeric_data, use = "complete.obs")
eigen_results <- eigen(cor_matrix)
kable(data.frame(Eigenvalues = eigen_results$values)) %>% kable_styling()
|
Eigenvalues
|
|
2.5048904
|
|
1.2162054
|
|
0.8937056
|
|
0.3851986
|
|
0.0000000
|
kable(as.data.frame(eigen_results$vectors)) %>% kable_styling()
|
V1
|
V2
|
V3
|
V4
|
V5
|
|
-0.5173164
|
0.3435734
|
-0.1580531
|
-0.6520227
|
-0.4052488
|
|
-0.2366539
|
-0.3405399
|
-0.8896621
|
0.1713247
|
0.0847152
|
|
-0.5423918
|
-0.2393841
|
0.2932737
|
0.5500118
|
-0.5098862
|
|
-0.6144324
|
-0.0402371
|
0.2391578
|
-0.0569615
|
0.7486072
|
|
-0.0682825
|
0.8408722
|
-0.2007850
|
0.4896495
|
0.0905545
|
Variance-Covariance Matrix
cov_matrix <- cov(numeric_data, use = "complete.obs")
kable(cov_matrix) %>% kable_styling()
|
|
Age
|
Debt
|
YearsEmployed
|
CreditScore
|
Income
|
|
Age
|
231.361400
|
9.345662
|
6.999375
|
33.30
|
2046.9725
|
|
Debt
|
9.345662
|
6.187675
|
0.605225
|
1.34
|
-112.3137
|
|
YearsEmployed
|
6.999375
|
0.605225
|
1.182050
|
2.81
|
-42.7750
|
|
CreditScore
|
33.300000
|
1.340000
|
2.810000
|
8.30
|
11.5500
|
|
Income
|
2046.972500
|
-112.313750
|
-42.775000
|
11.55
|
151957.8000
|
Correlation Matrix
kable(cor_matrix) %>% kable_styling()
|
|
Age
|
Debt
|
YearsEmployed
|
CreditScore
|
Income
|
|
Age
|
1.0000000
|
0.2470022
|
0.4232489
|
0.7599058
|
0.3452272
|
|
Debt
|
0.2470022
|
1.0000000
|
0.2237872
|
0.1869829
|
-0.1158264
|
|
YearsEmployed
|
0.4232489
|
0.2237872
|
1.0000000
|
0.8971175
|
-0.1009278
|
|
CreditScore
|
0.7599058
|
0.1869829
|
0.8971175
|
1.0000000
|
0.0102845
|
|
Income
|
0.3452272
|
-0.1158264
|
-0.1009278
|
0.0102845
|
1.0000000
|