Pages

Sunday, July 14, 2019

To Plot Additional Parameters in Time Series Graph




In this Article, we will learn to plot additional parameters to the time-series graph to understand if additional variables has any impact on predicted (to be estimated) values.

We will take SBI closing price on daily basis and will analyze the impact of RBI Repo Rate changes on that. Below code reads the RBI Repo Rate table and rename 'Last.Update' column to Date


> Repo <- read.csv(file = "Repo-Rate.csv", header=TRUE,sep = ",")
> names(Repo)[names(Repo)== "Last.Update"] <- "Date"
> typeof(Repo$Date)
[1] "integer"

We have taken the closing NSE price from NSE website and retained only Date & Closing price information for our comparison.

> SBI <- read.csv(file = "03-06-2018-TO-31-05-2019SBINALLN.csv", header=TRUE,sep = ",")
> SBIEQ <- SBI[SBI$Series == 'EQ',]
> SBIEQ$Date <- as.Date(SBIEQ$Date,format = "%d-%b-%Y")
> SBIRepo <- data.frame(SBIEQ$Date,SBIEQ$Close.Price)
> names(SBIRepo)[names(SBIRepo) == "SBIEQ.Date"] <- "Date"

Changing the date format to text so we can combine easily using Merge function in R

> SBIRepo$Date <- as.character(SBIRepo$Date)
> Repo$Date <- as.character(Repo$Date)
> PlotG <- merge.data.frame(SBIRepo,Repo,by= 'Date',all.x=T)

Convert Date back to original format as ggplot() will not be able to plot time series graph on text data

> PlotG$Date <- as.Date(PlotG$Date,format="%Y-%m-%d")
> ggplot(PlotG,aes(x=Date, y= SBIEQ.Close.Price, color = Rate )) + geom_point() + scale_x_date()