House Prices atau harga rumah sangatlah bervariasi mulai yang murah hingga mahal dengan berbagai macam faktor.
Menggunakan linear regression analysis, akan dibuat model machine learning untuk memprediksi harga rumah (Prices)
house <- read_csv("house-price.csv")
## Parsed with column specification:
## cols(
## Area = col_double(),
## Garage = col_double(),
## FirePlace = col_double(),
## Baths = col_double(),
## `White Marble` = col_double(),
## `Black Marble` = col_double(),
## `Indian Marble` = col_double(),
## Floors = col_double(),
## City = col_double(),
## Solar = col_double(),
## Electric = col_double(),
## Fiber = col_double(),
## `Glass Doors` = col_double(),
## `Swiming Pool` = col_double(),
## Garden = col_double(),
## Prices = col_double()
## )
glimpse(house)
## Rows: 500,000
## Columns: 16
## $ Area <dbl> 164, 84, 190, 75, 148, 124, 58, 249, 243, 242, 61, 18…
## $ Garage <dbl> 2, 2, 2, 2, 1, 3, 1, 2, 1, 1, 2, 2, 2, 3, 3, 3, 1, 3,…
## $ FirePlace <dbl> 0, 0, 4, 4, 4, 3, 0, 1, 0, 2, 4, 0, 0, 3, 3, 4, 0, 3,…
## $ Baths <dbl> 2, 4, 4, 4, 2, 3, 2, 1, 2, 4, 5, 4, 2, 3, 1, 1, 5, 3,…
## $ `White Marble` <dbl> 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,…
## $ `Black Marble` <dbl> 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1,…
## $ `Indian Marble` <dbl> 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0,…
## $ Floors <dbl> 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1,…
## $ City <dbl> 3, 2, 2, 1, 2, 1, 3, 1, 1, 2, 1, 2, 1, 3, 3, 1, 3, 1,…
## $ Solar <dbl> 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1,…
## $ Electric <dbl> 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,…
## $ Fiber <dbl> 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1,…
## $ `Glass Doors` <dbl> 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0,…
## $ `Swiming Pool` <dbl> 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,…
## $ Garden <dbl> 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0,…
## $ Prices <dbl> 43800, 37550, 49500, 50075, 52400, 54300, 34400, 5042…
Jika dilihat dari Data House, dataset house memiliki 16 kolom dan row 500,000.
Berikut beberapa keterangan kolom: Area: Luas area tanah
Garage: Jumlah garasi
FirePlace: Jumlah FirePlace
Baths: Jumlah kamar mandi
White Marble: Apakah Jenis lantai adalah White Marble
Black Marble: Apakah Jenis lantai adalah Black Marble
Indian Marble: Apakah Jenis lantai adalah Indian Marble
Glass Doors: Apakah Menggunakan Pintu Kaca
Swiming Pool: Apakah Menggunakan Terdapat Kolam Renang
Garden: Apakah terdapat kebun
Prices: Harga rumah
Mencari Korelasi
ggcorr(house, label=T, label_size=2.9, hjust=1)
Dari grafik korelasi diatas bisa dilihat Floors atau lantai memiliki korelasi tertinggi terhadap Prices. Dan jika dilihat variable prediktor, variable tersebut memiliki korelasi yang sedikit atau bahkan tidak ada korelasi sama sekali.
Dalam membuat model, data akan dipisah menjadi dua kategori yaitu data train sebanyak 70% dan data test sebanyak 30%. Dengan begini model dapat ditest dengan data yang belum pernah dilihat model tersebut.
set.seed(384)
sample.size <- round(0.7 * nrow(house), 0)
index <- sample(seq_len(nrow(house)), size=sample.size)
house.train <- house[index, ]
house.test <- house[-index, ]
Dalam pembuatan model, model akan dibuat dengan Linear Regression menggunakan Step-wise Regression dengan direction backward untuk memilih variable prediktor.
model.all <- lm(Prices~., house.train)
model.step <- step(model.all, direction="backward")
## Start: AIC=-12140348
## Prices ~ Area + Garage + FirePlace + Baths + `White Marble` +
## `Black Marble` + `Indian Marble` + Floors + City + Solar +
## Electric + Fiber + `Glass Doors` + `Swiming Pool` + Garden
## Warning: attempting model selection on an essentially perfect fit is nonsense
##
## Step: AIC=-12140348
## Prices ~ Area + Garage + FirePlace + Baths + `White Marble` +
## `Black Marble` + Floors + City + Solar + Electric + Fiber +
## `Glass Doors` + `Swiming Pool` + Garden
## Warning: attempting model selection on an essentially perfect fit is nonsense
## Df Sum of Sq RSS AIC
## - `Swiming Pool` 1 0.0000e+00 0.0000e+00 -12140349
## - Garden 1 0.0000e+00 0.0000e+00 -12140349
## <none> 0.0000e+00 -12140348
## - Solar 1 5.4684e+09 5.4684e+09 3379827
## - Electric 1 1.3672e+11 1.3672e+11 4506446
## - FirePlace 1 3.9372e+11 3.9372e+11 4876656
## - Garage 1 5.2601e+11 5.2601e+11 4978042
## - Baths 1 1.0962e+12 1.0962e+12 5235044
## - Area 1 1.1268e+12 1.1268e+12 5244677
## - `Black Marble` 1 1.4585e+12 1.4585e+12 5334992
## - `Glass Doors` 1 1.7327e+12 1.7327e+12 5395279
## - City 1 2.8540e+12 2.8540e+12 5569942
## - `White Marble` 1 1.1435e+13 1.1435e+13 6055730
## - Fiber 1 1.2080e+13 1.2080e+13 6074935
## - Floors 1 1.9687e+13 1.9687e+13 6245877
##
## Step: AIC=-12140349
## Prices ~ Area + Garage + FirePlace + Baths + `White Marble` +
## `Black Marble` + Floors + City + Solar + Electric + Fiber +
## `Glass Doors` + Garden
## Warning: attempting model selection on an essentially perfect fit is nonsense
## Df Sum of Sq RSS AIC
## - Garden 1 0.0000e+00 0.0000e+00 -12140350
## <none> 0.0000e+00 -12140349
## - Solar 1 5.4684e+09 5.4684e+09 3379825
## - Electric 1 1.3672e+11 1.3672e+11 4506444
## - FirePlace 1 3.9372e+11 3.9372e+11 4876655
## - Garage 1 5.2601e+11 5.2601e+11 4978041
## - Baths 1 1.0962e+12 1.0962e+12 5235046
## - Area 1 1.1268e+12 1.1268e+12 5244675
## - `Black Marble` 1 1.4585e+12 1.4585e+12 5334990
## - `Glass Doors` 1 1.7327e+12 1.7327e+12 5395277
## - City 1 2.8540e+12 2.8540e+12 5569941
## - `White Marble` 1 1.1435e+13 1.1435e+13 6055730
## - Fiber 1 1.2080e+13 1.2080e+13 6074939
## - Floors 1 1.9687e+13 1.9687e+13 6245875
##
## Step: AIC=-12140350
## Prices ~ Area + Garage + FirePlace + Baths + `White Marble` +
## `Black Marble` + Floors + City + Solar + Electric + Fiber +
## `Glass Doors`
## Warning: attempting model selection on an essentially perfect fit is nonsense
## Df Sum of Sq RSS AIC
## <none> 0.0000e+00 -12140350
## - Solar 1 5.4685e+09 5.4685e+09 3379831
## - Electric 1 1.3672e+11 1.3672e+11 4506442
## - FirePlace 1 3.9373e+11 3.9373e+11 4876655
## - Garage 1 5.2601e+11 5.2601e+11 4978039
## - Baths 1 1.0962e+12 1.0962e+12 5235044
## - Area 1 1.1268e+12 1.1268e+12 5244674
## - `Black Marble` 1 1.4585e+12 1.4585e+12 5334988
## - `Glass Doors` 1 1.7327e+12 1.7327e+12 5395277
## - City 1 2.8540e+12 2.8540e+12 5569941
## - `White Marble` 1 1.1435e+13 1.1435e+13 6055728
## - Fiber 1 1.2080e+13 1.2080e+13 6074938
## - Floors 1 1.9687e+13 1.9687e+13 6245874
summary(model.step)
##
## Call:
## lm(formula = Prices ~ Area + Garage + FirePlace + Baths + `White Marble` +
## `Black Marble` + Floors + City + Solar + Electric + Fiber +
## `Glass Doors`, data = house.train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.737e-05 -1.000e-10 0.000e+00 2.000e-10 1.885e-07
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.000e+03 2.701e-10 3.703e+12 <2e-16 ***
## Area 2.500e+01 6.916e-13 3.615e+13 <2e-16 ***
## Garage 1.500e+03 6.074e-11 2.470e+13 <2e-16 ***
## FirePlace 7.500e+02 3.510e-11 2.137e+13 <2e-16 ***
## Baths 1.250e+03 3.506e-11 3.565e+13 <2e-16 ***
## `White Marble` 1.400e+04 1.216e-10 1.151e+14 <2e-16 ***
## `Black Marble` 5.000e+03 1.216e-10 4.112e+13 <2e-16 ***
## Floors 1.500e+04 9.928e-11 1.511e+14 <2e-16 ***
## City 3.500e+03 6.084e-11 5.753e+13 <2e-16 ***
## Solar 2.500e+02 9.928e-11 2.518e+12 <2e-16 ***
## Electric 1.250e+03 9.928e-11 1.259e+13 <2e-16 ***
## Fiber 1.175e+04 9.928e-11 1.184e+14 <2e-16 ***
## `Glass Doors` 4.450e+03 9.928e-11 4.482e+13 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.937e-08 on 349987 degrees of freedom
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: 4.959e+27 on 12 and 349987 DF, p-value: < 2.2e-16
Dari hasil dari model, hasil adjusted R-squared adalah 1 alias sangat akurat. Model ini menggunakan beberapa variable prediction Area, Garage, FirePlace, Baths, White Marble, Black Marble, Floors, City, Solar, Electric, Fiber, Glass Doors
Model akan menggunakan Normality, Heteroscedasticity, Multicollinearity untuk mengevaluasi.
hist(model.step$residuals)
set.seed(384)
sample.residuals.index <- sample(seq_len(nrow(as.data.frame(model.step$residuals))), size=1000)
shapiro.test(tail(as.data.frame(model.step$residuals)[sample.residuals.index,], 5000))
##
## Shapiro-Wilk normality test
##
## data: tail(as.data.frame(model.step$residuals)[sample.residuals.index, ], 5000)
## W = 0.99845, p-value = 0.5226
Dari test Normality, p-value pada model adalah > 0.05 sehingga H0 diterima yang berarti penyebaran residual normal.
bptest(model.step)
##
## studentized Breusch-Pagan test
##
## data: model.step
## BP = 10.992, df = 12, p-value = 0.5296
p-value bernilai 0.5296 atau lebih dari 0.05 yang berarti H0 diterima atau residual tidak memiliki pola.
vif(model.step)
## Area Garage FirePlace Baths `White Marble`
## 1.000030 1.000022 1.000018 1.000039 1.332929
## `Black Marble` Floors City Solar Electric
## 1.332941 1.000030 1.000027 1.000035 1.000022
## Fiber `Glass Doors`
## 1.000026 1.000009
Dari hasil test Multicollinearity, Tidak ada nilai yang lebih dari 10 yang berarti antar predictor tidak ada yang saling bergantung satu sama lain.
Untuk mengetest keakuratan prediction akan digunakan MAPE (Mean Absolute Percentage Error)
Test training data
MAPE(model.step$fitted.values, house.train$Prices)
## [1] 4.491632e-15
Test testing data
house.test$predicted_price <- predict(model.step, house.test)
MAPE(house.test$predicted_price, house.test$Prices)
## [1] 7.733977e-13
Berdasarkan hasil test MAPE, model mendapatkan hasil 0.000000000000004491632 yang berarti terbukti akurat hingga 99%.
Model model.step mendapatkan Adjusted R Squared 1 yang artinya sangat berpengaruh. Model ini menggunakan beberapa variable prediction Area, Garage, FirePlace, Baths, White Marble, Black Marble, Floors, City, Solar, Electric, Fiber, Glass Doors. Berdasarkan hasil test MAPE, model mendapatkan hasil 0.000000000000004491632 yang berarti terbukti akurat hingga 99%.