Vous êtes sur la page 1sur 2

getwd() ##Get working directory

setwd("e:/Intro to R") ##Set working directory to work with our stored data
list.files()## To list the files in working directory
IBM <- read.csv("IBM.csv")## daily data from 01/01/1975 till 23 Sep
###Or lets retrieve the file from web by first storing the url
url <- "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 frame
IBM <-read.csv(url)
head(IBM) ### shows first 6 entries of our data frame IBM
tail(IBM) ### shows last 6 entries of our data frame
summary(IBM) ## Provides summary of each variable of IBM. For each of'em it prov
ides:
####1st Quartile, median, mean, 3rd quartile, no. of missing values
str(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 entries
IBM$Date <- as.Date(IBM$Date, format = "%Y-%m-%d") ### This converts our Date v
ariable into a Date class in R
str(IBM)
###Lets get rid off from other columns
IBMd <- IBM[, c("Date", "Adj.Close")] ###IBM daily
head(IBMd)
#Now lets sort this data by first date
###to do this we first need to generate an arrangement of numbers from 1 to 1027
5
ord <-order(IBMd$Date, decreasing = F)##It would give the arrangement of no.s ra
nging from 1 to 10275
head(ord)
IBMd <- IBMd[ord, ] ###IBM daily data sorted by first date i.e. oldest first
head(IBMd)
##Now if you see the output above, the very first entry above has a row number 1
0275
##in order to do analysis on this sorted df we first need to change the row numb
er
##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", "10
274"...'"10270"
##This means that these row numbers aren't a numeric instead its a characteristi
c 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) a
s both
###them essentially gives number of observations!
head(IBMd)
##LinePlot Note: We will analyse our unsorted ie the IBM data frame graphically
plot(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 =

"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 plot
abline(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 width
abline(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 Stock
tapply(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 data
mean(IBM$Adj.Close)
##Computing correlation
cor(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"))

Vous aimerez peut-être aussi