Time Series Linear Model
- Time series data with the aim of forecasting the future trend. Given that we have continuous time data, we need to apply regression for prediction.
- We created three different time series linear models for three different time periods.
- We used the tslm() function and forcast() functions to build the models and to make the future forecast.
- Trend and seasonality are the common property in time series data. Therefore, we have included both in our analysis.
- We limited y axis, and only ploted the forecast portion above zero. Because negative values do not make sense here.
# Import Library
library(forecast)
# Apply time series linear regression to the sub-meter 3 ts object with trend and season properties
fitSM3 <- tslm(tsSM3_070809weekly ~ trend + season)
# Create the forecast for sub-meter 3. Forecast ahead 20 times periods and with 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= "Garage & Yard Forecast 2007-2009 Every Monday 8:00pm")

# My trial (weekly--"Saturday") for Sub-meter 1
# Subset to 1 observation per week on Satursday at 8:00pm for 2007, 2008 and 2009
fitSM1 <- tslm(tsSM1_070809weekly_Sat ~ trend + season)
# Create Sub-meter 1 forecast with confidence levels 80 and 90
forecastfitSM1c <- forecast(fitSM1, h=24, level=c(80,90))
# Plot sub-meter 3 forecast, limit y and add labels
plot(forecastfitSM1c, ylim = c(0, 20), ylab= "Watt-Hours", xlab="Time", main = "Kitchen Forecast 2007-2009 Every Saturday 8:00pm")

# My trail Sub-Meter 2 (2007-2009) daily data for 365 days, taken at 18:00pm per day
fitSM2 <- tslm(tsSM2_070809daily ~ trend + season)
# Create sub-meter 2 forecast & forecast ahead 24 (1 day) period with confidence levels 80 and 90
forecastfitSM2c <- forecast(fitSM2, h=24, level=c(80,90))
# Plot sub-meter 2 forecast, limit y and add labels
plot(forecastfitSM2c, ylim = c(0, 20), ylab= "Watt-Hours", xlab="Time", main = "Laundry Room Forecast 2007-2009 Daily 8:00pm")

Decomposing a Seasonal Time Series
- It is necessary to adjust for seasonal component in the time series analysis. Thus, we can analyze the trend of a time series independently of the seasonal factors to better reveal what underlying trends are.
- We used the decompose() function in the forecast package, which estimates the trend, seasonal, and random components of a time series.
# Decompose Sub-meter 3 into trend, seasonal and random
components070809SM3weekly <- decompose(tsSM3_070809weekly)
# Plot decomposed sub-meter 3
plot(components070809SM3weekly)

- Decomposition of Sub-meter 3 (Water Heater & AC) power consumption at 8:00pm every Monday from 2007 to 2009.
- Trend component shows some local minimums like Aug 2007 (lowest), May 2008 and Feb 2009.
- Suggest to investigate on these time periods to find out more insights on how to save the energy in the future.
# Decompose Sub-meter 1 into trend, seasonal and random
components070809SM1weekly_Sat <- decompose(tsSM1_070809weekly_Sat)
# Plot decomposed sub-meter 1
plot(components070809SM1weekly_Sat)

- Decomposition of Sub-meter 1 (Kitchen) power consumption at 8:00pm every Saturday from 2007 to 2009.
- Trend component shows a near flat line in the 1st half of 2008 and then it started to fluctuate afterwards.
# Decompose Sub-meter 2 into trend, seasonal and random
components070809SM2daily <- decompose(tsSM2_070809daily)
# Plot decomposed sub-meter 2
plot(components070809SM2daily)

- Decomposition of Sub-meter 2 (Laundry Room) power consumption at 8:00pm every day from 2007 to 2009.
- Trend component shows a regular daily pattern.
Holt-Winters Forecasting
- Using exponential smoothing to make forecast. i.e. repetitive over some period.
- First, we need to remove the seasonal component which identified via decomposition.
- Second, We ran decompose again to verify if the seasonal component has been removed.
- Third, we applied HoltWinters() function to fit for the model. Again, we created three different models for our three different time periods.
- For simplicity, we set both beta and gamma to FALSE, it is the simple exponential smoothing.
- If both set beta and gamma to TRUE, it is the triple exponential smoothing or Holt-Winters Forecasting.
- Last, we made the forecast and plots.
# Remove Seasonal Components
# Seasonal adjusting sub-meter 3 by subtracting the seasonal component & plot
tsSM3_070809Adjusted <- tsSM3_070809weekly - components070809SM3weekly$seasonal
# Test Seasonal Adjustment by running Decompose again
plot(decompose(tsSM3_070809Adjusted))

- Note: seasonal scale for the seasonal section. -1e-15 through 5e-16. That’s a decimal with 15 zeros before 1. A very very small number indeed.
# Holt Winters Simple Exponential Smoothing & Plot (if we do not set beta=false, gamma=false, then it is a Triple exponential smoothing)
tsSM3_HW070809 <- HoltWinters(tsSM3_070809weekly, beta=FALSE, gamma=FALSE)
plot(tsSM3_HW070809, ylim = c(0, 25))

# Forecast HoltWinters with diminished confidence levels
tsSM3_HW070809forC <- forecast(tsSM3_HW070809, h=25, level=c(10,25))
# Plot only the forecasted area
plot(tsSM3_HW070809forC, ylim = c(0, 20), ylab= "Watt-Hours", xlab="Time - Sub-meter 3", start(2010), main = "Garage & Yard HoltWinters Forecast, Every Monday 8:00pm")

- Note: We set the parameters to beta=FALSE, gamma=FALSE in above HoltWinters time series analysis, it actually refer to the simple exponential smoothing, as it only has a alpha level (y-intercept), therefore, the plot shows flat line.
# My trail Sub-Meter 1 (weekly--"Saturday") for SubMeter 1
# Seasonal adjusting sub-meter 1 by subtracting the seasonal component & plot
tsSM1_070809Adjusted <- tsSM1_070809weekly_Sat - components070809SM1weekly_Sat$seasonal
## Holt Winters Simple Exponential Smoothing & Plot
tsSM1_HW070809 <- HoltWinters(tsSM1_070809weekly_Sat, beta=FALSE, gamma=FALSE)
plot(tsSM1_HW070809, ylim = c(0, 25))

## Forecast HoltWinters with diminished confidence levels
tsSM1_HW070809forC <- forecast(tsSM1_HW070809, h=25, level=c(10,25))
## Plot only the forecasted area
plot(tsSM1_HW070809forC, ylim = c(0, 20), ylab= "Watt-Hours", xlab="Time - Sub-meter 1", start(2010), main = "Kitchen HoltWinters Forecast, Every Saturday 8:00pm")

- Note: We set the parameters to beta=FALSE, gamma=FALSE in above HoltWinters time series analysis, it actually refer to the simple exponential smoothing, as it only has a alpha level (y-intercept), therefore, the plot shows flat line.
# My trail Sub-Meter 2 (daily data for 365 days and from 2007-2009, taken at 18:00pm per day)
# Seasonal adjusting sub-meter 2 by subtracting the seasonal component & plot
tsSM2_070809Adjusted <- tsSM2_070809daily - components070809SM2daily$seasonal
# Holt Winters Triple Exponential Smoothing & Plot
tsSM2_HW070809 <- HoltWinters(tsSM2_070809daily)
plot(tsSM2_HW070809, ylim = c(0, 25))

# Forecast HoltWinters with 80-90 confidence levels
tsSM2_HW070809forC <- forecast(tsSM2_HW070809, h=25, level=c(80,90))
# Plot only the forecasted area
plot(tsSM2_HW070809forC, ylim = c(0, 20), ylab= "Watt-Hours", xlab="Time - Sub-meter 2", start(2010), main = "Laundry Room HoltWinters Forecast, Everyday 8:00pm")

- Note: We code tsSM2_HW070809 <- HoltWinters(tsSM2_070809daily) without specifying the beta and gamma value, it is recongnized as a triple exponential smoothing or HoltWinters Forecasting. In that case, we have considered alpha (y-intercept), beta (trend, slope) and gamma (seasonality) and the plots are flutuated as shown in the above.