Working With Time Series R-Script- Absolute Basics
-
Upload
vaibhav-garg -
Category
Documents
-
view
7 -
download
0
description
Transcript of Working With Time Series R-Script- Absolute Basics
7/17/2019 Working With Time Series R-Script- Absolute Basics
http://slidepdf.com/reader/full/working-with-time-series-r-script-absolute-basics 1/2
getwd() ##Get working directorysetwd("e:/Intro to R") ##Set working directory to work with our stored datalist.files()## To list the files in working directoryIBM <- read.csv("IBM.csv")## daily data from 01/01/1975 till 23 Sep###Or lets retrieve the file from web by first storing the urlurl <- "http://real-chart.finance.yahoo.com/table.csv?s=IBM&a=00&b=1&c=1975&d=08&e=23&f=2015&g=d&ignore=.csv"###And now pass on the command to read this .csv file containing our data into a data frameIBM <-read.csv(url)head(IBM) ### shows first 6 entries of our data frame IBMtail(IBM) ### shows last 6 entries of our data framesummary(IBM) ## Provides summary of each variable of IBM. For each of'em it provides:####1st Quartile, median, mean, 3rd quartile, no. of missing valuesstr(IBM) ###Displays the structure of IBM data frame showing details such as:### No. of observations, No of variables### For each variable it shows its class, and first few entriesIBM$Date <- as.Date(IBM$Date, format = "%Y-%m-%d") ### This converts our Date variable into a Date class in Rstr(IBM)###Lets get rid off from other columnsIBMd <- IBM[, c("Date", "Adj.Close")] ###IBM dailyhead(IBMd)
#Now lets sort this data by first date###to do this we first need to generate an arrangement of numbers from 1 to 10275ord <-order(IBMd$Date, decreasing = F)##It would give the arrangement of no.s ranging from 1 to 10275head(ord)IBMd <- IBMd[ord, ] ###IBM daily data sorted by first date i.e. oldest firsthead(IBMd)##Now if you see the output above, the very first entry above has a row number 10275##in order to do analysis on this sorted df we first need to change the row number##for each of 10275 observations. Now if we pass on the command head(row.names(I
BMd))head(row.names(IBMd))### We would see a character vector displaying "10275", "10274"...'"10270"##This means that these row numbers aren't a numeric instead its a characteristic value##Now in order to achieve our goal we are going to use as.character(), length()and row.names()row.names(IBMd) <- as.character(1:length(IBMd$Date))## Note that we can use Adj.Close as variable###instead of Date variable because all we need is the number of observations###or alternatively we could have used nrow(IBMd) instead of length(IBMd$Date) as both###them essentially gives number of observations!
head(IBMd)
##LinePlot Note: We will analyse our unsorted ie the IBM data frame graphicallyplot(IBM$Date, IBM$Adj.Close, type = 'l', col = "grey")plot(IBM$Date, IBM$Adj.Close, type = 's', col = "grey")plot(IBM$Date, IBM$Adj.Close, type = 'o', col = "grey")plot(IBM$Date, IBM$Adj.Close, type = 'o', col = "grey", pch = 4, xlim = as.Date(c("2015-06-20","2015-09-25")))plot(IBM$Date, IBM$Adj.Close, type= "s", lty = 7, col = "grey", xlim = as.Date(c('2015-09-01','2015-09-23')), xlab = 'Time Line', ylab = "Closing price", main =
7/17/2019 Working With Time Series R-Script- Absolute Basics
http://slidepdf.com/reader/full/working-with-time-series-r-script-absolute-basics 2/2
"IBM Stock Trend")plot(IBM$Date, IBM$Adj.Close, type= "l", lty = 5, col = "green", xlab = "Date",ylab = "IBM Price")
#draws a vertical line to the existing plotabline(v=as.Date(c("2000-03-01")), lwd=2)abline(v=as.Date(c("1983-01-01","1983-12-31")), lwd=2, col = "red")
abline(v=as.Date(c("2000-03-01")), lwd = 2)###lwd stands for line widthabline(v=as.Date(c("1995-01-01","2015-09-23")), lwd = 2)abline(v=as.Date(c("1997-09-01","1997-10-01","1997-11-01")),col = "green")#Monthly Average price of IBM Stocktapply(IBM$Close, months(IBMd$Date), mean)##tapply takes three arguments,###1st is our variable that we want to analyse###2nd this argument groups our data according to the given parameter###3rd this argument is then applied to the grouped datamean(IBM$Adj.Close)##Computing correlationcor(IBM$Close, IBM$Adj.Close, method = c("kendall"))cor(IBM$Close, IBM$Adj.Close, method = c("spearman"))cor(IBM$Close, IBM$Adj.Close, method = c("pearson"))