Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

####Part02

# Read CSV into R


# library(readr)
#MyData <- read.csv(file="E:/IIM Sambalpur/Term V/BAR/Data/Sensex.csv")

##Descriptives in R
##or use dropdown from import
library(readr)
SENSEX <- read_csv("E:/IIM Sambalpur/Term V/BAR/Data/Sensex.csv") #import csv data

##preprocessing of data
SENSEX<-SENSEX[,1:5] #selects column1 to 5
SENSEX<-SENSEX[-133:-134,] #deletes row133 & 134
SENSEX<-SENSEX[1:132,]
SENSEX<-SENSEX[-c(-133:-134),] #rejects everything else apart from row133&134

#census vs hypothesis testing


library(xlsx)
summary(SENSEX$Close)

#comment pre-processing to get output


# Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
##8892 16864 19343 20665 26369 33602 2

install.packages("Hmisc")
library(Hmisc)
describe(SENSEX$Close)

install.packages("pastecs")
library(pastecs)
stat.desc(SENSEX[,5])

install.packages("psych")
library(psych)
describe(SENSEX[,5])

##download data from website


install.packages("tseries")
library(tseries)
gvkpil<-get.hist.quote(instrument = "GVKPIL.NS", #file??
start="2017-04-01", end="2018-03-31", #start & end date
quote = c("Open", "High", "Low", "Close"), #columns needed
provider = "yahoo", compression = "d") ##d=daily, m=monthly
tatasteel <- get.hist.quote(instrument = "TATASTEEL.NS", start="2017-04-01",
end="2018-03-31",
quote = c("Open", "High", "Low", "Close"),
provider = "yahoo", compression = "d")
#view(gvkpil)

TSL<-as.data.frame(tatasteel)
GVK<-as.data.frame(gvkpil)
# view(TSL)

write.csv(GVK,"D:/Analytics Consulting using ML/gvksp.csv") #error?

ticker<- c("TATASTEEL.NS", "GVKPIL.NS", "BANKINDIA.NS", "TCS.NS")


# view(ticker)
start <- "2017-04-01"
end <- "2018-03-31"
stock_comb<- list(); # empty list to fill in the data #doubt??
#storing data of all stocks
for(i in 1:length(ticker))
{
stock_comb[[i]] <- get.hist.quote(instrument = ticker[i], start=start, end=end,
quote = "Close", provider = "yahoo",
compression = "d")
}
# tried to view it as data frame
##stock_comb1 <- as.data.frame(stock_comb)
#view(stock_comb1)

##doubt??
##look for observation no.=247 say (against ??? trading days)
stock_prices<-matrix(unlist(stock_comb),247,4)
stock_comb[1] ##replace 1 with 2:4

##Export data from R


write.csv(stock_prices,"D:/Term I-XIMB_Biz Stat/stock_data.csv")

##Plotting in R

# #simple histogram
# hist(TSL[,1]) #histogram of column1
# hist(GVK[,1])
# # Colored Histogram with Different Number of Bins
# hist(TSL$Close, breaks=4, col="red") #contains only 4 sets (2 consecutive sets of previous one
merged)
# hist(GVK$Close, breaks=6, col="yellow")

#simple histogram
hist(TSL[,1]) #histogram of column1
# Colored Histogram with Different Number of Bins
hist(TSL$Close, breaks=4, col="red") #contains only 4 sets (2 consecutive sets of previous one
merged)
hist(GVK[,1])
hist(GVK$Close, breaks=6, col="yellow")
## doubt how is it plotted for continuous data

# Add a Normal Curve

x <- GVK$Open

h<-hist(x, breaks=5, col="red",


xlab="Stock Price", # x-axis name
main="Histogram with Normal Curve") #graph name
#doubt??
xfit<-seq(min(x),max(x),length=40)
yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
yfit <- yfit*diff(h$mids[1:2])*length(x)
lines(xfit, yfit, col="blue", lwd=2) #forming a normal distribution

#doubt??
##kernel plots
plot(density(x))
plot(density(GVK$Close))
#doubt??
##boxplot
boxplot(TSL)
boxplot(GVK$Close)

##BOXPLOTS FOR multiple vaariables


boxplot.matrix(stock_prices, use.cols = TRUE)

#doubt??
library(e1071)
skewness(stock_prices[,1])##= -0.4219908
skewness(stock_prices[,2])#=0.2083
skewness(stock_prices[,3])#=0.00599
skewness(stock_prices[,4])#=0.8538869

colMeans(stock_prices)##595.25129 12.85344 153.78340 2610.22835

##compare skewness
skew_1<-(stock_prices[,1]-595.25129)^3
skew_11<-sum(skew_1)/247 ## = -351452.152699
skew_1f<-skew_11/(var(stock_prices[,1])^1.5)

##compare skewness
skew_2<-sum((stock_prices[,2]-12.85344)^3)/nrow(stock_prices)##=28.383
skew_3<-sum((stock_prices[,3]-153.7834)^3)/247##=99.01688
skew_4<-mean((stock_prices[,4]-2610.22835)^3)##=9366989.915

skew_2/var(stock_prices[,2])^1.5 ##=0.2083754
skew_3/var(stock_prices[,3])^1.5 ##=0.005992238
skew_4/var(stock_prices[,4])^1.5 ##=0.8538869

#####freeing up memory

# remove(SENSEX)
# remove(gvkpil)
# remove(GVK)
# remove(TSL)
# remove(h)
# remove(stock_comb)
# remove(stock_comb1)
# remove(stock_prices)
# remove(start)
# remove(end)
# remove(i)
# remove(x)
# remove(ticker)
# remove(tatasteel)
# remove(xfit)
# remove(yfit)
# remove(skew_1)
# remove(skew_11)
# remove(skew_1f)
# remove(skew_2)
# remove(skew_3)
# remove(skew_4)

You might also like