Time series: Parameters that can be used to predict the future,
1. Generating Time Series objects
1Sales <-C (18, 33, 41, 7, 34, 35, 24, 25, 24, 21, 25, 20, 222, 31, 40, 29, 25, 21, 22, 54, 31, 25, 26, 35)3 #1. Generating Time Series Objects4Tsales <-ts (Sales,start = C (2003,1), frequency = 12)5 plot (tsales)6 #2. Obtaining Object Information7 Start (tsales)8 End (Tsales)9 frequency (tsales)Ten #3. To the same fetch subset OneTsales.subset <-window (tsales,start=c (2003,5), End=c (2004,6)) ATsales.subset
Conclusion: A manually generated time series diagram
2. Simple Moving Average
Case study: Relationship of Nile flow and year
1 Library (forecast)2Opar <-par (no.readonly =T)3Par (Mfrow=c (2,2))4Ylim <-C (Min (Nile), Max (Nile))5Plot (nile,main='Raw Time Series')6Plot (MA (nile,3), main ='Simple Moving Averages (k=3)', Ylim =Ylim)7Plot (MA (nile,7), main ='Simple Moving Averages (k=3)', Ylim =Ylim)8Plot (MA (nile,15), main ='Simple Moving Averages (k=3)', Ylim =Ylim)9Par (OPAR)
Conclusion: With the increase of k value, the image is more and more smooth we need to find the most reflective law of K value
3. Using STL to do seasonal decomposition
Case: Arirpassengers year and passenger relationship
1 #1. Draw a time series2 plot (airpassengers)3Lairpassengers <-log (airpassengers)4Plot (Lairpassengers,ylab ='log (airpassengers)')5 #2. Decomposition time series6Fit <-STL (Lairpassengers,s.window ='period')7 plot (FIT)8 fit$time.series9Par (Mfrow=c (2,1))Ten #3. Visualization of monthly graphs OneMonthplot (airpassengers,xlab="', ylab="') A #4. Visualization of Quarterly charts -Seasonplot (airpassengers,year.labels = T,main ="')
Original diagram Logarithmic transformations
General Trend Chart Monthly Quarterly Chart
4. Exponential Forecasting Model
4.1 Single Exponential smoothing
Case study: Predicting temperature changes in Connecticut State
#1. Fitting the ModelFit2 <-ets (Nhtemp,model ='ANN') Fit2#2. Forward PredictionForecast (fit2,1) Plot (Forecast (Fit2,1), Xlab =' Year', Ylab= Expression (Paste ("Temperature (", Degree*f,")",)), main="New Haven Annual Mean temperature")#3. Get an accurate measurementAccuracy (FIT2)
Conclusion: Light gray is 80% confidence interval, dark gray is 95% confidence interval
4.2 Exponential model with horizontal, slope and seasonal items
Case: Forecast 5-month passenger traffic
1 #1. Smooth parameters2FIT3 <-ETS (log (airpassengers), model ='AAA')3 accuracy (FIT3)4 #2. Future value Predictions5Pred <-Forecast (fit3,5)6 pred7Plot (pred,main='Forecast for Air travel', Ylab ='Log (airpassengers)', Xlab =' Time')8 #3. Using the original scale forecast9Pred$mean <-exp (Pred$mean)TenPred$lower <-exp (pred$lower) OnePred$upper <-exp (pred$upper) AP <-Cbind (pred$mean,pred$lower,pred$upper) -Dimnames (P) [[2]] <-C ('mean','Lo','Lo','Hi','Hi') -P
Conclusion: The table shows that there will be 509200 passengers in March, and a 95% confidence interval is [454900,570000]
4.3ets Automatic Prediction
Case study: Automatic prediction of Johnsonjohnson stock trends
1 fit4 <- ETS (Johnsonjohnson)2fit43 Plot (Forecast (FIT4), main=' Johnson and Johnson forecasts',4 ylab="Quarterly Earnings (Dollars)", xlab="time")
Conclusion: The predicted value is indicated by Blue Line, light gray is 80% confidence space, dark gray indicates 95% confidence space
5.ARIMA predictions
Steps:
1. Ensure the timing is smooth
2. Find a reasonable model (select a possible P-value or Q-value)
3. Fitting the Model
4. Evaluating models from the perspective of statistical assumptions and forecast accuracy
5. Forecasting
Library (tseries) plot (Nile)#1. Original sequence differential oncendiffs (Nile) dnile<-diff (Nile)#2. Post-Differential graphicsplot (Dnile) adf.test (dnile) Acf (dnile) PACF (dnile)# 3. Fitting the ModelFit5 <-Arima (Nile,order = C (0,1,1)) fit5accuracy (FIT5)#4. Evaluation Modelqqnorm (fit5$residuals) qqline (fit5$residuals) box.test (Fit5$residuals,type='Ljung-box')#5. Predictive ModelsForecast (fit5,3) Plot (Forecast (FIT5,3), Xlab =' Year', Ylab ='Annual Flow')
Original diagram One-time differential graphics
Normal Q-q diagram (if the normal distribution is met, the point falls on the line in the diagram) using Arima (0 , 1, 1) The predicted value of the model
Arima Auto Forecast
Case: Predicting sunspots after 3 months
1 fit6 <- Auto.arima (sunspots)2fit63 Forecast (fit6,3) 4accuracy (FIT6)5"year",6 " Monthly Sunspot Numbers ")
Conclusion: The function is automatically selected (2,1,2) compared with other models, the AIC has the lowest value and the prediction result is more accurate.
R Language-time series