Pendahuluan

Dokumen ini berisi perhitungan Variance-Covariance Matrix, Correlation Matrix, serta perhitungan Eigenvalues dan Eigenvectors dari Housing Prices Dataset yang bersumber dari Kaggle dengan link sebagai berikut: Housing Prices Dataset.


Load Data

# Install packages 
if (!require("corrplot")) install.packages("corrplot", dependencies=TRUE)

# Load library
library(corrplot)

# Load data
data_path <- "Housing.csv" 
data <- read.csv(data_path, header = TRUE)
head(data)
##      price area bedrooms bathrooms stories mainroad guestroom basement
## 1 13300000 7420        4         2       3      yes        no       no
## 2 12250000 8960        4         4       4      yes        no       no
## 3 12250000 9960        3         2       2      yes        no      yes
## 4 12215000 7500        4         2       2      yes        no      yes
## 5 11410000 7420        4         1       2      yes       yes      yes
## 6 10850000 7500        3         3       1      yes        no      yes
##   hotwaterheating airconditioning parking prefarea furnishingstatus
## 1              no             yes       2      yes        furnished
## 2              no             yes       3       no        furnished
## 3              no              no       2      yes   semi-furnished
## 4              no             yes       3      yes        furnished
## 5              no             yes       2       no        furnished
## 6              no             yes       2      yes   semi-furnished
# Gunakan hanya kolom numerik
numeric_data <- data[sapply(data, is.numeric)]

if (ncol(numeric_data) == 0) {
  stop("Tidak ada kolom numerik dalam dataset!")
}

Variance-Covariance Matrix

# Hitung Variance-Covariance Matrix
var_cov_matrix <- tryCatch({
  cov(numeric_data)
}, error = function(e) {
  stop("Gagal menghitung variance-covariance matrix: ", e$message)
})

# Tampilkan hasilnya
print(var_cov_matrix)
##                  price         area     bedrooms    bathrooms      stories
## price     3.498544e+12 2.175676e+09 5.059464e+05 4.864093e+05 6.826446e+05
## area      2.175676e+09 4.709512e+06 2.432321e+02 2.113466e+02 1.581294e+02
## bedrooms  5.059464e+05 2.432321e+02 5.447383e-01 1.386738e-01 2.615893e-01
## bathrooms 4.864093e+05 2.113466e+02 1.386738e-01 2.524757e-01 1.421715e-01
## stories   6.826446e+05 1.581294e+02 2.615893e-01 1.421715e-01 7.525432e-01
## parking   6.194673e+05 6.599897e+02 8.856247e-02 7.684161e-02 3.404277e-02
##                parking
## price     6.194673e+05
## area      6.599897e+02
## bedrooms  8.856247e-02
## bathrooms 7.684161e-02
## stories   3.404277e-02
## parking   7.423300e-01

Correlation Matrix

# Hitung Correlation Matrix
cor_matrix <- tryCatch({
  cor(numeric_data)
}, error = function(e) {
  stop("Gagal menghitung correlation matrix: ", e$message)
})

# Tampilkan hasilnya
print(cor_matrix)
##               price       area  bedrooms bathrooms    stories    parking
## price     1.0000000 0.53599735 0.3664940 0.5175453 0.42071237 0.38439365
## area      0.5359973 1.00000000 0.1518585 0.1938195 0.08399605 0.35298048
## bedrooms  0.3664940 0.15185849 1.0000000 0.3739302 0.40856424 0.13926990
## bathrooms 0.5175453 0.19381953 0.3739302 1.0000000 0.32616471 0.17749582
## stories   0.4207124 0.08399605 0.4085642 0.3261647 1.00000000 0.04554709
## parking   0.3843936 0.35298048 0.1392699 0.1774958 0.04554709 1.00000000
# Visualisasi Correlation Matrix menggunakan corrplot
if (!is.null(cor_matrix)) {
  corrplot(cor_matrix, method = "color", 
           col = colorRampPalette(c("blue", "white", "red"))(200),  
           tl.col = "black", tl.cex = 0.8, 
           addCoef.col = "black", number.cex = 0.7, 
           main = "Correlation Matrix")
}


Eigenvalues dan Eigenvectors

# Hitung Eigenvalues dan Eigenvectors
if (!is.null(var_cov_matrix)) {
  eig <- eigen(var_cov_matrix)
  
  cat("\nEigenvalues:\n")
  print(eig$values)
  
  cat("\nEigenvectors:\n")
  print(eig$vectors)
} else {
  cat("\nGagal menghitung eigen karena var_cov_matrix NULL.\n")
}
## 
## Eigenvalues:
## [1] 3.498546e+12 3.356500e+06 7.345801e-01 5.958731e-01 3.626262e-01
## [6] 1.677009e-01
## 
## Eigenvectors:
##              [,1]          [,2]          [,3]          [,4]          [,5]
## [1,] 9.999998e-01  6.218809e-04  2.350022e-07  2.360030e-07 -1.896406e-08
## [2,] 6.218809e-04 -9.999998e-01 -1.068146e-04  4.693651e-05  1.227917e-05
## [3,] 1.446162e-07  2.127405e-05 -4.831235e-01 -3.524835e-01 -7.742078e-01
## [4,] 1.390319e-07  2.715391e-05 -1.156247e-01 -7.418855e-02 -1.559105e-01
## [5,] 1.951224e-07  7.936672e-05 -7.760615e-01 -2.230034e-01  5.897316e-01
## [6,] 1.770643e-07 -8.185750e-05  3.885243e-01 -9.058261e-01  1.688515e-01
##               [,6]
## [1,]  1.167426e-07
## [2,] -2.065080e-05
## [3,]  2.072422e-01
## [4,] -9.781712e-01
## [5,]  1.465063e-02
## [6,] -4.137110e-03