Teori Resiko Week 12

Ferdinand Nathaniel

2024-05-05

DataSet

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)

set.seed(123)

n = 831 

start = ymd("2022-01-01")
end = ymd("2024-04-10")
dates = seq(start, end, by = "day")

promotional = runif(n, min = 1000, max = 5000)
price = rnorm(n, 50, 10)
weather = sample(c('sunny', 'cloudy', 'rainy'), size = n , replace = T)

salestrend = 0.1 * seq(1, n)
seasonal = sin(seq(1, n) * 2 * pi / 365 * 7) * 100
noise = rnorm(n, 0, 100)
revenue = 1000 + salestrend + seasonal + noise

simulate = tibble(Date = dates, Promotional_Spending = promotional, Price = price, Weather = weather, Revenue = revenue)

head (simulate)
## # A tibble: 6 × 5
##   Date       Promotional_Spending Price Weather Revenue
##   <date>                    <dbl> <dbl> <chr>     <dbl>
## 1 2022-01-01                2150.  57.5 cloudy    1171.
## 2 2022-01-02                4153.  55.9 cloudy    1056.
## 3 2022-01-03                2636.  51.8 cloudy    1074.
## 4 2022-01-04                4532.  54.0 rainy     1022.
## 5 2022-01-05                4762.  37.0 cloudy    1062.
## 6 2022-01-06                1182.  56.3 rainy      926.

Explorations

Weathers

Pertama, kita mengeksplor data untuk melihat maisng-masing variabel yang akan diteliti.

ggplot(simulate, aes(x = Weather)) +
  geom_bar(fill = "beige") +
  labs(title = "Observasi Cuaca",
       x = "Cuaca",
       y = "Jumlah")

  theme_minimal()
## List of 97
##  $ line                      :List of 6
##   ..$ colour       : chr "black"
##   ..$ linewidth    : num 0.5
##   ..$ linetype     : num 1
##   ..$ lineend      : chr "butt"
##   ..$ arrow        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_line" "element"
##  $ rect                      :List of 5
##   ..$ fill         : chr "white"
##   ..$ colour       : chr "black"
##   ..$ linewidth    : num 0.5
##   ..$ linetype     : num 1
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_rect" "element"
##  $ text                      :List of 11
##   ..$ family       : chr ""
##   ..$ face         : chr "plain"
##   ..$ colour       : chr "black"
##   ..$ size         : num 11
##   ..$ hjust        : num 0.5
##   ..$ vjust        : num 0.5
##   ..$ angle        : num 0
##   ..$ lineheight   : num 0.9
##   ..$ margin       : 'margin' num [1:4] 0points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ title                     : NULL
##  $ aspect.ratio              : NULL
##  $ axis.title                : NULL
##  $ axis.title.x              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 2.75points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.x.top          :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 0
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 2.75points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.x.bottom       : NULL
##  $ axis.title.y              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 1
##   ..$ angle        : num 90
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 2.75points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.y.left         : NULL
##  $ axis.title.y.right        :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 0
##   ..$ angle        : num -90
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 0points 2.75points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text                 :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : chr "grey30"
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x               :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 2.2points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x.top           :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 0
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 2.2points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x.bottom        : NULL
##  $ axis.text.y               :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 1
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 2.2points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.y.left          : NULL
##  $ axis.text.y.right         :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 0
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 0points 2.2points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.ticks                : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ axis.ticks.x              : NULL
##  $ axis.ticks.x.top          : NULL
##  $ axis.ticks.x.bottom       : NULL
##  $ axis.ticks.y              : NULL
##  $ axis.ticks.y.left         : NULL
##  $ axis.ticks.y.right        : NULL
##  $ axis.ticks.length         : 'simpleUnit' num 2.75points
##   ..- attr(*, "unit")= int 8
##  $ axis.ticks.length.x       : NULL
##  $ axis.ticks.length.x.top   : NULL
##  $ axis.ticks.length.x.bottom: NULL
##  $ axis.ticks.length.y       : NULL
##  $ axis.ticks.length.y.left  : NULL
##  $ axis.ticks.length.y.right : NULL
##  $ axis.line                 : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ axis.line.x               : NULL
##  $ axis.line.x.top           : NULL
##  $ axis.line.x.bottom        : NULL
##  $ axis.line.y               : NULL
##  $ axis.line.y.left          : NULL
##  $ axis.line.y.right         : NULL
##  $ legend.background         : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ legend.margin             : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
##   ..- attr(*, "unit")= int 8
##  $ legend.spacing            : 'simpleUnit' num 11points
##   ..- attr(*, "unit")= int 8
##  $ legend.spacing.x          : NULL
##  $ legend.spacing.y          : NULL
##  $ legend.key                : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ legend.key.size           : 'simpleUnit' num 1.2lines
##   ..- attr(*, "unit")= int 3
##  $ legend.key.height         : NULL
##  $ legend.key.width          : NULL
##  $ legend.text               :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.text.align         : NULL
##  $ legend.title              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 0
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.title.align        : NULL
##  $ legend.position           : chr "right"
##  $ legend.direction          : NULL
##  $ legend.justification      : chr "center"
##  $ legend.box                : NULL
##  $ legend.box.just           : NULL
##  $ legend.box.margin         : 'margin' num [1:4] 0cm 0cm 0cm 0cm
##   ..- attr(*, "unit")= int 1
##  $ legend.box.background     : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ legend.box.spacing        : 'simpleUnit' num 11points
##   ..- attr(*, "unit")= int 8
##  $ panel.background          : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ panel.border              : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ panel.spacing             : 'simpleUnit' num 5.5points
##   ..- attr(*, "unit")= int 8
##  $ panel.spacing.x           : NULL
##  $ panel.spacing.y           : NULL
##  $ panel.grid                :List of 6
##   ..$ colour       : chr "grey92"
##   ..$ linewidth    : NULL
##   ..$ linetype     : NULL
##   ..$ lineend      : NULL
##   ..$ arrow        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_line" "element"
##  $ panel.grid.major          : NULL
##  $ panel.grid.minor          :List of 6
##   ..$ colour       : NULL
##   ..$ linewidth    : 'rel' num 0.5
##   ..$ linetype     : NULL
##   ..$ lineend      : NULL
##   ..$ arrow        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_line" "element"
##  $ panel.grid.major.x        : NULL
##  $ panel.grid.major.y        : NULL
##  $ panel.grid.minor.x        : NULL
##  $ panel.grid.minor.y        : NULL
##  $ panel.ontop               : logi FALSE
##  $ plot.background           : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ plot.title                :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 1.2
##   ..$ hjust        : num 0
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 5.5points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.title.position       : chr "panel"
##  $ plot.subtitle             :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 0
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 5.5points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.caption              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : num 1
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 5.5points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.caption.position     : chr "panel"
##  $ plot.tag                  :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 1.2
##   ..$ hjust        : num 0.5
##   ..$ vjust        : num 0.5
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.tag.position         : chr "topleft"
##  $ plot.margin               : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
##   ..- attr(*, "unit")= int 8
##  $ strip.background          : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ strip.background.x        : NULL
##  $ strip.background.y        : NULL
##  $ strip.clip                : chr "inherit"
##  $ strip.placement           : chr "inside"
##  $ strip.text                :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : chr "grey10"
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 4.4points 4.4points 4.4points 4.4points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ strip.text.x              : NULL
##  $ strip.text.x.bottom       : NULL
##  $ strip.text.x.top          : NULL
##  $ strip.text.y              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : num -90
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ strip.text.y.left         :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : num 90
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ strip.text.y.right        : NULL
##  $ strip.switch.pad.grid     : 'simpleUnit' num 2.75points
##   ..- attr(*, "unit")= int 8
##  $ strip.switch.pad.wrap     : 'simpleUnit' num 2.75points
##   ..- attr(*, "unit")= int 8
##  - attr(*, "class")= chr [1:2] "theme" "gg"
##  - attr(*, "complete")= logi TRUE
##  - attr(*, "validate")= logi TRUE

Dapat Dilihat bahwa ketiganya cenderung terdistribusi sama rata jumlahnya.

Daily Revenue

ggplot(simulate, aes(x = Date, y = Revenue)) +
  geom_line(color = "red") +
  labs(title = "Daily Trend",
       x = "Tanggal",
       y = "Revenue") +
  theme_minimal()

Monthly Revenue

newdataset = simulate %>% 
    group_by(month = lubridate::floor_date(Date, 'month')) %>%
    summarize(Average= mean(Revenue))

ggplot(newdataset, aes(x = month, y = Average)) +
  geom_line(color = "red") +
  labs(title = "Monthly Trend",
       x = "Bulan",
       y = "Revenue") +
  theme_minimal()

Secara Bulanan, dapat dilihat bahwa Trwend cenderung naik turunn atau adanya sebuah Seasonal pada Data.

Correlation

cor(simulate[, c(2,3,5)])
##                      Promotional_Spending       Price     Revenue
## Promotional_Spending           1.00000000  0.02653594  0.06869197
## Price                          0.02653594  1.00000000 -0.04170500
## Revenue                        0.06869197 -0.04170500  1.00000000

Terlihat Bahwa Korelasi cenderung Rendah antar variabel numerik.

Regression Model

simulate$Weather = as.factor(simulate$Weather)
regmodel = lm(Revenue ~ Promotional_Spending + Price + Weather , data = simulate)

summary(regmodel)
## 
## Call:
## lm(formula = Revenue ~ Promotional_Spending + Price + Weather, 
##     data = simulate)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -353.07  -87.42    2.86   79.01  337.62 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           1.054e+03  2.465e+01  42.781   <2e-16 ***
## Promotional_Spending  7.874e-03  3.691e-03   2.133   0.0332 *  
## Price                -5.408e-01  4.232e-01  -1.278   0.2016    
## Weatherrainy         -1.845e+01  1.038e+01  -1.777   0.0759 .  
## Weathersunny         -1.143e+01  1.056e+01  -1.083   0.2791    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 121.9 on 826 degrees of freedom
## Multiple R-squared:  0.01044,    Adjusted R-squared:  0.005645 
## F-statistic: 2.178 on 4 and 826 DF,  p-value: 0.06971
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
predictions = predict(regmodel)

actualplot = plot_ly(x = dates, y = revenue, type = 'scatter', mode = 'lines', name = 'Actual') %>%
  add_trace(y =predictions , name ='Predicted') %>%
  layout(title ='Regression Model Forecasting', 
         xaxis = list(title = 'Date'), 
         yaxis = list(title = 'Revenue'))

actualplot

Plot Yang ada menggambarkan bahwa dataset tidak cocok menggunakan data linear. Dibuktikan dengan nilai R-SQuared yang sangatlah rendah. 1.2% saja. Hal ini juga disebabkan oleh penggunaan variabel prediktor yang kurang berpengaruh signifikan terhadap variabel dependen.

Time Series

ACF (Autocorrelation Function) disajikan pada grafik ACF, sumbu horizontal mewakili lag (waktu antara pengamatan), sedangkan sumbu vertikal menunjukkan nilai korelasi antara observasi yang dipisahkan oleh lag tersebut. Puncak-puncak yang signifikan di atas interval abu-abu menunjukkan autocorrelation yang signifikan pada lag-lag tersebut. Dengan menganalisis plot ACF, kita dapat mengidentifikasi pola-pola musiman, tren

Auto-Correlation

simulate$Date <- as.Date(simulate$Date)
ts_data <- ts(simulate$Revenue, start = c(year(simulate$Date[1]), month(simulate$Date[1])), frequency = 12)


acf(ts_data)

plot(acf(ts_data), main = "Autocorrelation Function (ACF)")

Pada grafik diatas, terjadi Autokorelasi diluar batas toleransi autokorelasi yang berarti adanya Seasonal atau Trend yangterjadi pada Data (Data tidak menunjukkan Stasioneritas)

ARIMA Model

library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
simulate$Date <- as.Date(simulate$Date)
ts_data <- ts(simulate$Revenue, start = c(year(simulate$Date[1]), month(simulate$Date[1])), frequency = 12)

arima_model <- auto.arima(ts_data)

summary(arima_model)
## Series: ts_data 
## ARIMA(2,1,2) 
## 
## Coefficients:
##          ar1      ar2      ma1     ma2
##       0.8409  -0.0523  -1.6604  0.7353
## s.e.  0.0834   0.0448   0.0752  0.0636
## 
## sigma^2 = 11965:  log likelihood = -5073
## AIC=10156.01   AICc=10156.08   BIC=10179.62
## 
## Training set error measures:
##                      ME     RMSE      MAE        MPE     MAPE      MASE
## Training set -0.3706182 109.0548 87.14948 -0.9728241 8.543867 0.6587493
##                      ACF1
## Training set -0.001315256

Menggunakan model time Series ARIMA hasil prediksi menghasilkan Eror Rate hanya 8,54% saja dibandingkan dengan R-Squared Model Regresi Linear.

Forecasting

forecast_arima <- forecast(arima_model)

plot_time_series <- plot_ly(x = ~time(ts_data), y = ~as.numeric(ts_data), type = 'scatter', mode = 'lines', name = 'Actual') %>%
  add_trace(x = time(forecast_arima$mean), y = forecast_arima$mean, mode = 'lines', name = 'Predicted (ARIMA)') %>%
  layout(title = 'Actual vs. ARIMA Forecast',
         xaxis = list(title = 'Time'),
         yaxis = list(title = 'Sales Revenue'))

plot_time_series

Decompositioon

library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
decomposed <- decompose(ts_data)

trend <- decomposed$trend
seasonal <- decomposed$seasonal
cycle <- decomposed$random

decomposition_data <- data.frame(Date = time(ts_data), Trend = trend, Seasonal = seasonal, Cycle = cycle)
melted_data <- melt(decomposition_data, id.vars = "Date")
head(melted_data)
##       Date variable value
## 1 2022.000    Trend    NA
## 2 2022.083    Trend    NA
## 3 2022.167    Trend    NA
## 4 2022.250    Trend    NA
## 5 2022.333    Trend    NA
## 6 2022.417    Trend    NA
melted_data = na.omit(melted_data)
ggplot(melted_data, aes(x = Date, y = value, color = variable)) +
  geom_line() +
  facet_wrap(~ variable, scales = "free_y", nrow = 3) +
  labs(title = "Decomposition of Time Series Data",
       x = "Date",
       y = "Value") +
  theme_minimal()

Summary

regression_forecast_values <- predict(regmodel, newdata = simulate)
regression_mape <- (mean((abs(simulate$Revenue - regression_forecast_values)/simulate$Revenue)))

arima_forecast_values <- forecast_arima$mean[1:length(ts_data)]
arima_mape <- mean((abs(as.vector(ts_data) - arima_forecast_values) / as.vector(ts_data)), na.rm = TRUE)

cat("MAPE untuk model regresi:", regression_mape, "\n")
## MAPE untuk model regresi: 0.09618761
cat("MAPE untuk model ARIMA:", arima_mape, "\n")
## MAPE untuk model ARIMA: 0.0740447

Dalam hasil evaluasi model, MAPE (Mean Absolute Percentage Error) digunakan sebagai metrik untuk mengevaluasi kedua model, yaitu model regresi dan model ARIMA, dalam forecasting nilai sales revenue di masa depan. Nilai MAPE yang lebih rendah menunjukkan tingkat kesalahan yang lebih kecil dalam prediksi. Dalam hal ini, model regresi linear memiliki MAPE lebih besar dengan signifikansi yang rendah sehingga model dapayt dikatakan tidak kompeten, sedangkan model ARIMA memiliki MAPE sebesar 7.5% . Dari hasil tersebut, dapat disimpulkan bahwa model regresi memilikitidak valid dalam menghadapi trend dan seasonal dengan model ARIMA, sehingga dapat dianggap lebih baik ARIMA dalam memprediksi sales revenue di masa depan.