1. Load Data
data <- read.csv ("C:/Semester 4/Analisis multivariat/Dataset/Housing.csv")
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
  1. Data Numeric
data_numeric <- data[,sapply(data, is.numeric)]
head(data_numeric)
##      price area bedrooms bathrooms stories parking
## 1 13300000 7420        4         2       3       2
## 2 12250000 8960        4         4       4       3
## 3 12250000 9960        3         2       2       2
## 4 12215000 7500        4         2       2       3
## 5 11410000 7420        4         1       2       2
## 6 10850000 7500        3         3       1       2
  1. Exploratory Data Analysis (EDA)
summary(data_numeric)
##      price               area          bedrooms       bathrooms    
##  Min.   : 1750000   Min.   : 1650   Min.   :1.000   Min.   :1.000  
##  1st Qu.: 3430000   1st Qu.: 3600   1st Qu.:2.000   1st Qu.:1.000  
##  Median : 4340000   Median : 4600   Median :3.000   Median :1.000  
##  Mean   : 4766729   Mean   : 5151   Mean   :2.965   Mean   :1.286  
##  3rd Qu.: 5740000   3rd Qu.: 6360   3rd Qu.:3.000   3rd Qu.:2.000  
##  Max.   :13300000   Max.   :16200   Max.   :6.000   Max.   :4.000  
##     stories         parking      
##  Min.   :1.000   Min.   :0.0000  
##  1st Qu.:1.000   1st Qu.:0.0000  
##  Median :2.000   Median :0.0000  
##  Mean   :1.806   Mean   :0.6936  
##  3rd Qu.:2.000   3rd Qu.:1.0000  
##  Max.   :4.000   Max.   :3.0000

#visualisasi distribusi harga rumah

hist(data$price, main="Distribusi Harga Rumah", xlab="Harga", col="blue")

## Boxplot harga rumah untuk mendeteksi outlier

boxplot(data$price, main="Boxplot Harga Rumah")

4. Normalisasi

housing_scaled <- scale(data_numeric)
  1. Variance-Covariance Matrix
cov_matrix <- cov(data_numeric)
head(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
  1. Correlation Matrix
cor_matrix <- cor(data_numeric)
head(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
  1. Eigenvalues dan Eigenvectors dari Variance-Covariance Matrix
eigen_results <- eigen(cov_matrix)

##EigenValues

head.matrix(eigen_results$values)
## [1] 3.498546e+12 3.356500e+06 7.345801e-01 5.958731e-01 3.626262e-01
## [6] 1.677009e-01

##EigenVectors

head.matrix(eigen_results$vectors)
##              [,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
  1. PCA
pca_result <- prcomp(data_numeric, scale = TRUE)
summary(pca_result)
## Importance of components:
##                          PC1    PC2    PC3    PC4     PC5     PC6
## Standard deviation     1.599 1.1032 0.8229 0.8104 0.76866 0.54963
## Proportion of Variance 0.426 0.2029 0.1129 0.1094 0.09847 0.05035
## Cumulative Proportion  0.426 0.6289 0.7417 0.8512 0.94965 1.00000

plot

plot(pca_result, type="l")

  1. Regresi Linier Prediksi Harga Rumah
model <- lm(price ~ area + bedrooms + bathrooms + stories + parking, data=data)
summary(model)
## 
## Call:
## lm(formula = price ~ area + bedrooms + bathrooms + stories + 
##     parking, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3396744  -731825   -64056   601486  5651126 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -145734.5   246634.5  -0.591   0.5548    
## area            331.1       26.6  12.448  < 2e-16 ***
## bedrooms     167809.8    82932.7   2.023   0.0435 *  
## bathrooms   1133740.2   118828.3   9.541  < 2e-16 ***
## stories      547939.8    68894.5   7.953 1.07e-14 ***
## parking      377596.3    66804.1   5.652 2.57e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1244000 on 539 degrees of freedom
## Multiple R-squared:  0.5616, Adjusted R-squared:  0.5575 
## F-statistic: 138.1 on 5 and 539 DF,  p-value: < 2.2e-16
  1. Clustering K-Means
set.seed(123)
kmeans_result <- kmeans(data_numeric, centers=3)
data$cluster <- as.factor(kmeans_result$cluster)

plot clustering

plot(data_numeric[,1:2], col=kmeans_result$cluster)

Kesimpulan : Analisis multivariat terhadap data perumahan dilakukan melalui berbagai tahap, dimulai dengan Exploratory Data Analysis (EDA) untuk memahami distribusi harga rumah. Hasilnya menunjukkan bahwa harga rumah memiliki distribusi right-skewed, dengan beberapa outlier pada harga tinggi, seperti yang terlihat pada histogram dan boxplot. Variance-Covariance dan Correlation Matrix menunjukkan bahwa luas tanah, jumlah kamar mandi, dan jumlah lantai memiliki korelasi positif signifikan terhadap harga rumah, dengan luas tanah memiliki korelasi tertinggi (0.54).

Selanjutnya, dilakukan Principal Component Analysis (PCA) untuk mereduksi dimensi data. Hasilnya menunjukkan bahwa dua komponen utama pertama menjelaskan sekitar 62.89% variansi dalam dataset, yang ditunjukkan dalam Scree Plot. Regresi linier digunakan untuk memprediksi harga rumah, di mana hasil regresi menunjukkan bahwa luas tanah memiliki pengaruh paling besar terhadap harga rumah, diikuti oleh jumlah kamar mandi dan jumlah lantai, dengan semua variabel signifikan pada tingkat kepercayaan tinggi.

Akhirnya, dilakukan Clustering K-Means untuk mengelompokkan rumah berdasarkan harga dan luas tanah. Hasilnya menunjukkan bahwa rumah dapat dikelompokkan menjadi tiga kategori utama, di mana rumah dengan harga lebih rendah cenderung memiliki luas yang lebih kecil, sementara rumah dengan harga lebih tinggi memiliki variasi luas yang lebih besar. Dengan demikian, analisis ini mengonfirmasi bahwa luas tanah merupakan faktor utama dalam menentukan harga rumah, serta menunjukkan pola pengelompokan harga yang jelas dalam dataset.