今天的实验主要是对10月1日交通流量进行BoxJenkins建模。
library(TFTSA) library(forecast) library(ggplot2)
tml1001all <- read.csv("D:\\data\\thesis\\201610\\tml1001all.csv",header = T) dim(tml1001all)
names(tml1001all)
tml1001 <- tml1001all[,c(5,15)] names(tml1001) <- c("时间序号","机动车流量") names(tml1001)
ggplot(tml1001,aes(x=tml1001$时间序号,y=tml1001$机动车流量))+ geom_point(colour="steelblue")+ geom_line(colour="steelblue")
flow1001 <- tml1001$机动车流量 dif1001 <- diff(flow1001)
eacf(dif1001)
识别出差分序列似乎是MA(1)模型
fit_auto1 <- auto.arima(dif1001) fit_auto1
fit_auto2 <- auto.arima(flow1001) fit_auto2
识别出差分序列是MA(1)型,原序列为IMA(1,1)型,系数显著不为0
set.seed(123) fit_auto3 <- armasubsets(y=dif1001,nar=5,nma=5,y.name='test',ar.method = "ols") plot(fit_auto3)
可以看出差分序列是MA(1)型的
fit1 <- arima(flow1001,order=c(0,1,1),method="CSS") fit1
fit1res <- residuals(fit1) fit1fit <- fitted(fit1)
length(fit1fit) length(fit1res)
plot(x=tml1001$时间序号,y=tml1001$机动车流量,type="b") lines(fit1fit,col="red")
fit1fit <- as.vector(fit1fit) fit1res <- as.vector(fit1res) fit1df <- cbind(tml1001,fit1fit,fit1res) names(fit1df)[c(3,4)] <- c("拟合值","残差") names(fit1df)
#write.csv(fit1df,"D:\\data\\thesis\\201610\\tmldata\\fitarima.csv")
ggplot(fit1df,aes(x=fit1df$时间序号,y=fit1df$机动车流量))+ geom_point(colour="steelblue")+ geom_line(colour="steelblue")+ geom_line(aes(x=fit1df$时间序号,y=fit1df$拟合值),colour="red",size=1)+ #xlab("时间序号")+ylab("车流量")+scale_color_hue()+ scale_color_manual(values=c("steelblue","red"))+ xlab("Timestamp")+ylab("Traffic volume")+labs(color="Legend")+ scale_x_continuous(breaks = seq(0,288,24),limits = c(0,288))+ scale_y_continuous(breaks = seq(0,120,20)) #ggsave("D:\\王致远\\论文\\大论文\\实验\\绘图\\ARIMA结果.jpg",width=7.29,height=4.5,dpi=600)
fit1df <- fit1df[,c(1,3,2)] #fit1df <- fit1df[,1:3] names(fit1df) <- c("Timestamp","real value","forecast value") fit1melt <- reshape2::melt(fit1df,id.vars="Timestamp") ggplot(fit1melt,aes(x=fit1melt$Timestamp,y=fit1melt$value,group=fit1melt$variable,color=fit1melt$variable))+ geom_line()+geom_point()+ scale_color_manual(values=c("steelblue","red"))+ xlab("Timestamp")+ylab("Traffic volume")+labs(color="Legend")+ scale_x_continuous(breaks = seq(0,288,24))+ scale_y_continuous(breaks = seq(0,120,20))+ theme_bw()
ggsave(file="plot/05_arima.jpg",width=7.29,height=4.5,dpi=600)
TFTSA::flow_evaluate(fit1df$机动车流量,fit1df$拟合值)
plot(1:288,fit1res)
Box.test(fit1res)
qqnorm(fit1res) qqline(fit1res)
acf(fit1res)
pacf(fit1res)
fit1density <- density(fit1res) plot(fit1density)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.