How to do it...  

Temperature prediction is particularly important for electricity generation, because electricity demand is highly dependent on it. We will work with a monthly time series containing average temperatures for Buenos Aires, Argentina from January 2001 to December 2018. The objective is to use the forecast package to build a model automatically that can be used for prediction. 

  1. We load the forecast package, and then we load the temperature dataset, and we transform the data into a proper time series:
library(forecast) 
average_temp = read.csv("./temperature.csv")
raverage_temp$indice_tiempo = as.Date(average_temp$indice_tiempo,"%Y-%m-%d")
average_temp = ts(average_temp$temperatura_promedio,start=c(2001,1),frequency = 12)
  1. We plot the series, mainly to identify whether we have a trend, a seasonal component, and a nonzero mean. This series shows that no trend is discernible, and the seasonal component is very clear:
plot.ts(average_temp) 

The following output shows the average temperatures for Buenos Aires, Argentina:

  1. We run the auto.arima function, which will choose the best model for us. We set up a maximum AR p order = 5, a maximum MA q order = 5, a maximum seasonal P = 2, and a maximum seasonal Q = 2 (we will capture up to two years of seasonal correlation). We want to search among seasonal models, because we know that temperature is, almost by definition, seasonal.  Because the temperature should be nonzero on average, we will set up allowmean=TRUE. Because we don't think that there is a trend here, we should set up allowdrift =FALSE:
best_mode = auto.arima(average_temp,max.p=5,max.q=5,max.Q=2,max.P=2,allowmean   
= TRUE,allowdrift = FALSE)

The following output shows the best model found via the automatic auto.arima function:

  1. We now plot the series, along with the predicted values for it. Our predictions match the data very well, which should not be that surprising. The seasonal component is very large, and capturing it is usually not that hard:
plot.ts(average_temp) 
lines(best_mode$fitted,col="red")

The following output shows the actual versus the predicted temperatures:

  1. Finally, we plot the forecast for the next 48 months. The data is stored in the forecast table. 

The following output shows the forecasts for the next 48 months:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.120.136