<jupyter_start><jupyter_text>Exponential smoothing models for Power consumption forecasting<jupyter_code>library(readr)
library(dplyr)
library(ggplot2)
library(gridExtra)
library(xts)
library(fpp2)
library(forecast)
library(DMwR)
library(Metrics)
options(repr.plot.width=8, repr.plot.height=4)<jupyter_output><empty_output><jupyter_text>1. Data import<jupyter_code>path <- '../data/power-consumption/processed_data.csv'
data <- read_csv(path)
head(data)
tail(data)
nrow(data)<jupyter_output><empty_output><jupyter_text>2. Convert to ts object<jupyter_code># define date range as per the data
date_range <- seq(as.Date('2006-12-16'), as.Date('2010-11-26'), by='day')

# no need to put the date column as it will generate as ts index
dayofyear <- as.numeric(format(date_range[1], '%j'))

power_data <- ts(data[, 2], start=c(2006, dayofyear), frequency = 365)
head(power_data)
autoplot(power_data) +
    ggtitle('Global active power') +
    ylab('kWatt')<jupyter_output><empty_output><jupyter_text>3. Train test split<jupyter_code># train and test split
# last four weeks will be test set rest training set
# we will drop the explanatory  variables for baseline models
# as they will not be need for forecasting

test_size <- as.numeric(7*1)
train_size <- nrow(power_data)- test_size

train <- head(power_data, train_size)
test <- tail(power_data, test_size)

autoplot(train, series = 'train') + 
    autolayer(test, series = 'test') +
    ggtitle('Global active power') +
    ylab('kWatt') + 
    xlab('Year') +
    guides(colour=guide_legend(title="Data"))
tail(train)
head(test)
tail(test)<jupyter_output><empty_output><jupyter_text>Exponetial smoothing<jupyter_code># multi-step forecasting using Naive model
ets_multistep <- function(train, test, h=7){
                            n <- length(test)
                            test_pred <- ts(numeric(n), start=c(2010, 325), freq=365)

                            fit <- ets(train, model="ZZZ", damped=NULL, alpha=NULL, beta=NULL,
                                        gamma=NULL, phi=NULL, lambda=NULL, biasadj=FALSE,
                                        additive.only=FALSE, restrict=TRUE,
                                        allow.multiplicative.trend=FALSE)
                            train_pred <- fitted(fit)

                            for(i in 1:n)
                            {  
                              x <- window(power_data, end=2010 + (318+(i-1))/365)
                              refit <- ets(x, model=fit)
                              test_pred[i] <- forecast(refit, h=h)$mean[h]
                            }
                            output <- list(train=train_pred, test=test_pred, model=refit)
                            return(output) 
                            }
autoplot(test, series = 'test') +
    autolayer(result$test, series = 'test prediction') +
    xlab('Year') +
    ylab('kWatt') +
    ggtitle('Naive method forecasting')
train_pred <- result$train
test_pred <- result$test

train_rmse <- sqrt(mse(train, train_pred))
train_mae <- mae(train, train_pred)
train_nrmse <- train_rmse/sd(train)


test_rmse <- sqrt(mse(test, test_pred))
test_mae <- mae(test, test_pred)
test_nrmse <- test_rmse/sd(test)

# print(paste0('Training RMSE :', round(train_rmse, 3)))
print(paste0('Training NRMSE :', round(train_nrmse, 3)))
print(paste0('Training MAE :', round(train_mae, 3)))

# print(paste0('Test RMSE :', round(test_rmse, 3)))
print(paste0('Test NRMSE :', round(test_nrmse, 3)))
print(paste0('Test MAE :', round(test_mae, 3)))<jupyter_output>[1] "Training NRMSE :0.742"
[1] "Training MAE :324"
[1] "Test NRMSE :0.995"
[1] "Test MAE :332.433"
{'fraction_non_alphanumeric': 0.13595166163141995, 'fraction_numerical': 0.024718483932985445, 'mean_word_length': 2.954397394136808, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '449236', 'license': 'MIT', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1213, 'n_words': 251, 'path': '03.power_consumption/02.energy-forecasting-ETS.ipynb', 'repo_name': 'mkumar73/time-series', 'size': '3641'}