商院本科生吧 关注:3贴子:54
  • 1回复贴,共1

相关性分析,R软件

只看楼主收藏回复

library(quantmod)
library(PerformanceAnalytics)
setSymbolLookup(GLDQ=list(name="000651.sz"),RSFZ=list(name="002146.sz"),TQLY=list(name="0002466.sz"),HKWS=list(name="002415.sz"))
getSymbols(c(GLDQ,RSFZ,TQLY,HKWS),start="2010-1-1",end="2018-1-1")
##install.packages("PerformanceAnalytics")
##install.packages("tseries")
library(PerformanceAnalytics)
library(tseries)
GLDQ <- get.hist.quote('000651.sz', start="2014-1-1", end="2018-4-16", 'Close', 'yahoo')
RSFZ<- get.hist.quote('002146.sz', start="2014-1-1", end="2018-4-16", 'Close', 'yahoo')
TQLY <- get.hist.quote('002466.sz', start="2014-1-1", end="2018-4-16", 'Close', 'yahoo')
HKWS<- get.hist.quote('002415.sz', start="2014-1-1", end="2018-4-16", 'Close', 'yahoo')
KDXF<- get.hist.quote('002230.sz', start="2014-1-1", end="2018-4-16", 'Close', 'yahoo')
prc.ALL <- merge(GLDQ,RSFZ, TQLY, HKWS,KDXF)
rts.ALL <- diff(log(prc.ALL))
rts <- na.omit(rts.ALL)
colnames(rts) <- c("GLDQ", 'RSFZ', 'TQLY', 'HKWS','KDXF')
head(prc.ALL) #查看原始数据前几行
plot(prc.ALL,main = "5只股票收盘价走势图") #原始数据图
head(rts.ALL) #取对数差分后数据
plot(rts.ALL,main = "5只股票日收益时序图") #对数差分后数据图
head(rts) #去掉缺省值后的前几行数据
cumreturn <- cumprod(1 + rts ) #计算各股票累积收益率
head(cumreturn,6)
plot.zoo(cumreturn,type="l",lty=1:5,ylim=c(0,3.5),col=1:5,
main="5只股票的累计收益率时序图(Y2014-Y2018)")
cor(rts) #观察股票收益率相关性
pairs(data.frame(rts), pch=20, col='darkblue',
main="5支股票的相关性收益率图")
qqnorm(rts$GLDQ,col="red",main="格力电器QQ图")
hist(GLDQ,prob=T,breaks=20,col="red",freq = FALSE,border="white",xlab=" ",ylab=" ",main = "格力电器概率分布区间")
lines(density(na.omit(GLDQ)), col="blue", lwd=2)
qqnorm(rts$RSFZ,col="red",main="荣盛发展QQ图")
hist(RSFZ,prob=T,breaks=20,col="red",freq = FALSE,border="white",xlab=" ",ylab=" ",main = "格力电器概率分布区间")
lines(density(na.omit(RSFZ)), col="blue", lwd=2)
qqnorm(rts$TQLY,col="red",main="天齐锂业QQ图")
hist(TQLY,prob=T,breaks=20,col="red",freq = FALSE,border="white",xlab=" ",ylab=" ",main = "格力电器概率分布区间")
lines(density(na.omit(TQLY)), col="blue", lwd=2)
qqnorm(rts$HKWS,col="red",main="海康威视QQ图")
hist(HKWS,prob=T,breaks=20,col="red",freq = FALSE,border="white",xlab=" ",ylab=" ",main = "格力电器概率分布区间")
lines(density(na.omit(HKWS)), col="blue", lwd=2)
qqnorm(rts$KDXF,col="red",main="科大讯飞QQ图")
hist(KDXF,prob=T,breaks=20,col="red",freq = FALSE,border="white",xlab=" ",ylab=" ",main = "格力电器概率分布区间")
lines(density(na.omit(KDXF)), col="blue", lwd=2)
##自相关性
library(xts)
GLDQ1=na.omit(GLDQ)
acf(coredata(GLDQ1),main="格力电器自相关图")
RSFZ1=na.omit(RSFZ)
acf(coredata(RSFZ1),main="荣盛发展自相关图")
TQLY1=na.omit(TQLY)
acf(coredata(TQLY1),main="天齐锂业自相关图")
HKWS1=na.omit(HKWS)
acf(coredata(HKWS1),main="海康威视自相关图")
KDXF1=na.omit(KDXF)
acf(coredata(KDXF1),main="科大讯飞自相关图")
##波动率
library(fBasics)
basicStats(rts$GLDQ)
basicStats(rts$RSFZ)
basicStats(rts$TQLY)
basicStats(rts$HKWS)
basicStats(rts$KDXF)
##平稳性
plot.zoo(prc.ALL,main = "5只股票收盘价走势图",lty=1:5,col=1:5)



IP属地:湖南1楼2018-05-23 15:53回复
    请问有个报错怎么改啊。
    没有"get.hist.quote"这个函数


    2楼2021-01-21 19:41
    回复