I'm new to time series analysis in R and am having some trouble getting forecasts from a particular time series set and model I've built.
This is my time series data, it's 144 days of the asx200 closing price, but my classmates and I were never given the start and end dates, so I've defined the ts object as below
dataTS <- ts(data$`Closing Price`, frequency=255)
dataTS
Time Series:
Start = c(1, 1)
End = c(1, 144)
Frequency = 255
[1] 80.00000 80.00000 80.00000 80.00000 80.00000 80.00000 80.00000 80.00000 80.00000 81.38536 79.83485 79.93370 80.23789 81.33359 81.14499 79.80375 79.43928 81.41525
[19] 80.70474 79.69196 81.87581 83.51553 82.56475 80.87173 80.72899 83.64094 84.88065 82.33275 84.32522 84.89717 83.40710 81.37416 80.95209 82.92598 83.99487 81.82895
[37] 84.99273 85.50362 83.98170 81.17617 81.80690 86.13822 87.06587 83.57672 86.29252 85.58256 83.77695 80.39828 82.20623 88.28351 89.03392 85.63961 87.10639 84.92215
[55] 82.17131 80.00454 81.63989 87.61051 87.22371 84.96421 88.55203 87.53684 84.11937 82.27707 83.09376 87.79680 88.11651 85.91388 89.92467 88.68123 84.58792 83.52971
[73] 85.20692 90.62572 91.49070 90.07306 95.31403 93.25239 86.41805 83.94959 87.10673 93.23133 92.43702 90.80849 95.48193 91.64825 82.60358 78.17113 81.01983 86.86542
[91] 86.53665 85.42238 90.39699 86.85428 78.12587 74.29434 78.62797 84.89108 83.41155 80.45274 84.53619 80.45623 71.39215 66.56142 71.14703 75.80771 74.22025 72.14369
[109] 76.89173 71.38474 61.95091 57.37281 63.16960 67.80788 65.79209 63.46010 69.09905 63.59795 53.19971 49.52717 56.96862 60.89146 59.20133 58.66473 65.12076 58.77136
[127] 47.24968 44.21060 50.65772 52.97805 50.63288 50.26958 56.19423 48.64413 35.37176 30.81471 36.18630 38.41415 35.68011 34.78598 40.24962 33.00511 20.00861 15.46719
Following this, I've built this quadratic model:
t = time(dataTS)
t2 = t^2
model2 = lm(dataTS ~ t + t2)
summary(model2)
The model showed a good R^2 etc., so I want to do a forecast.
I've used the code below for the forecast:
h <- 5
t <- time(dataTS)
t2 <- t^2
aheadTimes <- data.frame(t= seq(1, 144+h, 1),
t2 = seq(1, 144+h, 1)^2)
frcModel2 <- predict(model2, newdata= aheadTimes, interval = 'prediction')
frcModel2
And this code below to plot it:
plot(dataTS, xlim=c(1,2), ylab="ASX200 Closing Price Series", main = "Forecasts from the quadratic model fitted to the ASX200 Closing Price Series")
lines(ts(as.vector(frcModel2[,3]), start = 1.59), col="blue", type="l")
lines(ts(as.vector(frcModel2[,1]), start = 1.59), col = "red", type="l")
lines(ts(as.vector(frcModel2[,2]), start = 1.59), col = "blue", type="l")
legend("topleft", lty=1, pch=1, col=c("black","blue","red"),
text.width = 18,
c("Data", "5% forecast limits", "Forecast"))
However, the resulting plot looks super weird and I don't really understand what I've done wrong?
I'm not sure whether I've messed up the forecast itself or the plot, but I definitely know the plot doesn't look how it should.
Any help would be appreciated, I'm a little stuck!
Let me know if I need to provide any further information to help you help me :)


