Load Require Libraries
{r} library(forecast) library(tseries) library(ggplot2) library(googledrive) library(seastests)
Import Libraries from Google Drive, So u dont need to Input manually
```{r} file_id <- “16oU7e4C0C3El8G0z43PsyQ1DPreS8cFd” temp_file <- tempfile(fileext = “.csv”)
#Check file in temporary if (!file.exists(temp_file)) { drive_download(as_id(file_id), path = temp_file, overwrite = TRUE) } # read the file data <- read.csv(temp_file)
head(data)
Convert the timestamp into standart timestamp format, and Split between train and test
```{r}
data$ts <- as.POSIXct(data$ts, format = "%Y-%m-%dT%H:%M:%SZ")
head(data)
train <- data[data$ts < as.POSIXct("2020-02-26 00:00:00"), ]
test <- data[data$ts >= as.POSIXct("2020-02-26 00:00:00"), ]
# View the test data
head(test)
Check Missing or Infinite Values
```{r} any(is.na(train\(rate)) any(is.nan(train\)rate)) any(is.infinite(train$rate))
Convert into time series data, and check for stationarity and seasonality
```{r}
# Convert Training Data to Time Series
train_ts <- ts(train$rate, frequency = 144)
# Stationarity Test
adf_result <- adf.test(train_ts)
print(adf_result)
# Seasonality Test
is_seasonal <- isSeasonal(train$rate, freq = 144)
print(is_seasonal)
Step 1 : Box-Cox Transfromation is needed?
```{r} lambda <- BoxCox.lambda(train_ts) print(paste(“Optimal Lambda for Box-Cox:”, lambda))
if (abs(lambda - 1) > 0.01) { train_ts <- BoxCox(train_ts, lambda) print(“Box-Cox Transformation Applied.”) } else { print(“Box-Cox Transformation Not Needed.”) }
Step 2: Check Stationarity and Differencing
```{r}
adf_test_original <- adf.test(train_ts)
print(adf_test_original)
if (adf_test_original$p.value > 0.05) {
train_ts_diff <- diff(train_ts, differences = 1)
print("Differencing applied.")
adf_test_diff <- adf.test(train_ts_diff)
print(adf_test_diff)
} else {
train_ts_diff <- train_ts
print("No differencing required.")
}
Step 3 : Analyze ACF and PACF to Determine p and q
```{r} acf_plot <- acf(train_ts_diff, main = “ACF of Differenced Data”) pacf_plot <- pacf(train_ts_diff, main = “PACF of Differenced Data”)
acf_lags <- which(abs(acf_plot\(acf) > 2 / sqrt(length(train_ts_diff))) pacf_lags <- which(abs(pacf_plot\)acf) > 2 / sqrt(length(train_ts_diff))) print(paste(“Significant ACF Lags for q:”, paste(acf_lags, collapse = “,”))) print(paste(“Significant PACF Lags for p:”, paste(pacf_lags, collapse = “,”)))
Step 4 : Check for Seasonality (P, D, Q, and Period)
```{r}
acf(train_ts, lag.max = 144, main = "ACF for Seasonal Pattern")
pacf(train_ts, lag.max = 144, main = "PACF for Seasonal Pattern")
print("Identify seasonal lags from the ACF/PACF for multiples of the seasonal period (e.g., 144).")
Step 5 Fit ARIMA Model
{r} fit <- arima(train$rate, order = c(2, 1, 1), seasonal = list(order = c(1, 0, 1), period = 144), method = "CSS") summary(fit)
RESIDUAL ANALYSIS
{r} residuals_fit <- residuals(fit)
{r} # 1. White Noise Test: Box-Pierce Test box_test <- Box.test(residuals_fit, lag = 144, type = "Box-Pierce") print(box_test)
```{r}
ljung_box_test <- Box.test(residuals_fit, lag = 144, type = “Ljung-Box”) print(ljung_box_test)
```{r}
# 3. Normality Test: Kolmogorov-Smirnov Test
num_ties <- sum(duplicated(residuals_fit))
total_data <- length(residuals_fit)
threshold <- 0.05
if (num_ties / total_data <= threshold) {
ks_test <- ks.test(residuals_fit, "pnorm", mean(residuals_fit), sd(residuals_fit))
print(ks_test)
} else {
print("Ties Proportion Higher than 5%, Cannot use K-S Test.")
}
{r} # 4. Normality Test: Anderson-Darling Test ad_test <- ad.test(residuals_fit) print(ad_test)
{r} # 5. Normality Test: Shapiro-Wilk Test shapiro_test <- shapiro.test(residuals_fit) print(shapiro_test)
Conclusion: You can look the result here,Compared with original test data With RMSE 0.3
| TS | Actual | Predict |
| 2020-02-26T00:00:00Z | 141 | 141.5174 |
| 2020-02-26T00:10:00Z | 128 | 139.5495 |
| 2020-02-26T00:20:00Z | 130 | 135.692 |
| 2020-02-26T00:30:00Z | 134 | 136.5786 |
| 2020-02-26T00:40:00Z | 142 | 136.6773 |
| 2020-02-26T00:50:00Z | 142 | 134.5072 |
| 2020-02-26T01:00:00Z | 136 | 134.1035 |
| 2020-02-26T01:10:00Z | 135 | 130.0035 |
| 2020-02-26T01:20:00Z | 140 | 127.8785 |
| 2020-02-26T01:30:00Z | 129 | 126.2612 |
| 2020-02-26T01:40:00Z | 129 | 125.9926 |
| 2020-02-26T01:50:00Z | 125 | 125.4369 |
| 2020-02-26T02:00:00Z | 103 | 126.6704 |
| 2020-02-26T02:10:00Z | 107 | 124.7865 |
| 2020-02-26T02:20:00Z | 93 | 122.5993 |
| 2020-02-26T02:30:00Z | 86 | 120.0269 |
| 2020-02-26T02:40:00Z | 91 | 120.3486 |
| 2020-02-26T02:50:00Z | 91 | 117.031 |
| 2020-02-26T03:00:00Z | 86 | 116.1453 |
| 2020-02-26T03:10:00Z | 101 | 116.2643 |
| 2020-02-26T03:20:00Z | 70 | 113.9112 |
| 2020-02-26T03:30:00Z | 75 | 113.0112 |
| 2020-02-26T03:40:00Z | 95 | 115.4018 |
| 2020-02-26T03:50:00Z | 101 | 112.1715 |
| 2020-02-26T04:00:00Z | 81 | 111.3802 |
| 2020-02-26T04:10:00Z | 71 | 109.0358 |
| 2020-02-26T04:20:00Z | 92 | 110.1733 |
| 2020-02-26T04:30:00Z | 88 | 107.5942 |
| 2020-02-26T04:40:00Z | 88 | 101.6167 |
| 2020-02-26T04:50:00Z | 56 | 96.12061 |
| 2020-02-26T05:00:00Z | 55 | 94.63293 |
| 2020-02-26T05:10:00Z | 61 | 99.29391 |
| 2020-02-26T05:20:00Z | 76 | 96.83201 |
| 2020-02-26T05:30:00Z | 84 | 97.03091 |
| 2020-02-26T05:40:00Z | 74 | 96.93842 |
| 2020-02-26T05:50:00Z | 86 | 94.32271 |
| 2020-02-26T06:00:00Z | 65 | 96.92843 |
| 2020-02-26T06:10:00Z | 77 | 96.61597 |
| 2020-02-26T06:20:00Z | 69 | 96.50155 |
| 2020-02-26T06:30:00Z | 77 | 96.37299 |
| 2020-02-26T06:40:00Z | 83 | 92.81127 |
| 2020-02-26T06:50:00Z | 80 | 96.04254 |
| 2020-02-26T07:00:00Z | 74 | 98.36837 |
| 2020-02-26T07:10:00Z | 71 | 97.25855 |
| 2020-02-26T07:20:00Z | 71 | 100.286 |
| 2020-02-26T07:30:00Z | 83 | 102.161 |
| 2020-02-26T07:40:00Z | 86 | 105.093 |
| 2020-02-26T07:50:00Z | 125 | 114.1457 |
| 2020-02-26T08:00:00Z | 135 | 116.1138 |
| 2020-02-26T08:10:00Z | 127 | 118.2818 |
| 2020-02-26T08:20:00Z | 116 | 117.5348 |
| 2020-02-26T08:30:00Z | 121 | 124.8011 |
| 2020-02-26T08:40:00Z | 157 | 128.5446 |
| 2020-02-26T08:50:00Z | 172 | 129.26 |
| 2020-02-26T09:00:00Z | 146 | 133.2686 |
| 2020-02-26T09:10:00Z | 141 | 135.7412 |
| 2020-02-26T09:20:00Z | 144 | 136.0459 |
| 2020-02-26T09:30:00Z | 163 | 140.628 |
| 2020-02-26T09:40:00Z | 173 | 150.0062 |
| 2020-02-26T09:50:00Z | 173 | 152.2175 |
| 2020-02-26T10:00:00Z | 176 | 157.4188 |
| 2020-02-26T10:10:00Z | 180 | 163.198 |
| 2020-02-26T10:20:00Z | 181 | 165.3746 |
| 2020-02-26T10:30:00Z | 185 | 166.8319 |
| 2020-02-26T10:40:00Z | 190 | 168.0124 |
| 2020-02-26T10:50:00Z | 190 | 168.0379 |
| 2020-02-26T11:00:00Z | 192 | 171.0506 |
| 2020-02-26T11:10:00Z | 197 | 175.1155 |
| 2020-02-26T11:20:00Z | 188 | 176.3258 |
| 2020-02-26T11:30:00Z | 186 | 178.3961 |
| 2020-02-26T11:40:00Z | 182 | 179.4963 |
| 2020-02-26T11:50:00Z | 200 | 180.1012 |
| 2020-02-26T12:00:00Z | 205 | 180.8383 |
| 2020-02-26T12:10:00Z | 204 | 183.6205 |
| 2020-02-26T12:20:00Z | 206 | 184.8142 |
| 2020-02-26T12:30:00Z | 214 | 186.6678 |
| 2020-02-26T12:40:00Z | 210 | 187.1752 |
| 2020-02-26T12:50:00Z | 207 | 186.7964 |
| 2020-02-26T13:00:00Z | 208 | 186.8564 |
| 2020-02-26T13:10:00Z | 205 | 189.0648 |
| 2020-02-26T13:20:00Z | 205 | 189.1767 |
| 2020-02-26T13:30:00Z | 209 | 190.1158 |
| 2020-02-26T13:40:00Z | 209 | 190.5881 |