Background
Objective
Dataset Information
Dataset has been cleaned and pre-processed in the previous step (Power Consumption Visualization and Analysis).
Time Series Linear Model
# load forecast package
library(forecast)
# Apply time series linear regression to the sub-meter 3 time-series object with trand and season properties.
fitSM3 <- tslm(tsSM3_070809weekly ~ trend + season)
# Create the forecast for sub-meter 3. Forecast ahead 20 time periods(weeks), with a confidence levels 80 and 90.
forecastfitSM3c <- forecast(fitSM3, h=20, level=c(80,90))
## Plot sub-meter 3 forecast, limit y and add labels
plot(forecastfitSM3c, ylim = c(0, 20), ylab= "Watt-Hours", xlab="Time",
main = "Sub-meter 3 Forecast, Mondays, 20:00 ")
# Forecast of Sub-meter 1 for the first 20 weeks at 8pm of 2010.
fitSM1 <- tslm(tsSM1_070809weeklySat20 ~ trend + season)
forecastfitSM1 <- forecast(fitSM1, h = 20, level = c(80,90))
plot(forecastfitSM1, ylim = c(0, 70), ylab = "Watt-Hours", xlab = "Time",
main = "Sub-meter 1 Forecast, Saturdays, 20:00 ")
# Forecast of Sub-meter 2 for the first week of June, 2007..
fitSM2 <- tslm(tsSM2_07Monthly ~ trend + season)
forecastfitSM2 <- forecast(fitSM2, h = 168, level = c(80, 90))
plot(forecastfitSM2, ylim = c(0, 40), ylab = "Watt-Hours", xlab = "Time",
main = "Sub-meter 2 Forecast, First Week of Jun, 2007")
Decomposing a Seasonal Time Series
# Decompose Sub-meter 3 into trend, seasonal and random
components070809SM3weekly <- decompose(tsSM3_070809weekly)
## Plot decomposed sub-meter 3
plot(components070809SM3weekly)
# Sub-meter 1 decomposition and the plot
components070809SM1weekly <- decompose(tsSM1_070809weeklySat20)
plot(components070809SM1weekly)
# Sub-meter 2 decomposition and the plot
components07SM2monthly <- decompose(tsSM2_07Monthly)
plot(components07SM2monthly)
Holt-Winters Forecasting Since our data has trend and seasonal components, here we are going to try anoter way to make time series forecasting.
# Seasonal adjusting sub-meter 3 by subtracting the seasonal component
tsSM3_070809Adjusted <- tsSM3_070809weekly - components070809SM3weekly$seasonal
# Test Seasonal Adjustment by running Decompose again
plot(decompose(tsSM3_070809Adjusted))
# Holt Winters Exponential Smoothing
tsSM3_HW070809 <- HoltWinters(tsSM3_070809weekly)
# Forecast HoltWinters with confidence levels
tsSM3_HW070809for <- forecast(tsSM3_HW070809, h=25, level=c(85,95))
# Plot only the forecasted area
plot(tsSM3_HW070809for, ylim = c(0, 30), ylab= "Watt-Hours", xlab="Time - Sub-meter 3",
start(2010), main = "Sub-meter 3 HWForecast, Mondays, 20:00")
# Sub_meter 1
tsSM1_070809Adjusted <- tsSM1_070809weeklySat20 - components070809SM1weekly$seasonal
tsSM1_HW070809 <- HoltWinters(tsSM1_070809Adjusted)
tsSM1_HW070809for <- forecast(tsSM1_HW070809, h=25, level=c(85,95))
plot(tsSM1_HW070809for, ylim = c(0, 60), ylab= "Watt-Hours", xlab="Time - Sub-meter 1",
start(2010), main = "Sub-meter 1 HWForecast, Saturdays, 20:00")
# Sub_meter 2
tsSM2_07Adjusted <- tsSM2_07Monthly - components07SM2monthly$seasonal
tsSM2_HW07 <- HoltWinters(tsSM2_07Adjusted)
tsSM2_HW07for <- forecast(tsSM2_HW07, h=24, level=c(85,95))
plot(tsSM2_HW07for, ylim = c(0, 20), ylab= "Watt-Hours", xlab="Time - Sub-meter 2",
start(30), main = "Sub-meter 2 HWForecast, May, 2007")