资源描述:
《reilly media] r cookbook time series analysis》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、CHAPTER14TimeSeriesAnalysisIntroductionTimeseriesanalysishasbecomeahottopicwiththeriseofquantitativefinanceandautomatedtradingofsecurities.Manyofthefacilitiesdescribedinthischapterwereinventedbypractitionersandresearchersinfinance,securitiestrading,andportfoliomanagement.Beforest
2、artinganytimeseriesanalysisinR,akeydecisionisyourchoiceofdatarep-resentation(objectclass).Thisisespeciallycriticalinanobject-orientedlanguagesuchasRbecausethechoiceaffectsmorethanhowthedataisstored;italsodictateswhichfunctions(methods)willbeavailableforloading,processing,analyzin
3、g,printing,andplottingyourdata.WhenIfirststartedusingR,forexample,Isimplystoredmytimeseriesdatainvectors.Thatseemednatural.However,Iquicklydiscoveredthatnoneofthecoolestanalyticsfortimeseriesanalysisworkedwithsimplevectors.Iswitchedtousinganobjectclassintendedfortimeseriesanalysi
4、s,andthiswasthegatewaytoimportantfunctionsandanalytics.Thischapter’sfirstreciperecommendsusingthezooorxtspackagesforrepresentingtimeseriesdata.Theyarequitegeneralandshouldmeettheneedsofmostusers.Nearlyeverysubsequentrecipeassumesyouareusingoneofthosetworepresentations.Thextsimple
5、mentationisasupersetofzoo,soxtscandoeverythingthatzoocando.Inthischapter,wheneverarecipeworksforazooobject,youcansafelyassume(unlessstatedotherwise)thatitalsoworksforanxtsobject.ThebasedistributionofRincludesatimeseriesclasscalledts.Idon’trecommendthisrepresentationforgeneraluseb
6、ecausetheimplementationitselfistoorestrictiveandbecausetheassociatedfunctions(methods)arelimitedinscope,usefulness,andpower.355DateVersusDatetimeEveryobservationinatimeserieshasanassociateddateortime.Theobjectclassesusedinthischapter,zooandxts,giveyouthechoiceofusingeitherdatesor
7、datetimesforrepresentingthedata’stimecomponent.Youwouldusedatestorepresentdailydata,ofcourse,andalsoforweekly,monthly,orevenannualdata;inthesecases,thedategivesthedayonwhichtheobservationoccurred.Youwouldusedatetimesforintradaydata,whereboththedateandtimeofobservationareneeded.In
8、describingthischapter’srecipes,it’sprett