0% found this document useful (0 votes)
20 views8 pages

Time Series Analysis in R with Examples

Uploaded by

2022bt0063
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Time Series Analysis in R with Examples

Uploaded by

2022bt0063
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Time Series Analysis

• Any metric which is measured over regular time intervals creates a time series.
• Analysis of time series is commercially important due to industrial necessity
and relevance, especially with respect to the forecasting (demand, supply, and
sale, etc.).
• A series of data points in which each data point is associated with a timestamp
is known as time series.
• Time Series Analysis in R is used to see how an object behaves over some
time.
• In R Programming Language, it can be easily done by the ts() function with
some parameters.
• The stock price at different points in a day in the stock market is the simplest
example of the time series.
• The amount of rainfall in an area in different months of the year is another
example of it.
• In the R-object, the time series data is known as the time-series object. It is just
like a vector or data frame.

Example of the COVID-19 pandemic situation. Taking the total number of positive
cases of COVID-19 cases weekly from 22 January 2020 to 15 April 2020 the world in
data vector.
x <- c(580, 7813, 28266, 59287, 75700,
87820, 95314, 126214, 218843, 471497,
936851, 1508725, 2072113)

# library required for decimal_date() function


library(lubridate)

# output to be created as png file


png(file ="[Link]")

# creating time series object


# from date 22 January, 2020
mts <- ts(x, start = decimal_date(ymd("2020-01-22")),
frequency = 365.25 / 7)

# plotting the graph


plot(mts, xlab ="Weekly Data",
ylab ="Total Positive Cases",
main ="COVID-19 Pandemic",
[Link] ="darkgreen")
Consider the annual rainfall details at a place starting from January 2024. 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.


[Link] <- ts(rainfall,start = c(2024,1),frequency = 12)

# Print the timeseries data.


print([Link])

# Plot a graph of the time series.


plot([Link])
Multivariate Time Series Analysis
Multivariate Time Series is creating multiple time series in a single chart. Taking data of total positive cases and total
deaths from COVID-19 weekly from 22 January 2020 to 15 April 2020 in a data vector.

positiveCases <- c(580, 7813, 28266, 59287, 75700, 87820, 95314, 126214, 218843, 471497, 936851,
1508725, 2072113)

deaths <- c(17, 270, 565, 1261, 2126, 2800, 3285, 4628, 8951, 21283, 47210, 88480, 138475)

# library required for decimal_date() function


library(lubridate)

# creating multivariate time series object


# from date 22 January, 2020
mts <- ts(cbind(positiveCases, deaths), start = decimal_date(ymd("2020-01-22")), frequency = 365.25 / 7)

# plotting the graph


plot(mts, xlab ="Weekly Data", main ="COVID-19 Cases", [Link] ="darkgreen")
Multivariate Time Series Analysis
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.


[Link] <- matrix(c(rainfall1,rainfall2),nrow = 12)

# Convert it to a time series object.


[Link] <- ts([Link],start = c(2012,1),frequency = 12)

# Print the timeseries data.


print([Link])

# Plot a graph of the time series.


plot([Link], main = "Multiple Time Series")
Multivariate Time Series Analysis

x <- c(580, 7813, 28266, 59287, 75700, 87820, 95314, 126214, 218843,471497, 936851, 1508725, 2072113)

# library required for decimal_date() function


library(lubridate)

# library required for forecasting


library(forecast)

# creating time series object


# from date 22 January, 2020
mts <- ts(x, start = decimal_date(ymd("2020-01-22")), frequency = 365.25 / 7)

# forecasting model using arima model


fit <- [Link](mts)

# Next 5 forecasted values


forecast(fit, 5)

# plotting the graph with next


# 5 weekly forecasted values
plot(forecast(fit, 5), xlab ="Weekly Data", ylab ="Total Positive Cases", main ="COVID-19 Pandemic", [Link] ="darkgreen")

You might also like