Metode Regresi Shrinkage

Gerry Alfa Dito1

Package

Silahkan install jika belum ada

install.packages("glmnet")
install.packages("tidyverse")
install.packages("glmnetUtils")
library(glmnetUtils)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.4     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

Deskripsi singkat data

Ask a home buyer to describe their dream house, and they probably won’t begin with the height of the basement ceiling or the proximity to an east-west railroad. But this playground competition’s dataset proves that much more influences price negotiations than the number of bedrooms or a white-picket fence.

With 79 explanatory variables describing (almost) every aspect of residential homes in Ames, Iowa, this competition challenges you to predict the final price of each home.

data ini bisa diperoleh di link berikut ini

Data House Price

Import Data

data_house <- read.csv("house_price1.csv",stringsAsFactors = TRUE)
glimpse(data_house)
## Rows: 1,460
## Columns: 81
## $ Id            <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16...
## $ MSSubClass    <int> 60, 20, 60, 70, 60, 50, 20, 60, 50, 190, 20, 60, 20, ...
## $ MSZoning      <fct> RL, RL, RL, RL, RL, RL, RL, RL, RM, RL, RL, RL, RL, R...
## $ LotFrontage   <int> 65, 80, 68, 60, 84, 85, 75, NA, 51, 50, 70, 85, NA, 9...
## $ LotArea       <int> 8450, 9600, 11250, 9550, 14260, 14115, 10084, 10382, ...
## $ Street        <fct> Pave, Pave, Pave, Pave, Pave, Pave, Pave, Pave, Pave,...
## $ Alley         <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...
## $ LotShape      <fct> Reg, Reg, IR1, IR1, IR1, IR1, Reg, IR1, Reg, Reg, Reg...
## $ LandContour   <fct> Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl, Lvl...
## $ Utilities     <fct> AllPub, AllPub, AllPub, AllPub, AllPub, AllPub, AllPu...
## $ LotConfig     <fct> Inside, FR2, Inside, Corner, FR2, Inside, Inside, Cor...
## $ LandSlope     <fct> Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl, Gtl...
## $ Neighborhood  <fct> CollgCr, Veenker, CollgCr, Crawfor, NoRidge, Mitchel,...
## $ Condition1    <fct> Norm, Feedr, Norm, Norm, Norm, Norm, Norm, PosN, Arte...
## $ Condition2    <fct> Norm, Norm, Norm, Norm, Norm, Norm, Norm, Norm, Norm,...
## $ BldgType      <fct> 1Fam, 1Fam, 1Fam, 1Fam, 1Fam, 1Fam, 1Fam, 1Fam, 1Fam,...
## $ HouseStyle    <fct> 2Story, 1Story, 2Story, 2Story, 2Story, 1.5Fin, 1Stor...
## $ OverallQual   <int> 7, 6, 7, 7, 8, 5, 8, 7, 7, 5, 5, 9, 5, 7, 6, 7, 6, 4,...
## $ OverallCond   <int> 5, 8, 5, 5, 5, 5, 5, 6, 5, 6, 5, 5, 6, 5, 5, 8, 7, 5,...
## $ YearBuilt     <int> 2003, 1976, 2001, 1915, 2000, 1993, 2004, 1973, 1931,...
## $ YearRemodAdd  <int> 2003, 1976, 2002, 1970, 2000, 1995, 2005, 1973, 1950,...
## $ RoofStyle     <fct> Gable, Gable, Gable, Gable, Gable, Gable, Gable, Gabl...
## $ RoofMatl      <fct> CompShg, CompShg, CompShg, CompShg, CompShg, CompShg,...
## $ Exterior1st   <fct> VinylSd, MetalSd, VinylSd, Wd Sdng, VinylSd, VinylSd,...
## $ Exterior2nd   <fct> VinylSd, MetalSd, VinylSd, Wd Shng, VinylSd, VinylSd,...
## $ MasVnrType    <fct> BrkFace, None, BrkFace, None, BrkFace, None, Stone, S...
## $ MasVnrArea    <int> 196, 0, 162, 0, 350, 0, 186, 240, 0, 0, 0, 286, 0, 30...
## $ ExterQual     <fct> Gd, TA, Gd, TA, Gd, TA, Gd, TA, TA, TA, TA, Ex, TA, G...
## $ ExterCond     <fct> TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, T...
## $ Foundation    <fct> PConc, CBlock, PConc, BrkTil, PConc, Wood, PConc, CBl...
## $ BsmtQual      <fct> Gd, Gd, Gd, TA, Gd, Gd, Ex, Gd, TA, TA, TA, Ex, TA, G...
## $ BsmtCond      <fct> TA, TA, TA, Gd, TA, TA, TA, TA, TA, TA, TA, TA, TA, T...
## $ BsmtExposure  <fct> No, Gd, Mn, No, Av, No, Av, Mn, No, No, No, No, No, A...
## $ BsmtFinType1  <fct> GLQ, ALQ, GLQ, ALQ, GLQ, GLQ, GLQ, ALQ, Unf, GLQ, Rec...
## $ BsmtFinSF1    <int> 706, 978, 486, 216, 655, 732, 1369, 859, 0, 851, 906,...
## $ BsmtFinType2  <fct> Unf, Unf, Unf, Unf, Unf, Unf, Unf, BLQ, Unf, Unf, Unf...
## $ BsmtFinSF2    <int> 0, 0, 0, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ BsmtUnfSF     <int> 150, 284, 434, 540, 490, 64, 317, 216, 952, 140, 134,...
## $ TotalBsmtSF   <int> 856, 1262, 920, 756, 1145, 796, 1686, 1107, 952, 991,...
## $ Heating       <fct> GasA, GasA, GasA, GasA, GasA, GasA, GasA, GasA, GasA,...
## $ HeatingQC     <fct> Ex, Ex, Ex, Gd, Ex, Ex, Ex, Ex, Gd, Ex, Ex, Ex, TA, E...
## $ CentralAir    <fct> Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y,...
## $ Electrical    <fct> SBrkr, SBrkr, SBrkr, SBrkr, SBrkr, SBrkr, SBrkr, SBrk...
## $ X1stFlrSF     <int> 856, 1262, 920, 961, 1145, 796, 1694, 1107, 1022, 107...
## $ X2ndFlrSF     <int> 854, 0, 866, 756, 1053, 566, 0, 983, 752, 0, 0, 1142,...
## $ LowQualFinSF  <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ GrLivArea     <int> 1710, 1262, 1786, 1717, 2198, 1362, 1694, 2090, 1774,...
## $ BsmtFullBath  <int> 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0,...
## $ BsmtHalfBath  <int> 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ FullBath      <int> 2, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 3, 1, 2, 1, 1, 1, 2,...
## $ HalfBath      <int> 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,...
## $ BedroomAbvGr  <int> 3, 3, 3, 3, 4, 1, 3, 3, 2, 2, 3, 4, 2, 3, 2, 2, 2, 2,...
## $ KitchenAbvGr  <int> 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2,...
## $ KitchenQual   <fct> Gd, TA, Gd, Gd, Gd, TA, Gd, TA, TA, TA, TA, Ex, TA, G...
## $ TotRmsAbvGrd  <int> 8, 6, 6, 7, 9, 5, 7, 7, 8, 5, 5, 11, 4, 7, 5, 5, 5, 6...
## $ Functional    <fct> Typ, Typ, Typ, Typ, Typ, Typ, Typ, Typ, Min1, Typ, Ty...
## $ Fireplaces    <int> 0, 1, 1, 1, 1, 0, 1, 2, 2, 2, 0, 2, 0, 1, 1, 0, 1, 0,...
## $ FireplaceQu   <fct> NA, TA, TA, Gd, TA, NA, Gd, TA, TA, TA, NA, Gd, NA, G...
## $ GarageType    <fct> Attchd, Attchd, Attchd, Detchd, Attchd, Attchd, Attch...
## $ GarageYrBlt   <int> 2003, 1976, 2001, 1998, 2000, 1993, 2004, 1973, 1931,...
## $ GarageFinish  <fct> RFn, RFn, RFn, Unf, RFn, Unf, RFn, RFn, Unf, RFn, Unf...
## $ GarageCars    <int> 2, 2, 2, 3, 3, 2, 2, 2, 2, 1, 1, 3, 1, 3, 1, 2, 2, 2,...
## $ GarageArea    <int> 548, 460, 608, 642, 836, 480, 636, 484, 468, 205, 384...
## $ GarageQual    <fct> TA, TA, TA, TA, TA, TA, TA, TA, Fa, Gd, TA, TA, TA, T...
## $ GarageCond    <fct> TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, TA, T...
## $ PavedDrive    <fct> Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y, Y,...
## $ WoodDeckSF    <int> 0, 298, 0, 0, 192, 40, 255, 235, 90, 0, 0, 147, 140, ...
## $ OpenPorchSF   <int> 61, 0, 42, 35, 84, 30, 57, 204, 0, 4, 0, 21, 0, 33, 2...
## $ EnclosedPorch <int> 0, 0, 0, 272, 0, 0, 0, 228, 205, 0, 0, 0, 0, 0, 176, ...
## $ X3SsnPorch    <int> 0, 0, 0, 0, 0, 320, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
## $ ScreenPorch   <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 0, 0, 0, 0, ...
## $ PoolArea      <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...
## $ PoolQC        <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...
## $ Fence         <fct> NA, NA, NA, NA, NA, MnPrv, NA, NA, NA, NA, NA, NA, NA...
## $ MiscFeature   <fct> NA, NA, NA, NA, NA, Shed, NA, Shed, NA, NA, NA, NA, N...
## $ MiscVal       <int> 0, 0, 0, 0, 0, 700, 0, 350, 0, 0, 0, 0, 0, 0, 0, 0, 7...
## $ MoSold        <int> 2, 5, 9, 2, 12, 10, 8, 11, 4, 1, 2, 7, 9, 8, 5, 7, 3,...
## $ YrSold        <int> 2008, 2007, 2008, 2006, 2008, 2009, 2007, 2009, 2008,...
## $ SaleType      <fct> WD, WD, WD, WD, WD, WD, WD, WD, WD, WD, WD, New, WD, ...
## $ SaleCondition <fct> Normal, Normal, Normal, Abnorml, Normal, Normal, Norm...
## $ SalePrice     <int> 208500, 181500, 223500, 140000, 250000, 143000, 30700...

Khusus yang menggunakan R versi 4.00 keatas, argumen stringsAsFactors = TRUE disertakan agar data yang berbentuk string bisa berubah menjadi factor.

Dalam Tutorial ini, kita hanya akan menggunakan predictor numerik terlebih dahulu.

data_house <- data_house %>% 
  select(-Id)%>% 
  select_if(is.numeric)

Pembagian data Training-Testing

jumlah_baris <- nrow(data_house)
set.seed(123)
index_train <- sample(seq(jumlah_baris),size = 0.8*jumlah_baris)
train_data_house <- data_house[index_train,]
test_data_house <- data_house[-index_train,]

Metode LASSO

Metode pertama yang kita coba adalah metode LASSO. Metode ini bisa dijalankan menggunakan package glmnet. Package glmnetUtils merupakan package tambahan dari glmnet yang memungkinkan syntax glmnet bisa dinput menggunakan object data.frame.

Fungsi cv.glmnet digunakan untuk menjalankan menjalankan metode LASSO, asalkan nilai argumen alpha=1. Kemudian, dengan fungsi ini juga dimungkinkan memilih parameter penalty lambda terbaik dengan menggunakan cross-validation (validasi-silang) yang didasarkan pada kriteria tertentu, contohnya saja kriteria "mse" dan "mae". Proses ini disebut juga sebagai Tuning Parameter. Dimana lambda merupakan parameter yang akan dicari nilai optimalnya berdasarkan nilai kebaikan model "mase" dan "mae". Argumen nfolds digunakan untuk menentukan jumlah folds/lipatan pada cross-validation.

set.seed(123)
model_lasso_mse <- cv.glmnet(SalePrice~.,data=train_data_house,alpha = 1,
                         type.measure="mse",
                         family="gaussian",
                         nfolds=5)
model_lasso_mae <- cv.glmnet(SalePrice~.,data=data_house,alpha = 1,
                         type.measure="mae",
                         family="gaussian",
                         nfolds=5)

Setelah sintaks diatas dijalankan, selanjutnya dimungkinkan untuk membuat plot nilai lambda dalam bentuk log dengan nilai kriteria yang dipilih.

plot(model_lasso_mse)

plot(model_lasso_mae)

Informasi penting yang bisa dilihat pada plot diatas adalah banyaknya peubah terpilih yang ditulis diatas plot. Kemudian ada garis vertikal putus-putus yang menandakan nilai lambda optimum berdasarkan kriteria yang dipilih. Nilai lambda optimum dipilih berdasarkan nilai mse dan mae terkecil. Selain itu, kriteria lambda optimum juga dipilih berdasarkan lembar selang terkecil dari garis pada setiap titik. Sehingga garis vertikal yang satunya menandakan hal tersebut.

Nilai koefisien dari variable terpilih bisa dikeluarkan dengan sintaks dibawah ini. Argumen s pada fungsi coef bisa diisi dengan "lambda.min"(peubah yang terpilih berdasarkan nilai lambda optimal) dan "lambda.1se"(peubah yang terpilih berdasarkan nilai lambda yang memilii selang terkecil).

coef_mse_min <- coef(model_lasso_mse,s = "lambda.min")
coef_mse_min
## 37 x 1 sparse Matrix of class "dgCMatrix"
##                           1
## (Intercept)   -5.883301e+05
## MSSubClass    -5.168126e+01
## LotFrontage    .           
## LotArea        .           
## OverallQual    2.420652e+04
## OverallCond    .           
## YearBuilt      4.864140e+01
## YearRemodAdd   2.116093e+02
## MasVnrArea     9.676808e+00
## BsmtFinSF1     1.135302e+01
## BsmtFinSF2     .           
## BsmtUnfSF      .           
## TotalBsmtSF    7.112597e+00
## X1stFlrSF      7.022706e+00
## X2ndFlrSF      .           
## LowQualFinSF   .           
## GrLivArea      3.594111e+01
## BsmtFullBath   .           
## BsmtHalfBath   .           
## FullBath       .           
## HalfBath       .           
## BedroomAbvGr   .           
## KitchenAbvGr   .           
## TotRmsAbvGrd   .           
## Fireplaces     3.489146e+03
## GarageYrBlt    .           
## GarageCars     1.459427e+04
## GarageArea     5.528539e+00
## WoodDeckSF     3.777160e+00
## OpenPorchSF    .           
## EnclosedPorch  .           
## X3SsnPorch     .           
## ScreenPorch    .           
## PoolArea       .           
## MiscVal        .           
## MoSold         .           
## YrSold         .
coef_mae_min <- coef(model_lasso_mae,s = "lambda.min")
coef_mae_min
## 37 x 1 sparse Matrix of class "dgCMatrix"
##                           1
## (Intercept)   -8.399319e+05
## MSSubClass    -1.460230e+02
## LotFrontage    .           
## LotArea        4.174760e-01
## OverallQual    2.014898e+04
## OverallCond    2.801012e+03
## YearBuilt      1.982961e+02
## YearRemodAdd   1.932217e+02
## MasVnrArea     2.951887e+01
## BsmtFinSF1     1.307127e+01
## BsmtFinSF2     .           
## BsmtUnfSF      .           
## TotalBsmtSF    5.603783e+00
## X1stFlrSF      1.273547e+00
## X2ndFlrSF      .           
## LowQualFinSF   .           
## GrLivArea      4.268969e+01
## BsmtFullBath   6.612093e+03
## BsmtHalfBath   .           
## FullBath       2.740680e+03
## HalfBath       .           
## BedroomAbvGr  -4.737239e+03
## KitchenAbvGr  -1.714647e+04
## TotRmsAbvGrd   2.857109e+03
## Fireplaces     4.312434e+03
## GarageYrBlt    .           
## GarageCars     1.680392e+04
## GarageArea     2.631647e+00
## WoodDeckSF     1.795685e+01
## OpenPorchSF    .           
## EnclosedPorch  .           
## X3SsnPorch     .           
## ScreenPorch    3.625235e+01
## PoolArea      -3.348420e+01
## MiscVal        .           
## MoSold         .           
## YrSold         .
coef_mse_se <- coef(model_lasso_mse,s = "lambda.1se")
coef_mse_se
## 37 x 1 sparse Matrix of class "dgCMatrix"
##                           1
## (Intercept)   -1.129208e+04
## MSSubClass     .           
## LotFrontage    .           
## LotArea        .           
## OverallQual    2.376792e+04
## OverallCond    .           
## YearBuilt      .           
## YearRemodAdd   .           
## MasVnrArea     .           
## BsmtFinSF1     .           
## BsmtFinSF2     .           
## BsmtUnfSF      .           
## TotalBsmtSF    3.114671e-01
## X1stFlrSF      5.698300e+00
## X2ndFlrSF      .           
## LowQualFinSF   .           
## GrLivArea      2.138504e+01
## BsmtFullBath   .           
## BsmtHalfBath   .           
## FullBath       .           
## HalfBath       .           
## BedroomAbvGr   .           
## KitchenAbvGr   .           
## TotRmsAbvGrd   .           
## Fireplaces     .           
## GarageYrBlt    .           
## GarageCars     4.907356e+03
## GarageArea     .           
## WoodDeckSF     .           
## OpenPorchSF    .           
## EnclosedPorch  .           
## X3SsnPorch     .           
## ScreenPorch    .           
## PoolArea       .           
## MiscVal        .           
## MoSold         .           
## YrSold         .
coef_mae_se <- coef(model_lasso_mae,s = "lambda.1se")
coef_mae_se
## 37 x 1 sparse Matrix of class "dgCMatrix"
##                           1
## (Intercept)   -8.085531e+05
## MSSubClass    -1.131442e+02
## LotFrontage    .           
## LotArea        3.445968e-01
## OverallQual    2.159650e+04
## OverallCond    2.419910e+02
## YearBuilt      1.132477e+02
## YearRemodAdd   2.649755e+02
## MasVnrArea     2.662895e+01
## BsmtFinSF1     1.383423e+01
## BsmtFinSF2     .           
## BsmtUnfSF      .           
## TotalBsmtSF    5.772098e+00
## X1stFlrSF      2.585809e+00
## X2ndFlrSF      .           
## LowQualFinSF   .           
## GrLivArea      4.079566e+01
## BsmtFullBath   4.167229e+03
## BsmtHalfBath   .           
## FullBath       .           
## HalfBath       .           
## BedroomAbvGr   .           
## KitchenAbvGr  -1.152123e+04
## TotRmsAbvGrd   7.159270e+02
## Fireplaces     4.270772e+03
## GarageYrBlt    .           
## GarageCars     1.650510e+04
## GarageArea     3.440339e+00
## WoodDeckSF     1.341781e+01
## OpenPorchSF    .           
## EnclosedPorch  .           
## X3SsnPorch     .           
## ScreenPorch    1.506419e+01
## PoolArea       .           
## MiscVal        .           
## MoSold         .           
## YrSold         .

Metode Ridge

Metode kedua yang kita coba adalah metode Ridge. Metode ini pada dasarnya bukan merupakan metode seleksi, namun metode untuk mengatasi multikolinearitas.

model_ridge_mae <- cv.glmnet(SalePrice~.,data=train_data_house,alpha = 0,type.measure="mae",family="gaussian",nfolds=5)
plot(model_ridge_mae)

coef_mae_min_ridge <- coef(model_ridge_mae,s = "lambda.min")
coef_mae_min_ridge
## 37 x 1 sparse Matrix of class "dgCMatrix"
##                           1
## (Intercept)   -8.237211e+05
## MSSubClass    -1.214354e+02
## LotFrontage   -4.574031e+01
## LotArea        2.300521e-01
## OverallQual    1.301566e+04
## OverallCond    3.127520e+03
## YearBuilt      1.672262e+02
## YearRemodAdd   2.743747e+02
## MasVnrArea     2.602720e+01
## BsmtFinSF1     1.064146e+01
## BsmtFinSF2    -9.542542e-01
## BsmtUnfSF      6.198993e-01
## TotalBsmtSF    1.252100e+01
## X1stFlrSF      1.788744e+01
## X2ndFlrSF      1.231750e+01
## LowQualFinSF   1.856550e+01
## GrLivArea      1.852220e+01
## BsmtFullBath   6.751838e+03
## BsmtHalfBath   2.816516e+03
## FullBath       9.353710e+03
## HalfBath       4.623787e+03
## BedroomAbvGr  -5.122754e+03
## KitchenAbvGr  -2.337142e+04
## TotRmsAbvGrd   5.123836e+03
## Fireplaces     8.301955e+03
## GarageYrBlt    8.213071e+01
## GarageCars     1.233860e+04
## GarageArea     2.284099e+01
## WoodDeckSF     2.752769e+01
## OpenPorchSF    1.157703e+01
## EnclosedPorch  3.157537e-01
## X3SsnPorch     3.272470e+01
## ScreenPorch    4.890435e+01
## PoolArea      -5.142969e+01
## MiscVal       -1.997507e+00
## MoSold        -1.153980e+02
## YrSold        -1.249700e+02

Metode Elastic net

Metode ketiga yang kita coba adalah metode Elastic Net. Metode ini merupakan gabungan dari metode Lasso dan juga Ridge.

model_elastic_mae <- cv.glmnet(SalePrice~.,data=train_data_house,alpha = 0.5,type.measure="mae",family="gaussian",nfolds=5)
plot(model_elastic_mae)

coef_mae_min_elastic <- coef(model_ridge_mae,s = "lambda.min")
coef_mae_min_elastic
## 37 x 1 sparse Matrix of class "dgCMatrix"
##                           1
## (Intercept)   -8.237211e+05
## MSSubClass    -1.214354e+02
## LotFrontage   -4.574031e+01
## LotArea        2.300521e-01
## OverallQual    1.301566e+04
## OverallCond    3.127520e+03
## YearBuilt      1.672262e+02
## YearRemodAdd   2.743747e+02
## MasVnrArea     2.602720e+01
## BsmtFinSF1     1.064146e+01
## BsmtFinSF2    -9.542542e-01
## BsmtUnfSF      6.198993e-01
## TotalBsmtSF    1.252100e+01
## X1stFlrSF      1.788744e+01
## X2ndFlrSF      1.231750e+01
## LowQualFinSF   1.856550e+01
## GrLivArea      1.852220e+01
## BsmtFullBath   6.751838e+03
## BsmtHalfBath   2.816516e+03
## FullBath       9.353710e+03
## HalfBath       4.623787e+03
## BedroomAbvGr  -5.122754e+03
## KitchenAbvGr  -2.337142e+04
## TotRmsAbvGrd   5.123836e+03
## Fireplaces     8.301955e+03
## GarageYrBlt    8.213071e+01
## GarageCars     1.233860e+04
## GarageArea     2.284099e+01
## WoodDeckSF     2.752769e+01
## OpenPorchSF    1.157703e+01
## EnclosedPorch  3.157537e-01
## X3SsnPorch     3.272470e+01
## ScreenPorch    4.890435e+01
## PoolArea      -5.142969e+01
## MiscVal       -1.997507e+00
## MoSold        -1.153980e+02
## YrSold        -1.249700e+02

Komparasi Model

pred_lasso <- predict(model_lasso_mae,newdata = test_data_house,na.action=na.pass)
pred_ridge <- predict(model_ridge_mae,newdata = test_data_house,na.action=na.pass)
pred_elastic <- predict(model_elastic_mae,newdata = test_data_house,na.action=na.pass)
y_test <- test_data_house$SalePrice
mae <- function(response,pred){
  mean(abs(response-pred),na.rm = TRUE)
}
mae(y_test,pred_lasso)
## [1] 20180.57
mae(y_test,pred_ridge)
## [1] 20148.01
mae(y_test,pred_elastic)
## [1] 21608.56