OBJECTIVES

Pada latihan kali ini kita akan memprediksi harga rumah menggunakan metode regresi linear berdasarkan atribut-atribut propertinya. Latihan ini menggunakan R studio dan library yang digunakan adalah sebagai berikut:

library(dplyr) # piping
library(GGally) # mengecek korelasi antara variabel target dengan prediktor
library(MLmetrics) # menghitung MAE, MAPE, dan RMSE
library(performance) # membandingkan performa model
library(lmtest) # uji statistik bp test
library(car) # uji VIF (Variance Inflation Factor)

1. Data Import

Tahap pertama kita akan membaca data .csv dengan variabel bernama houses

houses <- read.csv("HousePrices_HalfMil.csv")

a. Cek tipe data

str(houses)
#> 'data.frame':    500000 obs. of  16 variables:
#>  $ Area         : int  164 84 190 75 148 124 58 249 243 242 ...
#>  $ Garage       : int  2 2 2 2 1 3 1 2 1 1 ...
#>  $ FirePlace    : int  0 0 4 4 4 3 0 1 0 2 ...
#>  $ Baths        : int  2 4 4 4 2 3 2 1 2 4 ...
#>  $ White.Marble : int  0 0 1 0 1 0 0 1 0 0 ...
#>  $ Black.Marble : int  1 0 0 0 0 1 0 0 0 0 ...
#>  $ Indian.Marble: int  0 1 0 1 0 0 1 0 1 1 ...
#>  $ Floors       : int  0 1 0 1 1 1 0 1 1 0 ...
#>  $ City         : int  3 2 2 1 2 1 3 1 1 2 ...
#>  $ Solar        : int  1 0 0 1 1 0 0 0 0 1 ...
#>  $ Electric     : int  1 0 0 1 0 0 1 1 0 0 ...
#>  $ Fiber        : int  1 0 1 1 0 1 1 0 0 0 ...
#>  $ Glass.Doors  : int  1 1 0 1 1 1 1 1 0 0 ...
#>  $ Swiming.Pool : int  0 1 0 1 1 1 0 1 1 1 ...
#>  $ Garden       : int  0 1 0 1 1 1 1 0 0 0 ...
#>  $ Prices       : int  43800 37550 49500 50075 52400 54300 34400 50425 29575 22300 ...

Setelah mengecek jenis data, terdapat data yang memiliki jenis boolean, maka di sini kita hanya akan mengganti Floor sebagai factor untuk membatasi jenis kategorikal agar model tidak terlalu kompleks.

houses <- houses %>% 
  mutate(Floors = as.factor(Floors))
head(houses)

Tipe data Floors telah berganti menjadi tipe factor

b. Keterangan setiap kolom

  • Area: Luas bangunan rumah
  • Garage: Jumlah garasi
  • FirePlace: Jumlah perapian di dalam rumah
  • Baths: Jumlah kamar mandi di dalam rumah
  • White.Marble: Terdapat marmer putih (0 = no, 1 = yes)
  • Black.Marble: Terdapat marmer hitam (0 = no, 1 = yes)
  • Indian.Marble: Terdapat marmer khas India (0 = no, 1 = yes)
  • Floors: Jumlah level lantai rumah (0 = 1 lantai, 1 = lebih dari 1 lantai)
  • City: Lokasi daerah rumah
  • Solar: Sumber energi rumah berasal dari tenaga solar (0 = no, 1 = yes)
  • Electric: Sumber energi rumah berasal dari tenaga listrik (0 = no, 1 = yes)
  • Fiber: Sumber energi rumah menggunakan kabel fiber optik (0 = no, 1 = yes)
  • Glass.Doors: Terdapat pintu yang berbahan kaca (0 = no, 1 = yes)
  • Swiming.Pool: Terdapat kolam renang (0 = no, 1 = yes)
  • Garden: Terdapat taman (0 = no, 1 = yes)
  • Prices: Harga rumah

2. Data Wrangling

a. Cek missing value

anyNA(houses)
#> [1] FALSE

Data houses tidak mempunyai missing values

b. Mengecek korelasi menggunakan GGcorr

ggcorr(houses, label = TRUE)

Karena hasil dari uji korelasi terdapat calon prediktor yang tidak berpengaruh sama sekali, maka kita akan menghapus beberapa prediktor bernilai 0: - Garden - Swimming pool - Solar

dan kita akan menghapus nilai korelasi 0.1 yang terwakilkan oleh jenis parameter lain (solar, fiber, electric adalah jenis supply listrik, maka akan terwakilkan dengan fiber saja, begitu pun dengan jenis marble, kita akan menghapus White.Marble)

Kemudian akan kita simpan dalam data frame baru bernama house_ver1

houses_ver1 <- houses %>% 
  select(-c(Solar, Swiming.Pool, Garden, Electric, Black.Marble))
head(houses_ver1)

Data frame house_ver1 memiliki 11 kolom. Untuk mengurangi kompleksitas data menggunakan regresi linear dikarenakan banyak data yang binomial, maka akan dikurangi lagi beberapa variabel yang bernilai 0.1 kecuali area (dikarenakan area adalah integer): - Baths - FirePlace - Garage

Maka akan dibentuk data frame baru bernama house_ver2

houses_ver2 <- houses %>% 
  select(-c(Baths, FirePlace, Garage, Solar, Swiming.Pool, Garden, Electric, Black.Marble))
head(houses_ver2)

Setelah terbentuk data frame baru, maka kita akan bandingkan lagi hasil korelasi menggunakan GGcorr untuk house_ver1 dan house_ver2

Korelasi house_ver1

ggcorr(houses_ver1, label = TRUE)

Korelasi house_ver2

ggcorr(houses_ver2, label = TRUE)

Berdasarkan keragaman data yang dimiliki oleh houses_ver1 dan houses_ver2, untuk pemodelan kali ini kita akan mencoba kedua data frame houses_ver1 dan houses_ver2 untuk dibandingkan

3. Exploratory Data Analysis

Selanjutnya kita akan mengecek variasi data Area

a. distribusi variabel (boxplot)

boxplot(houses$Area)

Berdasarkan data area di atas, tidak terdapat outliers sehingga kita tidak perlu subsetting data Area

4. Cross-Validation

Membagi data menjadi data train dan data test menjadi 70:30

# mengunci nilai random
RNGkind(sample.kind = "Rounding") 
set.seed(417)

# index sampling
index <- sample(nrow(houses_ver1), nrow(houses_ver1)*0.7)

# splitting houses_ver1
houses_train1 <- houses_ver1[index,]
houses_test1 <- houses_ver1[-index,]

# splitting houses_ver2
houses_train2 <- houses_ver2[index,]
houses_test2 <- houses_ver2[-index,]

5. Pembuatan model

Selanjutnya kita akan membuat model dari data frame terpilih yaitu houses_ver1 dengan variasi model: ## a. menggunakan seluruh prediktor

#model all predictor
model_all_house1 <- lm(Prices~ ., houses_train1) 
model_all_house2 <- lm(Prices~ ., houses_train2)

Melihat summary hasil model houses_ver1

summary(model_all_house1) 
#> 
#> Call:
#> lm(formula = Prices ~ ., data = houses_train1)
#> 
#> Residuals:
#>    Min     1Q Median     3Q    Max 
#> -756.8 -745.3  494.1  743.8  755.5 
#> 
#> Coefficients:
#>                  Estimate  Std. Error t value            Pr(>|t|)    
#> (Intercept)    6744.70846     5.66459  1190.7 <0.0000000000000002 ***
#> Area             24.99998     0.01501  1665.1 <0.0000000000000002 ***
#> Garage         1500.35944     1.31912  1137.4 <0.0000000000000002 ***
#> FirePlace       750.17354     0.76205   984.4 <0.0000000000000002 ***
#> Baths          1250.83981     0.76193  1641.7 <0.0000000000000002 ***
#> White.Marble   9001.55053     2.64057  3408.9 <0.0000000000000002 ***
#> Indian.Marble -4997.78614     2.63969 -1893.3 <0.0000000000000002 ***
#> Floors1       14997.94572     2.15555  6957.8 <0.0000000000000002 ***
#> City           3501.11622     1.31994  2652.5 <0.0000000000000002 ***
#> Fiber         11749.55988     2.15555  5450.8 <0.0000000000000002 ***
#> Glass.Doors    4450.55744     2.15556  2064.7 <0.0000000000000002 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 637.6 on 349989 degrees of freedom
#> Multiple R-squared:  0.9972, Adjusted R-squared:  0.9972 
#> F-statistic: 1.26e+07 on 10 and 349989 DF,  p-value: < 0.00000000000000022

Melihat summary hasil model houses_ver2

summary(model_all_house2) 
#> 
#> Call:
#> lm(formula = Prices ~ ., data = houses_train2)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -6280.2 -1760.7    -1.2  1752.6  6268.6 
#> 
#> Coefficients:
#>                 Estimate Std. Error t value            Pr(>|t|)    
#> (Intercept)   15006.9879    16.2700   922.4 <0.0000000000000002 ***
#> Area             25.0212     0.0584   428.4 <0.0000000000000002 ***
#> White.Marble   9014.7637    10.2716   877.6 <0.0000000000000002 ***
#> Indian.Marble -5000.8616    10.2681  -487.0 <0.0000000000000002 ***
#> Floors1       14989.6257     8.3849  1787.7 <0.0000000000000002 ***
#> City           3500.4559     5.1345   681.8 <0.0000000000000002 ***
#> Fiber         11751.8543     8.3849  1401.6 <0.0000000000000002 ***
#> Glass.Doors    4435.0551     8.3849   528.9 <0.0000000000000002 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 2480 on 349992 degrees of freedom
#> Multiple R-squared:  0.9581, Adjusted R-squared:  0.9581 
#> F-statistic: 1.143e+06 on 7 and 349992 DF,  p-value: < 0.00000000000000022

Berdasarkan kedua data di atas didapat model_all_house1 memiliki adjusted R-squared sebesar 0.9972 model_all_house2 memiliki adjusted R-squared sebesar 0.9581

b. Menggunakan forward step regression

# model tanpa prediktor
model_none1 <- lm(Prices ~ 1, houses_train1)
model_none2 <- lm(Prices ~ 1, houses_train2)

Untuk forward selection, kita perlu mendefinisikan parameter scope untuk menandakan batas atas maksimal kombinasi prediktor.

model_forward1 <- step(object = model_none1,
                       direction = 'forward',
                       scope = list(lower = model_none1, upper = model_all_house1))
#> Start:  AIC=6581515
#> Prices ~ 1
#> 
#>                 Df      Sum of Sq            RSS     AIC
#> + Floors         1 19707838107844 31658808883795 6412126
#> + Fiber          1 12074905798157 39291741193482 6487725
#> + White.Marble   1 10290339094701 41076307896938 6503271
#> + Indian.Marble  1  7053095141871 44313551849768 6529822
#> + City           1  2818433019648 48548213971991 6561766
#> + Glass.Doors    1  1711049338371 49655597653268 6569659
#> + Area           1  1123725508963 50242921482676 6573775
#> + Baths          1  1069409925679 50297237065960 6574153
#> + Garage         1   516421688524 50850225303114 6577980
#> + FirePlace      1   420576765150 50946070226489 6578639
#> <none>                            51366646991639 6581515
#> 
#> Step:  AIC=6412126
#> Prices ~ Floors
#> 
#>                 Df      Sum of Sq            RSS     AIC
#> + Fiber          1 12030321065914 19628487817881 6244816
#> + White.Marble   1 10295308424245 21363500459550 6274462
#> + Indian.Marble  1  7035185164162 24623623719633 6324170
#> + City           1  2810489195010 28848319688784 6379591
#> + Glass.Doors    1  1709054356714 29949754527080 6392705
#> + Area           1  1136253484240 30522555399555 6399336
#> + Baths          1  1088397417524 30570411466271 6399884
#> + Garage         1   526539680257 31132269203538 6406258
#> + FirePlace      1   412936934711 31245871949084 6407533
#> <none>                            31658808883795 6412126
#> 
#> Step:  AIC=6244816
#> Prices ~ Floors + Fiber
#> 
#>                 Df      Sum of Sq            RSS     AIC
#> + White.Marble   1 10320962098276  9307525719605 5983663
#> + Indian.Marble  1  7028844452656 12599643365224 6089658
#> + City           1  2836321677254 16792166140627 6190194
#> + Glass.Doors    1  1723238570480 17905249247400 6212657
#> + Area           1  1133192754780 18495295063101 6224005
#> + Baths          1  1094431421040 18534056396841 6224738
#> + Garage         1   524247148595 19104240669286 6235343
#> + FirePlace      1   404293422776 19224194395105 6237534
#> <none>                            19628487817881 6244816
#> 
#> Step:  AIC=5983663
#> Prices ~ Floors + Fiber + White.Marble
#> 
#>                 Df     Sum of Sq           RSS     AIC
#> + City           1 2851264463181 6456261256423 5855644
#> + Glass.Doors    1 1724684873272 7582840846333 5911937
#> + Indian.Marble  1 1454629512007 7852896207597 5924185
#> + Area           1 1109718382443 8197807337162 5939230
#> + Baths          1 1082505500346 8225020219258 5940390
#> + Garage         1  518565528551 8788960191054 5963600
#> + FirePlace      1  395447252911 8912078466693 5968469
#> <none>                           9307525719605 5983663
#> 
#> Step:  AIC=5855644
#> Prices ~ Floors + Fiber + White.Marble + City
#> 
#>                 Df     Sum of Sq           RSS     AIC
#> + Glass.Doors    1 1716321440348 4739939816075 5747487
#> + Indian.Marble  1 1457486443133 4998774813290 5766096
#> + Area           1 1123244804732 5333016451691 5788749
#> + Baths          1 1085800536681 5370460719742 5791198
#> + Garage         1  515964523156 5940296733267 5826494
#> + FirePlace      1  395879764800 6060381491623 5833499
#> <none>                           6456261256423 5855644
#> 
#> Step:  AIC=5747487
#> Prices ~ Floors + Fiber + White.Marble + City + Glass.Doors
#> 
#>                 Df     Sum of Sq           RSS     AIC
#> + Indian.Marble  1 1457764820835 3282174995240 5618858
#> + Area           1 1127726692956 3612213123119 5652393
#> + Baths          1 1089740297523 3650199518553 5656054
#> + Garage         1  523172905401 4216766910674 5706554
#> + FirePlace      1  396704531581 4343235284494 5716897
#> <none>                           4739939816075 5747487
#> 
#> Step:  AIC=5618858
#> Prices ~ Floors + Fiber + White.Marble + City + Glass.Doors + 
#>     Indian.Marble
#> 
#>             Df     Sum of Sq           RSS     AIC
#> + Area       1 1129122366384 2153052628855 5471293
#> + Baths      1 1093791199985 2188383795254 5476990
#> + Garage     1  518078299929 2764096695310 5558732
#> + FirePlace  1  395979489326 2886195505914 5573861
#> <none>                       3282174995240 5618858
#> 
#> Step:  AIC=5471293
#> Prices ~ Floors + Fiber + White.Marble + City + Glass.Doors + 
#>     Indian.Marble + Area
#> 
#>             Df     Sum of Sq           RSS     AIC
#> + Baths      1 1091680829989 1061371798867 5223731
#> + Garage     1  519785281739 1633267347117 5374588
#> + FirePlace  1  394454715410 1758597913446 5400465
#> <none>                       2153052628855 5471293
#> 
#> Step:  AIC=5223731
#> Prices ~ Floors + Fiber + White.Marble + City + Glass.Doors + 
#>     Indian.Marble + Area + Baths
#> 
#>             Df    Sum of Sq           RSS     AIC
#> + Garage     1 525101517070  536270281796 4984795
#> + FirePlace  1 393137716995  668234081872 5061796
#> <none>                      1061371798867 5223731
#> 
#> Step:  AIC=4984795
#> Prices ~ Floors + Fiber + White.Marble + City + Glass.Doors + 
#>     Indian.Marble + Area + Baths + Garage
#> 
#>             Df    Sum of Sq          RSS     AIC
#> + FirePlace  1 393981753465 142288528331 4520424
#> <none>                      536270281796 4984795
#> 
#> Step:  AIC=4520424
#> Prices ~ Floors + Fiber + White.Marble + City + Glass.Doors + 
#>     Indian.Marble + Area + Baths + Garage + FirePlace

model step regression menggunakan data frame houses_ver2

model_forward2 <- step(object = model_none2,
                       direction = 'forward',
                       scope = list(lower = model_none2, upper = model_all_house2))
#> Start:  AIC=6581515
#> Prices ~ 1
#> 
#>                 Df      Sum of Sq            RSS     AIC
#> + Floors         1 19707838107844 31658808883795 6412126
#> + Fiber          1 12074905798157 39291741193482 6487725
#> + White.Marble   1 10290339094701 41076307896938 6503271
#> + Indian.Marble  1  7053095141871 44313551849768 6529822
#> + City           1  2818433019648 48548213971991 6561766
#> + Glass.Doors    1  1711049338371 49655597653268 6569659
#> + Area           1  1123725508963 50242921482676 6573775
#> <none>                            51366646991639 6581515
#> 
#> Step:  AIC=6412126
#> Prices ~ Floors
#> 
#>                 Df      Sum of Sq            RSS     AIC
#> + Fiber          1 12030321065914 19628487817881 6244816
#> + White.Marble   1 10295308424245 21363500459550 6274462
#> + Indian.Marble  1  7035185164162 24623623719633 6324170
#> + City           1  2810489195010 28848319688784 6379591
#> + Glass.Doors    1  1709054356714 29949754527080 6392705
#> + Area           1  1136253484240 30522555399555 6399336
#> <none>                            31658808883795 6412126
#> 
#> Step:  AIC=6244816
#> Prices ~ Floors + Fiber
#> 
#>                 Df      Sum of Sq            RSS     AIC
#> + White.Marble   1 10320962098276  9307525719605 5983663
#> + Indian.Marble  1  7028844452656 12599643365224 6089658
#> + City           1  2836321677254 16792166140627 6190194
#> + Glass.Doors    1  1723238570480 17905249247400 6212657
#> + Area           1  1133192754780 18495295063101 6224005
#> <none>                            19628487817881 6244816
#> 
#> Step:  AIC=5983663
#> Prices ~ Floors + Fiber + White.Marble
#> 
#>                 Df     Sum of Sq           RSS     AIC
#> + City           1 2851264463181 6456261256423 5855644
#> + Glass.Doors    1 1724684873272 7582840846333 5911937
#> + Indian.Marble  1 1454629512007 7852896207597 5924185
#> + Area           1 1109718382443 8197807337162 5939230
#> <none>                           9307525719605 5983663
#> 
#> Step:  AIC=5855644
#> Prices ~ Floors + Fiber + White.Marble + City
#> 
#>                 Df     Sum of Sq           RSS     AIC
#> + Glass.Doors    1 1716321440348 4739939816075 5747487
#> + Indian.Marble  1 1457486443133 4998774813290 5766096
#> + Area           1 1123244804732 5333016451691 5788749
#> <none>                           6456261256423 5855644
#> 
#> Step:  AIC=5747487
#> Prices ~ Floors + Fiber + White.Marble + City + Glass.Doors
#> 
#>                 Df     Sum of Sq           RSS     AIC
#> + Indian.Marble  1 1457764820835 3282174995240 5618858
#> + Area           1 1127726692956 3612213123119 5652393
#> <none>                           4739939816075 5747487
#> 
#> Step:  AIC=5618858
#> Prices ~ Floors + Fiber + White.Marble + City + Glass.Doors + 
#>     Indian.Marble
#> 
#>        Df     Sum of Sq           RSS     AIC
#> + Area  1 1129122366384 2153052628855 5471293
#> <none>                  3282174995240 5618858
#> 
#> Step:  AIC=5471293
#> Prices ~ Floors + Fiber + White.Marble + City + Glass.Doors + 
#>     Indian.Marble + Area

Berdasarkan prediktor yang dipilih dari hasil forward step regression, didapat bahwa model yang menggunakan seluruh prediktor menghasilkan AIC terkecil. Sehingga menunjukkan bahwa model awal (menggunakan seluruh prediktor) dan model forward memiliki hasil yang sama.

Selanjutnya kita akan melihat summary hasil model untuk kedua data frame

data frame houses_ver1

summary(model_forward1) 
#> 
#> Call:
#> lm(formula = Prices ~ Floors + Fiber + White.Marble + City + 
#>     Glass.Doors + Indian.Marble + Area + Baths + Garage + FirePlace, 
#>     data = houses_train1)
#> 
#> Residuals:
#>    Min     1Q Median     3Q    Max 
#> -756.8 -745.3  494.1  743.8  755.5 
#> 
#> Coefficients:
#>                  Estimate  Std. Error t value            Pr(>|t|)    
#> (Intercept)    6744.70846     5.66459  1190.7 <0.0000000000000002 ***
#> Floors1       14997.94572     2.15555  6957.8 <0.0000000000000002 ***
#> Fiber         11749.55988     2.15555  5450.8 <0.0000000000000002 ***
#> White.Marble   9001.55053     2.64057  3408.9 <0.0000000000000002 ***
#> City           3501.11622     1.31994  2652.5 <0.0000000000000002 ***
#> Glass.Doors    4450.55744     2.15556  2064.7 <0.0000000000000002 ***
#> Indian.Marble -4997.78614     2.63969 -1893.3 <0.0000000000000002 ***
#> Area             24.99998     0.01501  1665.1 <0.0000000000000002 ***
#> Baths          1250.83981     0.76193  1641.7 <0.0000000000000002 ***
#> Garage         1500.35944     1.31912  1137.4 <0.0000000000000002 ***
#> FirePlace       750.17354     0.76205   984.4 <0.0000000000000002 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 637.6 on 349989 degrees of freedom
#> Multiple R-squared:  0.9972, Adjusted R-squared:  0.9972 
#> F-statistic: 1.26e+07 on 10 and 349989 DF,  p-value: < 0.00000000000000022

data frame houses_ver2

summary(model_forward2)
#> 
#> Call:
#> lm(formula = Prices ~ Floors + Fiber + White.Marble + City + 
#>     Glass.Doors + Indian.Marble + Area, data = houses_train2)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -6280.2 -1760.7    -1.2  1752.6  6268.6 
#> 
#> Coefficients:
#>                 Estimate Std. Error t value            Pr(>|t|)    
#> (Intercept)   15006.9879    16.2700   922.4 <0.0000000000000002 ***
#> Floors1       14989.6257     8.3849  1787.7 <0.0000000000000002 ***
#> Fiber         11751.8543     8.3849  1401.6 <0.0000000000000002 ***
#> White.Marble   9014.7637    10.2716   877.6 <0.0000000000000002 ***
#> City           3500.4559     5.1345   681.8 <0.0000000000000002 ***
#> Glass.Doors    4435.0551     8.3849   528.9 <0.0000000000000002 ***
#> Indian.Marble -5000.8616    10.2681  -487.0 <0.0000000000000002 ***
#> Area             25.0212     0.0584   428.4 <0.0000000000000002 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 2480 on 349992 degrees of freedom
#> Multiple R-squared:  0.9581, Adjusted R-squared:  0.9581 
#> F-statistic: 1.143e+06 on 7 and 349992 DF,  p-value: < 0.00000000000000022

c. Kandidat model, AIC & adjusted r-squared

Berdasarkan hasil summary model, menunjukkan bahwa adjusted R-squared hasil dari forward step dari data frame house_ver1 memiliki adjusted R-squared sebesar 0.9972, yang berarti memiliki nilai mendekati 1, yang mana efektivitasnya lebih tinggi dibandingkan model menggunakan data frame houses_ver2.

6. Prediksi model & error

a. Predict

Prediksi model menggunakan data houses_test1 dan houses_test2

pred_house1 <- predict(object = model_all_house1, newdata = houses_test1)
pred_house2 <- predict(object = model_all_house2, newdata = houses_test2)

b. Error

Error akan dihitung menggunakan metode MAE dan RMSE agar hasil error lebih mudah diinterpretasi

Error menggunakan model dari data frame houses_ver1

# Menghitung MAE dan RMSE dari library MLmetrics
MAE(y_pred = pred_house1,
    y_true = houses_ver1$Prices)
#> [1] 13782.04
RMSE(y_pred = pred_house1,
     y_true = houses_ver1$Prices)
#> [1] 17092.52
MAPE(y_pred = pred_house1,
     y_true = houses_ver1$Prices)*100
#> [1] 37.73359

Error menggunakan model dari data frame houses_ver2

# Menghitung MAE dan RMSE dari library MLmetrics
MAE(y_pred = pred_house2,
    y_true = houses_ver2$Prices)
#> [1] 13652.66
RMSE(y_pred = pred_house2,
     y_true = houses_ver2$Prices)
#> [1] 16925.78
MAPE(y_pred = pred_house2,
     y_true = houses_ver2$Prices)*100
#> [1] 37.41

Untuk dapat menginterpretasikan error di atas, kita perlu mengetahui range data harga rumah

range(houses_ver1$Prices)
#> [1]  7725 77975

Dari hasil di atas menunjukkan hasil error data frame houses_ver1 MAE : 13,782.04 RMSE : 17,092.52 MAPE : 37.73%

data frame houses_ver2 MAE : 13,652.66 RMSE : 16,925.78 MAPE : 37.41%

Range harga rumah menunjukkan nilai 7,725 - 77,975.

Maka dapat disimpulkan berdasarkan Mean Absolute Error, Root Mean Square Error, dan MAPE bahwa hasil prediksi tersebut menunjukkan error yang terbilang cukup tinggi.

7. Evaluasi model

Untuk mengevaluasi model regresi linear yang kita punya, kita akan melakukan beberapa tes sebagai berikut

a. Normality of Residuals

Model linear regression diharapkan menghasilkan error yang berdistribusi normal. Dengan begitu, error lebih banyak berkumpul di sekitar angka nol berikut visualisasi histogram residual menggunakan fungsi hist()

# histogram residual
hist(model_all_house1$residuals)

Berdasarkan hasil data histogram di atas dari model_all_house1 dari data frame house_ver1 menunjukkan bahwa error memiliki distribusi yang tidak normal yang menunjukkan bahwa model tidak cukup ideal untuk melakukan prediksi.

# histogram residual
hist(model_all_house2$residuals)

Berdasarkan hasil data histogram di atas dari model_all_house2 dari data frame house_ver2 menunjukkan bahwa error memiliki distribusi yang normal yang menunjukkan bahwa model memenuhi syarat untuk uji regresi linear.

b. Linearity

Linearity artinya target variabel dengan prediktornya memiliki hubungan yang linear atau hubungannya bersifat garis lurus.

berikut adalah plot plot residual vs fitted values untuk model model_all_house1

plot(model_all_house1, which = 1)

berikut adalah plot plot residual vs fitted values untuk model model_all_house2

plot(model_all_house2, which = 1)

Dari kedua hasil linearity test di atas, nilai residual tidak “bounce randomly” di sekitar nilai 0 untuk model model_all_house1 dan “bounce_randomly” di sekitar nilai 0 untuk model model_all_house2. Maka yang memenuhi uji linearitas adalah model_all_house2

# shapiro test dari residual
# shapiro.test(model_all_house1$residuals)
# shapiro.test(model_all_house2$residuals)

Shapiro test tidak dapat dilakukan untuk kedua model dikarenakan ukuran sample tidak berada di antara nilai 3-5,000. Hal ini dikarenakan data terlalu banyak yang bersifat binomial meskipun bukan bertipe factor.

c. Homoscedasticity of Residuals

Uji homoscedasticity adalah ketika error yang dihasilkan oleh model menyebar secara acak atau dengan variasi konstan

# bptest dari model
bptest(model_all_house1)
#> 
#>  studentized Breusch-Pagan test
#> 
#> data:  model_all_house1
#> BP = 10.108, df = 10, p-value = 0.431
bptest(model_all_house2)
#> 
#>  studentized Breusch-Pagan test
#> 
#> data:  model_all_house2
#> BP = 6.214, df = 7, p-value = 0.515

Kesimpulan: Dikarenakan hasil model_all_house1 dan model_all_house2 memiliki p-value > 0.05 sehingga gagal tolak H0 (menerima H0) –> model mempunyai error yang menyebar konstan atau homoscedasticity –> asumsinya terpenuhi.

d. Uji Multicollinearity

Uji multikolinearitas menunjukkan apabila antar variabel memiliki korelasi atau tidak. Hasil yang baik adalah apabila nilai Variance Inflation Factor (VIF) < 10.

# vif dari model
vif(model_all_house1)
#>          Area        Garage     FirePlace         Baths  White.Marble 
#>      1.000035      1.000043      1.000014      1.000028      1.333636 
#> Indian.Marble        Floors          City         Fiber   Glass.Doors 
#>      1.333625      1.000014      1.000028      1.000017      1.000026
vif(model_all_house2)
#>          Area  White.Marble Indian.Marble        Floors          City 
#>      1.000031      1.333624      1.333610      1.000005      1.000025 
#>         Fiber   Glass.Doors 
#>      1.000012      1.000009

Berdasarkan hasil vif di atas, menunjukkan bahwa kedual model tersebut memiliki vif < 10. Hal ini menunjukkan tidak adanya hubungan antar prediktor pada kedua model.

KESIMPULAN DAN SARAN

Kesimpulan dari uji regresi linear kali ini adalah adanya kendala pada data yang memiliki jenis data binomial yang mempengaruhi formula pada pembuatan model regresi linear. Hal ini ditunjukkan oleh hasil evaluasi model pada data frame house_ver2 yang memiliki hasil yang lebih baik apabila mengurangi beberapa prediktor binomial. Namun meskipun begitu, data frame house_ver2 masih memiliki error MAPE yang cukup tinggi yaitu sebesar 37.41%. Maka dari itu, saran yang tepat untuk uji regresi linear pada data harga rumah ini adalah memperbanyak prediktor yang tidak memiliki data binomial.