# Membuat dataset berdasarkan tabel yang diberikan
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),
  Income = c(202, 560, 824, 3, 0)  # Periksa nilai nol di sini
)

# Menampilkan data
knitr::kable(data)
Age Debt YearsEmployed Income
30.83 0.000 1.25 202
58.67 4.460 3.04 560
24.50 0.500 1.50 824
27.83 1.540 3.75 3
20.17 5.625 1.71 0
# Menghitung matriks kovarians
cov_matrix <- cov(data)

# Menghitung eigenvalues dan eigenvectors
eig <- eigen(cov_matrix)

# Menampilkan hasil
cat("Eigenvalues:\n")
## Eigenvalues:
print(eig$values)
## [1] 1.321310e+05 2.031939e+02 4.936385e+00 7.643939e-01
cat("\n*Eigenvectors:*\n")
## 
## *Eigenvectors:*
print(eig$vectors)
##               [,1]        [,2]         [,3]         [,4]
## [1,] -0.0149162116  0.99691360  0.063910703 -0.043083646
## [2,]  0.0017756745  0.06497029 -0.997623986  0.022849306
## [3,]  0.0007050725  0.04153604  0.025582743  0.998809183
## [4,] -0.9998869218 -0.01472719 -0.002707029  0.001387607
# Menampilkan Variance-Covariance Matrix
cat("Variance-Covariance Matrix:\n")
## Variance-Covariance Matrix:
print(cov_matrix)
##                       Age        Debt YearsEmployed      Income
## Age            231.361400    9.345663      6.999375   1967.6875
## Debt             9.345663    6.187675      0.605225   -234.7763
## YearsEmployed    6.999375    0.605225      1.182050    -93.2750
## Income        1967.687500 -234.776250    -93.275000 132101.2000
# Menghitung Correlation Matrix dengan validasi nilai nol
if (all(data$Income == 0)) {
  cat("Peringatan: Semua nilai dalam kolom Income adalah nol, matriks korelasi tidak dapat dihitung dengan benar.\n")
} else {
  cor_matrix <- cor(data)
  cat("Correlation Matrix:\n")
  print(cor_matrix)
}
## Correlation Matrix:
##                     Age       Debt YearsEmployed     Income
## Age           1.0000000  0.2470022     0.4232489  0.3559240
## Debt          0.2470022  1.0000000     0.2237872 -0.2596791
## YearsEmployed 0.4232489  0.2237872     1.0000000 -0.2360445
## Income        0.3559240 -0.2596791    -0.2360445  1.0000000