《时间序列分析及应用:R语言》 笔记

#《时间序列分析及应用:R语言(原书第2版)》#
#Time Series Analysis with Application in R#
#Jonathan D.Cryer 著#
#Kung-Sik Chan 著#
#机械工程出版社#

第一章 引论
    1.1 时间序列举例
        1.1.1 洛杉矶降雨量
            install.packages('TSA')
            install.packages('tseries')
            install.packages('forecast')          
            library(TSA)    #包
            library(tseries)    #包
            library(forecast)   #包

            View(larain) #看一下数据
            class(larain)   #[1] "ts"   ts为时间序列类型

            win.graph(width=4.875,height=2.5,pointsize=8)
            data(larain);plot(larain,ylab='Inches',xlab='Year',type='o')    #画图

            win.graph(width=3,height=3,pointsize=8)
            plot(y=larain,x=zlag(larain),ylab='Inches',xlab='Previous Year Inches') #看当年和前一年的降雨量图,发现没什么关系,没有研究意义
        1.1.2 化工过程
            win.graph(width=4.875,height=2.5,pointsize=8)
            data(color);plot(color,ylab='Color Property',xlab='Batch',type='o')    #画图

            win.graph(width=3,height=3,pointsize=8)
            plot(y=color,x=zlag(color),ylab='Color Property',xlab='Previous Batch Color Property') #有关系,但关系不大
        1.1.3 加拿大野兔
            win.graph(width=4.875,height=2.5,pointsize=8)
            data(hare);plot(hare,ylab='Abundance',xlab='Year',type='o')    #画图

            win.graph(width=3,height=3,pointsize=8)
            plot(y=hare,x=zlag(hare),ylab='Abundance',xlab='Previous Year Abundance') #关系明显
        1.1.4 月平均气温
            win.graph(width=4.875,height=2.5,pointsize=8)
            data(tempdub);plot(tempdub,ylab='Tempurature',type='o')    #画图,良好的季节性
        1.1.5 销售量
            data(oilfilters);plot(oilfilters,ylab='Sales',type='o')    #画图

            plot(oilfilters,type='l',ylab='Sales')
            points(y=oilfilters,x=time(oilfilters),pch=as.vector(season(oilfilters)))

第二章 基本概念
    随机游动时间序列图
        win.graph(width=4.875,height=2.5,pointsize=8)
        data(rwalk)
        plot(rwalk,type='o')

第三章 趋势
    3.3 回归方法
        3.3.1 时间的线性趋势和二次趋势
            data(rwalk)
            model1=lm(rwalk~time(rwalk))
            summary(model1)
            plot(rwalk,type='o')
            abline(model1)
        3.3.2 周期性或季节性趋势
            data(tempdub)
            month.=season(tempdub)
            model2=lm(tempdub~month.-1)
            summary(model2)

            model3=lm(tempdub~month.)
            summary(model3)

        3.3.3 余弦趋势
            har.=harmonic(tempdub,1)
            model4=lm(tempdub~har.)
            summary(model4)

            win.graph(width=4.875,height=2.5,pointsize=8)
            plot(ts(fitted(model4),freq=12,start=c(1964,1)),ylab='Temperature',type='l',ylim=range(c(fitted(model4),tempdub)));
            points(tempdub)

第四章 平稳时间序列模型
    ARMA_自回归滑动平均模型
    
第九章 预测
    9.1 预测的图示
        9.1.1 确定型预测
            气温余弦函数预测
            现有的数据:1964-1976年
            预测:1977-1978年
                data(tempdub)
                tempdub1 = ts( data = c(tempdub,rep(NA,24)) , start=start(tempdub) , freq = frequency(tempdub) )
                har. = harmonic( tempdub ,1 )
                m5.tempdub = arima ( tempdub , order=c(0,0,0) , xreg = har. )
                newhar. = harmonic( ts(rep(1,24) , start = c(1976,1) , freq = 12) , 1 ) 
                win.graph(width=4.875,height=2.5,pointsize=8)
                plot( m5.tempdub , n.ahead = 24 , nl = c(1972.1) , newxreg = newhar. , type = 'b' , ylab = 'Temperature' , xlab = 'Year')
                #n.ahead为预期数
                #注意:做这个预期,只能装载一个TSA包
        9.1.2 ARIMA模型
            9.1.2.1 化工过程
                data(color)
                m1.color = arima(color,order = c(1,0,0))    #auto.arima(color)可以确定order怎么填.加载tseries包forecast包可用auto.arima,但是要解除,detach("package:tseries")
                plot(m1.color , n.ahead = 12 , type = 'b' , xlab = 'Time' , ylab = 'Color Property')
                abline( h = coef(m1.color)[names(coef(m1.color))=='intercept'])
                            
            9.1.2.2 野兔丰度
                data(hare)
                m1.hare = arima(hare,order = c(3,0,0))    #auto.arima(hare)可以确定order怎么填
                plot(m1.hare , n.ahead = 25 , type = 'b' , xlab = 'Time' , ylab = 'hare')
                abline( h = coef(m1.hare)[names(coef(m1.hare))=='intercept'])

第十章 季节模型
    data(co2)
    win.graph(width=4.875,height=3,pointsize=8)
    plot(co2 , ylab = 'co2')
    
    plot( window(co2 , start=c(2000,1)) , ylab = 'co2')   #window 取时间序列的子集
    Month = c('J','F','M','A','M','J','J','A','S','O','N','D')
    points( window(co2, start=c(2000,1)) , pch = Month)

    10.1 季节ARIMA模型预测
        data(co2)
        win.graph(width=4.875,height=3,pointsize=8)
        m1.co2 = arima( co2  ,order=c(0,1,1), seasonal = list( order=c(0,1,1) , period = 12) )
        plot(m1.co2 , n1 = c(2002,1) , n.ahead = 48 ,xlab = 'Year' , type = 'o' , ylab = 'co2 levels')
  
第十一章 时间序列回归模型
    11.1 干预分析
        美国航空里程
        911影响了规律
        data(airmiles)
        win.graph(width=4.875,height=3,pointsize=8)
        plot( airmiles , ylab = 'airmiles')   
        Month = c('J','F','M','A','M','J','J','A','S','O','N','D')
        points( airmiles, pch = Month)
        
        建模,拟合
        ????????????
        air.m1= arimax(log(airmiles),order = c(0,1,1) ,  seasonal = list( order=c(0,1,1) , period = 12) ,
        xtransf = data.frame( I911 = 1*(seq(airmiles)==69),I911 = 1*(seq(airmiles)==69)),
        transfer = list(c(0,0),c(1,0)),
        xreg = data.frame(Dec96 = 1*(seq(airmiles)==12),Jan97=1*(seq(airmiles)==13),
        Dec02=1*(seq(airmiles)==84)),
        method = 'ML')
        air.m1
        
        plot(log(airmiles),ylab = 'Log(airmiles)')
        points(fitted(air.m1))
            
    11.2 伪相关例子


打赏

暂无评论

发布评论