R Language Time series application (decompose, holt-winters Preliminary)

Source: Internet
Author: User

for the obvious periodic time series, can use decompose function to decompose the data into the seasonal part, the trend part, the random part three KINDS. the decompose function has two types, "additive" and "multiplicative", and a fliter option. Indicates whether to add linear filter, general Fliter Select Null. The following example shows an example of using decompose to analyze time series data containing seasonal factors

The monthly milk production data of each cow in 1962-1970 was imported into Outcome. For time series data, It is often also used to convert the form of a TS function into a dedicated data form for a time series:

Outcome<-ts (b,frequency = 12,start = C (1962,1))

Result1<-decompose (outcome,type = "additive")

Plot (Result1)

Result2<--decompose (outcome,type = "multiplicative")

Plot (Result2)

Select the multiplicative model to proceed with the Analysis. Use the following command to extract trend items from the model and fit them using a linear model.

Results<-result1

Plot.ts (results$trend)

Abline (lm (results$trend~time (outcome)), col= "red")

REG<-LM (results$trend~time (outcome))

Summary (reg)

Par (mfrow=c (2,2))

Plot (reg,which=c (1:4))

Summary of Model regression, and the following chart

Ver

Coefficients:

Estimate Std. Error t value Pr (>|t|)

(Intercept) -4.269e+04 4.678e+02-91.25 <2e-16 * * *

Time (outcome) 2.207e+01 2.379e-01 92.75 <2e-16 * * *

---

Signif. codes:0 ' * * * ' 0.001 ' * * ' 0.01 ' * ' 0.05 '. ' 0.1 "1

Residual standard error:5.383 on 94 degrees of freedom

(observations deleted due to Missingness)

Multiple r-squared:0.9892, adjusted r-squared:0.9891

f-statistic:8603 on 1 and 94 DF, P-value: < 2.2e-16

obviously, the regression model fitting effect is general, and the residual error has obvious self-correlation. Using this method to predict the model needs to extract the seasonal factor of the sequence and multiply it with the predicted data of the linear fitting trend Item. First we extract the seasonal index, get the seasonal index sea, pay attention to the first results$seasonal to vector type, so that the subsequent Calculation.

Note If the previous decomposition takes an additive model, the original data should be used minus the seasonal Factor.

Sea<-as.vector (results$seasonal)

sea<-sea[1:12]

Par (mfrow=c ())

Plot (sea,type = "l")

pre<-1971+ (0:11)/12

Pre<--42690.59+22.07*pre

Pred<-pre*sea

finally, the results of pred are Predicted.

> Sea [1] 0.9752900 0.9239188 1.0481092 1.0718562 1.1567964 1.1178806

[7] 1.0416054 0.9783800 0.9233615 0.9286178 0.8919580 0.9422262

> pred [1] 789.3802 749.5006 852.1739 873.4530 944.7980 915.0700

[7] 854.5487 804.4770 760.9361 766.9756 738.3376 781.6811

Another way of predicting time series with seasonal factors is to use the X11 method, which differs from the previous decompose in the extraction of seasonal factors, and the X11 method extracts the seasonal factor by the average of the variable in a particular season/all seasons of the Variable. Use X11 method to get the seasonal index sea2

Xbar<-rep (0,12)

For (i in 1:108) {

xbar[i%%12+1]<-xbar[i%%12+1]+b[i]

}

Xbar<-xbar/9

Ex<-sum (B)/108

Sea2<-xbar/ex

> sea2 [1] 0.9551790 0.9607222 0.9125752 1.0381691 1.0643016 1.1536269

[7] 1.1165664 1.0429205 0.9841622 0.9309471 0.9385493 0.9022806

from the Model Remove seasonal factors and use linear fit for trend items

Trend<-c (0)

For (i in 1:108) {

trend[i]<-b[i]/sea2[i%%12+1]

}

Trend<-ts (trend,frequency = 12,start = C (1962,1))

Ts.plot (trend)

REG2<-LM (trend~time (TREND))
Abline (lm (trend~time (trend)), col= "red")

It is obvious that the trend item extracted by the X11 method has no smoothing of decompose function extraction, but its trend fitting effect is better, and the residual person basically conforms to normal Distribution.

Call:

LM (formula = Trend ~ Time (TREND))

Coefficients:

(Intercept) Time (trend)

-41832.05 21.63

The effective method of time series prediction is the Holt-winters method smoothing prediction, which has three parameters to Control: Alpha,beta and gamma, respectively, corresponding to the horizontal at the current time point, the slope of the trend part and the seasonal part. The values of the parameters Alpha,beta and gamma are between 0 and 1, and the closer they are to 0 means that the nearest observations take on a relatively small weight for future predictions. If we use decompose to extract seasonal factors in advance

The number of slaughtered pigs per month from January 1980 to August 1995 in a city is imported into the data set and transformed with TS function

Datats<-ts (data,frequency = 12,start =c (1980,1))

Ts.plot (datats)

Datatshw<-holtwinters (datats)

#需要使用forecast包中Forecast. Holtwinters and plot.forecast functions

Datatshwforecast<-forecast. Holtwinters (datatshw,h=48)
Plot.forecast (datatshwforecast)

The values of the smoothed parameters stored in the datatshw are as Follows:

Smoothing Parameters:

alpha:0.3074266

beta:0.00366508

gamma:0.4374631

Reference: http://blog.csdn.net/jiabiao1602/article/details/43153139

Http://www.dataguru.cn/article-3235-1.html

Http://bbs.pinggu.org/thread-4116352-1-1.html

R Language Time series application (decompose, holt-winters Preliminary)

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.