绝代码农 发表于 2021-7-1 20:43:03

可视化绘图分析(一)

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
    data2 <- subset(data,Year== y)
    hist(data2[,5],main = paste("Histogram of",y, colnames(data)), 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, y= KR$KR_Return,
   main = "Relationship between BA and KR Return in 2015 ",
   xlab = "Return for BA", ylab = "Return for KR", pch="·")
plot(x=BA$BA_Return, y= KR$KR_Return,
   main = "Relationship between BA and KR Return in 2016 ",
   xlab = "Return for BA", ylab = "Return for KR", pch="·")
plot(x=BA$BA_Return, y= KR$KR_Return,
   main = "Relationship between BA and KR Return in 2017 ",
   xlab = "Return for BA", ylab = "Return for KR", pch="·")
plot(x=BA$BA_Return, y= KR$KR_Return,
   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 = 'BAprice', 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 = 'BAreturn', xlab = 'Date', main = 'Timeseries Plot for BA Return (Absolute Value)')

### Problem 3. Linear Regression for KR ###
t <- nrow(KR)
kr_0 <- KR
kr_1 <- KR
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")


  
页: [1]
查看完整版本: 可视化绘图分析(一)