library(TTR)
## Warning: package 'TTR' was built under R version 4.2.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Seri Waktu Penjualan Properti Rumah Rata-Rata Pergerakan Harga Median yang dikelompokkan berdasarkan interval triwulan per jenis properti dan jumlah kamar tidur.

1 Baca Data

properti <- read.csv("data_input/ma_lga_12345.csv")
properti
  • Deskripsi Tabel
    • Saledate = tanggal penjualan properti berdasrkan Triwulan
    • MA = Pergerakan rata rata Harga per triwulan
    • type = tyepe properti
    • bedrooms = Jumlah kamar

2 Data Pre-processing

properti_ts <- ts(data = properti$MA,
             start = 2007,
             #end = 2009,
             frequency = 3)

properti_ts
## Time Series:
## Start = c(2007, 1) 
## End = c(2122, 2) 
## Frequency = 3 
##   [1]  441854  441854  441854  441854  451583  440256  442566  446113  440123
##  [10]  442131  459222  456822  457806  459109  460758  464788  467546  470333
##  [19]  470365  469149  465919  463090  451077  451516  454270  456548  469920
##  [28]  472726  475326  478413  478398  477238  477330  479010  482440  486436
##  [37]  489104  491152  494544  498846  504592  506578  507248  506116  504318
##  [46]  506001  496133  500158  510712  421291  421291  421291  421291  416031
##  [55]  419628  423811  426488  437724  444351  449742  457394  466433  474590
##  [64]  483176  491715  498022  503891  507090  507744  507449  507014  506615
##  [73]  506615  506380  505739  505823  506406  508499  512374  516618  522103
##  [82]  528926  534927  542051  549278  556586  564267  572582  581485  590949
##  [91]  601041  609355  615743  619638  622466  624602  626608  628423  630814
## [100]  631875  548969  548969  548969  548969  552484  559580  561852  565467
## [109]  569682  574680  579369  588379  599614  608528  615603  623105  628969
## [118]  634155  636582  637421  635411  633695  634803  633875  634229  635515
## [127]  636687  641125  648174  655757  664635  673762  684006  694800  706711
## [136]  718261  727736  737159  745430  755683  771216  789732  810694  828058
## [145]  836056  837295  830727  820924  811121  803925  791446  735904  735904
## [154]  735904  735904  758340  764025  770046  765555  765515  771280  773355
## [163]  776325  772699  775199  778470  789627  789614  790965  794533  792171
## [172]  800432  804474  807826  812224  805066  805682  811908  820368  843904
## [181]  855039  866489  880625  891981  909131  923594  933589  952327  968331
## [190]  980953  995349 1004117 1010848 1015529 1017752 1007114 1002323  998136
## [199]  995363  970268  326076  326076  326076  326076  327321  324712  323556
## [208]  318922  316914  316751  317711  318695  324778  329856  333049  337144
## [217]  337400  339125  341807  344793  347754  348491  348512  347962  345573
## [226]  343298  341289  338293  336520  334488  332703  330278  328300  326476
## [235]  324725  325127  325521  327870  330319  332481  334804  336637  338105
## [244]  339220  339350  337838  336551  335449  368817  368817  368817  368817
## [253]  373482  377481  382010  380810  385791  391161  396448  402898  408608
## [262]  412509  415991  417970  419777  421158  423144  424673  424249  425453
## [271]  425922  425751  426621  428398  428365  429283  429361  428911  429832
## [280]  431567  432730  432791  432801  431418  430880  430654  430308  429897
## [289]  429059  428878  428532  427856  427623  426970  426936  426669  425659
## [298]  424412  518911  518911  518911  518911  518911  518911  518911  518911
## [307]  523285  522862  524008  535063  538694  555117  550851  547981  539828
## [316]  530987  540344  537592  548326  555644  566706  580696  581428  586470
## [325]  583883  583370  598512  598812  599507  602877  603343  612295  617363
## [334]  622045  616198  610618  606935  605273  606850  604413  604293  603434
## [343]  603281  601167  605637  599339  597884

3 EDA

3.1 Visualisasi autoplot()

library(fpp)
## Warning: package 'fpp' was built under R version 4.2.3
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Loading required package: fma
## Warning: package 'fma' was built under R version 4.2.3
## Loading required package: expsmooth
## Warning: package 'expsmooth' was built under R version 4.2.3
## Loading required package: lmtest
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: tseries
## Warning: package 'tseries' was built under R version 4.2.3
properti_ts %>% 
  autoplot()

properti_decom1 <- properti_ts %>% 
  decompose()

properti_decom1 %>% 
autoplot()

properti_ts
## Time Series:
## Start = c(2007, 1) 
## End = c(2122, 2) 
## Frequency = 3 
##   [1]  441854  441854  441854  441854  451583  440256  442566  446113  440123
##  [10]  442131  459222  456822  457806  459109  460758  464788  467546  470333
##  [19]  470365  469149  465919  463090  451077  451516  454270  456548  469920
##  [28]  472726  475326  478413  478398  477238  477330  479010  482440  486436
##  [37]  489104  491152  494544  498846  504592  506578  507248  506116  504318
##  [46]  506001  496133  500158  510712  421291  421291  421291  421291  416031
##  [55]  419628  423811  426488  437724  444351  449742  457394  466433  474590
##  [64]  483176  491715  498022  503891  507090  507744  507449  507014  506615
##  [73]  506615  506380  505739  505823  506406  508499  512374  516618  522103
##  [82]  528926  534927  542051  549278  556586  564267  572582  581485  590949
##  [91]  601041  609355  615743  619638  622466  624602  626608  628423  630814
## [100]  631875  548969  548969  548969  548969  552484  559580  561852  565467
## [109]  569682  574680  579369  588379  599614  608528  615603  623105  628969
## [118]  634155  636582  637421  635411  633695  634803  633875  634229  635515
## [127]  636687  641125  648174  655757  664635  673762  684006  694800  706711
## [136]  718261  727736  737159  745430  755683  771216  789732  810694  828058
## [145]  836056  837295  830727  820924  811121  803925  791446  735904  735904
## [154]  735904  735904  758340  764025  770046  765555  765515  771280  773355
## [163]  776325  772699  775199  778470  789627  789614  790965  794533  792171
## [172]  800432  804474  807826  812224  805066  805682  811908  820368  843904
## [181]  855039  866489  880625  891981  909131  923594  933589  952327  968331
## [190]  980953  995349 1004117 1010848 1015529 1017752 1007114 1002323  998136
## [199]  995363  970268  326076  326076  326076  326076  327321  324712  323556
## [208]  318922  316914  316751  317711  318695  324778  329856  333049  337144
## [217]  337400  339125  341807  344793  347754  348491  348512  347962  345573
## [226]  343298  341289  338293  336520  334488  332703  330278  328300  326476
## [235]  324725  325127  325521  327870  330319  332481  334804  336637  338105
## [244]  339220  339350  337838  336551  335449  368817  368817  368817  368817
## [253]  373482  377481  382010  380810  385791  391161  396448  402898  408608
## [262]  412509  415991  417970  419777  421158  423144  424673  424249  425453
## [271]  425922  425751  426621  428398  428365  429283  429361  428911  429832
## [280]  431567  432730  432791  432801  431418  430880  430654  430308  429897
## [289]  429059  428878  428532  427856  427623  426970  426936  426669  425659
## [298]  424412  518911  518911  518911  518911  518911  518911  518911  518911
## [307]  523285  522862  524008  535063  538694  555117  550851  547981  539828
## [316]  530987  540344  537592  548326  555644  566706  580696  581428  586470
## [325]  583883  583370  598512  598812  599507  602877  603343  612295  617363
## [334]  622045  616198  610618  606935  605273  606850  604413  604293  603434
## [343]  603281  601167  605637  599339  597884

4 Build Model ARIMA

properti_holts <- HoltWinters(x = properti_ts,
                         gamma = T)

properti_holts
## Holt-Winters exponential smoothing with trend and additive seasonal component.
## 
## Call:
## HoltWinters(x = properti_ts, gamma = T)
## 
## Smoothing parameters:
##  alpha: 0.996756
##  beta : 0
##  gamma: TRUE
## 
## Coefficients:
##           [,1]
## a  596176.6635
## b    1137.4000
## s1   -528.7849
## s2  -1945.2835
## s3   1707.3365
properti_holts2 <-  HoltWinters(x = properti_ts, 
                           alpha = 0.8,
                           beta = 0.2,
                           gamma = T)
properti_holts3 <- HoltWinters(x = properti_ts, 
                           alpha = 0.7,
                           beta = 0.05,
                           gamma = T)

#Model Evaluation

properti_holts$fitted %>% 
  head()
## Time Series:
## Start = c(2008, 1) 
## End = c(2009, 3) 
## Frequency = 3 
##              xhat    level  trend      season
## 2008.000 438304.5 440498.8 1137.4 -3331.77778
## 2008.333 449732.2 445174.3 1137.4  3420.55556
## 2008.667 449205.1 448156.4 1137.4   -88.77778
## 2009.000 438190.9 440373.8 1137.4 -3320.26309
## 2009.333 450436.0 445872.1 1137.4  3426.55947
## 2009.667 443720.1 442700.5 1137.4  -117.80846

Nilai hasil prediksi hanya tertampung pada variabel xhat, maka dari itu kita hanya akan mengambil nilai pada variabel tersebut saja.

properti_holts2$fitted[,1] %>% 
  head()
## Time Series:
## Start = c(2008, 1) 
## End = c(2009, 3) 
## Frequency = 3 
## [1] 438304.5 449601.8 449699.7 440123.0 449418.3 441353.1
accuracy(properti_holts$fitted[,1], properti_ts)
##                ME     RMSE      MAE        MPE     MAPE       ACF1 Theil's U
## Test set -687.077 36628.84 8087.364 -0.4175741 1.714465 0.05037177  1.015088
accuracy(properti_holts2$fitted[,1], properti_ts)
##                 ME     RMSE      MAE        MPE     MAPE       ACF1 Theil's U
## Test set -37.46197 41087.86 10516.06 0.02278259 2.496971 0.09806313   1.70262
accuracy(properti_holts3$fitted[,1], properti_ts)
##                ME     RMSE      MAE         MPE     MAPE      ACF1 Theil's U
## Test set 17.50677 41653.45 11704.52 -0.06116779 2.751971 0.2357503  1.699795
properti_ts %>% 
  autoplot() +
  autolayer(properti_holts$fitted[,1]) +
  autolayer(properti_holts2$fitted[,1]) +
  autolayer(properti_holts3$fitted[,1]) 

5 Holt’s Winter Exponential

merupakan metode forecasting yang tepat digunakan untuk data yang memiliki efek trend dan seasonal

5.1 cross Validation

# Data Train
properti_train <- head(properti_ts, -24)

# Data Test
properti_test <- tail(properti_ts, 24)

5.2 Membuat Model

membuat model dengan data properti train dan seasenal multiplikatif

properti_hw <- HoltWinters(x = properti_train,
                            seasonal = "multiplicative")

properti_hw
## Holt-Winters exponential smoothing with trend and multiplicative seasonal component.
## 
## Call:
## HoltWinters(x = properti_train, seasonal = "multiplicative")
## 
## Smoothing parameters:
##  alpha: 0.9986851
##  beta : 0
##  gamma: 1
## 
## Coefficients:
##            [,1]
## a  5.780988e+05
## b  1.137400e+03
## s1 9.986007e-01
## s2 9.937442e-01
## s3 1.005759e+00

5.3 Melakukan forecast masa depan

properti_forecast <- forecast(object = properti_hw, 
                               h = 50) # nilai prediksi yang akan di tentukan dalam bulan hari atau tahun
properti_forecast
##          Point Forecast    Lo 80    Hi 80    Lo 95     Hi 95
## 2114.667       578425.7 529657.8 627193.6 503841.6  653009.8
## 2115.000       576742.9 507987.2 645498.7 471590.1  681895.8
## 2115.333       584859.9 515318.8 654400.9 478506.0  691213.7
## 2115.667       581833.1 497264.1 666402.2 452495.9  711170.3
## 2116.000       580133.8 482898.4 677369.1 431425.2  728842.4
## 2116.333       588291.7 489945.8 686637.6 437884.7  738698.7
## 2116.667       585240.5 476036.4 694444.7 418227.3  752253.8
## 2117.000       583524.6 464436.2 702613.1 401394.5  765654.7
## 2117.333       591723.6 471275.0 712172.1 407513.3  775933.8
## 2117.667       588648.0 459422.8 717873.2 391015.1  786280.8
## 2118.000       586915.5 449404.0 724427.0 376609.7  797221.2
## 2118.333       595155.4 456073.3 734237.5 382447.8  807863.0
## 2118.667       592055.4 445519.5 738591.3 367948.1  816162.7
## 2119.000       590306.3 436563.8 744048.9 355177.4  825435.3
## 2119.333       598587.3 443088.8 754085.7 360772.9  836401.6
## 2119.667       595462.8 433455.5 757470.2 347694.0  843231.7
## 2120.000       593697.2 425280.6 762113.7 336126.3  851268.1
## 2120.333       602019.1 431679.1 772359.1 341506.5  862531.7
## 2120.667       598870.2 422745.3 774995.2 329510.4  868230.1
## 2121.000       597088.0 415177.4 778998.7 318879.7  875296.4
## 2121.333       605451.0 421462.7 789439.2 324065.2  886836.7
## 2121.667       602277.7 413085.7 791469.6 312933.5  891621.8
## 2122.000       600478.9 406008.2 794949.6 303061.7  897896.1
## 2122.333       608882.8 412191.1 805574.5 308068.8  909696.8
## 2122.667       605685.1 404272.1 807098.1 297650.4  913719.8
## 2123.000       603869.8 397602.5 810137.0 288411.1  919328.4
## 2123.333       612314.7 403691.6 820937.7 293253.2  931376.1
## 2123.667       609092.5 396158.7 822026.4 283438.3  934746.8
## 2124.000       607260.6 389835.8 824685.4 274738.0  939783.2
## 2124.333       615746.5 395838.5 835654.5 279426.2  952066.8
## 2124.667       612499.9 388637.4 836362.5 270131.7  954868.2
## 2125.000       610651.5 382614.4 838688.5 261898.8  959404.1
## 2125.333       619178.4 388536.9 849819.8 266442.6  971914.1
## 2125.667       615907.4 381625.4 850189.4 257604.0  974210.8
## 2126.000       614042.3 375865.4 852219.3 249782.1  978302.5
## 2126.333       622610.2 381713.0 863507.4 254189.8  991030.6
## 2126.667       619314.8 375057.4 863572.2 245755.4  992874.2
## 2127.000       617433.2 369530.7 865335.6 238299.1  996567.2
## 2127.333       626042.1 375308.3 876775.8 242577.9 1009506.2
## 2127.667       622722.2 368881.2 876563.3 234505.8 1010938.6
## 2128.000       620824.0 363563.5 878084.5 227378.0 1014270.0
## 2128.333       629473.9 369275.2 889672.6 231534.3 1027413.5
## 2128.667       626129.7 363053.8 889205.5 223789.9 1028469.4
## 2129.000       624214.9 357924.9 890504.8 216959.6 1031470.2
## 2129.333       632905.8 363574.5 902237.0 220999.2 1044812.3
## 2129.667       629537.1 357539.8 901534.3 213553.2 1045521.0
## 2130.000       627605.7 352582.7 902628.8 206994.2 1048217.2
## 2130.333       636337.6 358173.5 914501.7 210922.3 1061752.9
## 2130.667       632944.5 352309.3 913579.7 203750.0 1062139.0
## 2131.000       630996.6 347509.3 914483.9 197440.2 1064553.0

5.4 Evaluasi Hasil Forecast

accuracy(properti_forecast$mean, properti_test)
##                ME     RMSE      MAE      MPE     MAPE      ACF1 Theil's U
## Test set 11130.32 15310.29 12623.03 1.829348 2.079165 0.6582717  3.170081
  • Dari Hasil evaluasi berdasrkan akurasi pada data test di dapatkan hasil MAE = 12623.03

6 Model SARIMA*

auto_arima <- auto.arima(y = properti_ts)
accuracy(auto_arima)
##                    ME     RMSE      MAE        MPE     MAPE      MASE
## Training set 450.9275 36269.57 7122.507 -0.1888173 1.498312 0.3587781
##                    ACF1
## Training set 0.05290092

6.1 No-autocorrelation residual

yang diinginkan p-value > 0.05 (alpha), no-autocorrelation dengan mengguakan Ljung-Box test

# menggunakan Ljung-Box test
Box.test(x = auto_arima$residuals, type = "Ljung-Box")
## 
##  Box-Ljung test
## 
## data:  auto_arima$residuals
## X-squared = 0.9795, df = 1, p-value = 0.3223

hasil test tersebut masih terdapat autokorelasi, maka kita akan melakukan tuning kembali terhadap model

6.2 Normality of residual

Untuk mengecek normality residual pada hasil forecasting time series kita bisa melakukan uji normality (shapiro test) dengan menggunakan fungsi shapiro.test(residual model). Hipotesis yang digunakan yaitu:

H_0: residual menyebar normal H_1: residual tidak menyebar normal yang diinginkan p-value > 0.05 (alpha), residual menyebar normal

shapiro.test(auto_arima$residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  auto_arima$residuals
## W = 0.14208, p-value < 2.2e-16

Dari uji Shapiro menunjukan hasil = 1.923 berarti residual menyebar normal