可视化绘图分析(一)

81 篇文章 2 订阅
77 篇文章 18 订阅
getwd()
###### Import data & Summary statistics ######
data<-read.csv("data.csv")
keydata <- data[,c("Year","Month","Day","TradingDay","BA","KR")]
BA <- data.frame(keydata[-length(keydata$BA), c("Year","Month","Day","TradingDay")], diff(keydata$BA), abs(diff(keydata$BA)/keydata$BA[-length(keydata$BA)]))
colnames(BA) <- c("Year","Month","Day","TradingDay","BA_Return","BA_Volatility")
KR <- data.frame(keydata[-length(keydata$KR), c("Year","Month","Day","TradingDay")], diff(keydata$KR), abs(diff(keydata$KR)/keydata$KR[-length(keydata$KR)]))
colnames(KR) <- c("Year","Month","Day","TradingDay","KR_Return","KR_Volatility")


### Problem 1 ###
BA <- BA[-which(BA$Year==2015&BA$Month==1&BA$Day==2),]
KR <- KR[-which(KR$Year==2015&KR$Month==1&KR$Day==2),]
summary(BA$BA_Return)
summary(KR$KR_Return)
cor(BA$BA_Return,KR$KR_Return)
# Univariate analysis #
# (1) Boxplot for 'summary(BA$BA_Return)' and 'summary(KR$KR_Return)'
par(mfrow=c(1,2))
boxplot(BA$BA_Return, main="BA_Return")
abline(h=mean(BA$BA_Return))
mean(BA$BA_Return)
text(x=1.37338,y=1.153728,"mean=0.1995")
boxplot(KR$KR_Return,main="KR_Return")
abline(h=mean(KR$KR_Return))
mean(KR$KR_Return)
text(x=1.37338,y=0.173728,"mean=-0.0019")

在这里插入图片描述

# (2) Histogram of 4 years
year <- 2015:2018
simon <- function(data){
  for (j in 1:4) {
    y <- year[j]
    data2 <- subset(data,Year== y)
    hist(data2[,5],main = paste("Histogram of",y, colnames(data)[5]), xlab = "Returns",ylab = "Density")
  }
}

par(mfrow=c(2,2))
simon(BA)
par(mfrow=c(2,2))
simon(KR)

在这里插入图片描述

# Multivariate analysis #
# (1) Scatter plot for the relationship between BA and KR Return in 4 years

par(mfrow=c(2,2))
plot(x=BA$BA_Return[BA$Year==2015], y= KR$KR_Return[KR$Year==2015], 
     main = "Relationship between BA and KR Return in 2015 ", 
     xlab = "Return for BA", ylab = "Return for KR", pch="·")
plot(x=BA$BA_Return[BA$Year==2016], y= KR$KR_Return[KR$Year==2016], 
     main = "Relationship between BA and KR Return in 2016 ", 
     xlab = "Return for BA", ylab = "Return for KR", pch="·")
plot(x=BA$BA_Return[BA$Year==2017], y= KR$KR_Return[KR$Year==2017], 
     main = "Relationship between BA and KR Return in 2017 ", 
     xlab = "Return for BA", ylab = "Return for KR", pch="·")
plot(x=BA$BA_Return[BA$Year==2018], y= KR$KR_Return[KR$Year==2018], 
     main = "Relationship between BA and KR Return in 2018 ", 
     xlab = "Return for BA", ylab = "Return for KR", pch="·")

在这里插入图片描述

par(mfrow=c(2,1))
# Timeseries Plot for BA Price from 2015 to 2018
purchase.PBA <- ts(keydata$BA, start = c(2015,1,5), frequency = 252)
ts.plot(purchase.PBA, col = "red", ylab = 'BA  price', xlab = 'Date', main = 'Timeseries Plot for BA Price')
# Timeseries Plot for BA Return (Absolute Value) from 2015 to 2018
purchase.RBA <- ts(abs(diff(keydata$BA)), start = c(2015,1,5), frequency = 252)
ts.plot(purchase.RBA, col = "black", ylab = 'BA  return', xlab = 'Date', main = 'Timeseries Plot for BA Return (Absolute Value)')

在这里插入图片描述

### Problem 3. Linear Regression for KR ###
t <- nrow(KR)
kr_0 <- KR[1:t-1,5]
kr_1 <- KR[2:t,5]
liner_model <- data.frame(kr_0,y=kr_1)
cor(y=liner_model$y,x=liner_model)
model <- lm(y~.,data = liner_model)

cor(y=liner_model$y,x=liner_model)
summary(model)

# plot for liner model and using two transformation methods

par(mfrow=c(1,3))
plot(kr_0,y=kr_1, main = "Returns of KR on a given day and a previous trading day",
     abline(lm(kr_0~kr_1),col = "red", lty=5),cex.lab= 1.3, cex = 1.3,pch = "·",
     xlab = "Returns of KR on the previous trading day",ylab = "Returns of KR on a given day")
plot(log(kr_0),y=kr_1, main = "Returns of KR on a given day and a previous trading day",
     abline(lm(kr_0~kr_1),col = "red", lty=5),cex.lab= 1.3, cex = 1.3,pch = "·",
     xlab = "log(Returns of KR on the previous trading day)",ylab = "Returns of KR on a given day")
plot(sqrt(kr_0),y=kr_1, main = "Returns of KR on a given day and a previous trading day",
     abline(lm(kr_0~kr_1),col = "red", lty=5),cex.lab= 1.3, cex = 1.3,pch = "·",
     xlab = "sqrt(Returns of KR on the previous trading day)",ylab = "Returns of KR on a given day")

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值