I am forecasting GDP growth for my dissertation. The data is from 1984-2017 (quaterly values). I am using the following code to fit and forecast a linear regression model.
library(forecast) attach(mydata) ts(GDPgrowth, start = 1984, end = 2017, frequency = 4) ts(spread, start = 1984, end = 2017, frequency = 4)
---sample of the first 27 observations---
model = lm(GDPgrowth ~ spread, data= mydata[1:27, ]) summary(model) prediction = predict(model, mydata[28:38, ], type = "response")
---this works and i get the predicted values for observations 28-38. now when i try the same for the entire data set i get NA for all the valued beyond 2017.---
model1 = lm(GDPgrowth ~ spread, data= mydata) summary(model1) prediction1 = predict(model, mydata[134:145, ], type = "response") prediction1
1 2 3 4 5 6 7 8 9 10 11 12 1.064371 NA NA NA NA NA NA NA NA NA NA NA
I have heard about using for loops to forecast but i am a beginner to r and world appreciate any help. thanks.