2. Proste Wykresy
summary(qgas)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 5.149 11.198 105.108 99.129 163.702 252.000
qgas_learn <- window(qgas, end = c(1990, 12))
qgas_test <- window(qgas, start = c(1991, 1))
ts.plot(qgas_learn, qgas_test, lty = c(1,2), xlim = c(1985, end(qgas_test)[1]), ylim = c(50, 250))
abline(v = time(qgas_test)[1], lty =3, col = "blue")
legend("topleft", legend = c("Dane uczące", "Dane testowe"), lty = c(1,2), col = c("black", "black"))

4. Modele Prognozowania
4.1 Modele Proste
H <- length(qgas_test)
fc_mean <- meanf(qgas_learn, h = H)
fc_naive <- naive(qgas_learn, h = H)
fc_snaive <- snaive(qgas_learn, h = H)
plot(qgas_learn, main = "Mean, Naive, SNaive vs Test", ylab = "Produkcja", lwd = 2,
xlim = c(1985, end(qgas_test)[1]), ylim = c(50, 250))
#lines(fc_mean$mean, col = "pink", lty = 2, lwd = 2)
lines(fc_naive$mean, col = "blue", lty = 3, lwd = 2)
lines(fc_snaive$mean, col = "green", lty = 4, lwd = 2)
lines(qgas_test, col = "purple", lty = 1, lwd = 2)
legend("topleft", legend = c("Dane uczące", "Mean", "Naive", "SNaive", "Test"),
col = c("black", "pink", "blue", "green", "purple"), lty = c(1,2,3,4,1))

4.2 Średnie Kroczące
fc_ma4 <- forecast(ma(qgas_learn, order = 4), h = H)
fc_ma8 <- forecast(ma(qgas_learn, order = 8), h = H)
fc_ma12 <- forecast(ma(qgas_learn, order = 12), h = H)
plot(qgas_learn, main = "Średnie kroczące vs Test", ylab = "Produkcja", lwd = 2,
xlim = c(1985, end(qgas_test)[1]), ylim = c(50, 250))
lines(fc_ma4$mean, col = "pink", lty = 2)
lines(fc_ma8$mean, col = "blue", lty = 3)
lines(fc_ma12$mean, col = "green", lty = 4)
lines(qgas_test, col = "purple", lty = 1)
legend("topleft", legend = c("Dane uczące", "MA(4)", "MA(8)", "MA(12)", "Test"),
col = c("black", "pink", "blue", "green", "purple"), lty = c(1,2,3,4,1))

4.3 Modele ARIMA
fit_auto <- auto.arima(qgas_learn)
fc_auto <- forecast(fit_auto, h = H)
fit_arima <- Arima(qgas_learn, order = c(1,1,1), seasonal = c(0,1,1))
fc_arima <- forecast(fit_arima, h = H)
plot(qgas_learn, main = "ARIMA vs Test", ylab = "Produkcja", lwd = 2,
xlim = c(1985, end(qgas_test)[1]), ylim = c(50, 250))
lines(fc_auto$mean, col = "pink", lty = 2)
lines(fc_arima$mean, col = "blue", lty = 3)
lines(qgas_test, col = "purple", lty = 1)
legend("topleft", legend = c("Dane uczące", "Auto ARIMA", "ARIMA(1,1,1)", "Test"),
col = c("black", "pink", "blue", "purple"), lty = c(1,2,3,1))

4.4 Model ETS
fit_ets <- ets(qgas_learn)
fc_ets <- forecast(fit_ets, h = H)
plot(qgas_learn, main = "ETS vs Test", ylab = "Produkcja", lwd = 2,
xlim = c(1985, end(qgas_test)[1]), ylim = c(50, 250))
lines(fc_ets$mean, col = "red", lty = 2)
lines(qgas_test, col = "blue", lty = 1)
legend("topleft", legend = c("Dane uczące", "ETS", "Test"), col = c("black", "red", "blue"), lty = c(1,2,1))

## TSLM
```r
fit_tslm <- tslm(qgas_learn ~ trend + season)
fc_tslm <- forecast(fit_tslm, h = H)
plot(qgas_learn, main="TSLM Forecast", ylab="Production", xlab="Time", lwd=2, xlim=c(1970, end(qgas_test)[1]), ylim=c(50,250))
lines(fc_tslm$mean, col="red", lty=2, lwd=2)
lines(qgas_test, col="blue", lty=1, lwd=2)
legend("bottomleft", legend=c("Train", "TSLM", "Test"), col=c("black", "red", "blue"), lty=c(1,2,1), lwd=2)

Prophet
df <- data.frame(ds = as.yearmon(time(qgas_learn)), y = as.numeric(qgas_learn))
modeldf <- prophet(df, yearly.seasonality = TRUE, weekly.seasonality = FALSE, daily.seasonality = FALSE)
futuredf <- make_future_dataframe(modeldf, periods = H, freq = "quarter")
forecastdf <- predict(modeldf, futuredf)
plot(modeldf, forecastdf)

prophet_plot_components(modeldf, forecastdf)

NNETAR
fit_nnet_tuned <- nnetar(qgas_learn, size = 115, repeats = 1)
fc_nnet_tuned <- forecast(fit_nnet_tuned, h = H)
plot(qgas_learn, main="NNETAR Forecast", ylab="Production", xlab="Time", lwd=2, xlim=c(1970, end(qgas_test)[1]), ylim=c(20,250))
lines(fc_nnet_tuned$mean, col="red", lty=2, lwd=2)
lines(qgas_test, col="blue", lty=1, lwd=2)
legend("bottomleft", legend=c("Train", "NNETAR", "Test"), col=c("black", "red", "blue"), lty=c(1,2,1), lwd=2)

ADAM
fit_adam <- adam(qgas_learn, model = "ZZZ")
fc_adam <- forecast(fit_adam, h = H)
plot(qgas_learn, main="ADAM Forecast", ylab="Production", xlab="Time", lwd=2, xlim=c(1970, end(qgas_test)[1]), ylim=c(50,250))
lines(fc_adam$mean, col="red", lty=2, lwd=2)
lines(qgas_test, col="blue", lty=1, lwd=2)
legend("bottomleft", legend=c("Train", "ADAM", "Test"), col=c("black", "red", "blue"), lty=c(1,2,1), lwd=2)

Porównanie modeli na jednym wykresie
time_forecast <- time(qgas_test)
plot_df <- data.frame(
Time = time_forecast,
qgas_test = as.numeric(qgas_test),
Naive = as.numeric(fc_naive$mean),
SNaive = as.numeric(fc_snaive$mean),
MA4 = as.numeric(fc_ma4$mean),
MA8 = as.numeric(fc_ma8$mean),
MA12 = as.numeric(fc_ma12$mean),
AutoARIMA = as.numeric(fc_auto$mean),
ARIMA111 = as.numeric(fc_arima$mean),
ETS = as.numeric(fc_ets$mean),
TSLM = as.numeric(fc_tslm$mean),
NNETAR = as.numeric(fc_nnet_tuned$mean),
ADAM = as.numeric(fc_adam$mean)
)
plot_ly(data = plot_df, x = ~Time) %>%
add_lines(y = ~qgas_test, name = "Qgas Test", line = list(color = 'black')) %>%
add_lines(y = ~Naive, name = "Naive") %>%
add_lines(y = ~SNaive, name = "SNaive") %>%
add_lines(y = ~MA4, name = "MA(4)") %>%
add_lines(y = ~MA8, name = "MA(8)") %>%
add_lines(y = ~MA12, name = "MA(12)") %>%
add_lines(y = ~AutoARIMA, name = "Auto ARIMA") %>%
add_lines(y = ~ARIMA111, name = "ARIMA(1,1,1)") %>%
add_lines(y = ~ETS, name = "ETS") %>%
add_lines(y = ~TSLM, name = "TSLM") %>%
add_lines(y = ~NNETAR, name = "NNETAR") %>%
add_lines(y = ~ADAM, name = "ADAM") %>%
layout(title = "Forecast Comparison", xaxis = list(title = "Time"), yaxis = list(title = "Production"), legend = list(orientation = "h", x = 0.1, y = -0.2))