Logan Rasmussen June 4, 2021 Analysis of U.S. Electricity Monthly Total Net Generation Abstract This project attempts to accurately predict data points via forecasting on time series data using various time series analysis techniques in R. The data used, “Monthly Electricity Net Generation in the U.S.,” collected from January 1973 to June 2013, originates from the U.S. Energy Information Administration (EIA). The data was plotted to visualize the time series as well as its characteristics so that transformations and differencing could be appropriately applied. A model was identified to use for forecasting by analysing the ACF and PACF of the autocorrelations and partial autocorrelations of the transformed and differenced data and tested by diagnostic checking of the residuals. The model, ARIMA(1,1,1)(2,1,1)[12], was used to forecast 120 data points 10 years ahead. The forecast worked as intended but failed to reflect some aspects of the time series. Introduction The data was obtained from the R package “fpp2” which was created by Rob Hyndman. The data, which measures the generation of electricity in billions of kWh, was gathered monthly for 39 years and 5 months, totaling 472 data values. Net generation of electricity is generally considered to be correlated with population growth, advancement in technology, and increase of individual quality of living as a result of the two. The software used to analyze the time series is R. Accurate and effective forecasting requires accurate and effective models, and to formulate these models the original data was log-transformed, differenced to remove trend and seasonality, and chosen by observing the ACF and PACF after the necessary steps. Ultimately, these models didn’t pass diagnostic checking because the p-values of the portmanteau tests were below 0.05. However, an auto-arima model, ARIMA(1,1,1)(2,1,1)[12], improved in diagnostic tests compared to the previous models and was chosen as the model to forecast. Forecasting 120 data points 10 years ahead shows a leveling in trend and a no increase or decrease of variance over time. Based on the forecast, monthly electricity net generation in the U.S. will not drastically increase or decrease even with the recent increase in variance and leveling of trend. 1 Time Series Plot and Analysis In R created a time series plot of the “usmelec” dataset from the package fpp2. A mean and moving average line is added to the plot to better visualize the time series’ characteristics such as stationarity and trend. The mean of the data is 259.5576 and the variance is 4743.699 . There is an overall positive linear trend present within the time series, however a couple years before 2010 there is a noticeable leveling in the trend. There is also an increase of variance over time as well, so a transformation should be made to make the variance more constant over time. After, the transformation, differencing should be applied to make the data stationary. 2 Stationarity I calculated a lambda value for a possible Box-Cox transformation to reduce the increase of variance over time. The value of lambda given was λ = 0.1414141 . A log transformation of the data is applied because 0 is included within the confidence interval for lambda. The log transformation of the data still has a minor increase of variance over time, but less so compared to the original data. The mean and variance of the log-transformed data is 5.522271 and 0.07557367 respectively. Compared to the mean and variance of the data if it underwent a Box-Cox transformation, which are 8.380619 and 0.357679 respectively, the log transformation is the right choice as its values are lower, thus demonstrating a better effect on diminishing the weight of variance in later time. 3 Data is differenced at lag 1 to eliminate trend and lag 12 to eliminate seasonality. Variance of differencing: Original: 0.07557367 Differenced at lag 1: 0.007266157 Differenced at lag 12: 0.001463298 Differenced at additional lag: 0.003810601 The variance of the transformed data decreases after differencing at lag 1 and laf 12 again but increases after differencing an additional lag. To avoid overdifferencing, I did not difference anymore than at lags 1 and 12. The mean of the transformed data differenced at lags 1 and 12 is close to 0 at 0.000005361186. The trend is level, and seasonality is no longer apparent. The data now looks stationary. The histogram shows the data acting like white noise. 4 Preliminary Model Identification The candidate parameters are chosen from the SARIMA model because the data has a period of s = 12 and was differenced at d = 1 and D = 1. Possible q and Q parameters are ascertained from the ACF while possible p and P parameters are ascertained from the PACF. The ACF has spikes at lags 1, 2, and 12, so possible values to test are q = 1, 2, and Q = 1. The PACF has spikes at lags 1 and 2 so possible values to test are p = 1, 2. The PACF also has spikes at 12 and at following increments of 12, so it is reasonable to test P = 1, 2, 3. I chose four models to test: 1. SARIMA(1, 1, 1)(0, 1, 1)[12] 2. SARIMA(2, 1, 1)(0, 1, 1)[12] 3. SARIMA(1, 1, 2)(0, 1, 1)[12] 4. SARIMA(1, 1, 1)(3, 1, 1)[12] 5 Model Fitting and Diagnostic Checking The model coefficients are estimated using the maximum likelihood method. 1. SARIMA(1, 1, 1)(0, 1, 1)[12]: AR(1) MA(1) SMA(1) 0.4120 -0.8484 -0.8408 2. SARIMA(2, 1, 1)(0, 1, 1)[12]: AR(1) AR(2) MA(1) SMA(1) 0.4087 -0.0355 -0.8343 -0.8404 3. SARIMA(1, 1, 2)(0, 1, 1)[12]: AR(1) MA(1) MA(2) SMA(1) 0.2908 -0.7135 -0.0979 -0.8398 4. SARIMA(1, 1, 1)(3, 1, 1)[12]: AR(1) MA(1) SAR(1) SAR(2) SAR(3) SMA(1) 0.4123 -0.8467 -0.0427 -0.1369 -0.1011 -0.7763 AICc: 1. SARIMA(1, 1, 1)(0, 1, 1)[12]: -2082.016 2. SARIMA(2, 1, 1)(0, 1, 1)[12]: -2080.424 3. SARIMA(1, 1, 2)(0, 1, 1)[12]: -2080.559 4. SARIMA(1, 1, 1)(3, 1, 1)[12]: -2083.285 The model with the lowest AICc criterion value should be considered as the best model. The second and third models have the lowest AICc values, so p and q should each stay at 1. The fourth model has a higher AICc value compared to the first model by 1.269 as Q is increased to 3. However, the difference of values is less than 2, and as a result the fourth model can not be considered significantly better than the first model. Following the principle of parsimony, the first model, SARIMA(1, 1, 1)(0, 1, 1)[12], is a good choice as it has a low AICc value and has less parameters than the fourth model. SARIMA(1, 1, 1)(0, 1, 1)[12]: 6 When it comes to diagnostic checking, there are many possible models to test, however in this case a common problem is present among the four tested models as well as the models that were not tested: inadequacy. The mean( -0.000947825 ), variance( 0.0006655331 ), skewness( 0.2036996 ), and kurtosis( 0.5338779 ) of the residuals are within acceptable range to be considered having Gaussian properties. However, lag 10 and lag 15 spike over the confidence interval in the ACF and PACF. All of the lags should be within the 95% confidence interval, or extremely close. A single lag out of the confidence interval may not be enough to reject the null hypothesis depending on how lenient the decision-making is, so that is a condition that should be sought to attain. The normal q-q plot looks mostly normal, except that the data looks somewhat skewed to the right. Portmanteau tests should be conducted for further diagnostic checking. 7 The portmanteau tests output results that reject the notion that the sequence of rescaled residuals if the chosen model has properties similar to those of WN(0, 1) sequence. The df is calculated by subtracting p + q = 3 from the square root of n = 472, chosen to be 22. Box-Ljung Test: data: usmelec_res^2 X-squared = 25.258 , df = 22 , p-value = 0.2849 Box-Ljung test: data: usmelec_res X-squared = 38.692 , df = 19 , p-value = 0.00484 Box-Pierce test: data: usmelec_res X-squared = 37.508 , df = 19 , p-value = 0.006851 Shapiro-Wilk normality test: data: usmelec_res W = 0.99029 , p-value = 0.002711 The p-value for the Box-Ljung test of squared residuals is above 0.05. The rest of the p-values are below 0.05, thus the null hypothesis is rejected; the residuals do not resemble white noise. 8 There is another model that improves in AICc, ACF and PACF, and the portmanteau tests. Using the function auto.arima() in R gives us a better model. Auto.arima(): Series: usmelec ARIMA(1,1,1)(2,1,1)[12] Box Cox transformation: lambda= -0.5738168 Coefficients: ar1 ma1 sar1 sar2 sma1 0.3888 -0.8265 0.0403 -0.0958 -0.8471 s.e. 0.0630 0.0375 0.0555 0.0531 0.0341 sigma^2 estimated as 1.274e-06 : log likelihood= 2547.32 AIC= -5082.63 AICc= -5082.45 BIC= -5057.68 The new model, ARIMA(1,1,1)(2,1,1)[12], created by auto.arima() has a much higher AICc than before at -5082.45 . The AICc decreases further to -5083.98 if the coefficient for sar1 is changed to 0. Because the coefficient’s value is 0.0403 and its squared error is 0.0555 , 0 lies within the confidence interval for the coefficient. Updated Coefficients and AICc: ar1 ma1 sar1 sar2 sma1 0.3898 -0.8254 0 -0.1057 -0.8333 s.e. 0.0626 0.0371 0 0.0513 0.0308 AICc = -5083.98 ARIMA(1, 1, 1)(2, 1, 1)[12]: 9 The diagnostic checking of residuals between SARIMA(1, 1, 1)(0, 1, 1)[12], the old model, and ARIMA(1,1,1)(2,1,1)[12], the new model, can be compared. Compared to the previous model, mean and variance decreased, skewness remained the same, and kurtosis increased. The mean( 0.000001227523 ), variance( -0.08128992 ), skewness( 0.2036996 ), and kurtosis( 1.725181 ) of the residuals are within acceptable range to be considered having Gaussian properties. The kurtosis is a little high, but it is appropriately within range of -2 and 2. Compared to the previous model, The ACF and PACF no longer have a significant spike at lag 10, although the spike at lag 15 is still present. The normal q-q values fit closer to the q-q line now. The plots of the residuals resemble white noise. 10 Returning back to the portmanteau tests, it is expected that better results are attained with the new model compared to the old model. The df is calculated by subtracting p + q = 5 from the square root of n = 472, chosen to be 22. Box-Ljung test: data: usmelec_res^2 X-squared = 12.402 , df = 22 , p-value = 0.9485 Box-Ljung test: data: usmelec_res X-squared = 27.814 , df = 17 , p-value = 0.04715 Box-Pierce test: data: usmelec_res X-squared = 26.906 , df = 17 , p-value = 0.05945 Shapiro-Wilk normality test: data: usmelec_res W = 0.98344 , p-value = 2.431e-05 The p-value for the Box-Ljung test of squared residuals remains above 0.05. The p-value for the Box-Ljung test improved to above 0.05. The p-values for the Box-Ljung test of residuals and the Shapiro-Wilk normality test remain below 0.05, however the p-value for the Box-Ljung test did improve to be close to 0.05, from 0.00484 to 0.04715 If the teachings of this class are closely followed, the null hypothesis is rejected and the residuals are not considered white noise. However, this is the best model formulated for this time series data. The improvements in diagnostic checking signify a better model, and depending on the choice of the decision-maker, can be considered white noise, so forecasting will commence with the chosen model. 11 Forecasting I forecasted 120 data points 10 years ahead, from 2013 to 2023, using the model ARIMA(1, 1, 1)(2, 1, 1)[12]. The 95% confidence interval was calculated by adding and subtracting two times the squared errors of the predictions from the predictions themselves. The range of the confidence interval increases over time due to the fact that forecasting accuracy decreases over time. 12 Conclusion The chosen model ARIMA(1, 1, 1)(2, 1, 1)[12] was used to forecast monthly electricity net generation in the U.S. 10 years ahead, from 2013 to 2023. It can confidently be assumed at a level of alpha = 0.5 that the net generation of electricity in the U.S. in billions of kWh won’t rise above ~450 and won’t fall below ~200. The peaks and dips should generally fall between 400 and 300. A yearly range of 100 billion kWh seems generally normal for the years since the late 1990s. Overall, the forecast works, but there are improvements to be made. The start of the forecast should reflect the high peaks of the late 2000s, and the predictions for each of the 10 years look too similar. My goal was to predict data using the best chosen model. I carefully went through steps to ensure the process was completed to the best of my ability. There are many steps to take to improve on this project and expand on it. There may be more advanced models that fit the original data better and result in a better forecast. One could also obtain the updated data, compare with the forecast, and perform a new forecast with the complete set of data. Questions to further delve into such as why the forecast failed to predict reality and what actions can be taken from observing and predicting the data are necessary steps for a project such as this one to be holistically beneficial and meaningful, but with my goals in mind I successfully completed what I set out to do. 13 References 1. https://cran.r-project.org/web/packages/fpp2/fpp2.pdf 2. Professor Feldmen “Lecture notes and examples” 14 Appendix # Necessary Packages library(fpp2) library(urca) library(forecast) library(fBasics) library(qpcR) library(MASS) library(ggplot2) library(ggfortify) library(tidyverse) # Plot of time series data with moving average line and mean line plot(usmelec, ylab = "Generation of Electricity in Billions of kWh", xlab = "Time", main = " Monthly Electricity Net Generation in the U.S.", col = "black") lines(ma(usmelec, order = 50), col = "red") abline(h = mean(usmelec), col = "blue") # Mean and variance of original data var(usmelec) mean(usmelec) # Finding lambda value t <- 1:length(usmelec) fit <- lm(usmelec ~ t) bcTransform <- boxcox(usmelec ~ t, plotit = TRUE) lambda <- bcTransform$x[which(bcTransform$y == max(bcTransform$y))] lambda # Transformation along with properties usmelec_bc <- (1/lambda)*(usmelec^lambda-1) usmelec_log <- log(usmelec) mean(usmelec_bc) mean(usmelec_log) var(usmelec_bc) var(usmelec_log) plot(usmelec_bc) plot(usmelec_log, ylab = "usmelec Log", main = "Log Transformation") 15 # Differencing for linear trend and seasonality usmelec_diff <- diff(usmelec_log, 1) usmelec_sdiff <- diff(usmelec_diff, 12) usmelec_overdiff <- diff(usmelec_sdiff, 1) # Variance and mean after differencing var(usmelec_diff) var(usmelec_sdiff) var(usmelec_overdiff) mean(usmelec_sdiff) # Plot and histogram of differenced data plot(usmelec_sdiff, main = "Log Transformation Differenced at Lags 1 and 12") abline(h = mean(usmelec_sdiff), col = "blue") lines(ma(usmelec_sdiff, order = 50), col = "red") hist(usmelec_sdiff) # ACF and PACF of differenced data acf(usmelec_sdiff, lag.max = 50, main = "") title("ACF: First and Seasonally Differenced Time Series", line = -1, outer = TRUE) pacf(usmelec_sdiff, lag.max = 50, main = "") title("PACF: First and Seasonally Differenced Time Series", line = -1, outer = TRUE) # Four Candidate Models model_1 <- arima(x = usmelec_log, order = c(1, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12), method = "ML") AICc(model_1) model_1 model_2 <- arima(x = usmelec_log, order = c(2, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12), method = "ML") AICc(model_2) model_2 model_3 <- arima(x = usmelec_log, order = c(0, 1, 2), seasonal = list(order = c(0, 1, 1), period = 12), method = "ML") AICc(model_3) model_3 model_4 <- arima(x = usmelec_log, order = c(1, 1, 1), seasonal = list(order = c(2, 1, 1), period = 12), fixed = c(NA, NA, NA, NA, NA)) AICc(model_4) Model_4 16 # Auto-arima model auto_us = auto.arima(usmelec, seasonal = TRUE, stepwise = FALSE, approximation = FALSE, lambda = "auto", fixed = c(NA, NA, 0, NA, NA)) summary(auto_us) # Residuals Visuals checkresiduals(auto_us) usmelec_res = residuals(auto_us) qqnorm(usmelec_res) qqline(usmelec_res) pacf(usmelec_res, main = "") title("PACF of Residuals") # Residuals Tests Box.test(usmelec_res^2, lag = 22, fitdf = 0, type = c("Ljung-Box")) Box.test(usmelec_res, lag = 22, fitdf = 5, type = c("Ljung-Box")) Box.test(usmelec_res, lag = 22, fitdf = 5, type = c("Box-Pierce")) shapiro.test(usmelec_res) mean(usmelec_res) var(usmelec_res) skewness(usmelec_res) kurtosis(usmelec_res) # Forecasting transformed data plot(forecast(auto_us, level = c(95), h = 120), xlab = "Time", main = "Forecast of Transformed Data") # Forecasting original data auto_new <- arima(x = usmelec, order = c(1, 1, 1), seasonal = list(order = c(2, 1, 1), period = 12)) plot(forecast(auto_new, level = c(95), h = 120), xlab = "Time", main = "Forecast of Original Data") 17