library(rlang)
library('wavelets')
library('readxl')
library('ggplot2')
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
df <- read_excel("C:/Users/LENOVO/Downloads/Data Laprak 3 ARW.xlsx")
data <- df$Penumpang
data
##   [1] 11504 11705 13066 12657 13312 12929 14087 12991 13183 13949 13369 13344
##  [13] 13647 10759 13167 14189 14939 14820 16092 15144 14792 15465 14141 14766
##  [25] 14697 14116 15756 15435 16054 16636 17463 16769 15604 16901 15696 15011
##  [37] 14246 13593 16826 16458 17467 17746 17959 17204 16840 17438 16434 17211
##  [49] 17039 14880 16617 16156 16565 16808 17181 16140 16713 16542 16088 17299
##  [61] 16446 14519 16584 16031 17018 16806 17632 14492 16353 16062 15765 16333
##  [73] 15801 15126 16701 16376 17401 17687 17956 16675 16063 16828 15436 15745
##  [85] 14573 14315 15521 15724 15795 16932 19917 19031 19439 20198 19578 20992
##  [97] 20698 19628 22427 21502 22547 23415 22125 22763 23219 24503 23986 25791
## [109] 24254 22394 26841 26150 27450 27118 27077 27351 27125 28280 27253 29328
## [121] 27886 26058 28156 28000 30176 28730 28216 29125 29019 29765 29178 31530
## [133] 30359 26837 31612 30934 33157 30181 33669 33255 31921 34498 33798 36140
## [145] 34107 30721 35272 35135 34877 32270 36089 34560 33878 35602 34637 37197
## [157] 34435 31282 35068 35106 34514 34261 38303 34542 34615 35814 35228 36710
# j4 aj1 before
modwt_timeseries <- function (x,w='haar',j=4) { 
  n=length(x) 
  x.modwt=modwt(x,w,j) 
  d1=x.modwt@W$W1 
  d2=x.modwt@W$W2 
  d3=x.modwt@W$W3 
  d4=x.modwt@W$W4 
  v4=x.modwt@V$V4 
  x1<-x2<-x3<-x4<-x5<-NULL 
  for (i in 1:(n-17)){ 
    x1<-c(x1,d1[i+16]) 
    x2<-c(x2,d2[i+16]) 
    x3<-c(x3,d3[i+16]) 
    x4<-c(x4,d4[i+16]) 
    x5<-c(x5,v4[i+16]) 
  } 
  y=x[18:n] 
  lm.y=lm(y~-1+x1+x2+x3+x4+x5) 
  koef<-lm.y$coeff 
  pred<-c(rep(0,17), lm.y$fitted)
  print(summary(lm.y))
  return(lm.y) 
}

modwt_timeseries(data)
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4610.9  -701.6   105.1   895.5  3589.5 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x1  0.08914    0.13917   0.641    0.523    
## x2  0.27084    0.18862   1.436    0.153    
## x3  0.85414    0.21281   4.014 9.52e-05 ***
## x4  1.00226    0.18467   5.427 2.33e-07 ***
## x5  1.01579    0.00686 148.074  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1356 on 146 degrees of freedom
## Multiple R-squared:  0.9969, Adjusted R-squared:  0.9968 
## F-statistic:  9518 on 5 and 146 DF,  p-value: < 2.2e-16
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5)
## 
## Coefficients:
##      x1       x2       x3       x4       x5  
## 0.08914  0.27084  0.85414  1.00227  1.01579
data_plot <- data.frame(
  n = c(1:length(data)),
  Data = data,
  Prediksi = c(rep(0, 17), modwt_timeseries(data)$fitted)
)
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4610.9  -701.6   105.1   895.5  3589.5 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x1  0.08914    0.13917   0.641    0.523    
## x2  0.27084    0.18862   1.436    0.153    
## x3  0.85414    0.21281   4.014 9.52e-05 ***
## x4  1.00226    0.18467   5.427 2.33e-07 ***
## x5  1.01579    0.00686 148.074  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1356 on 146 degrees of freedom
## Multiple R-squared:  0.9969, Adjusted R-squared:  0.9968 
## F-statistic:  9518 on 5 and 146 DF,  p-value: < 2.2e-16
data_plot %>% ggplot(aes(x=n)) + 
  geom_line(aes(y=Data), color='black') + 
  geom_line(aes(y=Prediksi), color='red') + 
  
  labs(title = "Prediksi vs Data Asli")

# j4 aj1 after
modwt_timeseries <- function (x,w='haar',j=4) { 
  n=length(x) 
  x.modwt=modwt(x,w,j) 
  d1=x.modwt@W$W1 
  d2=x.modwt@W$W2 
  d3=x.modwt@W$W3 
  d4=x.modwt@W$W4 
  v4=x.modwt@V$V4 
  x1<-x2<-x3<-x4<-x5<-NULL 
  for (i in 1:(n-17)){ 
    x1<-c(x1,d1[i+16]) 
    x2<-c(x2,d2[i+16]) 
    x3<-c(x3,d3[i+16]) 
    x4<-c(x4,d4[i+16]) 
    x5<-c(x5,v4[i+16]) 
  } 
  y=x[18:n] 
  lm.y=lm(y~-1+x3+x4+x5) 
  koef<-lm.y$coeff 
  pred<-c(rep(0,17), lm.y$fitted)
  print(summary(lm.y))
  return(lm.y) 
}

modwt_timeseries(data)
## 
## Call:
## lm(formula = y ~ -1 + x3 + x4 + x5)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4715.9  -643.9    62.9  1006.3  3699.1 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x3 0.920944   0.208751   4.412 1.96e-05 ***
## x4 1.010520   0.184870   5.466 1.91e-07 ***
## x5 1.016705   0.006846 148.515  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1359 on 148 degrees of freedom
## Multiple R-squared:  0.9969, Adjusted R-squared:  0.9968 
## F-statistic: 1.581e+04 on 3 and 148 DF,  p-value: < 2.2e-16
## 
## Call:
## lm(formula = y ~ -1 + x3 + x4 + x5)
## 
## Coefficients:
##     x3      x4      x5  
## 0.9209  1.0105  1.0167
data_plot <- data.frame(
  n = c(1:length(data)),
  Data = data,
  Prediksi = c(rep(0, 17), modwt_timeseries(data)$fitted)
)
## 
## Call:
## lm(formula = y ~ -1 + x3 + x4 + x5)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4715.9  -643.9    62.9  1006.3  3699.1 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x3 0.920944   0.208751   4.412 1.96e-05 ***
## x4 1.010520   0.184870   5.466 1.91e-07 ***
## x5 1.016705   0.006846 148.515  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1359 on 148 degrees of freedom
## Multiple R-squared:  0.9969, Adjusted R-squared:  0.9968 
## F-statistic: 1.581e+04 on 3 and 148 DF,  p-value: < 2.2e-16
data_plot %>% ggplot(aes(x=n)) + 
  geom_line(aes(y=Data), color='black') + 
  geom_line(aes(y=Prediksi), color='red') + 
  
  labs(title = "Prediksi vs Data Asli")

# j4 aj2 before
modwt_timeseries <- function (x,w='haar',j=4) { 
  n=length(x) 
  x.modwt=modwt(x,w,j) 
  d1=x.modwt@W$W1 
  d2=x.modwt@W$W2 
  d3=x.modwt@W$W3 
  d4=x.modwt@W$W4 
  v4=x.modwt@V$V4 
  x1<-x2<-x3<-x4<-x5<-x6<-x7<-x8<-x9<-x10<-NULL 
  for (i in 1:(n-17)){ 
    x1<-c(x1,d1[i+16]) 
    x2<-c(x2,d1[i+14]) 
    x3<-c(x3,d2[i+16]) 
    x4<-c(x4,d2[i+12]) 
    x5<-c(x5,d3[i+16]) 
    x6<-c(x6,d3[i+8]) 
    x7<-c(x7,d4[i+16]) 
    x8<-c(x8,d4[i]) 
    x9<-c(x9,v4[i+16]) 
    x10<-c(x10,v4[i]) 
  } 
  y=x[18:n] 
  lm.y=lm(y~-1+x1+x2+x3+x4+x5+x6+x7+x8+x9+x10) 
  koef<-lm.y$coeff 
  pred<-c(rep(0,17), lm.y$fitted)
  print(summary(lm.y))
  return(lm.y) 
}

modwt_timeseries(data)
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + 
##     x9 + x10)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3963.4  -615.4   -30.4   691.6  3427.8 
## 
## Coefficients:
##     Estimate Std. Error t value Pr(>|t|)    
## x1   0.10018    0.13143   0.762 0.447190    
## x2   0.16941    0.13134   1.290 0.199233    
## x3   0.20490    0.17900   1.145 0.254283    
## x4  -0.10849    0.18581  -0.584 0.560219    
## x5   1.15227    0.21259   5.420 2.51e-07 ***
## x6   0.97363    0.21648   4.498 1.42e-05 ***
## x7   0.75613    0.20392   3.708 0.000299 ***
## x8   0.05251    0.07375   0.712 0.477641    
## x9   0.97197    0.03799  25.588  < 2e-16 ***
## x10  0.03568    0.03822   0.933 0.352237    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1274 on 141 degrees of freedom
## Multiple R-squared:  0.9974, Adjusted R-squared:  0.9972 
## F-statistic:  5392 on 10 and 141 DF,  p-value: < 2.2e-16
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + 
##     x9 + x10)
## 
## Coefficients:
##       x1        x2        x3        x4        x5        x6        x7        x8  
##  0.10018   0.16941   0.20490  -0.10849   1.15227   0.97363   0.75613   0.05251  
##       x9       x10  
##  0.97197   0.03568
data_plot <- data.frame(
  n = c(1:length(data)),
  Data = data,
  Prediksi = c(rep(0, 17), modwt_timeseries(data)$fitted)
)
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + 
##     x9 + x10)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3963.4  -615.4   -30.4   691.6  3427.8 
## 
## Coefficients:
##     Estimate Std. Error t value Pr(>|t|)    
## x1   0.10018    0.13143   0.762 0.447190    
## x2   0.16941    0.13134   1.290 0.199233    
## x3   0.20490    0.17900   1.145 0.254283    
## x4  -0.10849    0.18581  -0.584 0.560219    
## x5   1.15227    0.21259   5.420 2.51e-07 ***
## x6   0.97363    0.21648   4.498 1.42e-05 ***
## x7   0.75613    0.20392   3.708 0.000299 ***
## x8   0.05251    0.07375   0.712 0.477641    
## x9   0.97197    0.03799  25.588  < 2e-16 ***
## x10  0.03568    0.03822   0.933 0.352237    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1274 on 141 degrees of freedom
## Multiple R-squared:  0.9974, Adjusted R-squared:  0.9972 
## F-statistic:  5392 on 10 and 141 DF,  p-value: < 2.2e-16
data_plot %>% ggplot(aes(x=n)) + 
  geom_line(aes(y=Data), color='black') + 
  geom_line(aes(y=Prediksi), color='red') + 
  
  labs(title = "Prediksi vs Data Asli")

# j4 aj2 after
modwt_timeseries <- function (x,w='haar',j=4) { 
  n=length(x) 
  x.modwt=modwt(x,w,j) 
  d1=x.modwt@W$W1 
  d2=x.modwt@W$W2 
  d3=x.modwt@W$W3 
  d4=x.modwt@W$W4 
  v4=x.modwt@V$V4 
  x1<-x2<-x3<-x4<-x5<-x6<-x7<-x8<-x9<-x10<-NULL 
  for (i in 1:(n-17)){ 
    x1<-c(x1,d1[i+16]) 
    x2<-c(x2,d1[i+14]) 
    x3<-c(x3,d2[i+16]) 
    x4<-c(x4,d2[i+12]) 
    x5<-c(x5,d3[i+16]) 
    x6<-c(x6,d3[i+8]) 
    x7<-c(x7,d4[i+16]) 
    x8<-c(x8,d4[i]) 
    x9<-c(x9,v4[i+16]) 
    x10<-c(x10,v4[i]) 
  } 
  y=x[18:n] 
  lm.y=lm(y~-1+x5+x6+x7+x9) 
  koef<-lm.y$coeff 
  pred<-c(rep(0,17), lm.y$fitted)
  print(summary(lm.y))
  return(lm.y) 
}

modwt_timeseries(data)
## 
## Call:
## lm(formula = y ~ -1 + x5 + x6 + x7 + x9)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4192.1  -671.4    21.6   788.9  3536.2 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x5 1.226033   0.206003   5.952 1.87e-08 ***
## x6 0.997206   0.213116   4.679 6.48e-06 ***
## x7 0.677824   0.187094   3.623 0.000401 ***
## x9 1.007587   0.006698 150.429  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1272 on 147 degrees of freedom
## Multiple R-squared:  0.9973, Adjusted R-squared:  0.9972 
## F-statistic: 1.354e+04 on 4 and 147 DF,  p-value: < 2.2e-16
## 
## Call:
## lm(formula = y ~ -1 + x5 + x6 + x7 + x9)
## 
## Coefficients:
##     x5      x6      x7      x9  
## 1.2260  0.9972  0.6778  1.0076
data_plot <- data.frame(
  n = c(1:length(data)),
  Data = data,
  Prediksi = c(rep(0, 17), modwt_timeseries(data)$fitted)
)
## 
## Call:
## lm(formula = y ~ -1 + x5 + x6 + x7 + x9)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4192.1  -671.4    21.6   788.9  3536.2 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x5 1.226033   0.206003   5.952 1.87e-08 ***
## x6 0.997206   0.213116   4.679 6.48e-06 ***
## x7 0.677824   0.187094   3.623 0.000401 ***
## x9 1.007587   0.006698 150.429  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1272 on 147 degrees of freedom
## Multiple R-squared:  0.9973, Adjusted R-squared:  0.9972 
## F-statistic: 1.354e+04 on 4 and 147 DF,  p-value: < 2.2e-16
data_plot %>% ggplot(aes(x=n)) + 
  geom_line(aes(y=Data), color='black') + 
  geom_line(aes(y=Prediksi), color='red') + 
  
  labs(title = "Prediksi vs Data Asli")

# j5 aj1 before
modwt_timeseries <- function (x,w='haar',j=5) { 
  n=length(x) 
  x.modwt=modwt(x,w,j) 
  d1=x.modwt@W$W1 
  d2=x.modwt@W$W2 
  d3=x.modwt@W$W3 
  d4=x.modwt@W$W4 
  d5=x.modwt@W$W5 
  v5=x.modwt@V$V5 
  x1<-x2<-x3<-x4<-x5<-x6<-NULL 
  for (i in 1:(n-33)){ 
    x1<-c(x1,d1[i+32]) 
    x2<-c(x2,d2[i+32]) 
    x3<-c(x3,d3[i+32]) 
    x4<-c(x4,d4[i+32]) 
    x5<-c(x5,d5[i+32]) 
    x6<-c(x6,v5[i+32]) 
  } 
  y=x[34:n] 
  lm.y=lm(y~-1+x1+x2+x3+x4+x5+x6) 
  koef<-lm.y$coeff 
  pred<-c(rep(0,33), lm.y$fitted)
  print(summary(lm.y))
  return(lm.y) 
}

modwt_timeseries(data)
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5 + x6)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4652.9  -747.5    95.4   839.2  3664.1 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x1 0.078220   0.146313   0.535 0.593843    
## x2 0.204330   0.201529   1.014 0.312528    
## x3 0.853323   0.238098   3.584 0.000479 ***
## x4 0.943103   0.232272   4.060 8.45e-05 ***
## x5 1.137435   0.172344   6.600 9.69e-10 ***
## x6 1.010127   0.009692 104.220  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1406 on 129 degrees of freedom
## Multiple R-squared:  0.997,  Adjusted R-squared:  0.9968 
## F-statistic:  7059 on 6 and 129 DF,  p-value: < 2.2e-16
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5 + x6)
## 
## Coefficients:
##      x1       x2       x3       x4       x5       x6  
## 0.07822  0.20433  0.85332  0.94310  1.13743  1.01013
data_plot <- data.frame(
  n = c(1:length(data)),
  Data = data,
  Prediksi = c(rep(0, 33), modwt_timeseries(data)$fitted)
)
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5 + x6)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4652.9  -747.5    95.4   839.2  3664.1 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x1 0.078220   0.146313   0.535 0.593843    
## x2 0.204330   0.201529   1.014 0.312528    
## x3 0.853323   0.238098   3.584 0.000479 ***
## x4 0.943103   0.232272   4.060 8.45e-05 ***
## x5 1.137435   0.172344   6.600 9.69e-10 ***
## x6 1.010127   0.009692 104.220  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1406 on 129 degrees of freedom
## Multiple R-squared:  0.997,  Adjusted R-squared:  0.9968 
## F-statistic:  7059 on 6 and 129 DF,  p-value: < 2.2e-16
data_plot %>% ggplot(aes(x=n)) + 
  geom_line(aes(y=Data), color='black') + 
  geom_line(aes(y=Prediksi), color='red') + 
  
  labs(title = "Prediksi vs Data Asli")

# j5 aj1 after
modwt_timeseries <- function (x,w='haar',j=5) { 
  n=length(x) 
  x.modwt=modwt(x,w,j) 
  d1=x.modwt@W$W1 
  d2=x.modwt@W$W2 
  d3=x.modwt@W$W3 
  d4=x.modwt@W$W4 
  d5=x.modwt@W$W5 
  v5=x.modwt@V$V5 
  x1<-x2<-x3<-x4<-x5<-x6<-NULL 
  for (i in 1:(n-33)){ 
    x1<-c(x1,d1[i+32]) 
    x2<-c(x2,d2[i+32]) 
    x3<-c(x3,d3[i+32]) 
    x4<-c(x4,d4[i+32]) 
    x5<-c(x5,d5[i+32]) 
    x6<-c(x6,v5[i+32]) 
  } 
  y=x[34:n] 
  lm.y=lm(y~-1+x3+x4+x5+x6) 
  koef<-lm.y$coeff 
  pred<-c(rep(0,33), lm.y$fitted)
  print(summary(lm.y))
  return(lm.y) 
}

modwt_timeseries(data)
## 
## Call:
## lm(formula = y ~ -1 + x3 + x4 + x5 + x6)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4744.3  -716.0    17.1   883.0  3758.8 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x3 0.902901   0.233323   3.870 0.000171 ***
## x4 0.951328   0.231523   4.109 6.96e-05 ***
## x5 1.140659   0.171856   6.637 7.70e-10 ***
## x6 1.010570   0.009659 104.622  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1402 on 131 degrees of freedom
## Multiple R-squared:  0.9969, Adjusted R-squared:  0.9968 
## F-statistic: 1.064e+04 on 4 and 131 DF,  p-value: < 2.2e-16
## 
## Call:
## lm(formula = y ~ -1 + x3 + x4 + x5 + x6)
## 
## Coefficients:
##     x3      x4      x5      x6  
## 0.9029  0.9513  1.1407  1.0106
data_plot <- data.frame(
  n = c(1:length(data)),
  Data = data,
  Prediksi = c(rep(0, 33), modwt_timeseries(data)$fitted)
)
## 
## Call:
## lm(formula = y ~ -1 + x3 + x4 + x5 + x6)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4744.3  -716.0    17.1   883.0  3758.8 
## 
## Coefficients:
##    Estimate Std. Error t value Pr(>|t|)    
## x3 0.902901   0.233323   3.870 0.000171 ***
## x4 0.951328   0.231523   4.109 6.96e-05 ***
## x5 1.140659   0.171856   6.637 7.70e-10 ***
## x6 1.010570   0.009659 104.622  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1402 on 131 degrees of freedom
## Multiple R-squared:  0.9969, Adjusted R-squared:  0.9968 
## F-statistic: 1.064e+04 on 4 and 131 DF,  p-value: < 2.2e-16
data_plot %>% ggplot(aes(x=n)) + 
  geom_line(aes(y=Data), color='black') + 
  geom_line(aes(y=Prediksi), color='red') + 
  
  labs(title = "Prediksi vs Data Asli")

# j5 aj2 before
modwt_timeseries <- function (x,w='haar',j=5) { 
  n=length(x) 
  x.modwt=modwt(x,w,j) 
  d1=x.modwt@W$W1 
  d2=x.modwt@W$W2 
  d3=x.modwt@W$W3 
  d4=x.modwt@W$W4 
  d5=x.modwt@W$W5 
  v5=x.modwt@V$V5 
  x1<-x2<-x3<-x4<-x5<-x6<-x7<-x8<-x9<-x10 <- x11 <- x12<-NULL 
  for (i in 1:(n-33)){ 
    x1<-c(x1,d1[i+32]) 
    x2<-c(x2,d1[i+30]) 
    x3<-c(x3,d2[i+32]) 
    x4<-c(x4,d2[i+28]) 
    x5<-c(x5,d3[i+32]) 
    x6<-c(x6,d3[i+24]) 
    x7<-c(x7,d4[i+32]) 
    x8<-c(x8,d4[i+16]) 
    x9<-c(x9,d5[i+32]) 
    x10<-c(x10,d5[i]) 
    x11<-c(x11,v5[i+32]) 
    x12<-c(x12,v5[i]) 
  } 
  y=x[34:n] 
  lm.y=lm(y~-1+x1+x2+x3+x4+x5+x6+x7+x8+x9+x10+x11+x12) 
  koef<-lm.y$coeff 
  pred<-c(rep(0,33), lm.y$fitted)
  print(summary(lm.y))
  return(lm.y) 
}

modwt_timeseries(data)
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + 
##     x9 + x10 + x11 + x12)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3996.5  -539.3    18.6   681.6  3449.0 
## 
## Coefficients:
##      Estimate Std. Error t value Pr(>|t|)    
## x1   0.104178   0.140015   0.744 0.458268    
## x2   0.155851   0.139973   1.113 0.267690    
## x3   0.200672   0.192753   1.041 0.299878    
## x4  -0.101963   0.203299  -0.502 0.616886    
## x5   1.296472   0.253750   5.109  1.2e-06 ***
## x6   1.025869   0.261367   3.925 0.000143 ***
## x7   0.825482   0.239412   3.448 0.000774 ***
## x8   0.285650   0.239050   1.195 0.234410    
## x9   0.744673   0.210185   3.543 0.000560 ***
## x10  0.029890   0.052964   0.564 0.573547    
## x11  0.999291   0.027677  36.106  < 2e-16 ***
## x12  0.009052   0.026930   0.336 0.737348    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1339 on 123 degrees of freedom
## Multiple R-squared:  0.9974, Adjusted R-squared:  0.9971 
## F-statistic:  3894 on 12 and 123 DF,  p-value: < 2.2e-16
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + 
##     x9 + x10 + x11 + x12)
## 
## Coefficients:
##        x1         x2         x3         x4         x5         x6         x7  
##  0.104178   0.155851   0.200672  -0.101963   1.296472   1.025869   0.825482  
##        x8         x9        x10        x11        x12  
##  0.285650   0.744673   0.029890   0.999291   0.009052
data_plot <- data.frame(
  n = c(1:length(data)),
  Data = data,
  Prediksi = c(rep(0, 33), modwt_timeseries(data)$fitted)
)
## 
## Call:
## lm(formula = y ~ -1 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + 
##     x9 + x10 + x11 + x12)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3996.5  -539.3    18.6   681.6  3449.0 
## 
## Coefficients:
##      Estimate Std. Error t value Pr(>|t|)    
## x1   0.104178   0.140015   0.744 0.458268    
## x2   0.155851   0.139973   1.113 0.267690    
## x3   0.200672   0.192753   1.041 0.299878    
## x4  -0.101963   0.203299  -0.502 0.616886    
## x5   1.296472   0.253750   5.109  1.2e-06 ***
## x6   1.025869   0.261367   3.925 0.000143 ***
## x7   0.825482   0.239412   3.448 0.000774 ***
## x8   0.285650   0.239050   1.195 0.234410    
## x9   0.744673   0.210185   3.543 0.000560 ***
## x10  0.029890   0.052964   0.564 0.573547    
## x11  0.999291   0.027677  36.106  < 2e-16 ***
## x12  0.009052   0.026930   0.336 0.737348    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1339 on 123 degrees of freedom
## Multiple R-squared:  0.9974, Adjusted R-squared:  0.9971 
## F-statistic:  3894 on 12 and 123 DF,  p-value: < 2.2e-16
data_plot %>% ggplot(aes(x=n)) + 
  geom_line(aes(y=Data), color='black') + 
  geom_line(aes(y=Prediksi), color='red') + 
  
  labs(title = "Prediksi vs Data Asli")

# j5 aj2 after
modwt_timeseries <- function (x,w='haar',j=5) { 
  n=length(x) 
  x.modwt=modwt(x,w,j) 
  d1=x.modwt@W$W1 
  d2=x.modwt@W$W2 
  d3=x.modwt@W$W3 
  d4=x.modwt@W$W4 
  d5=x.modwt@W$W5 
  v5=x.modwt@V$V5 
  x1<-x2<-x3<-x4<-x5<-x6<-x7<-x8<-x9<-x10 <- x11 <- x12<-NULL 
  for (i in 1:(n-33)){ 
    x1<-c(x1,d1[i+32]) 
    x2<-c(x2,d1[i+30]) 
    x3<-c(x3,d2[i+32]) 
    x4<-c(x4,d2[i+28]) 
    x5<-c(x5,d3[i+32]) 
    x6<-c(x6,d3[i+24]) 
    x7<-c(x7,d4[i+32]) 
    x8<-c(x8,d4[i+16]) 
    x9<-c(x9,d5[i+32]) 
    x10<-c(x10,d5[i]) 
    x11<-c(x11,v5[i+32]) 
    x12<-c(x12,v5[i]) 
  } 
  y=x[34:n] 
  lm.y=lm(y~-1+x5+x6+x7+x9+x11) 
  koef<-lm.y$coeff 
  pred<-c(rep(0,33), lm.y$fitted)
  print(summary(lm.y))
  return(lm.y) 
}

modwt_timeseries(data)
## 
## Call:
## lm(formula = y ~ -1 + x5 + x6 + x7 + x9 + x11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4230.9  -654.9    58.9   826.9  3496.2 
## 
## Coefficients:
##     Estimate Std. Error t value Pr(>|t|)    
## x5  1.305798   0.242280   5.390 3.21e-07 ***
## x6  1.042135   0.258080   4.038 9.16e-05 ***
## x7  0.756270   0.224340   3.371 0.000986 ***
## x9  0.868622   0.176021   4.935 2.41e-06 ***
## x11 1.011774   0.009145 110.638  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1327 on 130 degrees of freedom
## Multiple R-squared:  0.9973, Adjusted R-squared:  0.9972 
## F-statistic:  9513 on 5 and 130 DF,  p-value: < 2.2e-16
## 
## Call:
## lm(formula = y ~ -1 + x5 + x6 + x7 + x9 + x11)
## 
## Coefficients:
##     x5      x6      x7      x9     x11  
## 1.3058  1.0421  0.7563  0.8686  1.0118
data_plot <- data.frame(
  n = c(1:length(data)),
  Data = data,
  Prediksi = c(rep(0, 33), modwt_timeseries(data)$fitted)
)
## 
## Call:
## lm(formula = y ~ -1 + x5 + x6 + x7 + x9 + x11)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4230.9  -654.9    58.9   826.9  3496.2 
## 
## Coefficients:
##     Estimate Std. Error t value Pr(>|t|)    
## x5  1.305798   0.242280   5.390 3.21e-07 ***
## x6  1.042135   0.258080   4.038 9.16e-05 ***
## x7  0.756270   0.224340   3.371 0.000986 ***
## x9  0.868622   0.176021   4.935 2.41e-06 ***
## x11 1.011774   0.009145 110.638  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1327 on 130 degrees of freedom
## Multiple R-squared:  0.9973, Adjusted R-squared:  0.9972 
## F-statistic:  9513 on 5 and 130 DF,  p-value: < 2.2e-16
data_plot %>% ggplot(aes(x=n)) + 
  geom_line(aes(y=Data), color='black') + 
  geom_line(aes(y=Prediksi), color='red') + 
  
  labs(title = "Prediksi vs Data Asli")