<p>I will continue in describing forecast methods, which are suitable to seasonal (or multi-seasonal) time series. In the previous <a href="https://petolau.github.io/Forecast-electricity-consumption-with-similar-day-approach-in-R/">post</a> <strong>smart meter</strong> data of electricity consumption were introduced and a forecast method using <strong>similar day approach</strong> was proposed. ARIMA and exponential smoothing (common methods of time series analysis) were used as forecast methods. The biggest disadvantage of this approach was that we created multiple models at once for different days in the week, which is computationally expensive and it can be a little bit unclear. Regression methods are more suitable for multi-seasonal times series. They can handle multiple seasonalities through independent variables (inputs of a model), so just one model is needed. In this post, I will introduce the most basic regression method - <strong>multiple linear regression</strong> (MLR).</p>
<p>I have prepared a file with four aggregated time series for analysis and forecast. It can be found on <a href="https://github.com/PetoLau/petolau.github.io/tree/master/_rmd">my github repo</a>, the name of the file is DT_4_ind. Of course, I’m using EnerNOC smart meter data again and time series were aggregated by four located industries. The file was created easily by the package <code>feather</code> (<a href="https://cran.r-project.org/package=feather">CRAN link</a>), so only by this package, you can read this file again. The <code>feather</code> is a useful tool to share data for <strong>R</strong> and Python users.</p>
<p>Data manipulation will be done by <code>data.table</code> package, visualizations by <code>ggplot2</code>, <code>plotly</code> and <code>animation</code> packages.</p>
<p>The first step to do some “magic” is to scan all of the needed packages.</p>
<pre><code><span>library</span><span>(</span><span>feather</span><span>)</span><span>
</span><span>library</span><span>(</span><span>data.table</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>plotly</span><span>)</span><span>
</span><span>library</span><span>(</span><span>animation</span><span>)</span></code></pre><p>Read the mentioned smart meter data by <code>read_feather</code> to one <code>data.table</code>.</p>
<pre><code><span>DT</span><span> </span><span>&lt;-</span><span> </span><span>as.data.table</span><span>(</span><span>read_feather</span><span>(</span><span>&quot;DT_4_ind&quot;</span><span>))</span></code></pre><p>Let’s plot, what I have prepared for you - aggregated time series of electricity consumption by industry.</p>
<pre><code><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>DT</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>date</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span> </span><span>facet_grid</span><span>(</span><span>type</span><span> </span><span>~</span><span> </span><span>.</span><span>,</span><span> </span><span>scales</span><span> </span><span>=</span><span> </span><span>&quot;free_y&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>),</span><span>
</span><span>panel.grid.major</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>),</span><span>
</span><span>panel.grid.major.x</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>),</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>10</span><span>),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>),</span><span>
</span><span>strip.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>9</span><span>,</span><span> </span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Date&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Load (kW)&quot;</span><span>)</span></code></pre><p></p>
<p>An interesting fact is that the consumption of the industry Food Sales &amp; Storage isn’t changing during holidays as much as others.</p>
<h3>Multiple linear regression model for double seasonal time series</h3>
<p>The aim of the multiple linear regression is to model dependent variable (output) by independent variables (inputs). Another target can be to analyze influence (correlation) of independent variables to the dependent variable. Like in the previous post, we want to forecast consumption one week ahead, so regression model must capture weekly pattern (seasonality). Variables (inputs) will be of two types of seasonal dummy variables - daily (\( d_1, \dots, d_{48} \)) and weekly (\( w_1, \dots, w_6 \)). In the case of the daily variable, there will be \( 1 \), when the consumption during the day will be measured at the particular time, otherwise \( 0 \). In the case of the week variable there will be \( 1 \), when the consumption is measured at the particular day of the week, otherwise \( 0 \).</p>
<p>The regression model can be formally written as:</p>
<p>where \( y_i \) is the electricity consumption at the time \( i \), where \( i = 1, \dots, N \). \( \beta_1, \dots, \beta_{54} \) are regression coefficients, which we want to estimate. \( d_1, \dots, d_{48} \) and \( w_1, \dots, w_6 \) are dummy independent variables. \( \varepsilon_i \) is a random error. Assumption for the errors are that they are independently identical distributed (i.i.d.) with distribution \( \varepsilon \sim N(0,~\sigma^2) \).</p>
<p>Estimation of regression coefficients is done by ordinary least squares (OLS). So if we wrote our model as:</p>
<p>where \( Y \) is a vector of the length \( N \), \( \beta \) is a vector of the length \( p \) and \( \mathbf{X} \) is a matrix of the size \( N\times p,\) then OLS estimation of \( \beta \) is:</p>
<p>You are maybe asking, where is independent variable \( w_7 \) or intercept \( \beta_0 \). We must omit them due to collinearity of independent variables. The model matrix \( \mathbf{X} \) must be a regular matrix, not singular. Thereto, intercept has no sense in the time series regression model, because we do not usually consider time 0.</p>
<h3>Regression analysis of time series</h3>
<p>Let’s finally do some regression analysis of our proposed model. Firstly, prepare <code>DT</code> to work with a regression model. Transform the characters of weekdays to integers.</p>
<pre><code><span>DT</span><span>[,</span><span> </span><span>week_num</span><span> </span><span>:=</span><span> </span><span>as.integer</span><span>(</span><span>as.factor</span><span>(</span><span>DT</span><span>[,</span><span> </span><span>week</span><span>]))]</span></code></pre><p>Store informations in variables of the type of industry, date, weekday and period.</p>
<pre><code><span>n_type</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>DT</span><span>[,</span><span> </span><span>type</span><span>])</span><span>
</span><span>n_date</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>DT</span><span>[,</span><span> </span><span>date</span><span>])</span><span>
</span><span>n_weekdays</span><span> </span><span>&lt;-</span><span> </span><span>unique</span><span>(</span><span>DT</span><span>[,</span><span> </span><span>week</span><span>])</span><span>
</span><span>period</span><span> </span><span>&lt;-</span><span> </span><span>48</span></code></pre><p>Let’s look at some data chunk of consumption and do regression analysis on it. I have picked aggregate consumption of education (schools) buildings for two weeks. Store it in variable <code>data_r</code> and plot it.</p>
<pre><code><span>data_r</span><span> </span><span>&lt;-</span><span> </span><span>DT</span><span>[(</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>2</span><span>]</span><span> </span><span>&amp;</span><span> </span><span>date</span><span> </span><span>%in%</span><span> </span><span>n_date</span><span>[</span><span>57</span><span>:</span><span>70</span><span>])]</span><span>
</span><span>ggplot</span><span>(</span><span>data_r</span><span>,</span><span> </span><span>aes</span><span>(</span><span>date_time</span><span>,</span><span> </span><span>value</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>),</span><span>
</span><span>panel.grid.major</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>),</span><span>
</span><span>panel.grid.major.x</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>),</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>10</span><span>),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Date&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Load (kW)&quot;</span><span>)</span></code></pre><p></p>
<p>Let’s now create the mentioned independent dummy variables and store all of them in the <code>matrix_train</code>. When we are using the method <code>lm</code> in <strong>R</strong>, it’s simple to define dummy variables in one vector. Just use <code>as.factor</code> for some vector of classes. We don’t need to create 48 vectors for daily dummy variables and 6 vectors for weekly dummy variables.</p>
<pre><code><span>N</span><span> </span><span>&lt;-</span><span> </span><span>nrow</span><span>(</span><span>data_r</span><span>)</span><span>
</span><span>window</span><span> </span><span>&lt;-</span><span> </span><span>N</span><span> </span><span>/</span><span> </span><span>period</span><span> </span><span># number of days in the train set
# 1, ..., period, 1, ..., period - and so on for the daily season # using feature &quot;week_num&quot; for the weekly season
</span><span>matrix_train</span><span> </span><span>&lt;-</span><span> </span><span>data.table</span><span>(</span><span>Load</span><span> </span><span>=</span><span> </span><span>data_r</span><span>[,</span><span> </span><span>value</span><span>],</span><span>
</span><span>Daily</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>rep</span><span>(</span><span>1</span><span>:</span><span>period</span><span>,</span><span> </span><span>window</span><span>)),</span><span>
</span><span>Weekly</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>data_r</span><span>[,</span><span> </span><span>week_num</span><span>]))</span></code></pre><p>Let’s create our first multiple linear model with the function <code>lm</code>. <code>lm</code> automatically add to the linear model intercept, so we must define it now <code>0</code>. Also, we can simply put all the variables to the model just by the dot - <code>.</code>.</p>
<pre><code><span>lm_m_1</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>Load</span><span> </span><span>~</span><span> </span><span>0</span><span> </span><span>+</span><span> </span><span>.</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>matrix_train</span><span>)</span></code></pre><pre><code><span>smmr_1</span><span> </span><span>&lt;-</span><span> </span><span>summary</span><span>(</span><span>lm_m_1</span><span>)</span><span>
</span><span>paste</span><span>(</span><span>&quot;R-squared: &quot;</span><span>,</span><span>
</span><span>round</span><span>(</span><span>smmr_1</span><span>$</span><span>r.squared</span><span>,</span><span> </span><span>3</span><span>),</span><span>
</span><span>&quot;, p-value of F test: &quot;</span><span>,</span><span>
</span><span>1</span><span>-</span><span>pf</span><span>(</span><span>smmr_1</span><span>$</span><span>fstatistic</span><span>[</span><span>1</span><span>],</span><span> </span><span>smmr_1</span><span>$</span><span>fstatistic</span><span>[</span><span>2</span><span>],</span><span> </span><span>smmr_1</span><span>$</span><span>fstatistic</span><span>[</span><span>3</span><span>]))</span></code></pre><pre><code>## [1] &quot;R-squared:  0.955 , p-value of F test:  0&quot;</code></pre><p>You can see a nice summary of the linear model, but I will omit them now because of its long length (we have 54 variables). So I’m showing you only the two most important statistics: R-squared and p-value of F-statistic of the goodness of fit. They seem pretty good.</p>
<p>Let’s look at the fitted values.</p>
<pre><code><span>datas</span><span> </span><span>&lt;-</span><span> </span><span>rbindlist</span><span>(</span><span>list</span><span>(</span><span>data_r</span><span>[,</span><span> </span><span>.</span><span>(</span><span>value</span><span>,</span><span> </span><span>date_time</span><span>)],</span><span>
</span><span>data.table</span><span>(</span><span>value</span><span> </span><span>=</span><span> </span><span>lm_m_1</span><span>$</span><span>fitted.values</span><span>,</span><span> </span><span>data_time</span><span> </span><span>=</span><span> </span><span>data_r</span><span>[,</span><span> </span><span>date_time</span><span>])))</span><span>
</span><span>datas</span><span>[,</span><span> </span><span>type</span><span> </span><span>:=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;Real&quot;</span><span>,</span><span> </span><span>&quot;Fitted&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>data_r</span><span>))]</span><span>
</span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>datas</span><span>,</span><span> </span><span>aes</span><span>(</span><span>date_time</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>type</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>type</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>0.8</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_bw</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Time&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Load (kW)&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Fit from MLR&quot;</span><span>)</span></code></pre><p></p>
<p>That’s horrible! We are missing something here.</p>
<p>Look at the fitted values vs. residuals now.</p>
<pre><code><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>data.table</span><span>(</span><span>Fitted_values</span><span> </span><span>=</span><span> </span><span>lm_m_1</span><span>$</span><span>fitted.values</span><span>,</span><span>
</span><span>Residuals</span><span> </span><span>=</span><span> </span><span>lm_m_1</span><span>$</span><span>residuals</span><span>),</span><span>
</span><span>aes</span><span>(</span><span>Fitted_values</span><span>,</span><span> </span><span>Residuals</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>1.7</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_hline</span><span>(</span><span>yintercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Fitted values vs Residuals&quot;</span><span>)</span></code></pre><p></p>
<p>This is the typical example of <a href="https://en.wikipedia.org/wiki/Heteroscedasticity"><strong>heteroskedasticity</strong></a> - occurrence of nonconstant residuals (variance) in a regression model. The linear regression has an assumption that residuals must be from \( N(0,~\sigma^2) \) distribution and they are i.i.d. In the other words, the residuals must be symmetrically around zero.</p>
<p>Let’s look at the next proof that our residuals are not normal. We can use normal Q-Q plot here. I’m using the function from this <a href="http://stackoverflow.com/questions/4357031/qqnorm-and-qqline-in-ggplot2/">stackoverflow question</a> to plot it by <code>ggplot2</code>.</p>
<pre><code><span>ggQQ</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>lm</span><span>){</span><span>
</span><span># extract standardized residuals from the fit
</span><span> </span><span>d</span><span> </span><span>&lt;-</span><span> </span><span>data.frame</span><span>(</span><span>std.resid</span><span> </span><span>=</span><span> </span><span>rstandard</span><span>(</span><span>lm</span><span>))</span><span>
</span><span># calculate 1Q/4Q line
</span><span> </span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>quantile</span><span>(</span><span>d</span><span>$</span><span>std.resid</span><span>[</span><span>!</span><span>is.na</span><span>(</span><span>d</span><span>$</span><span>std.resid</span><span>)],</span><span> </span><span>c</span><span>(</span><span>0.25</span><span>,</span><span> </span><span>0.75</span><span>))</span><span>
</span><span>x</span><span> </span><span>&lt;-</span><span> </span><span>qnorm</span><span>(</span><span>c</span><span>(</span><span>0.25</span><span>,</span><span> </span><span>0.75</span><span>))</span><span>
</span><span>slope</span><span> </span><span>&lt;-</span><span> </span><span>diff</span><span>(</span><span>y</span><span>)</span><span>/</span><span>diff</span><span>(</span><span>x</span><span>)</span><span>
</span><span>int</span><span> </span><span>&lt;-</span><span> </span><span>y</span><span>[</span><span>1L</span><span>]</span><span> </span><span>-</span><span> </span><span>slope</span><span> </span><span>*</span><span> </span><span>x</span><span>[</span><span>1L</span><span>]</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>d</span><span>,</span><span> </span><span>aes</span><span>(</span><span>sample</span><span> </span><span>=</span><span> </span><span>std.resid</span><span>))</span><span> </span><span>+</span><span>
</span><span>stat_qq</span><span>(</span><span>shape</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span><span> </span><span>+</span><span> </span><span># open circles
</span><span> </span><span>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Normal Q-Q&quot;</span><span>,</span><span> </span><span># plot title
</span><span> </span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Theoretical Quantiles&quot;</span><span>,</span><span> </span><span># x-axis label
</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Standardized Residuals&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span># y-axis label
</span><span> </span><span>geom_abline</span><span>(</span><span>slope</span><span> </span><span>=</span><span> </span><span>slope</span><span>,</span><span> </span><span>intercept</span><span> </span><span>=</span><span> </span><span>int</span><span>,</span><span> </span><span>linetype</span><span> </span><span>=</span><span> </span><span>&quot;dashed&quot;</span><span>,</span><span>
</span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>&quot;firebrick1&quot;</span><span>)</span><span> </span><span># dashed reference line
</span><span> </span><span>return</span><span>(</span><span>p</span><span>)</span><span>
</span><span>}</span></code></pre><pre><code><span>ggQQ</span><span>(</span><span>lm_m_1</span><span>)</span></code></pre><p></p>
<p>Of course, it is absolutely not normal (points must be close the red line).</p>
<p>What can we do now? Use other regression methods (especially nonlinear ones)? No. Let’s think about why this happened. We have seen on fitted values, that measurements during the day were moved constantly by the estimated coefficient of week variable, but the behavior during the day wasn’t captured. We need to capture this behavior because especially weekends behave absolutely different. It can be handled by defining <strong>interactions</strong> between day and week dummy variables to the regression model. So we multiply every daily variable with every weekly one. Again, be careful with collinearity and singularity of the model matrix, so we must omit one daily dummy variable (for example \( d_{1} \)). Fortunately, this is done in method <code>lm</code> automatically, when we use factors as variables.</p>
<p>Let’s train a second linear model. Interactions should solve the problem, that we saw in the plot of fitted values.</p>
<pre><code><span>lm_m_2</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>Load</span><span> </span><span>~</span><span> </span><span>0</span><span> </span><span>+</span><span> </span><span>Daily</span><span> </span><span>+</span><span> </span><span>Weekly</span><span> </span><span>+</span><span> </span><span>Daily</span><span>:</span><span>Weekly</span><span>,</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>matrix_train</span><span>)</span></code></pre><p>Look at R-squared of previous model and the new one with interactions:</p>
<pre><code><span>c</span><span>(</span><span>Previous</span><span> </span><span>=</span><span> </span><span>summary</span><span>(</span><span>lm_m_1</span><span>)</span><span>$</span><span>r.squared</span><span>,</span><span> </span><span>New</span><span> </span><span>=</span><span> </span><span>summary</span><span>(</span><span>lm_m_2</span><span>)</span><span>$</span><span>r.squared</span><span>)</span></code></pre><pre><code>##  Previous       New 
## 0.9547247 0.9989725</code></pre><p>R-squared seems better.</p>
<p>Look at the comparison of residuals of two fitted models. Using the interactive plot <code>plotly</code> here.</p>
<pre><code><span>ggplot</span><span>(</span><span>data.table</span><span>(</span><span>Residuals</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>lm_m_1</span><span>$</span><span>residuals</span><span>,</span><span> </span><span>lm_m_2</span><span>$</span><span>residuals</span><span>),</span><span>
</span><span>Type</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>rep</span><span>(</span><span>&quot;MLR - simple&quot;</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>data_r</span><span>)),</span><span>
</span><span>rep</span><span>(</span><span>&quot;MLR with interactions&quot;</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>data_r</span><span>)))),</span><span> </span><span>aes</span><span>(</span><span>Type</span><span>,</span><span> </span><span>Residuals</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>Type</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_boxplot</span><span>()</span><span>
</span><span>ggplotly</span><span>()</span></code></pre>
<p>This is much better than the previous model, it seems that interactions are working.</p>
<p>Prove it with a sequence of three plots - fitted values, fit vs. residuals and Q-Q plot.</p>
<pre><code><span>datas</span><span> </span><span>&lt;-</span><span> </span><span>rbindlist</span><span>(</span><span>list</span><span>(</span><span>data_r</span><span>[,</span><span> </span><span>.</span><span>(</span><span>value</span><span>,</span><span> </span><span>date_time</span><span>)],</span><span>
</span><span>data.table</span><span>(</span><span>value</span><span> </span><span>=</span><span> </span><span>lm_m_2</span><span>$</span><span>fitted.values</span><span>,</span><span> </span><span>data_time</span><span> </span><span>=</span><span> </span><span>data_r</span><span>[,</span><span> </span><span>date_time</span><span>])))</span><span>
</span><span>datas</span><span>[,</span><span> </span><span>type</span><span> </span><span>:=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;Real&quot;</span><span>,</span><span> </span><span>&quot;Fitted&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>nrow</span><span>(</span><span>data_r</span><span>))]</span><span>
</span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>datas</span><span>,</span><span> </span><span>aes</span><span>(</span><span>date_time</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>type</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>type</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>0.8</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_bw</span><span>()</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Time&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Load (kW)&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Fit from MLR&quot;</span><span>)</span></code></pre><p></p>
<pre><code><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>data.table</span><span>(</span><span>Fitted_values</span><span> </span><span>=</span><span> </span><span>lm_m_2</span><span>$</span><span>fitted.values</span><span>,</span><span>
</span><span>Residuals</span><span> </span><span>=</span><span> </span><span>lm_m_2</span><span>$</span><span>residuals</span><span>),</span><span>
</span><span>aes</span><span>(</span><span>Fitted_values</span><span>,</span><span> </span><span>Residuals</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_point</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>1.7</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_hline</span><span>(</span><span>yintercept</span><span> </span><span>=</span><span> </span><span>0</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;red&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Fitted values vs Residuals&quot;</span><span>)</span></code></pre><p></p>
<pre><code><span>ggQQ</span><span>(</span><span>lm_m_2</span><span>)</span></code></pre><p></p>
<p>Everything seems much better than in the previous model. The fitted values seem almost perfect.</p>
<p>I also tried to work with a linear trend to boost this model, but it did not help (wasn’t significant). So go ahead and forecast consumption with this model.</p>
<h3>Forecast with multiple linear regression</h3>
<p>Again, I build function (as in the previous <a href="https://petolau.github.io/Forecast-electricity-consumption-with-similar-day-approach-in-R/">post</a>) to return the forecast of the one week ahead. So we can then simply compare with STL+ARIMA method (was better than STL+ETS). Arguments of this function are just <code>data</code> and <code>set_of_date</code>, so it’s easy to manipulate. Let’s add everything needed to function <code>predWeekReg</code> to create a regression model and forecast.</p>
<pre><code><span>predWeekReg</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>data</span><span>,</span><span> </span><span>set_of_date</span><span>){</span><span>
</span><span># Subsetting the dataset by dates
</span><span> </span><span>data_train</span><span> </span><span>&lt;-</span><span> </span><span>data</span><span>[</span><span>date</span><span> </span><span>%in%</span><span> </span><span>set_of_date</span><span>]</span><span>
</span><span>N</span><span> </span><span>&lt;-</span><span> </span><span>nrow</span><span>(</span><span>data_train</span><span>)</span><span>
</span><span>window</span><span> </span><span>&lt;-</span><span> </span><span>N</span><span> </span><span>/</span><span> </span><span>period</span><span> </span><span># number of days in the train set
</span><span> </span><span># 1, ..., period, 1, ..., period - and so on for the daily season </span><span> </span><span># Using feature &quot;week_num&quot; for the weekly season
</span><span> </span><span>matrix_train</span><span> </span><span>&lt;-</span><span> </span><span>data.table</span><span>(</span><span>Load</span><span> </span><span>=</span><span> </span><span>data_train</span><span>[,</span><span> </span><span>value</span><span>],</span><span>
</span><span>Daily</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>rep</span><span>(</span><span>1</span><span>:</span><span>period</span><span>,</span><span> </span><span>window</span><span>)),</span><span>
</span><span>Weekly</span><span> </span><span>=</span><span> </span><span>as.factor</span><span>(</span><span>data_train</span><span>[,</span><span> </span><span>week_num</span><span>]))</span><span>
</span><span># Creation of the model
</span><span> </span><span>lm_m</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>Load</span><span> </span><span>~</span><span> </span><span>0</span><span> </span><span>+</span><span> </span><span>Daily</span><span> </span><span>+</span><span> </span><span>Weekly</span><span> </span><span>+</span><span> </span><span>Daily</span><span>:</span><span>Weekly</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>matrix_train</span><span>)</span><span>
</span><span># Creation of the forecast for one week ahead
</span><span> </span><span>pred_week</span><span> </span><span>&lt;-</span><span> </span><span>predict</span><span>(</span><span>lm_m</span><span>,</span><span> </span><span>matrix_train</span><span>[</span><span>1</span><span>:</span><span>(</span><span>7</span><span>*</span><span>period</span><span>),</span><span> </span><span>-1</span><span>,</span><span> </span><span>with</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>])</span><span>
</span><span>return</span><span>(</span><span>as.vector</span><span>(</span><span>pred_week</span><span>))</span><span>
</span><span>}</span></code></pre><p>Define MAPE (Mean Absolute Percentage Error) for evaluation of our forecast.</p>
<pre><code><span>mape</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>real</span><span>,</span><span> </span><span>pred</span><span>){</span><span>
</span><span>return</span><span>(</span><span>100</span><span> </span><span>*</span><span> </span><span>mean</span><span>(</span><span>abs</span><span>((</span><span>real</span><span> </span><span>-</span><span> </span><span>pred</span><span>)</span><span>/</span><span>real</span><span>)))</span><span>
</span><span>}</span></code></pre><p>Now we are ready to produce forecasts. I set training set of the length of two weeks - experimentally proved. In experiments, a whole data set of the length of one year is used, so a forecast for 50 weeks will be produced. A sliding window approach for training is used.
Let’s produce (compute) forecast for every type of industry (4), to see differences between them:</p>
<pre><code><span>n_weeks</span><span> </span><span>&lt;-</span><span> </span><span>floor</span><span>(</span><span>length</span><span>(</span><span>n_date</span><span>)</span><span>/</span><span>7</span><span>)</span><span> </span><span>-</span><span> </span><span>2</span><span>
</span><span># Forecasts
</span><span>lm_pred_weeks_1</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>(</span><span>n_weeks</span><span>-1</span><span>),</span><span> </span><span>function</span><span>(</span><span>i</span><span>)</span><span>
</span><span>predWeekReg</span><span>(</span><span>DT</span><span>[</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>1</span><span>]],</span><span> </span><span>n_date</span><span>[((</span><span>i</span><span>*</span><span>7</span><span>)</span><span>+1</span><span>)</span><span>:</span><span>((</span><span>i</span><span>*</span><span>7</span><span>)</span><span>+7</span><span>*</span><span>2</span><span>)]))</span><span>
</span><span>lm_pred_weeks_2</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>(</span><span>n_weeks</span><span>-1</span><span>),</span><span> </span><span>function</span><span>(</span><span>i</span><span>)</span><span>
</span><span>predWeekReg</span><span>(</span><span>DT</span><span>[</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>2</span><span>]],</span><span> </span><span>n_date</span><span>[((</span><span>i</span><span>*</span><span>7</span><span>)</span><span>+1</span><span>)</span><span>:</span><span>((</span><span>i</span><span>*</span><span>7</span><span>)</span><span>+7</span><span>*</span><span>2</span><span>)]))</span><span>
</span><span>lm_pred_weeks_3</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>(</span><span>n_weeks</span><span>-1</span><span>),</span><span> </span><span>function</span><span>(</span><span>i</span><span>)</span><span>
</span><span>predWeekReg</span><span>(</span><span>DT</span><span>[</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>3</span><span>]],</span><span> </span><span>n_date</span><span>[((</span><span>i</span><span>*</span><span>7</span><span>)</span><span>+1</span><span>)</span><span>:</span><span>((</span><span>i</span><span>*</span><span>7</span><span>)</span><span>+7</span><span>*</span><span>2</span><span>)]))</span><span>
</span><span>lm_pred_weeks_4</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>(</span><span>n_weeks</span><span>-1</span><span>),</span><span> </span><span>function</span><span>(</span><span>i</span><span>)</span><span>
</span><span>predWeekReg</span><span>(</span><span>DT</span><span>[</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>4</span><span>]],</span><span> </span><span>n_date</span><span>[((</span><span>i</span><span>*</span><span>7</span><span>)</span><span>+1</span><span>)</span><span>:</span><span>((</span><span>i</span><span>*</span><span>7</span><span>)</span><span>+7</span><span>*</span><span>2</span><span>)]))</span><span>
</span><span># Evaluation (computation of errors)
</span><span>lm_err_mape_1</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>(</span><span>n_weeks</span><span>-1</span><span>),</span><span> </span><span>function</span><span>(</span><span>i</span><span>)</span><span>
</span><span>mape</span><span>(</span><span>DT</span><span>[(</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>1</span><span>]</span><span> </span><span>&amp;</span><span> </span><span>date</span><span> </span><span>%in%</span><span> </span><span>n_date</span><span>[(</span><span>15</span><span>+</span><span>(</span><span>i</span><span>*</span><span>7</span><span>))</span><span>:</span><span>(</span><span>21</span><span>+</span><span>(</span><span>i</span><span>*</span><span>7</span><span>))]),</span><span> </span><span>value</span><span>],</span><span>
</span><span>lm_pred_weeks_1</span><span>[,</span><span> </span><span>i</span><span>+1</span><span>]))</span><span>
</span><span>lm_err_mape_2</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>(</span><span>n_weeks</span><span>-1</span><span>),</span><span> </span><span>function</span><span>(</span><span>i</span><span>)</span><span>
</span><span>mape</span><span>(</span><span>DT</span><span>[(</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>2</span><span>]</span><span> </span><span>&amp;</span><span> </span><span>date</span><span> </span><span>%in%</span><span> </span><span>n_date</span><span>[(</span><span>15</span><span>+</span><span>(</span><span>i</span><span>*</span><span>7</span><span>))</span><span>:</span><span>(</span><span>21</span><span>+</span><span>(</span><span>i</span><span>*</span><span>7</span><span>))]),</span><span> </span><span>value</span><span>],</span><span>
</span><span>lm_pred_weeks_2</span><span>[,</span><span> </span><span>i</span><span>+1</span><span>]))</span><span>
</span><span>lm_err_mape_3</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>(</span><span>n_weeks</span><span>-1</span><span>),</span><span> </span><span>function</span><span>(</span><span>i</span><span>)</span><span>
</span><span>mape</span><span>(</span><span>DT</span><span>[(</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>3</span><span>]</span><span> </span><span>&amp;</span><span> </span><span>date</span><span> </span><span>%in%</span><span> </span><span>n_date</span><span>[(</span><span>15</span><span>+</span><span>(</span><span>i</span><span>*</span><span>7</span><span>))</span><span>:</span><span>(</span><span>21</span><span>+</span><span>(</span><span>i</span><span>*</span><span>7</span><span>))]),</span><span> </span><span>value</span><span>],</span><span>
</span><span>lm_pred_weeks_3</span><span>[,</span><span> </span><span>i</span><span>+1</span><span>]))</span><span>
</span><span>lm_err_mape_4</span><span> </span><span>&lt;-</span><span> </span><span>sapply</span><span>(</span><span>0</span><span>:</span><span>(</span><span>n_weeks</span><span>-1</span><span>),</span><span> </span><span>function</span><span>(</span><span>i</span><span>)</span><span>
</span><span>mape</span><span>(</span><span>DT</span><span>[(</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>4</span><span>]</span><span> </span><span>&amp;</span><span> </span><span>date</span><span> </span><span>%in%</span><span> </span><span>n_date</span><span>[(</span><span>15</span><span>+</span><span>(</span><span>i</span><span>*</span><span>7</span><span>))</span><span>:</span><span>(</span><span>21</span><span>+</span><span>(</span><span>i</span><span>*</span><span>7</span><span>))]),</span><span> </span><span>value</span><span>],</span><span>
</span><span>lm_pred_weeks_4</span><span>[,</span><span> </span><span>i</span><span>+1</span><span>]))</span></code></pre><p>Similarly, you can do this with the function <code>predWeek</code> from the previous post. I used STL+ARIMA method to compare with the MLR with interactions. Here is the <code>plotly</code> of MAPEs:</p>
<p>For every industry <strong>MLR</strong> was more accurate than STL+ARIMA, so our basic regression method is working very well for double seasonal time series.</p>
<p>I have created 4 (IMHO) interesting GIFs by the package <code>animation</code> to show whole forecast for a year. I have done it this way for every four industries:</p>
<pre><code><span>datas</span><span> </span><span>&lt;-</span><span> </span><span>data.table</span><span>(</span><span>value</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>as.vector</span><span>(</span><span>lm_pred_weeks_1</span><span>),</span><span>
</span><span>DT</span><span>[(</span><span>type</span><span> </span><span>==</span><span> </span><span>n_type</span><span>[</span><span>1</span><span>])</span><span> </span><span>&amp;</span><span> </span><span>(</span><span>date</span><span> </span><span>%in%</span><span> </span><span>n_date</span><span>[</span><span>-</span><span>c</span><span>(</span><span>1</span><span>:</span><span>14</span><span>,</span><span>365</span><span>)]),</span><span> </span><span>value</span><span>]),</span><span>
</span><span>date_time</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>rep</span><span>(</span><span>DT</span><span>[</span><span>-</span><span>c</span><span>(</span><span>1</span><span>:</span><span>(</span><span>14</span><span>*</span><span>48</span><span>),</span><span> </span><span>(</span><span>17473</span><span>:</span><span>nrow</span><span>(</span><span>DT</span><span>))),</span><span> </span><span>date_time</span><span>],</span><span> </span><span>2</span><span>)),</span><span>
</span><span>type</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>rep</span><span>(</span><span>&quot;MLR&quot;</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>lm_pred_weeks_1</span><span>)</span><span>*</span><span>ncol</span><span>(</span><span>lm_pred_weeks_1</span><span>)),</span><span>
</span><span>rep</span><span>(</span><span>&quot;Real&quot;</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>lm_pred_weeks_1</span><span>)</span><span>*</span><span>ncol</span><span>(</span><span>lm_pred_weeks_1</span><span>))),</span><span>
</span><span>week</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>rep</span><span>(</span><span>1</span><span>:</span><span>50</span><span>,</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>336</span><span>),</span><span> </span><span>rep</span><span>(</span><span>1</span><span>:</span><span>50</span><span>,</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>336</span><span>)))</span><span>
</span><span>saveGIF</span><span>({</span><span>
</span><span>oopt</span><span> </span><span>=</span><span> </span><span>ani.options</span><span>(</span><span>interval</span><span> </span><span>=</span><span> </span><span>0.9</span><span>,</span><span> </span><span>nmax</span><span> </span><span>=</span><span> </span><span>50</span><span>)</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>ani.options</span><span>(</span><span>&quot;nmax&quot;</span><span>)){</span><span>
</span><span>print</span><span>(</span><span>ggplot</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>datas</span><span>[</span><span>week</span><span> </span><span>==</span><span> </span><span>i</span><span>],</span><span> </span><span>aes</span><span>(</span><span>date_time</span><span>,</span><span> </span><span>value</span><span>,</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>type</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>type</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_line</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>0.8</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>min</span><span>(</span><span>datas</span><span>[,</span><span> </span><span>value</span><span>]),</span><span> </span><span>max</span><span>(</span><span>datas</span><span>[,</span><span> </span><span>value</span><span>])))</span><span> </span><span>+</span><span> </span><span>theme</span><span>(</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span> </span><span>panel.background</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>panel.grid.minor</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>),</span><span>
</span><span>panel.grid.major</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>),</span><span>
</span><span>panel.grid.major.x</span><span> </span><span>=</span><span> </span><span>element_line</span><span>(</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey90&quot;</span><span>),</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>15</span><span>),</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>10</span><span>),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span> </span><span>=</span><span> </span><span>12</span><span>,</span><span> </span><span>face</span><span> </span><span>=</span><span> </span><span>&quot;bold&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Time&quot;</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Load (kW)&quot;</span><span>,</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>&quot;Forecast of MLR (&quot;</span><span>,</span><span> </span><span>n_type</span><span>[</span><span>1</span><span>],</span><span> </span><span>&quot;); &quot;</span><span>,</span><span> </span><span>&quot;week: &quot;</span><span>,</span><span> </span><span>i</span><span>,</span><span> </span><span>&quot;; MAPE: &quot;</span><span>,</span><span>
</span><span>round</span><span>(</span><span>lm_err_mape_1</span><span>[</span><span>i</span><span>],</span><span> </span><span>2</span><span>),</span><span> </span><span>&quot;%&quot;</span><span>,</span><span> </span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;&quot;</span><span>)))</span><span>
</span><span>ani.pause</span><span>()</span><span>
</span><span>}},</span><span> </span><span>movie.name</span><span> </span><span>=</span><span> </span><span>&quot;industry_1.gif&quot;</span><span>,</span><span> </span><span>ani.height</span><span> </span><span>=</span><span> </span><span>450</span><span>,</span><span> </span><span>ani.width</span><span> </span><span>=</span><span> </span><span>750</span><span>)</span></code></pre><p>Here are the created GIFs:</p>
<p></p>
<p></p>
<p></p>
<p></p>
<p>In these animations we can see that the behavior of the electricity consumption can be very stochastic and many external factors influence it (holidays, weather etc.), so it’s a challenging task.</p>
<p>In my next post, I will continue with the introduction of regression methods, this time with GAM (Generalized Additive Model).</p>
<p>Script for the creation of this whole post can be found on <a href="https://github.com/PetoLau/petolau.github.io/tree/master/_Rscripts">my github repo</a>.</p>