0% found this document useful (0 votes)
44 views

21 - Practice Note On Time Series USING R

The document discusses time series analysis in R. It describes how to create, decompose, visualize and test time series objects. Functions like ts(), decompose() and plotting tools are used to analyze time series data and understand the underlying trends, seasonality and noise.

Uploaded by

Faguni guha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

21 - Practice Note On Time Series USING R

The document discusses time series analysis in R. It describes how to create, decompose, visualize and test time series objects. Functions like ts(), decompose() and plotting tools are used to analyze time series data and understand the underlying trends, seasonality and noise.

Uploaded by

Faguni guha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

TIME SERIES ANALYSIS

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

ADDITIVE MODEL RESULT= T+C+S+I

MULTIPLICATIVE MODEL RESULT= T * C * S * I

1ST STEP

CALCULATE TREND USING REGRESSION MODEL

RESULT/T = CSI

CYCLICAL - MOVING AVERAGE METHOD


C
CSI/C =SI

SEASONAL – ARIMA – AUTO REGRESSION MOVING


AVERAGE ANALYSIS

SI/S = I

DECOMPOSE

The time series object is created by using the ts() function.


Syntax : The basic syntax for ts() function in time series analysis is −

timeseries.object.name <- ts(data, start, end, frequency)


Following is the description of the parameters used −
data is a vector or matrix containing the values used in the time series.
start specifies the start time for the first observation in time series.
end specifies the end time for the last observation in time series.
frequency specifies the number of observations per unit time.
Except the parameter "data" all other parameters are optional.

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.

# Get the data points in form of a R vector.


rainfall <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071)

# Convert it to a time series object.


rainfall.timeseries <- ts(rainfall,start = c(2022,1),frequency = 12)
# Print the timeseries data.
print(rainfall.timeseries)

# Give the chart file a name.


#png(file = "rainfall.png")

# Plot a graph of the time series.


plot(rainfall.timeseries)

# Save the file.


dev.off()

frequency = 4 pegs the data points for every quarter of a year.


frequency = 6 pegs the data points for every 10 minutes of an hour.

Multiple Time Series


We can plot multiple time series in one chart by combining both the series into a matrix.
# Get the data points in form of a R vector.
rainfall1 <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071)
rainfall2 <- c(655,1306.9,1323.4,1172.2,562.2,824,822.4,1265.5,799.6,1105.6,1106.7,1337.8)

# Convert them to a matrix.


combined.rainfall <- matrix(c(rainfall1,rainfall2),nrow = 12)

# Convert it to a time series object.


rainfall.timeseries <- ts(combined.rainfall,start = c(2012,1),frequency = 12)

# Print the timeseries data.


print(rainfall.timeseries)

# Give the chart file a name.


#png(file = "rainfall_combined.png")

# Plot a graph of the time series.


plot(rainfall.timeseries, main = "Multiple Time Series")

# Save the file.


dev.off()

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:

Time Series = Trend + Seasonal + Residuals


The multiplicative decomposition assumes that the time series can be represented as the
product of its components:

Time Series = Trend * Seasonal * Residuals


The decompose() function takes a time series object as input and returns a list containing the
trend, seasonal, and random 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.

# Load the required libraries


library(ggplot2)

# Load the AirPassengers dataset


data(AirPassengers)

# Convert the data to a time series object


passengers_ts <- ts(AirPassengers, frequency = 12)
print(passengers_ts)

# Perform time series decomposition


passengers_decomp <- decompose(passengers_ts)
print(passengers_decomp)

# Plot the original time series


ggplot() +
geom_line(mapping= aes(x = time(passengers_ts), y = passengers_ts), color = "blue") +
labs(x = "Year", y = "Number of Passengers", title = "Original Time Series")

# Plot the trend component


ggplot() +
geom_line(aes(x = time(passengers_decomp$trend), y = passengers_decomp$trend), color =
"red") +
labs(x = "Year", y = "Trend", title = "Trend Component")
# Plot the seasonal component
ggplot() +
geom_line(aes(x = time(passengers_decomp$seasonal), y = passengers_decomp$seasonal),
color = "green") +
labs(x = "Year", y = "Seasonal", title = "Seasonal Component")

# Plot the random component


ggplot() +
geom_line(aes(x = time(passengers_decomp$random), y = passengers_decomp$random),
color = "purple") +
labs(x = "Year", y = "Random", title = "Random Component")

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)

chartSeries(AAPL, theme = chartTheme("white"), layout =


c(1, 5), colorset = blues9)
install.packages("TSA")
library(TSA)
apple=(AAPL$AAPL.Adjusted)
head(apple)
apple1=ts(apple)
class(apple1)
rapple=diff(log(apple))
head(rapple)
plot(rapple)

install.packages("fBasics")
library(fBasics)
basicStats(apple)
Installed necessary packages, including "tidyverse" and "zoo".

install.packages("tidyverse")
install.packages("zoo")

Loading and Exploring the "nottem" Dataset


The "nottem" dataset is part of the "datasets" package that
comes with R. It contains average monthly temperatures in
Nottingham, England.

Load the "nottem" dataset:


# Load the "datasets" package
library(datasets)

# Load the "nottem" dataset


data("nottem")
#Explore the dataset:
# Display the first few rows of the dataset
head(nottem)

# Summary statistics of the dataset


summary(nottem)

# Check the data types and structure of the dataset


str(nottem)
Data Preprocessing
Before performing time series analysis, it's essential to
preprocess the data. Ensure the dataset has a time-based index,
convert the date column to the appropriate date format, and
handle any missing or irregular data points. Fortunately, the
"nottem" dataset is already in a suitable format.

Visualizing Time Series Data


Visualization is crucial to understanding time series patterns
and trends.

# Convert the index to a time-based format


nottem_ts <- ts(nottem, frequency = 12, start = c(1920, 1))

# Plot the average monthly temperature time series data


plot(nottem_ts, main = "Average Monthly Temperature Time
Series", xlab = "Year", ylab = "Temperature (°F)", type = "l")
Time Series Decomposition
Time series decomposition helps in understanding the
underlying components of a time series, such as trend,
seasonality, and residual (noise).

# Decompose the time series into trend, seasonality, and


residual components
decomp <- decompose(nottem_ts)

# Plot the decomposed components


plot(decomp)

Stationarity Testing
A stationary time series has constant mean and variance over
time, which is essential for many time series models.

# Perform stationarity testing


library(tseries)
adf_test <- adf.test(nottem_ts)
kpss_test <- kpss.test(nottem_ts)
# Print the test results
print(paste("ADF Test p-value:", adf_test$p.value))
print(paste("KPSS Test p-value:", kpss_test$p.value))

Time Series Forecasting (Optional)


Time series forecasting involves predicting future values based
on historical data. There are various methods for forecasting,
such as ARIMA, exponential smoothing, and machine learning
models.

# Install the necessary package for forecasting


install.packages("forecast")

# Load the forecast library


library(forecast)

# Create a time series forecast using ARIMA


nottem_forecast <- forecast(nottem_ts, h = 24)
# Plot the forecast
plot(nottem_forecast, main = "Average Monthly Temperature
Time Series Forecast", xlab = "Year", ylab = "Temperature (°F)",
type = "l")

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.

Time series analysis is a powerful tool for understanding and


forecasting data with temporal patterns. It is widely used in
various domains, including climate science, finance, economics,
and more.

You might also like