Air ARIMA Algorithm
# Load necessary libraries
library(forecast)
library(ggplot2)
temperature_data <- read.csv("U:/CIS (Undergraduate)/Special Service_Predictive Model/Predicitive Model/Air_trainntestdata.csv")
temperature_data$Date <- as.Date(temperature_data$Date, format = "%d/%m/%Y %H:%M")
# Set the threshold for "failing" temperatures (-40°C)
threshold <- -40
# Create binary labels for "failing" and "non-failing" dates
temperature_data$Failure <- ifelse(temperature_data$Temperature > threshold, "Failure", "Non-Failure")
# Split the data into training and testing sets (80% training, 20% testing)
train_proportion <- 0.8
num_rows <- nrow(temperature_data)
train_size <- round(train_proportion * num_rows)
train_data <- temperature_data[1:train_size, ]
test_data <- temperature_data[(train_size + 1):num_rows, ]
# Plot the time series of air temperature with the threshold
ggplot(data = temperature_data, aes(x = Date, y = Temperature)) +
geom_line() +
geom_hline(yintercept = threshold, linetype = "dashed", color = "red") +
geom_vline(xintercept = test_data$Date[1], linetype = "dashed", color = "blue") +
labs(title = "Air Temperature Trend and Threshold",
x = "Date",
y = "Temperature (°C)") +
theme_minimal()

# Fit an ARIMA model to the training data
arima_model <- auto.arima(ts(train_data$Temperature))
# Print the model summary
summary(arima_model)
## Series: ts(train_data$Temperature)
## ARIMA(1,1,2)
##
## Coefficients:
## ar1 ma1 ma2
## 0.5937 -0.5185 -0.2203
## s.e. 0.0962 0.0967 0.0361
##
## sigma^2 = 27.22: log likelihood = -2532.01
## AIC=5072.03 AICc=5072.08 BIC=5090.89
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set -0.07951364 5.204257 1.780208 0.2144653 10.04248 1.0159
## ACF1
## Training set -0.001189639
# Make predictions for the next 5 years (1826 days for daily data)
forecast_values <- forecast(arima_model, h = 1826)
# Convert forecasted values to binary labels based on the threshold
predicted_labels <- ifelse(forecast_values$mean > threshold, "Failure", "Non-Failure")
# Calculate the accuracy rate for the testing data
accuracy_rate <- sum(predicted_labels == test_data$Failure) / length(test_data$Failure)
cat("Accuracy Rate on Testing Data:", accuracy_rate, "\n")
## Accuracy Rate on Testing Data: 8.821256
# Filter and print only future failing dates (temperature > -40°C)
future_failing_dates <- forecast_values$x[forecast_values$x > threshold]
cat("Predicted Future Failing Dates (Temperature > -40°C):\n")
## Predicted Future Failing Dates (Temperature > -40°C):
print(future_failing_dates)
## [1] -28.3692607 -28.7188703 -29.2320540 -27.3745733 -22.3815005 -29.3030851
## [7] -32.3617022 -22.9508122 -23.2365675 -25.6357933 -23.8828537 -25.2423270
## [13] -31.2312994 11.4823729 14.5517016 14.6657178 13.6556598 14.0870357
## [19] 13.6841479 13.3875479 13.7937616 13.9236806 13.4204746 13.8463989
## [25] 14.0582872 14.2364469 14.3575233 14.1442941 14.3747686 14.2184492
## [31] 14.3288774 14.5899654 14.4633451 14.5601260 14.7756034 13.9787500
## [37] 14.1947339 14.6482987 14.7869793 14.6060186 14.5272455 14.5743636
## [43] 14.8260650 14.9502316 14.7309492 14.6294562 14.8080673 14.7777547
## [49] 14.8267825 15.1141785 15.2734031 15.3349309 15.1416552 14.8918983
## [55] 15.1853821 14.8596182 14.8269909 14.7591667 14.3473960 14.5164700
## [61] 14.8950928 14.7468520 15.1785998 15.3067712 15.2259029 15.2836924
## [67] 15.4198612 15.5493058 15.2298035 15.3443173 15.4312502 15.6712734
## [73] 15.5474077 15.4634261 15.6428127 15.6698382 15.7723215 15.5498340
## [79] 15.5282063 15.8659458 16.0308915 15.8817016 15.7721066 15.8126046
## [85] 15.7657874 16.0098960 15.8737849 15.5279964 15.8700479 15.6659956
## [91] 15.8299076 15.9352085 15.8642710 15.8070025 15.8079169 15.5572803
## [97] 15.7425581 15.3948498 15.7213544 15.8640048 15.7031253 15.5452039
## [103] 15.5636576 15.8798612 15.3992130 15.7785651 16.0506021 15.7591785
## [109] 15.6253820 15.5113182 15.7489370 15.5474423 15.7462618 15.6746877
## [115] 15.5827317 15.5617480 15.9570373 16.1336807 15.9350351 16.0253938
## [121] 15.9894678 16.0464008 16.0314818 15.9198497 16.2614356 15.6412965
## [127] 15.6673499 16.0032642 16.2019732 16.3727125 15.6413358 15.5150047
## [133] 16.1019886 15.8806367 15.2412182 15.7158335 16.0516668 16.1531920
## [139] 16.1412241 16.2050547 16.0970720 16.2402318 16.2792594 16.2975002
## [145] 16.0405558 15.9207063 16.0594910 16.3306599 16.1236693 16.2146530
## [151] 15.7539238 15.9166669 15.6792247 16.3347918 16.3422340 16.4116785
## [157] 16.4792479 16.3153822 16.0586877 16.3805095 16.5417825 16.5034956
## [163] 16.3152088 16.0045606 15.9429054 15.9668867 15.3126042 15.9078936
## [169] 16.0915858 16.1229632 15.8488312 15.9148960 16.2927549 16.4003128
## [175] 16.0821993 16.3995257 16.3101507 16.3031715 16.5341090 16.1379053
## [181] 16.2071416 16.2260997 16.2900582 16.4043057 16.5173695 16.3574888
## [187] 16.1519496 16.0447061 16.2444794 16.0660188 16.0890048 16.3507990
## [193] 16.3450464 16.0689933 15.7293521 15.8146994 15.7726160 15.7322571
## [199] 15.8098730 15.8688427 15.9577432 16.1959378 16.6031830 15.9972456
## [205] 16.2574077 16.4725233 16.4836113 16.3809840 15.9205211 15.8787040
## [211] 15.6023499 16.2905266 16.6297331 16.2483451 16.1494678 16.0858565
## [217] 16.0469563 15.7374423 15.9545489 16.0803707 15.7645719 16.0967363
## [223] 16.1903705 16.1965406 16.7109085 6.5176750 -12.3030392 -19.2423692
## [229] -26.9859381 -24.5051516 -23.6876398 -24.2142592 -28.0296445 -24.9706171
## [235] -22.2339243 -26.8710714 -30.3328133 -34.1515170 -38.2693644 -35.4709153
## [241] -34.2732301 -34.4814590 -28.4401859 -20.8920434 -28.5553055 -17.6493019
## [247] 14.5542709 14.7806600 1.2044169 -34.9992265 -35.6212865 -36.2459742
## [253] -32.5831397 -31.3851890 -33.8952155 -36.3191043 -31.3050304 -33.3806185
## [259] -33.8290293 -34.9840685 -37.4891379 -35.4375341 -36.6705854 -35.3416941
## [265] -34.0554443 -35.3570453 -39.7206870 -27.0836306 -27.0184851 -2.5198496
## [271] -4.2713542 -1.5393866 0.7545949 2.1058797 2.9917014 3.0004283
## [277] -19.6108280 -36.2907561 -27.6856255 -9.7966885 2.0051693