R Language Learning Note (13): Time series

Source: Internet
Author: User

#生成时间序列对象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 =) tsales

Jan FEB Mar April may June Jul Sep Oct Nov
2003 18 33 41 7 34 35 24 25 24 21 25 20
2004 22 31 40 29 25 21 22 54 31 25 26 35


Plot (Tsales)

Start (Tsales) [1] 2003 1

End (Tsales)
[1] 2004 12

Frequency (Tsales)

[1] 12

Tsales.subset<-window (Tsales,start=c (2003,5), End=c (2004,6)) Tsales.subset

Jan FEB Mar April may June Jul Sep Oct Nov
2003 34 35 24 25 24 21 25 20
2004 22 31 40 29 25 21

#简单移动平均
Install.packages ("forecast")
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)

Fit$time.series
Exp (fit$time.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 Hi Lo 95
1972 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 ACF1
Training 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.


#有水平项, slope, and seasonal exponential model
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 ACF1
Training 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")


Pred$mean<-exp (Pred$mean)
Pred$lower<-exp (Pred$lower)
Pred$upper<-exp (Pred$upper)
P<-cbind (Pred$mean,pred$lower,pred$upper)
Dimnames (P) [[2]]<-c ("mean", "Lo", "Lo", "Hi", "Hi 95")
P

Mean Lo lo-Hi 95
Jan 1961 447.4958 427.3626 417.0741 468.5774 480.1365
Feb 1961 442.7926 419.1001 407.0756 467.8245 481.6434
Mar 1961 509.6958 478.7268 463.1019 542.6682 560.9776
APR 1961 499.2613 465.7258 448.8949 535.2116 555.2788
May 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)

#序列的变换以及稳定性评估
Library (forecast)
Library (tseries)
Plot (Nile)


Ndiffs (Nile)

[1] 1

Dnile<-diff (Nile)
Plot (Dnile)



#拟合ARIMA模型
Library (forecast)
Fit<-arima (Nile,order=c (0,1,1))
Fit

Accuracy (FIT)

ME RMSE MAE MPE MAPE MASE ACF1
Training set-11.9358 142.8071 112.1752-3.574702 12.93594 0.841824 0.1153593

#模型评价
Qqnorm (Fit$residuals)
Qqline (Fit$residuals)
Box.test (fit$residuals,type= "Ljung-box")


Box-ljung Test

Data:fit$residuals
x-squared = 1.3711, df = 1, P-value = 0.2416

#ARIMA Model Prediction
Forecast (fit,3)
Plot (Forecast (fit,3), xlab= "year", ylab= "annual Flow")


#ARIMA自动预测
Library (forecast)
Fit<-auto.arima (Sunspots)
Fit

Series:sunspots
ARIMA (2,1,2)

Coefficients:
AR1 ar2 ma1 Ma2
1.3467-0.3963-1.7710 0.8103
S.E. 0.0303 0.0287 0.0205 0.0194

Sigma^2 Estimated as 243.8:log likelihood=-11745.5
aic=23500.99 aicc=23501.01 bic=23530.71

Forecast (fit,3)

Point Forecast lo Hi Lo 95
Jan 1984 40.43784 20.42717 60.44850 9.834167 71.04150
Feb 1984 41.35311 18.26341 64.44281 6.040458 76.66576
Mar 1984 39.79670 15.23663 64.35677 2.235319 77.35808


Accuracy (FIT)

ME RMSE MAE MPE MAPE MASE ACF1
Training set-0.02672716 15.60055 11.02575 NaN Inf 0.4775401-0.01055012

Summary

This chapter explains how to use the R language for time series analysis, such as modeling, charting, and predicting future trends. This type of data analysis is completely out of the scope of program development, all the analysis is based on mathematical statistics, this should be the current data science direction.

R Language Learning Note (13): Time series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.