资源描述:
《时间序列 R语言考试基本代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、典型例题HW2——5HW3——3,4HW4——EXAM14,15——VARlibrary(quantmod)getSymbols("AAPL",from="2008-01-03",to="2015-01-28")dim(AAPL)head(AAPL)tail(AAPL)chartSeries(AAPL,theme="white")%Obtaintimeplotofclosingpriceandtradingvolumeda<-read.csv("d-vix0411.csv",header=T)%有时候不用header参数,看he
2、ad(da)的数据形式da<-read.table("d-vix0411.txt",header=T)AAPL.rtn=diff(log(AAPL$AAPL.Adjusted))%Computelogreturnsunrate<-as.numeric(UNRATE[,1])%Usearegularvector,insteadofan“xts”objectlibrary(fBasics)ts.plot(ibm,main="MonthlyIBMsimplereturns:1968-2015")%TimeplotbasicStats(
3、ibm)apply(rtn,2,basicStats)##Thiscommandsaysapply"basicStats"toeverycolumnsin"rtn"d4=density(ibm)plot(d4$x,d4$y,type='l',xlab="rtn",ylab='AAPL')##density图t.test(lnIBM)%%Testmean=0vsmean.not.Zero也可以用basicStats命令去计算normalTest(lnIBM,method=’jb’)%testingnormalityoffinanc
4、ialreturnseriess3=skewness(lnIBM);T<-length(lnIBM)tst<-s3/sqrt(6/T)%testskewnesspv<-2*pnorm(tst)%calculatepvaluek4<-kurtosis(lnIBM)tst<-k4/sqrt(24/T)%testexcesskurtosismu<-mean(sbux);v1<-var(sbux)%%prediction或者可以用t-test的信息lcl<-mu-1.96*sqrt(v1)ucl<-mu+1.96*sqrt(v1)c(l
5、cl,ucl)correlationcor(sp,ibm)[1]0.5785249cor(sp,ibm,method="kendall")%randomcopy[1]0.4172056cor(sp,ibm,method="spearman")%rankcorrelation[1]0.58267cor(rank(ibm),rank(sp))[1]0.58267x[1,]%showthefirstrowofthedataX[,4:5]%showthe4and5columnsofthedatay=ts(x[,3],frequency=
6、252,start=c(2004,1))<==Createatime-seriesobjectinRplot(y,type='l',xlab='year',ylab='rtn')par(mfcol=c(2,1))<==Toputtwoplotsonasinglepagehist(y,nclass=50)%直方图tdx=(c(1:615)+11)/12+1959%创建timeplot的横坐标plot(tdx,xt,xlab='year',ylab='temp',type='l')plot(tdx[-1],zt,xlab='year
7、',ylab='diff(temp)',type='l')##注意difference使得横坐标年份变化int=cbind(x[300:914,4],y[,4])<==LineupthetwoTBratesacf(ibm)m1<-acf(ibm)names(m1)m1$acfm2<-pacf(ibm)Box.test(ibm,lag=10)%Box-PierceQ(m)test,序列是否自相关Box.test(ibm,lag=10,type='Ljung')%Ljung-BoxQ(m)testm1=lm(r3~r1)%Fitar
8、egressionmodelwithlikelihoodmethodlAR,MA,ARMA,ARIMAmodelm1=ar(x,method='mle')%AutomaticARfittingusingAICcriterionnames(m1)m1$orderp