21 - Practice Note On Time Series USING R
21 - Practice Note On Time Series USING R
Time series is a series of data points in which each data point is associated with a timestamp. A
simple example is the price of a stock in the stock market at different points of time on a given
day. Another example is the amount of rainfall in a region at different months of the year. R
language uses many functions to create, manipulate and plot the time series data. The data for
the time series is stored in an R object called time-series object. It is also a R data object like a
vector or data frame.
Trend T
Cyclical C
Seasonal S
Irregular I
1ST STEP
RESULT/T = CSI
SI/S = I
DECOMPOSE
Example
Consider the annual rainfall details at a place starting from January 2012. We create an R time
series object for a period of 12 months and plot it.
Time series decomposition is a common technique in time series analysis that involves
breaking down a time series into its underlying components, such as trend, seasonal patterns,
and residuals (or random fluctuations). Decomposition helps in understanding the underlying
patterns and structures within the time series data.
In R, the decompose() function from the stats package is commonly used to perform time
series decomposition. It uses either an additive or multiplicative decomposition method.
The additive decomposition assumes that the time series can be represented as the sum of its
components:
Once the time series is decomposed, you can analyze and visualize the individual components
to gain insights:
Trend: The trend component represents the long-term pattern or direction of the time series. It
indicates whether the series is increasing, decreasing, or fluctuating over time.
Seasonal: The seasonal component captures the repetitive or cyclical patterns within the time
series. It shows the periodic fluctuations that occur at fixed intervals, such as daily, monthly, or
yearly patterns.
Residuals: The residual component, also known as the random component or noise, represents
the unexplained variation in the time series that is not accounted for by the trend and seasonal
components. It consists of random fluctuations, measurement errors, or other irregularities.
By decomposing a time series, you can gain a better understanding of the various components
and their contributions to the overall behavior of the data. This knowledge can be used to
model and forecast future values, identify anomalies or outliers, and analyze the impact of
different components on the time series.
R provides various visualization tools, such as plotting functions in packages like ggplot2, to
visualize the original time series and its decomposed components separately. These plots can
help in interpreting the patterns and making informed decisions or predictions based on the
decomposed time series data.
Overall, time series decomposition is a valuable technique in time series analysis that allows
for a deeper understanding of the underlying components and patterns within a time series.
In this example, we perform time series decomposition using the decompose() function on the
AirPassengers dataset.
The AirPassengers dataset is converted into a time series object (passengers_ts) using the ts()
function, specifying the frequency as 12 (since the data is monthly).
The decompose() function is then used to decompose the time series into its trend, seasonal,
and random components. The resulting components are stored in the passengers_decomp
object.
We plot the original time series, trend component, seasonal component, and random
component separately using the ggplot2 library.
The resulting plots show the original time series, the trend component (long-term pattern), the
seasonal component (repeating pattern), and the random component (residuals or noise).
install.packages("quantmod")
library(quantmod)
# Download data for a single symbol (AAPL) from
Yahoo Finance
getSymbols("AAPL")
head(AAPL)
tail(AAPL)
chart_Series(AAPL)
install.packages("fBasics")
library(fBasics)
basicStats(apple)
Installed necessary packages, including "tidyverse" and "zoo".
install.packages("tidyverse")
install.packages("zoo")
Stationarity Testing
A stationary time series has constant mean and variance over
time, which is essential for many time series models.
Conclusion
In this lesson, we learned how to perform time series analysis
using R programming with the default "nottem" dataset. We
covered data preprocessing, time series decomposition,
stationarity testing, and optional time series forecasting.