r语言股票下载 ar模型 描述统计 附代码数据.docx

上传人:b****6 文档编号:4324099 上传时间:2022-11-29 格式:DOCX 页数:18 大小:60.52KB
下载 相关 举报
r语言股票下载 ar模型 描述统计 附代码数据.docx_第1页
第1页 / 共18页
r语言股票下载 ar模型 描述统计 附代码数据.docx_第2页
第2页 / 共18页
r语言股票下载 ar模型 描述统计 附代码数据.docx_第3页
第3页 / 共18页
r语言股票下载 ar模型 描述统计 附代码数据.docx_第4页
第4页 / 共18页
r语言股票下载 ar模型 描述统计 附代码数据.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

r语言股票下载 ar模型 描述统计 附代码数据.docx

《r语言股票下载 ar模型 描述统计 附代码数据.docx》由会员分享,可在线阅读,更多相关《r语言股票下载 ar模型 描述统计 附代码数据.docx(18页珍藏版)》请在冰豆网上搜索。

r语言股票下载 ar模型 描述统计 附代码数据.docx

r语言股票下载ar模型描述统计附代码数据

christyxyu.R

Administrator

WedFeb0823:

39:

242017

#a.DownloadthemanualforTimeSeriesAnalysiswithR,PartIbyWalterZucchini,OlegNenadi´forreference

#asyoumayneedittocompletetheassignment.

#http:

//www.statoek.wiso.uni-goettingen.de/veranstaltungen/zeitreihen/sommer03/ts_r_intro.pdf

#b.DownloadQuantmodmanual

#c.SearchforMicrosoftsymbolinGooglefinance

library(quantmod)

##Loadingrequiredpackage:

xts

##Loadingrequiredpackage:

zoo

##

##Attachingpackage:

'zoo'

##Thefollowingobjectsaremaskedfrom'package:

base':

##

##as.Date,as.Date.numeric

##Loadingrequiredpackage:

TTR

##Version0.4-0includednewdatadefaults.See?

getSymbols.

#MicrosoftsymbolinGooglefinanceisMSFT

#d.DownloadMicrosoftstockpricedatafromJan3,2010toJan20,2016inRusinggetSymbolscommandin

#Quantmod,checkifdataisreadproperlyusingheadandtailcommand5

getSymbols('MSFT',from="2010-01-03",to="2016-01-20")

##Asof0.4-0,'getSymbols'usesenv=parent.frame()and

##auto.assign=TRUEbydefault.

##

##Thisbehaviorwillbephasedoutin0.5-0whenthecallwill

##defaulttouseauto.assign=FALSE.getOption("getSymbols.env")and

##getOptions("getSymbols.auto.assign")arenowcheckedforalternatedefaults

##

##Thismessageisshownoncepersessionandmaybedisabledbysetting

##options("getSymbols.warning4.0"=FALSE).See?

getSymbolsformoredetails.

##[1]"MSFT"

head(MSFT)

##MSFT.OpenMSFT.HighMSFT.LowMSFT.CloseMSFT.Volume

##2010-01-0430.6231.1030.5930.9538409100

##2010-01-0530.8531.1030.6430.9649749600

##2010-01-0630.8831.0830.5230.7758182400

##2010-01-0730.6330.7030.1930.4550559700

##2010-01-0830.2830.8830.2430.6651197400

##2010-01-1130.7130.7630.1230.2768754700

##MSFT.Adjusted

##2010-01-0425.71042

##2010-01-0525.71872

##2010-01-0625.56089

##2010-01-0725.29506

##2010-01-0825.46951

##2010-01-1125.14553

tail(MSFT)

##MSFT.OpenMSFT.HighMSFT.LowMSFT.CloseMSFT.Volume

##2016-01-1252.7653.1052.0652.7836095500

##2016-01-1353.8054.0751.3051.6466883600

##2016-01-1452.0053.4251.5753.1152381900

##2016-01-1551.3151.9750.3450.9970739100

##2016-01-1951.4851.6850.0650.5643564500

##2016-01-2049.9851.3849.1050.7963273000

##MSFT.Adjusted

##2016-01-1251.37038

##2016-01-1350.26083

##2016-01-1451.69157

##2016-01-1549.62819

##2016-01-1949.20968

##2016-01-2049.43353

#e.Plotgraphforstockprice5

plot(MSFT)

##Warninginplot.xts(MSFT):

onlytheunivariateserieswillbeplotted

#f.CalculatelogreturnsforAdjustedSeriesandPlotsimpletimeseriesgraphforreturns5

n<-length(MSFT[,4]);

lrest<-log(as.numeric(MSFT[,4])[-1]/as.numeric(MSFT[,4])[-n])

#g.Plotreturnsdistributiongraph5

hist(lrest,breaks=50,col="green",freq=F)

lines(density(lrest),col="red")

#h.Whatdidyoulearnaboutthedatainstep(g)5

#直方图来看,数据为左偏分布

#i.CalculateBasicStatisticsforReturnSeriesandreport5

summary(lrest)

##Min.1stQu.MedianMean3rdQu.Max.

##-0.1210000-0.00766500.00000000.00032570.00817500.0994100

#j.Lookatthebasicstatsandcommentonthevaluesforskewnessandkurtosis5

#

library(fBasics)

##Loadingrequiredpackage:

timeDate

##Loadingrequiredpackage:

timeSeries

##

##Attachingpackage:

'timeSeries'

##Thefollowingobjectismasked_by_'.GlobalEnv':

##

##MSFT

##Thefollowingobjectismaskedfrom'package:

zoo':

##

##time<-

##

##RmetricsPackagefBasics

##AnalysingMarketsandcalculatingBasicStatistics

##Copyright(C)2005-2014RmetricsAssociationZurich

##EducationalSoftwareforFinancialEngineeringandComputationalScience

##RmetricsisfreesoftwareandcomeswithABSOLUTELYNOWARRANTY.

##https:

//www.rmetrics.org---Mailto:

info@rmetrics.org

##

##Attachingpackage:

'fBasics'

##Thefollowingobjectismaskedfrom'package:

TTR':

##

##volatility

datadesc=function(X){

result=list(0);#resultlisttoreturn

mean=mean(X);#mean

var=var(X)#variance,

pearsonskew=3*(mean(X)-median(X))/sd(X)#Pearsoncoefficientofskewness

kurt=kurtosis(X)#kurtosis,

quantile1=quantile(X,probs=0.25)#firstquartile,

med=median(X)#median,

quantile3=quantile(X,probs=0.25)#thirdquartile,

max=max(X)#minimumand

min=min(X)#maximum.

result=list(

mean=mean,

variance=var,

skewness=pearsonskew,

kurtosis=kurt,

"firstquartile"=quantile1,

median=med,

"thirdquartile"=quantile3,

"maximum"=max,

minimum=min

return(result)

}

datadesc(lrest)

##$mean

##[1]0.0003256584

##

##$variance

##[1]0.0002164387

##

##$skewness

##[1]0.06640734

##

##$kurtosis

##[1]7.519028

##attr(,"method")

##[1]"excess"

##

##$`firstquartile`

##25%

##-0.007664925

##

##$median

##[1]0

##

##$`thirdquartile`

##25%

##-0.007664925

##

##$maximum

##[1]0.09941299

##

##$minimum

##[1]-0.1210332

#从峰度和偏度的值来看,由于偏度大于零,因此数据成右偏分布。

峰度大于0,说明它是比正态分布要陡峭

#k.SetupHypothesisfortestingmean,skewnessandkurtosis5

#H0:

Ifvaluesofmean,skewnessandkurtosisisclosetozero,thendatasetisnormallydistributed.

#l.TesttheHypothesisusingtestslearnedinclass(i-individualtests;andii-combinedtest–JB;

#reportyourtestresultswithp-valuesandwhetheryourejectoracceptH0)5

#i-individualtests;

#Shapiro-Wilk

shapiro.test(lrest)

##

##Shapiro-Wilknormalitytest

##

##data:

lrest

##W=0.93798,p-value<2.2e-16

#ii-combinedtest–JB

tseries:

:

jarque.bera.test(lrest)

##

##JarqueBeraTest

##

##data:

lrest

##X-squared=3599.1,df=2,p-value<2.2e-16

##从结果来看,由于p小于0.05,因此可以拒绝原假设H0

#m.Commentaboutdatapropertiesbasedonthesetests:

#从正态分布的Shapiro-Wilk检验和jarque-Bera正态性检验的结果来看,有p小于0.05,因此可以拒绝对数收益率符合正态分布的假设,可以认为收益率,不是正态分布

#i.Thedifferencebetweenstep(g)vsstep(j)5

#从密度直方图的结果只能从直觉上判断数据是否符合正态分布,而通过峰度和偏度来判断,更加精确,可以从数字的角度来判断数据是否呈现偏正态分布。

#ii.whyjointtestsisbetterthanindividual

#Ajointhypothesistestsmorethanoneconditionsimultaneously

#7.Downloadthesp500datausingSymbol‘^GSPC’fromJan3,2011toJan20,2016(50pts)

#a.Carryoutinitialsteps,suchascalculatelogreturns,drawtimeplot,distributiongraph,

#calculatereturns,basicstatandemployrelatedtests25

getSymbols('GSPC',from="2011-01-03",to="2016-01-20")

##[1]"GSPC"

head(GSPC)

##GSPC.OpenGSPC.HighGSPC.LowGSPC.CloseGSPC.Volume

##2011-01-0322.6022.60022.470822.49006600

##2011-01-0422.5922.59022.450022.47525100

##2011-01-0522.6122.76022.610022.710015000

##2011-01-0622.7622.93922.710022.760015900

##2011-01-0722.9322.93022.790022.92007100

##2011-01-1022.8122.97022.800022.970021500

##GSPC.Adjusted

##2011-01-0316.9375

##2011-01-0416.9264

##2011-01-0517.1032

##2011-01-0617.1409

##2011-01-0717.2614

##2011-01-1017.2990

tail(GSPC)

##GSPC.OpenGSPC.HighGSPC.LowGSPC.CloseGSPC.Volume

##2016-01-1221.4021.4121.340021.40006900

##2016-01-1321.4021.4221.300021.400011900

##2016-01-1421.4421.4421.220121.350011800

##2016-01-1521.3021.3021.050021.224511100

##2016-01-1921.3821.4021.050021.26598100

##2016-01-2021.2521.2721.074621.270023000

##GSPC.Adjusted

##2016-01-1220.4356

##2016-01-1320.4356

##2016-01-1420.3878

##2016-01-1520.2680

##2016-01-1920.3075

##2016-01-2020.3114

plot(GSPC)

##Warninginplot.xts(GSPC):

onlytheunivariateserieswillbeplotted

n<-length(GSPC[,4]);

lrest<-log(as.numeric(GSPC[,4])[-1]/as.numeric(GSPC[,4])[-n])

hist(lrest,breaks=50,col="green",freq=F)

lines(density(lrest),col="red")

datadesc(lrest)

##$mean

##[1]-4.395046e-05

##

##$variance

##[1]6.726461e-05

##

##$skewness

##[1]-0.0160765

##

##$kurtosis

##[1]5.903059

##attr(,"method")

##[1]"excess"

##

##$`firstquartile`

##25%

##-0.003570522

##

##$median

##[1]0

##

##$`thirdquartile`

##25%

##-0.003570522

##

##$maximum

##[1]0.04301739

##

##$minimum

##[1]-0.05164179

#Shapiro-Wilk

shapiro.test(lrest)

##

##Shapiro-Wilknormalitytest

##

##data:

lrest

##W=0.92202,p-value<2.2e-16

#ii-combinedtest–JB

tseries:

:

jarque.bera.test(lrest)

##

##JarqueBeraTest

##

##data:

lrest

##X-squared=1866.5,df=2,p-value<2.2e-16

#b.Whatdidyoulearnaboutthedatapropertiesinstep(a)5

#从描述统计量的结果来看,数据为左偏分布,从假设检验的结果来,由于p小于0.05,因此拒绝原假设,可以认为数据不符合正态分布

#c.Plotpacf,acf,alsochecktheorderofthemodelusing‘ar’commandinR

#WhichARmodelissuggested5

acf(lrest)

pacf(lrest)

ar(lrest)

##

##Call:

##ar(x=lrest)

##

##Coefficients:

##12345678

##-0.00600.05490.01320.0118-0.02370.04960.03420.0983

##910

##-0.04110.0416

##

##Orderselected10sigma^2estimatedas6.625e-05

#d.EstimatebothAR

(1)andAR

(2)modelforreturnstoSP500index

#andcommentonsignificanceofparameters5

fit=ar(lrest,FALSE,1)#fitar

(1)

fit

##

##Call:

##ar(x=lrest,aic=FALSE,order.max=1)

##

##Coefficients:

##1

##-0.0084

##

##Orderselected1sigma^2estimatedas6.731e-05

(1-pnorm(abs(fit$ar)/sqrt(diag(fit$asy.var.coef))))*2

##[1]0.7640941

fit=ar(lrest,FALSE,2)#fitar

(2)

fit

##

##Call:

##ar(x=lrest,aic=FALSE,order.max=2)

##

##Coefficients:

##12

##-0.00790.0649

##

##Orderselected2sigma^2estimatedas6.708e-05

(1-pnorm(abs(fit$ar)/sqrt(diag(fit$asy.var.coef))))*2

##[1]0.778622760.02073683

#从结果来,ar2模型的系数显著性相对ar1更强

#e.Checkandreportiftheresidualsformodelsestimatedinpart(d)arewhitenoiseornot5

fit=ar(lrest,FALSE,1)#fitar

(1)

fit

##

##Call:

##ar(x=lrest,aic=FAL

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 材料科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1