时间序列1.rar

  • zzcdc
    了解作者
  • R language
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2021-03-31 23:20
    上传日期
利用R语言进行详细的时间序列分析,以实例展示过程。
时间序列1.rar
  • 时间序列1.R
    13.1KB
内容介绍
#生成时间序列对象 sales<-c(18,33,41,7,34,35,24,25,24,21,25,20,22,31,40,29,25,21,22,54,31,25,26,35) tsales<-ts(sales,start=c(2003,1),frequency = 12) tsales plot(tsales) start(tsales) end(tsales) frequency(tsales) tsales.subset<-window(tsales,start=c(2003,5),end=c(2004,6)) tsales.subset #简单移动平均 install.packages("forecast") install.packages("zoo") library(forecast) opar<-par(no.readonly=TRUE)par(mfrow=c(2,2))ylim<-c(min(Nile),max(Nile))plot(Nile,main="Raw time series")plot(ma(Nile,3),main="Simple Moving Average (k=3)",ylim=ylim)plot(ma(Nile,7),main="Simple Moving Average (k=7)",ylim=ylim)plot(ma(Nile,15),main="Simple Moving Average (k=15)",ylim=ylim)par(opar) #季节性分解 plot(AirPassengers) lAirPassengers<-log(AirPassengers)plot(lAirPassengers,ylab="log(AirPassengers)") fit<-stl(lAirPassengers,s.window="period")plot(fit) fittime.seriesexp(fittime.seriesexp(fittime.series exp(fittime.series) par(mfrow=c(2,1))library(forecast)monthplot(AirPassengers,xlab="",ylab="")seasonplot(AirPassengers,year.labels="TRUE",main="") #单指数平滑library(forecast)fit<-ets(nhtemp,model="ANN")fit ETS(A,N,N) Call: ets(y = nhtemp, model = "ANN") Smoothing parameters: alpha = 0.182 Initial states: l = 50.2759 sigma: 1.1263 AIC AICc BIC 265.9298 266.3584 272.2129 forecast(fit,1) Point?? Forecast Lo 80?? ?? ??Hi 80?? ??Lo 95?? ?? Hi 951972?? ??51.87045 50.42708 53.31382 49.66301 54.0779 plot(forecast(fit,1),xlab="Year",ylab=expression(paste("Temperature (",degreee*F,")",)),main="New Haven Annual Mean Temperature") accuracy(fit) ME?? ?? ?? ?? RMSE?? ?? ??MAE?? ?? ?? ??MPE?? ?? ?? ??MAPE?? ?? ??MASE?? ?? ?? ACF1Training set 0.1460295 1.126268 0.8951331 0.2418693 1.748922 0.7512497 -0.00653111 ME: Mean Error RMSE: Root Mean Squared Error MAE: Mean Absolute Error MPE: Mean Percentage Error MAPE: Mean Absolute Percentage Error MASE: Mean Absolute Scaled Error ACF1: Autocorrelation of errors at lag 1. #有水平项,斜率以及季节性的指数模型library(forecast)fit<-ets(log(AirPassengers),model="AAA")fit ETS(A,A,A) Call: ets(y = log(AirPassengers), model = "AAA") Smoothing parameters: alpha = 0.6534 beta = 1e-04 gamma = 1e-04 Initial states: l = 4.8022 b = 0.01 s=-0.1047 -0.2186 -0.0761 0.0636 0.2083 0.217 0.1145 -0.011 -0.0111 0.0196 -0.1111 -0.0905 sigma: 0.0359 AIC AICc BIC -208.3619 -203.5047 -157.8750 accuracy(fit) pred<-forecast(fit,5)pred ME?? ?? ?? ?? ?? ?? RMSE?? ?? ?? ??MAE?? ?? ?? ?? ?? MPE?? ?? ?? ??MAPE?? ?? ?? MASE?? ?? ?? ACF1Training set -0.0006710596 0.03592072 0.02773886 -0.01250262 0.508256 0.2291672??0.09431354 plot(pred,main="Forecast for Air Travel",ylab="Log(AirePassengers)",xlab="Time") predmean<???exp(predmean<???exp(predmean<-exp(predmean)predlower<???exp(predlower<???exp(predlower<-exp(predlower)predupper<???exp(predupper<???exp(predupper<-exp(predupper)p<-cbind(predmean,predmean,predmean,predlower,pred$upper)dimnames(p)[[2]]<-c("mean","Lo 80","Lo 95","Hi 80","Hi 95")p mean?? ?? ??Lo 80?? ?? Lo 95?? ?? Hi 80?? ?? Hi 95Jan 1961 447.4958 427.3626 417.0741 468.5774 480.1365Feb 1961 442.7926 419.1001 407.0756 467.8245 481.6434Mar 1961 509.6958 478.7268 463.1019 542.6682 560.9776Apr 1961 499.2613 465.7258 448.8949 535.2116 555.2788May 1961 504.3514 467.5503 449.1688 544.0491 566.3135 #ETS函数的自动指数预测library(forecast)fit<-ets(JohnsonJohnson)fit ETS(M,A,M) Call: ets(y = JohnsonJohnson) Smoothing parameters: alpha = 0.1481 beta = 0.0912 gamma = 0.4908 Initial states: l = 0.6146 b = 0.005 s=0.692 1.2644 0.9666 1.077 sigma: 0.0889 AIC AICc BIC 166.6964 169.1289 188.5738 plot(forecast(fit),main="Johnson & Johnson Forecasts",ylab="Quarterly Earnings (Dollars)",xlab="Time",flty=2)
评论
    相关推荐