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

Time Series Analysis

This document discusses time series analysis in R. It introduces functions to import, visualize and analyze time series data. Some key functions covered include ts() to create time series objects, plot() to visualize time series, acf() and pacf() to examine stationarity, decompose() to break down components of variation, and auto.arima() and forecast() for time series forecasting. Examples are provided using various datasets like AirPassengers, EuStockMarkets, and stock price data.

Uploaded by

Anurag Sharma
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)
63 views

Time Series Analysis

This document discusses time series analysis in R. It introduces functions to import, visualize and analyze time series data. Some key functions covered include ts() to create time series objects, plot() to visualize time series, acf() and pacf() to examine stationarity, decompose() to break down components of variation, and auto.arima() and forecast() for time series forecasting. Examples are provided using various datasets like AirPassengers, EuStockMarkets, and stock price data.

Uploaded by

Anurag Sharma
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/ 4

#Time series analysis

TCS.BO <- read.csv("C:/Users/RISHI/Desktop/Data Analytics/FDP


105/TCS.BO.csv")
View(TCS.BO)

TCS<-TCS.BO[,c(1,6)] #only date and close price


head(TCS)
class(TCS)
str(TCS)
TCS$Date=as.Date(TCS$Date)
str(TCS)
plot(TCS$Adj.Close~TCS$Date,type="l",col="red")

# import another time series data


library(readxl)
myData <- read_excel("C:/Users/RISHI/Desktop/Data Analytics/FDP
105/HBS_Table_No._155___Quarterly_Estimates_of_Gross_Domestic_Product_at_Fa
ctor_Cost_(at_Constant_Prices)_(New_Series)_(Base__2004-05).xlsx",
sheet = "NAS�2011-12")
head(myData)
names(myData)
str(myData)
myData$GDP<-myData$`Total Gross Value Added at Basic Price`
names(myData)
Data<-myData[,c(2,12)]
head(Data)
str(Data)
plot(Data$GDP,type="l", col="blue")
ts1<-as.ts(Data$GDP)
plot(ts1,col="orange")
ts2=ts(Data$GDP, start=2011, frequency=4)
ts2
plot(ts2)
######################
# how to create a time series object in R?
# using ts function
# ts(data, start, end, frequency)
View(AirPassengers)
plot(AirPassengers)
View(EuStockMarkets)
plot(EuStockMarkets)
str(EuStockMarkets)

sales=c(79,11,74,86,65,23,45,49,99,24,40,48,51)
T1=ts(sales)
T1
plot(T1, col="blue", main= "time plot")
class(sales)
class(T1)
plot(sales)
sales.ts=ts(sales,start=2018, frequency = 12)
sales.ts
sales.ts2=ts(sales,start=2018, frequency = 1)
sales.ts2
sales.ts3=ts(sales,start=c(2018,4), frequency = 12)
sales.ts3
class(sales.ts)
sales.ts4=ts(sales, start=2018, frequency=4)
sales.ts4
plot(sales.ts)

# type="o" means both line and dots will be shown


plot(sales.ts, type="o", main="time plot of sales")

sales1=c(5,22,12,45,67,24,36,43,11,72,25,65,42)

sales1.ts=ts(sales1, start=2018, frequency = 12)

combine.ts=cbind(sales.ts,sales1.ts)
plot(combine.ts)

#plot in one common axis

plot(combine.ts,type="o",plot.type = "single",col=c("red","blue"))

# as.ts() converts a simple object into time series


y=cars$speed
class(y)
y1=as.ts(y)
class(y1)
str(y1)

###############################################
# more example of time series dataset
# EuStockMarket
# AirPassengers
# JohnsonJohnson
# airmiles
# google: TSA package
# airpass: TSA
# electricity: TSA
# gold : TSA
##########################################
# more time series related commands
is.ts(EuStockMarkets)
start(EuStockMarkets)
end(EuStockMarkets)
summary(EuStockMarkets)
cycle(EuStockMarkets)
cycle(AirPassengers)
frequency(EuStockMarkets)
frequency(AirPassengers)

aggregate(AirPassengers)

plot(aggregate(AirPassengers, FUN=mean)) #this will give trend


aggregate(AirPassengers, FUN=mean)
str(AirPassengers)
summary(AirPassengers)
cycle(AirPassengers)
start(AirPassengers)
end(AirPassengers)
frequency(AirPassengers)
boxplot(AirPassengers~cycle(AirPassengers))
# boxplot gives idea about seasonal effect
#decomposition
# In Time series analysis we are generally
# concerned with isolating different types
# of components. Typical components include
# trend and seasonality
# In finance we often encounter exponential
# trend growth, and seasonality.

decompose(AirPassengers) #by default additive


decompose(AirPassengers,type="multiplicative")
plot(decompose(AirPassengers,type="multiplicative"))

# add a trendline to the plot


plot(AirPassengers)
abline(lm(AirPassengers~time(AirPassengers)),col="red")

#############################################
# Most of the models of forecasting are based on
# the assumption of stationarity (AR, ARMA, ARIMA)
# How to check stationarity?
# 1. plot(), 2. ACF, 3. Specific tests (DF, ADF)
################################################

#Stationarity using plot


# 1. plot() ; if the plot exhibits trend, then the data is NS
# if the data exhibits changing variability, data is NS
plot(AirPassengers) # trend and seasonality both, therefore NS
plot(EuStockMarkets[,1]) #trend and changing variance both, therefore NS
plot(JohnsonJohnson) #trend and changing variance both, therefore NS

install.packages("TSA")
library(TSA)
data(electricity)
plot(electricity) #trend and changing variance both, therefore NS

#2. acf plot


# if the decay in acf value is slow and
# there are quite a few significant acf's
acf(EuStockMarkets[,1])
acf(AirPassengers)
acf(JohnsonJohnson)
library(TSA)
data(electricity)
acf(electricity)

# 3. Dickey Fuller test/ augmented dickey fuller


# null hypothesis in adf test: data is non-stationary
install.packages("tseries")
library(tseries)
adf.test(AirPassengers,k=12) # monthly data
adf.test(JohnsonJohnson, k=4) #quarterly data
data(google)
plot(google)
adf.test(google, k=252)
#kpss test
# null hypo in kpss test: data is stationary
kpss.test(AirPassengers)
kpss.test(JohnsonJohnson)
library(TSA)
###############################
# how to make a data stationary
# by differencing: eliminates trend
# by taking log: eliminates unequal variance
diff(AirPassengers)
plot(diff(AirPassengers))
plot(diff(log(AirPassengers)))
acf(diff(log(AirPassengers)))
adf.test(diff(log(AirPassengers)),k=12)
adf.test(diff(log(AirPassengers),differences=2))

######################################
# forecasting using ARIMA
# acf and pacf determines order of AR and MA
# forecasting using "forecast" package
install.packages("forecast")
library(forecast)
#auto.arima function chooses best p,d,q
fit=auto.arima(AirPassengers)
summary(fit)
forecast(fit, h=10) #using forecast
plot(forecast(fit, h=10))
plot(forecast(fit, h=5*12))
predict(fit, n.ahead = 5*12)
#########################################
install.packages("quantmod")
library(quantmod)
d=getSymbols("AAPL",auto.assign=F)
d
head(d)
str(d)
dim(d)
plot(d$AAPL.Close)
plot(Cl(d))
df=data.frame(getSymbols("AAPL", auto.assign=F))
df
head(df)
str(df)
dim(df)
colnames(df)<-c("Open", "High","Low","Close","Vol", "Adjusted")
head(df)
write.csv(df, "AAPL.csv")
a=read.csv("AAPL.csv")
str(a)
a
df1=read.csv("AAPL.csv", row.names=1)
df1
head(df1)
plot(Cl(df))
plot(Cl(df1))
###################################################

You might also like