时间序列代码(R语言)

时间序列代码(R语言)

#-----------------------线性回归---------------------------------
setwd("D:/data")
da = read.table("m-ibm6708.txt",header = T)#读到一个data.frame里
ibm = da$ibm
sp = da$sprtn
plot(sp,ibm)
cor(sp,ibm)
marketmodel = lm(ibm~sp)
summary(marketmodel)#回归结果的summary
#---------------------------------------------------------------

#---------------------------------------------------------
#Measure of Correlation and Dependence
cor(sp,ibm)    #pearson's correlation
cor(sp,ibm,method = "spearman")#Spearman's rank correlation
cor(rank(sp),rank(ibm))        #Spearman's rank correlation
cor(sp,ibm,method = "kendall")#Kendall's tau

#invariance to monotonic nonlinear transformation
cor(exp(sp),exp(ibm))                   #not invariant
cor(exp(sp),exp(ibm),method = "spearman")#invaraint
cor(rank(exp(sp)),rank(exp(ibm)))
cor(exp(sp),exp(ibm),method = "kendall")#invaraint
#---------------------------------------------------------

#--------------------------------------------------------------------
#ACF\PACF\纯随机性检验
#Monthly returns of IBM stock from 1926 to 2008

setwd("D:/data")
da2 = read.table("m-ibm6708.txt",header = T)
ibm=da2[,2]
par(mfcol=c(2,1))
acf(ibm,lag=100)#100阶的样本自相关系数
pacf(ibm,lag=100)#样本偏自相关系数
#纯随机性检验(希望p值大于0.05,就不存在纯随机性)
B1=Box.test(ibm,lag=5,type='Ljung')#H0:rho_1=...=rho_5
B2=Box.test(ibm,lag=10,type='Ljung')#H0:rho_1=...=rho_10
B1$statistic
B1$p.value
B2$statistic
B2$p.value                     #Question:Q(5),Q(10)怎么计算呢?
#--------------------------------------------------------------------

你可能感兴趣的:(时间序列)