This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(readr)
library(corrplot)
## corrplot 0.95 loaded
# Membaca dataset Housing.csv
file_path <- "Housing.csv"
data <- read_csv(file_path)
## Rows: 545 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (7): mainroad, guestroom, basement, hotwaterheating, airconditioning, pr...
## dbl (6): price, area, bedrooms, bathrooms, stories, parking
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Menampilkan struktur dataset
str(data)
## spc_tbl_ [545 × 13] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ price : num [1:545] 13300000 12250000 12250000 12215000 11410000 ...
## $ area : num [1:545] 7420 8960 9960 7500 7420 7500 8580 16200 8100 5750 ...
## $ bedrooms : num [1:545] 4 4 3 4 4 3 4 5 4 3 ...
## $ bathrooms : num [1:545] 2 4 2 2 1 3 3 3 1 2 ...
## $ stories : num [1:545] 3 4 2 2 2 1 4 2 2 4 ...
## $ mainroad : chr [1:545] "yes" "yes" "yes" "yes" ...
## $ guestroom : chr [1:545] "no" "no" "no" "no" ...
## $ basement : chr [1:545] "no" "no" "yes" "yes" ...
## $ hotwaterheating : chr [1:545] "no" "no" "no" "no" ...
## $ airconditioning : chr [1:545] "yes" "yes" "no" "yes" ...
## $ parking : num [1:545] 2 3 2 3 2 2 2 0 2 1 ...
## $ prefarea : chr [1:545] "yes" "no" "yes" "yes" ...
## $ furnishingstatus: chr [1:545] "furnished" "furnished" "semi-furnished" "furnished" ...
## - attr(*, "spec")=
## .. cols(
## .. price = col_double(),
## .. area = col_double(),
## .. bedrooms = col_double(),
## .. bathrooms = col_double(),
## .. stories = col_double(),
## .. mainroad = col_character(),
## .. guestroom = col_character(),
## .. basement = col_character(),
## .. hotwaterheating = col_character(),
## .. airconditioning = col_character(),
## .. parking = col_double(),
## .. prefarea = col_character(),
## .. furnishingstatus = col_character()
## .. )
## - attr(*, "problems")=<externalptr>
# Mengambil hanya kolom numerik
num_data <- data[sapply(data, is.numeric)]
head(num_data)
## # A tibble: 6 × 6
## price area bedrooms bathrooms stories parking
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 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
# Menghitung Variance-Covariance Matrix
cov_matrix <- cov(num_data)
# Menghitung Eigenvalue dan Eigenvector
eigen_values_vectors <- eigen(cov_matrix)
# Menampilkan hasil
cat("Eigenvalues:\n")
## Eigenvalues:
print(eigen_values_vectors$values)
## [1] 3.498546e+12 3.356500e+06 7.345801e-01 5.958731e-01 3.626262e-01
## [6] 1.677009e-01
cat("Eigenvectors:\n")
## Eigenvectors:
print(eigen_values_vectors$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
# Menampilkan Variance-Covariance Matrix
print("Variance-Covariance Matrix:")
## [1] "Variance-Covariance Matrix:"
print(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
# Menghitung dan menampilkan Correlation Matrix
cor_matrix <- cor(num_data)
print("Correlation Matrix:")
## [1] "Correlation Matrix:"
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
# Membuat visualisasi korelasi menggunakan heatmap
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.