Metode SSA dikombinasikan dengan bagging berbasis K-Means clustering (bagging-SSA) untuk meningkatkan akurasi peramalan harga saham BCA. Teknik bagging yang digabungkan dengan K-Means clustering bertujuan mengurangi variansi model dan meningkatkan stabilitas prediksi dengan membagi data ke dalam kelompok yang lebih homogen. Sebagai perbandingan, metode ARIMA juga diterapkan untuk mengevaluasi kinerja bagging-SSA.
library(readxl)
library(clusterCrit)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(data.table)
library(ggplot2)
library(readr)
library(openxlsx)
library(writexl)
library(Rssa)
## Loading required package: svd
##
## Attaching package: 'Rssa'
## The following object is masked from 'package:stats':
##
## decompose
library(readxl)
data<- read_excel("data BCA.xlsx",
col_types = c("date", "numeric"))
View(data)
# Load the data
set.seed(123)
total_data <- nrow(data)
total_data
## [1] 84
training_size <- floor((72/84) * total_data)
training_size
## [1] 72
testing_size <- total_data - training_size
testing_size
## [1] 12
# Data training dan testing
datrain0 <- data[1:training_size, ]
datest <- data[(training_size + 1):total_data, ]
datrain<- data.frame(datrain0)
datrain
## Date Price
## 1 2018-01-01 4545
## 2 2018-02-01 4635
## 3 2018-03-01 4660
## 4 2018-04-01 4420
## 5 2018-05-01 4540
## 6 2018-06-01 4295
## 7 2018-07-01 4655
## 8 2018-08-01 4960
## 9 2018-09-01 4830
## 10 2018-10-01 4730
## 11 2018-11-01 5210
## 12 2018-12-01 5200
## 13 2019-01-01 5635
## 14 2019-02-01 5515
## 15 2019-03-01 5550
## 16 2019-04-01 5750
## 17 2019-05-01 5820
## 18 2019-06-01 5995
## 19 2019-07-01 6190
## 20 2019-08-01 6100
## 21 2019-09-01 6070
## 22 2019-10-01 6290
## 23 2019-11-01 6280
## 24 2019-12-01 6685
## 25 2020-01-01 6480
## 26 2020-02-01 6290
## 27 2020-03-01 5525
## 28 2020-04-01 5170
## 29 2020-05-01 5190
## 30 2020-06-01 5695
## 31 2020-07-01 6240
## 32 2020-08-01 6275
## 33 2020-09-01 5420
## 34 2020-10-01 5790
## 35 2020-11-01 6205
## 36 2020-12-01 6770
## 37 2021-01-01 6760
## 38 2021-02-01 6710
## 39 2021-03-01 6215
## 40 2021-04-01 6405
## 41 2021-05-01 6375
## 42 2021-06-01 6025
## 43 2021-07-01 5970
## 44 2021-08-01 6550
## 45 2021-09-01 7000
## 46 2021-10-01 7475
## 47 2021-11-01 7275
## 48 2021-12-01 7300
## 49 2022-01-01 7625
## 50 2022-02-01 8050
## 51 2022-03-01 7975
## 52 2022-04-01 8125
## 53 2022-05-01 7750
## 54 2022-06-01 7250
## 55 2022-07-01 7350
## 56 2022-08-01 8200
## 57 2022-09-01 8550
## 58 2022-10-01 8800
## 59 2022-11-01 9300
## 60 2022-12-01 8550
## 61 2023-01-01 8475
## 62 2023-02-01 8750
## 63 2023-03-01 8750
## 64 2023-04-01 9050
## 65 2023-05-01 9050
## 66 2023-06-01 9150
## 67 2023-07-01 9125
## 68 2023-08-01 9175
## 69 2023-09-01 8825
## 70 2023-10-01 8750
## 71 2023-11-01 8975
## 72 2023-12-01 9400
# Check for missing values and display a message
if (sum(is.na(datrain)) > 0) {
cat("Data contains missing values. Missing values will be removed.\n")
data_saham <- na.omit(data_saham)
cat("Missing values have been removed.\n")
} else {
cat("No missing values found in the data.\n")
}
## No missing values found in the data.
# Mengonversi 7 kolom ke dalam time series, misalnya dengan frekuensi bulanan (12)
ts_price <- ts(datrain[2])
ts_price
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## Price
## [1,] 4545
## [2,] 4635
## [3,] 4660
## [4,] 4420
## [5,] 4540
## [6,] 4295
## [7,] 4655
## [8,] 4960
## [9,] 4830
## [10,] 4730
## [11,] 5210
## [12,] 5200
## [13,] 5635
## [14,] 5515
## [15,] 5550
## [16,] 5750
## [17,] 5820
## [18,] 5995
## [19,] 6190
## [20,] 6100
## [21,] 6070
## [22,] 6290
## [23,] 6280
## [24,] 6685
## [25,] 6480
## [26,] 6290
## [27,] 5525
## [28,] 5170
## [29,] 5190
## [30,] 5695
## [31,] 6240
## [32,] 6275
## [33,] 5420
## [34,] 5790
## [35,] 6205
## [36,] 6770
## [37,] 6760
## [38,] 6710
## [39,] 6215
## [40,] 6405
## [41,] 6375
## [42,] 6025
## [43,] 5970
## [44,] 6550
## [45,] 7000
## [46,] 7475
## [47,] 7275
## [48,] 7300
## [49,] 7625
## [50,] 8050
## [51,] 7975
## [52,] 8125
## [53,] 7750
## [54,] 7250
## [55,] 7350
## [56,] 8200
## [57,] 8550
## [58,] 8800
## [59,] 9300
## [60,] 8550
## [61,] 8475
## [62,] 8750
## [63,] 8750
## [64,] 9050
## [65,] 9050
## [66,] 9150
## [67,] 9125
## [68,] 9175
## [69,] 8825
## [70,] 8750
## [71,] 8975
## [72,] 9400
KMboot.norm <- function(x, num = 10, k_range = c(12,20)) {
freq <- frequency(x)
x_clust <- data.matrix(x)
if (sd(x) == 0) {
km_res <- rep(1, length(x))
} else {
km_res <- cluster::pam(x_clust, k = k_range[1])$cluster # Using PAM as an alternative
km_res
}}
k <-KMboot.norm(ts_price)
k
## [1] 1 1 1 1 1 1 1 2 1 1 2 2 3 3 3 4 4 5 6 5 5 6 6 7 6
## [26] 6 3 2 2 4 6 6 3 4 6 7 7 7 6 6 6 5 5 7 7 8 8 8 8 9
## [51] 9 9 9 8 8 9 10 11 12 10 10 11 11 12 12 12 12 12 11 11 12 12
KMboot.norm <- function(x, num = 100, k_range = c(12,20)) {
freq <- frequency(x)
x_clust <- data.matrix(x)
if (sd(x) == 0) {
km_res <- rep(1, length(x))
} else {
km_res <- cluster::pam(x_clust, k_range[1])$cluster
}
clus_means <- sapply(sort(unique(km_res)), function(i) mean(x[km_res == i]))
}
clusmeans <- KMboot.norm(ts_price)
clusmeans
## [1] 4590.000 5146.000 5529.000 5763.750 6032.000 6295.000 6745.833 7379.167
## [9] 8020.000 8525.000 8775.000 9153.125
KMboot.norm <- function(x, num = 100, k_range = c(12, 20)) {
freq <- frequency(x)
x_clust <- data.matrix(x)
# Periksa apakah standar deviasi adalah nol
if (sd(x) == 0) {
km_res <- rep(1, length(x))
} else {
# Gunakan jumlah cluster yang diinginkan
km_res <- cluster::pam(x_clust, 12)$cluster
}
# Hitung rata-rata dan standar deviasi untuk tiap cluster
clus_means <- sapply(sort(unique(km_res)), function(i) mean(x[km_res == i]))
clus_sd <- sapply(sort(unique(km_res)), function(i) sd(x[km_res == i]))
# Kembalikan hasil
list(cluster_means = clus_means, cluster_sd = clus_sd, clusters = km_res)
}
# Panggil fungsi dengan data
result <- KMboot.norm(ts_price)
result$cluster_sd
## [1] 161.55494 105.02381 77.08761 54.06401 53.22124 90.52624 147.45903
## [8] 144.40972 172.66297 43.30127 35.35534 139.79417
KMboot.norm <- function(x, num = 10, k_range = c(12,20)) {
freq <- frequency(x)
x_clust <- data.matrix(x)
if (sd(x) == 0) {
km_res <- rep(1, length(x))
} else {
km_res <- cluster::pam(x_clust, k = k_range[1])$cluster # Using PAM as an alternative
}
clus_means <- sapply(sort(unique(km_res)), function(i) mean(x[km_res == i]))
clus_means
clus_sd <- sapply(sort(unique(km_res)), function(i) sd(x[km_res == i]))
xs <- list()
xs[[1]] <- ts(x, freq = freq)
for (j in 2:num) {
xs[[j]] <- vector(length = length(x))
for (i in 1:length(x)) {
xs[[j]][i] <- rnorm(1, mean = clus_means[km_res[i]], sd = clus_sd[km_res[i]])
}
xs[[j]] <- ts(xs[[j]], freq = freq)
}
return(xs)
}
datbag<-KMboot.norm(ts_price)
datbag
## [[1]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## Price
## [1,] 4545
## [2,] 4635
## [3,] 4660
## [4,] 4420
## [5,] 4540
## [6,] 4295
## [7,] 4655
## [8,] 4960
## [9,] 4830
## [10,] 4730
## [11,] 5210
## [12,] 5200
## [13,] 5635
## [14,] 5515
## [15,] 5550
## [16,] 5750
## [17,] 5820
## [18,] 5995
## [19,] 6190
## [20,] 6100
## [21,] 6070
## [22,] 6290
## [23,] 6280
## [24,] 6685
## [25,] 6480
## [26,] 6290
## [27,] 5525
## [28,] 5170
## [29,] 5190
## [30,] 5695
## [31,] 6240
## [32,] 6275
## [33,] 5420
## [34,] 5790
## [35,] 6205
## [36,] 6770
## [37,] 6760
## [38,] 6710
## [39,] 6215
## [40,] 6405
## [41,] 6375
## [42,] 6025
## [43,] 5970
## [44,] 6550
## [45,] 7000
## [46,] 7475
## [47,] 7275
## [48,] 7300
## [49,] 7625
## [50,] 8050
## [51,] 7975
## [52,] 8125
## [53,] 7750
## [54,] 7250
## [55,] 7350
## [56,] 8200
## [57,] 8550
## [58,] 8800
## [59,] 9300
## [60,] 8550
## [61,] 8475
## [62,] 8750
## [63,] 8750
## [64,] 9050
## [65,] 9050
## [66,] 9150
## [67,] 9125
## [68,] 9175
## [69,] 8825
## [70,] 8750
## [71,] 8975
## [72,] 9400
##
## [[2]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## [1] 4499.452 4552.814 4841.817 4601.391 4610.887 4867.077 4664.463 5013.138
## [9] 4479.036 4518.001 5274.558 5183.789 5559.895 5537.532 5486.152 5860.358
## [17] 5790.666 5927.334 6358.491 6006.837 5975.169 6275.268 6202.120 6638.352
## [25] 6238.418 6142.310 5593.583 5162.108 5026.469 5831.536 6333.606 6268.288
## [33] 5598.003 5811.225 6369.375 6847.380 6827.513 6736.704 6267.302 6260.557
## [41] 6232.111 6020.934 5964.654 7065.665 6923.958 7216.979 7320.986 7311.777
## [49] 7491.801 8005.605 8063.739 8015.071 8012.598 7576.806 7346.563 8281.838
## [57] 8457.937 8795.669 9170.439 8534.351 8541.439 8757.240 8763.219 9010.734
## [65] 9003.295 9195.557 9215.782 9160.535 8807.607 8847.481 9084.482 8830.317
##
## [[3]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## [1] 4752.482 4475.425 4478.849 4755.686 4543.994 4392.787 4619.290 5131.413
## [9] 4590.931 4652.244 5107.072 5213.675 5512.003 5554.576 5613.553 5787.278
## [17] 5746.129 6093.141 6384.938 6061.186 6044.706 6238.158 6418.175 6657.320
## [25] 6493.011 6433.741 5510.830 5038.201 5071.390 5777.638 6272.668 6263.538
## [33] 5455.642 5761.316 6223.946 6499.880 6689.765 6881.348 6242.916 6350.037
## [41] 6148.539 6029.043 6059.643 6790.241 6761.416 7286.642 7256.461 7231.273
## [49] 7396.156 7856.406 7935.299 7975.782 8338.367 7285.019 7413.159 8033.461
## [57] 8483.350 8772.479 9355.065 8544.551 8526.785 8760.062 8702.407 9311.279
## [65] 8948.936 9256.565 9420.007 8951.277 8799.812 8765.730 8933.348 8941.383
##
## [[4]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## [1] 4331.264 4504.229 4353.846 4701.136 4929.283 4382.074 4717.263 5226.768
## [9] 4643.669 4427.092 5133.455 5116.552 5572.400 5500.290 5604.313 5743.499
## [17] 5820.664 5976.162 6180.923 6204.492 6009.814 6321.997 6352.626 6674.496
## [25] 6341.790 6328.401 5512.397 5152.857 5142.422 5878.823 6227.890 6195.784
## [33] 5531.913 5780.536 6334.517 6678.243 6589.036 6932.101 6263.347 6216.648
## [41] 6273.610 6021.506 6091.071 6758.329 6857.025 7307.064 7410.135 7332.279
## [49] 7392.825 7865.404 7793.673 8364.845 8123.720 7198.471 7290.908 7815.311
## [57] 8620.211 8821.401 9116.059 8548.521 8507.059 8758.162 8747.119 9070.001
## [65] 9383.912 9145.572 9169.795 9187.191 8818.575 8756.754 9014.378 9387.378
##
## [[5]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## [1] 4518.728 4473.185 4390.274 4382.448 4497.272 4689.839 4769.301 5220.314
## [9] 4531.249 4599.653 5072.001 5070.675 5597.196 5450.710 5679.729 5758.867
## [17] 5775.349 5992.695 6243.003 5961.907 6022.264 6332.929 6324.358 6630.589
## [25] 6223.609 6249.538 5644.328 5026.556 5127.195 5866.599 6285.859 6171.899
## [33] 5477.755 5789.996 6260.998 6662.980 6695.120 6759.178 6439.707 6286.983
## [41] 6392.841 6065.570 6025.952 6519.793 6668.990 7308.425 7385.976 7566.928
## [49] 7710.310 8287.210 7997.010 7716.713 7952.872 7392.049 7501.195 8186.193
## [57] 8554.631 8725.670 9271.900 8505.664 8532.569 8777.636 8790.138 9156.574
## [65] 8920.022 9256.083 9207.089 9115.988 8779.177 8779.739 9184.022 9382.506
##
## [[6]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## [1] 4554.611 4617.152 4778.758 4760.308 4775.023 4496.707 4913.511 5153.005
## [9] 4891.599 4371.755 5148.204 5277.271 5473.864 5470.977 5456.650 5706.847
## [17] 5740.115 6049.626 6112.661 6043.282 6097.817 6479.454 6412.791 6857.427
## [25] 6138.686 6240.548 5501.862 5219.887 5134.902 5695.702 6447.486 6377.505
## [33] 5547.303 5829.606 6173.806 6843.277 6668.725 6846.658 6289.494 6352.300
## [41] 6415.899 6032.388 6086.156 6570.588 6639.426 7598.556 7433.665 7082.806
## [49] 7182.186 7985.333 8169.488 8002.409 8127.774 7517.656 7620.483 8029.672
## [57] 8522.749 8713.014 9167.010 8500.238 8482.824 8768.639 8810.884 8874.550
## [65] 9093.394 9169.430 9028.260 9199.803 8789.546 8773.832 8808.407 9512.600
##
## [[7]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## [1] 4556.833 4695.203 4634.228 4755.541 4722.097 4556.107 4651.095 5046.710
## [9] 4728.440 4515.517 5399.819 4972.601 5493.232 5592.627 5568.325 5731.880
## [17] 5709.860 6039.689 6293.705 5936.719 6033.839 6312.221 6310.817 6590.262
## [25] 6338.103 6419.797 5564.170 5026.736 5100.247 5782.462 6236.425 6099.676
## [33] 5597.165 5718.905 6243.078 6967.597 6631.679 6870.544 6180.875 6262.905
## [41] 6288.341 5969.803 5998.218 6741.580 6844.734 7140.812 7328.659 7488.399
## [49] 7301.357 8059.245 8104.990 8066.245 8132.793 7361.446 7319.428 7563.626
## [57] 8520.976 8790.213 9227.971 8500.956 8602.055 8785.127 8779.466 9330.980
## [65] 9052.688 9090.170 9488.275 9154.681 8832.755 8724.141 9126.492 9206.026
##
## [[8]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## [1] 4638.473 4427.534 4593.111 4415.937 4705.141 4765.251 4230.542 5275.777
## [9] 4389.503 4663.470 5215.305 5125.007 5479.270 5541.744 5562.827 5811.505
## [17] 5652.792 5944.910 6424.489 6087.703 6055.167 6359.742 6378.028 6353.456
## [25] 6395.509 6251.096 5546.778 5115.001 5237.577 5744.910 6341.938 6259.633
## [33] 5444.760 5829.168 6362.071 7000.091 6755.441 6911.725 6473.827 6269.518
## [41] 6175.238 6019.261 6020.608 6768.200 6998.328 7332.068 7433.032 7346.287
## [49] 7382.120 8074.226 8249.333 8040.947 8143.081 7491.642 7511.269 7920.823
## [57] 8595.446 8761.531 9138.337 8585.797 8581.035 8736.463 8744.132 8963.273
## [65] 9178.546 9176.169 9204.026 9230.313 8753.720 8739.867 9296.664 9258.119
##
## [[9]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## [1] 4346.187 4574.628 4445.255 4255.460 4614.253 4577.203 4574.270 5168.701
## [9] 4732.567 4623.215 5081.260 5068.829 5518.840 5552.898 5448.854 5753.786
## [17] 5816.044 6026.237 6231.775 6017.314 6091.323 6344.793 6406.952 6766.345
## [25] 6332.141 6244.445 5575.667 5092.823 4996.807 5770.670 6471.151 6367.504
## [33] 5618.827 5783.151 6239.910 6716.011 6705.540 6676.719 6358.746 6186.607
## [41] 6373.429 6077.991 5968.208 6840.132 7104.192 7298.699 7501.179 7266.209
## [49] 7539.564 8063.135 8305.225 7768.090 8011.143 7303.074 7350.680 7911.295
## [57] 8488.894 8795.461 9001.088 8589.260 8473.636 8778.574 8793.844 9235.147
## [65] 9110.943 9164.239 9287.504 8949.520 8747.361 8786.328 9090.947 9344.644
##
## [[10]]
## Time Series:
## Start = 1
## End = 72
## Frequency = 1
## [1] 4698.767 4601.659 4346.414 4594.217 4538.881 4573.465 4399.113 5198.371
## [9] 4422.151 4553.453 5186.059 5063.712 5573.941 5427.513 5312.401 5788.888
## [17] 5809.193 6016.787 6340.637 5970.481 6025.233 6119.242 6401.928 7020.094
## [25] 6392.226 6292.524 5526.431 4986.777 5229.009 5752.357 6235.548 6167.175
## [33] 5505.892 5717.846 6259.058 6566.287 6994.684 6743.474 6392.311 6059.478
## [41] 6253.974 5996.050 5966.914 6973.895 6537.137 7425.145 7501.400 7404.899
## [49] 7252.771 8182.504 8049.454 7836.373 7780.335 7680.509 7281.184 7699.611
## [57] 8548.091 8785.968 8963.867 8440.868 8519.964 8815.284 8797.490 9084.215
## [65] 9036.510 9191.019 9175.122 9241.155 8761.006 8806.797 9036.982 9106.917
frame_datbag <- as.data.frame(datbag)
frame_datbag
## Price structure.c.4499.45238818793..4552.8136885272..4841.81703473831..
## 1 4545 4499.452
## 2 4635 4552.814
## 3 4660 4841.817
## 4 4420 4601.391
## 5 4540 4610.887
## 6 4295 4867.077
## 7 4655 4664.463
## 8 4960 5013.138
## 9 4830 4479.036
## 10 4730 4518.001
## 11 5210 5274.558
## 12 5200 5183.789
## 13 5635 5559.895
## 14 5515 5537.532
## 15 5550 5486.152
## 16 5750 5860.358
## 17 5820 5790.666
## 18 5995 5927.334
## 19 6190 6358.491
## 20 6100 6006.837
## 21 6070 5975.169
## 22 6290 6275.268
## 23 6280 6202.120
## 24 6685 6638.352
## 25 6480 6238.418
## 26 6290 6142.310
## 27 5525 5593.583
## 28 5170 5162.108
## 29 5190 5026.469
## 30 5695 5831.536
## 31 6240 6333.606
## 32 6275 6268.288
## 33 5420 5598.003
## 34 5790 5811.225
## 35 6205 6369.375
## 36 6770 6847.380
## 37 6760 6827.513
## 38 6710 6736.704
## 39 6215 6267.302
## 40 6405 6260.557
## 41 6375 6232.111
## 42 6025 6020.934
## 43 5970 5964.654
## 44 6550 7065.665
## 45 7000 6923.958
## 46 7475 7216.979
## 47 7275 7320.986
## 48 7300 7311.777
## 49 7625 7491.801
## 50 8050 8005.605
## 51 7975 8063.739
## 52 8125 8015.071
## 53 7750 8012.598
## 54 7250 7576.806
## 55 7350 7346.563
## 56 8200 8281.838
## 57 8550 8457.937
## 58 8800 8795.669
## 59 9300 9170.439
## 60 8550 8534.351
## 61 8475 8541.439
## 62 8750 8757.240
## 63 8750 8763.219
## 64 9050 9010.734
## 65 9050 9003.295
## 66 9150 9195.557
## 67 9125 9215.782
## 68 9175 9160.535
## 69 8825 8807.607
## 70 8750 8847.481
## 71 8975 9084.482
## 72 9400 8830.317
## structure.c.4752.48203121341..4475.42511036445..4478.84880634784..
## 1 4752.482
## 2 4475.425
## 3 4478.849
## 4 4755.686
## 5 4543.994
## 6 4392.787
## 7 4619.290
## 8 5131.413
## 9 4590.931
## 10 4652.244
## 11 5107.072
## 12 5213.675
## 13 5512.003
## 14 5554.576
## 15 5613.553
## 16 5787.278
## 17 5746.129
## 18 6093.141
## 19 6384.938
## 20 6061.186
## 21 6044.706
## 22 6238.158
## 23 6418.175
## 24 6657.320
## 25 6493.011
## 26 6433.741
## 27 5510.830
## 28 5038.201
## 29 5071.390
## 30 5777.638
## 31 6272.668
## 32 6263.538
## 33 5455.642
## 34 5761.316
## 35 6223.946
## 36 6499.880
## 37 6689.765
## 38 6881.348
## 39 6242.916
## 40 6350.037
## 41 6148.539
## 42 6029.043
## 43 6059.643
## 44 6790.241
## 45 6761.416
## 46 7286.642
## 47 7256.461
## 48 7231.273
## 49 7396.156
## 50 7856.406
## 51 7935.299
## 52 7975.782
## 53 8338.367
## 54 7285.019
## 55 7413.159
## 56 8033.461
## 57 8483.350
## 58 8772.479
## 59 9355.065
## 60 8544.551
## 61 8526.785
## 62 8760.062
## 63 8702.407
## 64 9311.279
## 65 8948.936
## 66 9256.565
## 67 9420.007
## 68 8951.277
## 69 8799.812
## 70 8765.730
## 71 8933.348
## 72 8941.383
## structure.c.4331.2639128214..4504.22942642791..4353.84615801143..
## 1 4331.264
## 2 4504.229
## 3 4353.846
## 4 4701.136
## 5 4929.283
## 6 4382.074
## 7 4717.263
## 8 5226.768
## 9 4643.669
## 10 4427.092
## 11 5133.455
## 12 5116.552
## 13 5572.400
## 14 5500.290
## 15 5604.313
## 16 5743.499
## 17 5820.664
## 18 5976.162
## 19 6180.923
## 20 6204.492
## 21 6009.814
## 22 6321.997
## 23 6352.626
## 24 6674.496
## 25 6341.790
## 26 6328.401
## 27 5512.397
## 28 5152.857
## 29 5142.422
## 30 5878.823
## 31 6227.890
## 32 6195.784
## 33 5531.913
## 34 5780.536
## 35 6334.517
## 36 6678.243
## 37 6589.036
## 38 6932.101
## 39 6263.347
## 40 6216.648
## 41 6273.610
## 42 6021.506
## 43 6091.071
## 44 6758.329
## 45 6857.025
## 46 7307.064
## 47 7410.135
## 48 7332.279
## 49 7392.825
## 50 7865.404
## 51 7793.673
## 52 8364.845
## 53 8123.720
## 54 7198.471
## 55 7290.908
## 56 7815.311
## 57 8620.211
## 58 8821.401
## 59 9116.059
## 60 8548.521
## 61 8507.059
## 62 8758.162
## 63 8747.119
## 64 9070.001
## 65 9383.912
## 66 9145.572
## 67 9169.795
## 68 9187.191
## 69 8818.575
## 70 8756.754
## 71 9014.378
## 72 9387.378
## structure.c.4518.72790110358..4473.1851175633..4390.2739652455..
## 1 4518.728
## 2 4473.185
## 3 4390.274
## 4 4382.448
## 5 4497.272
## 6 4689.839
## 7 4769.301
## 8 5220.314
## 9 4531.249
## 10 4599.653
## 11 5072.001
## 12 5070.675
## 13 5597.196
## 14 5450.710
## 15 5679.729
## 16 5758.867
## 17 5775.349
## 18 5992.695
## 19 6243.003
## 20 5961.907
## 21 6022.264
## 22 6332.929
## 23 6324.358
## 24 6630.589
## 25 6223.609
## 26 6249.538
## 27 5644.328
## 28 5026.556
## 29 5127.195
## 30 5866.599
## 31 6285.859
## 32 6171.899
## 33 5477.755
## 34 5789.996
## 35 6260.998
## 36 6662.980
## 37 6695.120
## 38 6759.178
## 39 6439.707
## 40 6286.983
## 41 6392.841
## 42 6065.570
## 43 6025.952
## 44 6519.793
## 45 6668.990
## 46 7308.425
## 47 7385.976
## 48 7566.928
## 49 7710.310
## 50 8287.210
## 51 7997.010
## 52 7716.713
## 53 7952.872
## 54 7392.049
## 55 7501.195
## 56 8186.193
## 57 8554.631
## 58 8725.670
## 59 9271.900
## 60 8505.664
## 61 8532.569
## 62 8777.636
## 63 8790.138
## 64 9156.574
## 65 8920.022
## 66 9256.083
## 67 9207.089
## 68 9115.988
## 69 8779.177
## 70 8779.739
## 71 9184.022
## 72 9382.506
## structure.c.4554.61132825134..4617.1517937178..4778.75819143426..
## 1 4554.611
## 2 4617.152
## 3 4778.758
## 4 4760.308
## 5 4775.023
## 6 4496.707
## 7 4913.511
## 8 5153.005
## 9 4891.599
## 10 4371.755
## 11 5148.204
## 12 5277.271
## 13 5473.864
## 14 5470.977
## 15 5456.650
## 16 5706.847
## 17 5740.115
## 18 6049.626
## 19 6112.661
## 20 6043.282
## 21 6097.817
## 22 6479.454
## 23 6412.791
## 24 6857.427
## 25 6138.686
## 26 6240.548
## 27 5501.862
## 28 5219.887
## 29 5134.902
## 30 5695.702
## 31 6447.486
## 32 6377.505
## 33 5547.303
## 34 5829.606
## 35 6173.806
## 36 6843.277
## 37 6668.725
## 38 6846.658
## 39 6289.494
## 40 6352.300
## 41 6415.899
## 42 6032.388
## 43 6086.156
## 44 6570.588
## 45 6639.426
## 46 7598.556
## 47 7433.665
## 48 7082.806
## 49 7182.186
## 50 7985.333
## 51 8169.488
## 52 8002.409
## 53 8127.774
## 54 7517.656
## 55 7620.483
## 56 8029.672
## 57 8522.749
## 58 8713.014
## 59 9167.010
## 60 8500.238
## 61 8482.824
## 62 8768.639
## 63 8810.884
## 64 8874.550
## 65 9093.394
## 66 9169.430
## 67 9028.260
## 68 9199.803
## 69 8789.546
## 70 8773.832
## 71 8808.407
## 72 9512.600
## structure.c.4556.83288991254..4695.20349427945..4634.22833018708..
## 1 4556.833
## 2 4695.203
## 3 4634.228
## 4 4755.541
## 5 4722.097
## 6 4556.107
## 7 4651.095
## 8 5046.710
## 9 4728.440
## 10 4515.517
## 11 5399.819
## 12 4972.601
## 13 5493.232
## 14 5592.627
## 15 5568.325
## 16 5731.880
## 17 5709.860
## 18 6039.689
## 19 6293.705
## 20 5936.719
## 21 6033.839
## 22 6312.221
## 23 6310.817
## 24 6590.262
## 25 6338.103
## 26 6419.797
## 27 5564.170
## 28 5026.736
## 29 5100.247
## 30 5782.462
## 31 6236.425
## 32 6099.676
## 33 5597.165
## 34 5718.905
## 35 6243.078
## 36 6967.597
## 37 6631.679
## 38 6870.544
## 39 6180.875
## 40 6262.905
## 41 6288.341
## 42 5969.803
## 43 5998.218
## 44 6741.580
## 45 6844.734
## 46 7140.812
## 47 7328.659
## 48 7488.399
## 49 7301.357
## 50 8059.245
## 51 8104.990
## 52 8066.245
## 53 8132.793
## 54 7361.446
## 55 7319.428
## 56 7563.626
## 57 8520.976
## 58 8790.213
## 59 9227.971
## 60 8500.956
## 61 8602.055
## 62 8785.127
## 63 8779.466
## 64 9330.980
## 65 9052.688
## 66 9090.170
## 67 9488.275
## 68 9154.681
## 69 8832.755
## 70 8724.141
## 71 9126.492
## 72 9206.026
## structure.c.4638.47271043278..4427.53449019512..4593.1114310377..
## 1 4638.473
## 2 4427.534
## 3 4593.111
## 4 4415.937
## 5 4705.141
## 6 4765.251
## 7 4230.542
## 8 5275.777
## 9 4389.503
## 10 4663.470
## 11 5215.305
## 12 5125.007
## 13 5479.270
## 14 5541.744
## 15 5562.827
## 16 5811.505
## 17 5652.792
## 18 5944.910
## 19 6424.489
## 20 6087.703
## 21 6055.167
## 22 6359.742
## 23 6378.028
## 24 6353.456
## 25 6395.509
## 26 6251.096
## 27 5546.778
## 28 5115.001
## 29 5237.577
## 30 5744.910
## 31 6341.938
## 32 6259.633
## 33 5444.760
## 34 5829.168
## 35 6362.071
## 36 7000.091
## 37 6755.441
## 38 6911.725
## 39 6473.827
## 40 6269.518
## 41 6175.238
## 42 6019.261
## 43 6020.608
## 44 6768.200
## 45 6998.328
## 46 7332.068
## 47 7433.032
## 48 7346.287
## 49 7382.120
## 50 8074.226
## 51 8249.333
## 52 8040.947
## 53 8143.081
## 54 7491.642
## 55 7511.269
## 56 7920.823
## 57 8595.446
## 58 8761.531
## 59 9138.337
## 60 8585.797
## 61 8581.035
## 62 8736.463
## 63 8744.132
## 64 8963.273
## 65 9178.546
## 66 9176.169
## 67 9204.026
## 68 9230.313
## 69 8753.720
## 70 8739.867
## 71 9296.664
## 72 9258.119
## structure.c.4346.18668425217..4574.62845888594..4445.25519947589..
## 1 4346.187
## 2 4574.628
## 3 4445.255
## 4 4255.460
## 5 4614.253
## 6 4577.203
## 7 4574.270
## 8 5168.701
## 9 4732.567
## 10 4623.215
## 11 5081.260
## 12 5068.829
## 13 5518.840
## 14 5552.898
## 15 5448.854
## 16 5753.786
## 17 5816.044
## 18 6026.237
## 19 6231.775
## 20 6017.314
## 21 6091.323
## 22 6344.793
## 23 6406.952
## 24 6766.345
## 25 6332.141
## 26 6244.445
## 27 5575.667
## 28 5092.823
## 29 4996.807
## 30 5770.670
## 31 6471.151
## 32 6367.504
## 33 5618.827
## 34 5783.151
## 35 6239.910
## 36 6716.011
## 37 6705.540
## 38 6676.719
## 39 6358.746
## 40 6186.607
## 41 6373.429
## 42 6077.991
## 43 5968.208
## 44 6840.132
## 45 7104.192
## 46 7298.699
## 47 7501.179
## 48 7266.209
## 49 7539.564
## 50 8063.135
## 51 8305.225
## 52 7768.090
## 53 8011.143
## 54 7303.074
## 55 7350.680
## 56 7911.295
## 57 8488.894
## 58 8795.461
## 59 9001.088
## 60 8589.260
## 61 8473.636
## 62 8778.574
## 63 8793.844
## 64 9235.147
## 65 9110.943
## 66 9164.239
## 67 9287.504
## 68 8949.520
## 69 8747.361
## 70 8786.328
## 71 9090.947
## 72 9344.644
## structure.c.4698.76749033617..4601.65889566047..4346.41435067706..
## 1 4698.767
## 2 4601.659
## 3 4346.414
## 4 4594.217
## 5 4538.881
## 6 4573.465
## 7 4399.113
## 8 5198.371
## 9 4422.151
## 10 4553.453
## 11 5186.059
## 12 5063.712
## 13 5573.941
## 14 5427.513
## 15 5312.401
## 16 5788.888
## 17 5809.193
## 18 6016.787
## 19 6340.637
## 20 5970.481
## 21 6025.233
## 22 6119.242
## 23 6401.928
## 24 7020.094
## 25 6392.226
## 26 6292.524
## 27 5526.431
## 28 4986.777
## 29 5229.009
## 30 5752.357
## 31 6235.548
## 32 6167.175
## 33 5505.892
## 34 5717.846
## 35 6259.058
## 36 6566.287
## 37 6994.684
## 38 6743.474
## 39 6392.311
## 40 6059.478
## 41 6253.974
## 42 5996.050
## 43 5966.914
## 44 6973.895
## 45 6537.137
## 46 7425.145
## 47 7501.400
## 48 7404.899
## 49 7252.771
## 50 8182.504
## 51 8049.454
## 52 7836.373
## 53 7780.335
## 54 7680.509
## 55 7281.184
## 56 7699.611
## 57 8548.091
## 58 8785.968
## 59 8963.867
## 60 8440.868
## 61 8519.964
## 62 8815.284
## 63 8797.490
## 64 9084.215
## 65 9036.510
## 66 9191.019
## 67 9175.122
## 68 9241.155
## 69 8761.006
## 70 8806.797
## 71 9036.982
## 72 9106.917
write.xlsx(frame_datbag, "D:/Dataoutputbagging.xlsx")
# Convert the bagging list into a data frame with 10 columns
library(readxl)
library(readxl)
daging <- read_excel("D:/PKM/DataBagging.xlsx",
col_names = FALSE)
## New names:
## • `` -> `...1`
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
## • `` -> `...9`
## • `` -> `...10`
frame_daging <- as.data.frame(do.call(cbind, daging))
# Buat data frame baru untuk data log
frame_daging_log <- as.data.frame(lapply(frame_daging, function(x) {
# Tambahkan konstanta kecil jika ada nilai nol atau negatif
if(any(x <= 0, na.rm = TRUE)) {
return(log(x + abs(min(x, na.rm = TRUE)) + 0.001))
} else {
return(log(x))
}
}))
# Beri nama kolom yang berbeda untuk data log
colnames(frame_daging_log) <- paste0("log_", colnames(frame_daging))
# Gabungkan data asli dan data log
frame_daging_combined <- cbind(frame_daging, frame_daging_log)
# Tampilkan beberapa baris pertama
head(frame_daging_combined)
## ...1 ...2 ...3 ...4 ...5 ...6 ...7 ...8
## 1 4642.477 4664.929 4588.894 4712.993 4461.950 4456.467 4415.734 4381.697
## 2 4449.354 4667.080 4811.823 4772.419 4635.609 4595.578 4607.317 4484.251
## 3 4581.645 4510.611 4487.360 4542.324 4470.338 4733.473 4848.244 4580.739
## 4 4736.764 4376.847 4680.164 4676.633 4505.452 4276.593 4345.192 4793.034
## 5 4468.878 4799.282 4645.116 4545.429 4875.850 4755.303 4660.063 4846.461
## 6 4538.043 4360.557 4399.443 4700.231 4697.964 4590.882 4735.326 4641.614
## ...9 ...10 log_...1 log_...2 log_...3 log_...4 log_...5 log_...6 log_...7
## 1 4622.962 4545 8.443003 8.447828 8.431394 8.458079 8.403341 8.402112 8.392929
## 2 4495.994 4635 8.400514 8.448289 8.478831 8.470609 8.441523 8.432850 8.435401
## 3 4679.696 4660 8.429813 8.414188 8.409020 8.421194 8.405219 8.462414 8.486372
## 4 4761.042 4420 8.463110 8.384084 8.451089 8.450334 8.413043 8.360912 8.376825
## 5 4979.934 4540 8.404893 8.476222 8.443572 8.421877 8.492050 8.467016 8.446784
## 6 4272.547 4295 8.420251 8.380355 8.389233 8.455367 8.454885 8.431827 8.462806
## log_...8 log_...9 log_...10
## 1 8.385191 8.438791 8.421783
## 2 8.408327 8.410942 8.441391
## 3 8.429616 8.450988 8.446771
## 4 8.474919 8.468222 8.393895
## 5 8.486004 8.513172 8.420682
## 6 8.442817 8.359966 8.365207
# Lihat struktur data
cat("Data asli:\n")
## Data asli:
dim(frame_daging)
## [1] 72 10
cat("\nData log:\n")
##
## Data log:
dim(frame_daging_log)
## [1] 72 10
print(frame_daging_log)
## log_...1 log_...2 log_...3 log_...4 log_...5 log_...6 log_...7 log_...8
## 1 8.443003 8.447828 8.431394 8.458079 8.403341 8.402112 8.392929 8.385191
## 2 8.400514 8.448289 8.478831 8.470609 8.441523 8.432850 8.435401 8.408327
## 3 8.429813 8.414188 8.409020 8.421194 8.405219 8.462414 8.486372 8.429616
## 4 8.463110 8.384084 8.451089 8.450334 8.413043 8.360912 8.376825 8.474919
## 5 8.404893 8.476222 8.443572 8.421877 8.492050 8.467016 8.446784 8.486004
## 6 8.420251 8.380355 8.389233 8.455367 8.454885 8.431827 8.462806 8.442817
## 7 8.390398 8.398030 8.368398 8.427498 8.353139 8.451491 8.396186 8.444977
## 8 8.553181 8.558730 8.504459 8.538916 8.548552 8.511652 8.544897 8.540990
## 9 8.446476 8.386193 8.450853 8.435556 8.415605 8.407891 8.460212 8.433325
## 10 8.454199 8.412020 8.430412 8.421610 8.390807 8.415733 8.422601 8.380935
## 11 8.520765 8.521539 8.583047 8.533840 8.550247 8.567056 8.541461 8.551875
## 12 8.548161 8.558562 8.557615 8.539506 8.548624 8.566742 8.552236 8.534985
## 13 8.604512 8.623966 8.629539 8.617649 8.613172 8.623810 8.616925 8.622418
## 14 8.617757 8.623627 8.636196 8.620651 8.644895 8.639882 8.609985 8.612927
## 15 8.636309 8.623912 8.610757 8.638903 8.585908 8.610550 8.615828 8.608495
## 16 8.654609 8.664557 8.664117 8.646524 8.667175 8.668796 8.676850 8.657286
## 17 8.666044 8.665323 8.667460 8.657469 8.656629 8.659006 8.667505 8.655896
## 18 8.698197 8.687311 8.716843 8.710390 8.710610 8.698928 8.696732 8.714445
## 19 8.740606 8.745369 8.758419 8.772534 8.740961 8.760702 8.729446 8.750515
## 20 8.708697 8.705826 8.708543 8.708586 8.699580 8.716946 8.701653 8.708643
## 21 8.698824 8.711207 8.697144 8.704713 8.689628 8.712553 8.716490 8.693825
## 22 8.722716 8.730287 8.757948 8.743078 8.744494 8.740158 8.751711 8.768808
## 23 8.765541 8.740300 8.754679 8.753451 8.782536 8.740732 8.737372 8.749800
## 24 8.824527 8.792931 8.830449 8.838070 8.838084 8.846351 8.835782 8.797785
## 25 8.739082 8.762600 8.753585 8.744863 8.771321 8.752572 8.745591 8.751945
## 26 8.718416 8.765738 8.744604 8.749863 8.760583 8.759370 8.731264 8.759951
## 27 8.643938 8.628684 8.616690 8.620782 8.633590 8.613029 8.624172 8.598282
## 28 8.612880 8.520711 8.559906 8.560749 8.564234 8.571386 8.576607 8.579887
## 29 8.550200 8.555157 8.549472 8.568404 8.555630 8.529332 8.554803 8.542745
## 30 8.667283 8.670077 8.651527 8.661959 8.653864 8.665192 8.661143 8.669805
## 31 8.764968 8.749923 8.743330 8.746415 8.739817 8.735690 8.738029 8.748550
## 32 8.737367 8.739337 8.728359 8.767369 8.724221 8.711455 8.755652 8.742719
## 33 8.612854 8.602503 8.604188 8.620054 8.617681 8.598861 8.602723 8.591846
## 34 8.643011 8.653083 8.657986 8.674035 8.667744 8.660621 8.643716 8.648970
## 35 8.736862 8.757738 8.721312 8.746620 8.762242 8.760128 8.746887 8.746916
## 36 8.830194 8.807200 8.779072 8.830011 8.818758 8.777646 8.815924 8.834615
## 37 8.810305 8.821643 8.840496 8.782300 8.804637 8.788944 8.867087 8.774678
## 38 8.811987 8.844594 8.803996 8.814220 8.886140 8.854527 8.789732 8.846303
## 39 8.745891 8.755793 8.720521 8.747197 8.758049 8.741252 8.749966 8.767430
## 40 8.720345 8.766946 8.745887 8.736546 8.767734 8.754757 8.759022 8.730933
## 41 8.759485 8.722737 8.766331 8.732502 8.742643 8.739904 8.762506 8.754280
## 42 8.692016 8.702354 8.710660 8.713165 8.704499 8.702196 8.704739 8.695672
## 43 8.702992 8.705408 8.708721 8.712867 8.708784 8.705603 8.698254 8.703729
## 44 8.807047 8.829249 8.842227 8.787880 8.852672 8.816920 8.815241 8.836777
## 45 8.811891 8.790790 8.799672 8.825914 8.809848 8.812462 8.858208 8.835484
## 46 8.934587 8.922070 8.920595 8.884403 8.922983 8.897180 8.915819 8.889658
## 47 8.894960 8.912414 8.894860 8.885632 8.864557 8.844917 8.865583 8.898821
## 48 8.890972 8.911565 8.906431 8.916646 8.896439 8.942333 8.933892 8.887128
## 49 8.876225 8.916318 8.948840 8.892900 8.880806 8.940855 8.916938 8.913039
## 50 8.972196 8.987185 9.010350 9.010857 9.013381 8.965703 8.988971 8.955552
## 51 8.964266 9.009424 9.006093 8.990523 8.986147 8.986068 8.989309 8.931422
## 52 8.998438 9.003553 8.965543 9.001170 8.981254 9.018872 8.986464 9.020847
## 53 8.970939 8.986455 8.972622 8.978356 9.018743 9.010958 8.995202 9.037479
## 54 8.912348 8.926572 8.950146 8.883629 8.928026 8.931092 8.896506 8.897471
## 55 8.903546 8.912125 8.884788 8.924159 8.888686 8.885219 8.911822 8.921201
## 56 8.958344 9.003989 8.994301 8.994806 8.998863 9.043695 8.969439 8.989532
## 57 9.046734 9.047736 9.055282 9.049543 9.044499 9.044984 9.051774 9.058219
## 58 9.083222 9.086010 9.083758 9.079941 9.077744 9.082695 9.084112 9.081892
## 59 9.135549 9.121790 9.138348 9.141899 9.146240 9.124006 9.129598 9.124956
## 60 9.060894 9.055056 9.049929 9.047206 9.058097 9.048706 9.055995 9.041578
## 61 9.050740 9.050250 9.046580 9.046945 9.056376 9.048829 9.045202 9.056244
## 62 9.073617 9.078535 9.078423 9.077162 9.077359 9.075660 9.079883 9.078247
## 63 9.076561 9.082818 9.076021 9.073255 9.072053 9.086179 9.082146 9.073991
## 64 9.128070 9.097351 9.131382 9.134291 9.103731 9.123217 9.105405 9.118764
## 65 9.150458 9.129130 9.138817 9.124784 9.136294 9.125802 9.145484 9.119912
## 66 9.123529 9.127844 9.153823 9.125010 9.117403 9.125047 9.105920 9.137898
## 67 9.139118 9.079814 9.127427 9.121188 9.088264 9.116104 9.124360 9.103860
## 68 9.133513 9.127461 9.108400 9.114029 9.119095 9.104379 9.124167 9.113297
## 69 9.074945 9.070887 9.083781 9.076339 9.085328 9.080684 9.080152 9.085640
## 70 9.078972 9.082317 9.083301 9.083088 9.082251 9.071191 9.078814 9.085121
## 71 9.141590 9.114894 9.118205 9.099828 9.109228 9.099947 9.128699 9.139460
## 72 9.135142 9.111180 9.097770 9.128553 9.097957 9.105274 9.105976 9.146910
## log_...9 log_...10
## 1 8.438791 8.421783
## 2 8.410942 8.441391
## 3 8.450988 8.446771
## 4 8.468222 8.393895
## 5 8.513172 8.420682
## 6 8.359966 8.365207
## 7 8.441206 8.445697
## 8 8.559250 8.509161
## 9 8.429712 8.482602
## 10 8.375244 8.461680
## 11 8.560995 8.558335
## 12 8.529947 8.556414
## 13 8.624790 8.636752
## 14 8.630336 8.615227
## 15 8.635505 8.621553
## 16 8.644623 8.656955
## 17 8.669439 8.669056
## 18 8.698133 8.698681
## 19 8.728800 8.730690
## 20 8.716364 8.716044
## 21 8.714897 8.711114
## 22 8.744016 8.746716
## 23 8.758374 8.745125
## 24 8.797695 8.807621
## 25 8.745337 8.776476
## 26 8.746162 8.746716
## 27 8.613840 8.617039
## 28 8.560846 8.550628
## 29 8.576868 8.554489
## 30 8.655059 8.647344
## 31 8.747041 8.738735
## 32 8.770776 8.744329
## 33 8.613164 8.597851
## 34 8.634614 8.663888
## 35 8.754875 8.733111
## 36 8.797113 8.820256
## 37 8.794611 8.818778
## 38 8.847266 8.811354
## 39 8.782109 8.734721
## 40 8.732445 8.764834
## 41 8.762229 8.760139
## 42 8.704003 8.703673
## 43 8.706084 8.694502
## 44 8.843047 8.787220
## 45 8.821340 8.853665
## 46 8.889614 8.919319
## 47 8.897167 8.892199
## 48 8.919754 8.895630
## 49 8.859624 8.939188
## 50 9.013000 8.993427
## 51 8.955372 8.984067
## 52 8.990166 9.002701
## 53 8.985587 8.955448
## 54 8.931406 8.888757
## 55 8.910105 8.902456
## 56 8.991886 9.011889
## 57 9.054298 9.053687
## 58 9.076324 9.082507
## 59 9.118854 9.137770
## 60 9.056722 9.053687
## 61 9.054224 9.044876
## 62 9.074510 9.076809
## 63 9.082229 9.076809
## 64 9.100586 9.110520
## 65 9.112671 9.110520
## 66 9.140176 9.121509
## 67 9.086866 9.118773
## 68 9.094304 9.124238
## 69 9.084892 9.085344
## 70 9.076374 9.076809
## 71 9.140667 9.102198
## 72 9.119278 9.148465
##Foecast Bagging ARIMA
# Function to forecast ARIMA for each column in the data frame
forecast_arima_per_column <- function(frame_daging, h = 12) {
forecasts <- lapply(frame_daging, function(column) {
ts_data <- ts(column, frequency = 12)
model <- auto.arima(ts_data)
forecast(model, h = h)$mean
})
forecast_df <- do.call(cbind, forecasts)
return(forecast_df)
}
# Perform forecasting ARIMA for each column
forecast_results <- forecast_arima_per_column(frame_daging)
forecast_results
## ...1 ...2 ...3 ...4 ...5 ...6 ...7 ...8
## Jan 7 9340.853 9046.788 8935.346 9278.080 8907.532 9062.885 9008.974 9370.636
## Feb 7 9406.109 9101.508 8935.346 9341.484 8878.722 9126.644 9008.974 9416.750
## Mar 7 9471.364 9109.929 8935.346 9404.888 8864.349 9190.404 9008.974 9583.556
## Apr 7 9536.619 9138.777 8935.346 9468.292 8968.649 9254.163 9008.974 9493.083
## May 7 9601.874 9203.334 8935.346 9531.696 9027.811 9317.923 9008.974 9568.691
## Jun 7 9667.129 9200.680 8935.346 9595.100 9034.753 9381.682 9008.974 9960.649
## Jul 7 9732.385 9104.020 8935.346 9658.504 8976.390 9445.441 9008.974 9827.055
## Aug 7 9797.640 9199.892 8935.346 9721.908 9008.661 9509.201 9008.974 9847.145
## Sep 7 9862.895 9086.560 8935.346 9785.312 8865.965 9572.960 9008.974 9785.331
## Oct 7 9928.150 9108.943 8935.346 9848.716 8852.677 9636.720 9008.974 9852.829
## Nov 7 9993.406 9174.160 8935.346 9912.120 8843.194 9700.479 9008.974 9794.828
## Dec 7 10058.661 9166.618 8935.346 9975.524 8900.268 9764.239 9008.974 10084.472
## ...9 ...10
## Jan 7 9172.684 9468.380
## Feb 7 9290.550 9536.761
## Mar 7 9355.715 9605.141
## Apr 7 9420.881 9673.521
## May 7 9486.046 9741.901
## Jun 7 9551.212 9810.282
## Jul 7 9616.377 9878.662
## Aug 7 9681.543 9947.042
## Sep 7 9746.708 10015.423
## Oct 7 9811.873 10083.803
## Nov 7 9877.039 10152.183
## Dec 7 9942.204 10220.563
# Create a data frame for the forecast results
forecast_results_df <- data.frame(
forecast_results
)
# Save the forecast results to Excel
write.xlsx(forecast_results_df, "D:/Dataoutput_forecast_per_column.xlsx")
ssaAPB=ssa(frame_daging_log,36)
plot(ssaAPB)
ssaAPB$sigma
## [1] 1013.8401085 4.1716425 3.9437670 2.7296936 2.6822809
## [6] 2.4228223 2.3979808 1.9632726 1.6353721 1.5671474
## [11] 1.4015890 1.1863005 1.1819543 1.0805019 1.0415161
## [16] 1.0182640 0.9636772 0.7875714 0.7650319 0.7457638
## [21] 0.7358670 0.7022417 0.6248702 0.5996979 0.5891723
## [26] 0.5207410 0.5007785 0.4849706 0.4679310 0.4557317
## [31] 0.4363597 0.4104731 0.3805248 0.3751769 0.3530436
## [36] 0.3475041
plot(ssaAPB, type= 'paired')
plot(wcor(ssaAPB, 1:10))
ramalAPB=rforecast(ssaAPB,groups=list(c(1)), len=12)
ramalAPB
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 9.153894 9.157251 9.159277 9.152927 9.157018 9.155914 9.157494 9.156179
## [2,] 9.164370 9.167732 9.169760 9.163402 9.167499 9.166393 9.167975 9.166658
## [3,] 9.174875 9.178241 9.180271 9.173906 9.178007 9.176901 9.178484 9.177166
## [4,] 9.185412 9.188781 9.190814 9.184442 9.188548 9.187440 9.189025 9.187705
## [5,] 9.195980 9.199353 9.201389 9.195009 9.199119 9.198010 9.199597 9.198276
## [6,] 9.206579 9.209956 9.211994 9.205607 9.209722 9.208612 9.210200 9.208877
## [7,] 9.217208 9.220589 9.222629 9.216235 9.220354 9.219243 9.220833 9.219509
## [8,] 9.227863 9.231248 9.233290 9.226889 9.231013 9.229900 9.231493 9.230167
## [9,] 9.238542 9.241931 9.243976 9.237566 9.241696 9.240582 9.242176 9.240848
## [10,] 9.249245 9.252638 9.254685 9.248268 9.252402 9.251287 9.252883 9.251554
## [11,] 9.259971 9.263367 9.265416 9.258992 9.263131 9.262015 9.263613 9.262282
## [12,] 9.270727 9.274128 9.276179 9.269748 9.273892 9.272773 9.274373 9.273041
## [,9] [,10]
## [1,] 9.154874 9.155718
## [2,] 9.165351 9.166196
## [3,] 9.175857 9.176703
## [4,] 9.186395 9.187242
## [5,] 9.196965 9.197813
## [6,] 9.207565 9.208414
## [7,] 9.218195 9.219045
## [8,] 9.228851 9.229702
## [9,] 9.239531 9.240383
## [10,] 9.250235 9.251088
## [11,] 9.260962 9.261816
## [12,] 9.271719 9.272574
SSA_bag<-data.frame(ramalAPB)
write_xlsx(SSA_bag,"D:/Dataoutput_forecast_bagging_SSA.xlsx")
# Transformasi dari log ke skala asli
SSA_bag_asli <- exp(SSA_bag)
# Beri nama kolom yang sesuai
colnames(SSA_bag_asli) <- paste0("Asli_", 1:ncol(SSA_bag_asli))
# Tampilkan hasil
cat("=== HASIL FORECAST DALAM SKALA ASLI ===\n")
## === HASIL FORECAST DALAM SKALA ASLI ===
print(SSA_bag_asli)
## Asli_1 Asli_2 Asli_3 Asli_4 Asli_5 Asli_6 Asli_7
## 1 9451.169 9482.957 9502.187 9442.035 9480.747 9470.285 9485.257
## 2 9550.706 9582.865 9602.320 9541.465 9580.630 9570.045 9585.193
## 3 9651.564 9684.101 9703.784 9642.216 9681.839 9671.131 9686.456
## 4 9753.799 9786.718 9806.632 9744.340 9784.430 9773.595 9789.100
## 5 9857.425 9890.733 9910.882 9847.855 9888.417 9877.455 9893.143
## 6 9962.460 9996.161 10016.548 9952.776 9993.818 9982.726 9998.600
## 7 10068.914 10103.014 10123.643 10059.116 10100.644 10089.421 10105.482
## 8 10176.774 10211.280 10232.154 10166.859 10208.881 10197.524 10213.777
## 9 10286.033 10320.950 10342.073 10276.001 10318.523 10307.031 10323.477
## 10 10396.712 10432.045 10453.420 10386.560 10429.589 10417.960 10434.603
## 11 10508.823 10544.579 10566.210 10498.550 10542.094 10530.325 10547.167
## 12 10622.472 10658.656 10680.546 10612.075 10656.141 10644.232 10661.275
## Asli_8 Asli_9 Asli_10
## 1 9472.789 9460.434 9468.423
## 2 9572.579 9560.079 9568.162
## 3 9673.694 9661.048 9669.225
## 4 9776.189 9763.394 9771.667
## 5 9880.079 9867.133 9875.504
## 6 9985.381 9972.283 9980.752
## 7 10092.107 10078.853 10087.423
## 8 10200.243 10186.831 10195.503
## 9 10309.781 10296.210 10304.986
## 10 10420.744 10407.011 10415.891
## 11 10533.142 10519.245 10528.231
## 12 10647.082 10633.018 10642.112
# Simpan ke Excel
write_xlsx(SSA_bag_asli, "D:/Dataoutput_forecast_bagging_SSA_asli.xlsx")
library(readxl)
SSA_Bagging <- read_excel("D:/Dataoutput_forecast_bagging_SSA_asli.xlsx")
View(SSA_Bagging)
ssaAPF=ssa(SSA_Bagging,6)
ramalAPF=rforecast(ssaAPF,groups=list(c(1:3)), len=12)
ramalAPF
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 10737.48 10774.09 10796.23 10726.96 10771.54 10759.49 10776.74 10762.38
## [2,] 10853.96 10890.99 10913.40 10843.31 10888.42 10876.23 10893.67 10879.15
## [3,] 10971.94 11009.42 11032.09 10961.17 11006.81 10994.48 11012.13 10997.43
## [4,] 11091.46 11129.38 11152.32 11080.56 11126.74 11114.26 11132.12 11117.25
## [5,] 11212.44 11250.81 11274.02 11201.42 11248.14 11235.51 11253.59 11238.54
## [6,] 11334.83 11373.64 11397.11 11323.68 11370.94 11358.17 11376.45 11361.22
## [7,] 11458.86 11498.12 11521.88 11447.58 11495.39 11482.47 11500.97 11485.56
## [8,] 11584.47 11624.21 11648.24 11573.06 11621.44 11608.37 11627.08 11611.50
## [9,] 11711.56 11751.76 11776.07 11700.01 11748.96 11735.73 11754.66 11738.90
## [10,] 11840.10 11880.75 11905.34 11828.42 11877.93 11864.55 11883.69 11867.75
## [11,] 11970.30 12011.42 12036.30 11958.48 12008.56 11995.03 12014.40 11998.27
## [12,] 12102.27 12143.89 12169.07 12090.31 12141.00 12127.30 12146.90 12130.58
## [,9] [,10]
## [1,] 10748.15 10757.35
## [2,] 10864.75 10874.06
## [3,] 10982.86 10992.28
## [4,] 11102.51 11112.04
## [5,] 11223.62 11233.27
## [6,] 11346.14 11355.89
## [7,] 11470.30 11480.17
## [8,] 11596.05 11606.04
## [9,] 11723.28 11733.38
## [10,] 11851.95 11862.17
## [11,] 11982.28 11992.62
## [12,] 12114.40 12124.86
SSA_2025<-data.frame(ramalAPF)
write_xlsx(SSA_2025,"D:/Dataoutput_forecast_bagging_SSA_2025.xlsx")