The UK gas dataset contains quarterly UK gas consumption data from 1960 (Q1) to 1986 (Q4), measured in millions of therms.
This dataset is useful for analysing trends in gas consumption, identifying seasonal variations, and forecasting future energy demand.
The purpose of this project is to analyse UK quarterly gas consumption from 1960 to 1986 using the UK gas dataset. The main objectives are to:
To understand the historical trends in UK quarterly gas consumption, we plot the UKgas dataset over time. This helps identify long-term trends, seasonal variations, and potential anomalies.
## Loading required package: Rcpp
## Warning: package 'Rcpp' was built under R version 4.4.3
## Loading required package: rlang
## Warning: package 'rlang' was built under R version 4.4.3
Loads the Prophet package, which is used for time series forecasting.
## Warning: package 'zoo' was built under R version 4.4.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
Loads the zoo package, which helps in handling time series data and converting dates into proper formats.
ukgas_df <- data.frame(
ds = as.Date(as.yearmon(time(UKgas))), # Extracts time in yearmon format
y = as.numeric(UKgas) # Converts CO2 values into numeric
)This function converts the UKgas dataset into a format compatible with Prophet, and extracts the time indices, converts them to a year-month format, and ensures they are in the required “YYYY-MM-DD” date format, it also converts gas consumption values into numeric form for modeling.
## ds y
## 1 1960-01-01 160.1
## 2 1960-04-01 129.7
## 3 1960-07-01 84.8
## 4 1960-10-01 120.1
## 5 1961-01-01 160.1
## 6 1961-04-01 124.9
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
Stores the trained model, which learns patterns from past data (trends & seasonality).
future_dates<- prophet::make_future_dataframe(ukgas_model, periods = 8, freq = "quarter")
tail(future_dates)## ds
## 111 1987-07-01
## 112 1987-10-01
## 113 1988-01-01
## 114 1988-04-01
## 115 1988-07-01
## 116 1988-10-01
This function uses the trained Prophet model to forecast gas consumption for the next 2 years (8 quarters), storing predicted values along with uncertainty intervals.
plot(ukgas_model, ukgas_forecast,type ="1",col = "blue", xlab= "Year", ylab ="Gas Consumption (Millions of Therms)", main = expression("UK Quarterly Gas Consumption (1960-1986)"))The figure above shows the quarterly UK gas consumption from 1960 to 1986, along with a forecasted trend using the Prophet model.
This visualization helps in understanding historical gas usage trends and predicting future consumption, which is valuable for energy planning and policy decisions.
Plots trend and seasonal patterns in gas consumption based on the Prophet forecast.
Shows long-term trends, quarterly seasonality, and other components of the time series.
##
## Call:
## lm(formula = y ~ year, data = ukgas_df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -368.74 -84.20 2.89 74.27 513.32
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -47159.213 4074.600 -11.57 <2e-16 ***
## year 24.073 2.065 11.66 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 167.2 on 106 degrees of freedom
## Multiple R-squared: 0.5618, Adjusted R-squared: 0.5576
## F-statistic: 135.9 on 1 and 106 DF, p-value: < 2.2e-16
Trend analysis
Seasonality
Comparing Actual vs Predicted Values
# Predict gas consumption using the linear model
ukgas_df$predicted <- predict(lm_model)
# Plot actual vs. predicted values
plot(ukgas_df$ds, ukgas_df$y, type = "l", col = "blue",
xlab = "Year", ylab = "Gas Consumption (Millions of Therms)",
main = "Actual vs. Predicted Gas Consumption")
lines(ukgas_df$ds, ukgas_df$predicted, col = "red", lwd = 2)
legend("topleft", legend = c("Actual", "Predicted"),
col = c("blue", "red"), lty = 1, lwd = 2)The graph above compares the actual UK gas consumption (blue lines) with the predicted values from the linear regression model (red line) over the period 1960 to o1986.
Interpretation
The blue line represents the actual gas consumption quarterly. The trend of it going up and down indicates the strong seasonality usage. Overall the blue line shows an increase in the consumption of gas over the years.
The red line represents the predicted values from the linear regression model.The upward slope captures the overall increasing of the gas usage, however, the predicted values do not capture the seasonal fluctuations. It captures the general increase of gas usage in the long term but not the seasonal variations.
This comparison evaluates the accuracy of the regression model, showcasing its strengths and limitations in predicting UK gas consumption trends.
Checking Residuals
plot(ukgas_df$ds, residuals(lm_model), type = "p", col = "red",
xlab = "Year", ylab = "Residuals",
main = "Residuals of Linear Regression Model")
abline(h = 0, col = "black", lwd = 2)The residual plot above displays the difference between the actual and predicted gas consumption values over time. Residuals represent the errors in the model—how far off the predictions are from the actual values.
Interpretations
## ds y year predicted
## 1 1960-01-01 160.1 1960 24.67619
## 2 1960-04-01 129.7 1960 24.67619
## 3 1960-07-01 84.8 1960 24.67619
## 4 1960-10-01 120.1 1960 24.67619
## 5 1961-01-01 160.1 1961 48.74960
## 6 1961-04-01 124.9 1961 48.74960
## 'data.frame': 108 obs. of 4 variables:
## $ ds : Date, format: "1960-01-01" "1960-04-01" ...
## $ y : num 160.1 129.7 84.8 120.1 160.1 ...
## $ year : num 1960 1960 1960 1960 1961 ...
## $ predicted: num 24.7 24.7 24.7 24.7 48.7 ...
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 84.8 153.3 220.9 337.6 469.9 1163.9
## ds yhat yhat_lower yhat_upper
## 1 1960-01-01 197.081175 56.77199 344.376825
## 2 1960-04-01 -4.817374 -151.80553 137.093884
## 3 1960-07-01 -138.025678 -269.92465 3.290908
## 4 1960-10-01 77.187060 -72.36734 214.878425
## 5 1961-01-01 219.609022 88.28171 370.301920
## 6 1961-04-01 18.177628 -122.06914 163.698510
## (Intercept) year
## -47159.21270 24.07341
2.5 Comments on Results
While the model captures the general increase in gas consumption, a more advanced time series approach may improve predictions.