R Language and Data Analysis VI: A brief introduction to time series

Source: Internet
Author: User
Tags natural logarithm

This year in a garment enterprise camp for 4 months, between a long period of time in the exploration of its spot and futures forecasts, time series is also the first choice to make sales forecasts, today and the small partners to share the basic nature of the time series and how to use R to dig the relevant properties of the time series.

First read a time series: from January 1946 to December 1959 the number of births per month in New York (originally collected by Newton) datasets can be downloaded from this link (http://robjhyndman.com/tsdldata/data/nybirths.dat). We read the data into R and store it in a time series object and enter the following code:

The monthly data is set FREQUENCY=12, the quarterly data is set frequency=4; the "start" parameter specifies the first year of data collection and the first interval of the year.

Next we use the plot function to plot the time series diagram:

Plot.ts (birthstimeseries)

You can see the seasonal changes in this time series in a certain month: there is a peak of birth every summer, and in winter it enters the trough. Similarly, such a time series may also be an additive model, and over time, seasonal fluctuations are roughly stable rather than dependent on time series levels, and the random fluctuations appear to be broadly stable over time.

Let's break down the time series and divide the time series into: non-seasonal data and seasonal data

A non-seasonal time series consists of a trend part and an irregular part. The Decomposition time series is an attempt to split the time series into these components, which means that the two parts of the trend and irregularities need to be estimated.

A seasonal time series contains a trend part, a seasonal part, and an irregular section. Decomposition of the time series means that the time series decomposition is called the three parts: that is to estimate the three parts.

The above infant birth is a distinct seasonal time series, and we use the "decompose ()" function provided by R to decompose the time series:

Birthstimeseriescomponents <-Decompose (birthstimeseries)

The estimated seasonal, trending, and irregular portions are now stored in variable birthstimeseriescomponents$seasonal, birthstimeseriescomponents$trend and The Birthstimeseriescomponents$random.

We can draw these three parts separately and observe their characteristics:

Plot (birthstimeseriescomponents)


The graph shows the original time series diagram (top), the estimated trend part of the graph (Part two), the estimated seasonal portion (the third part), and the estimated irregular portion (bottom). We can see that the estimated trend has fallen from 24 in 1947 to 22 in 1948, followed by a steady increase until 1949 of 27.

The above example shows the multi-model addition of time series, which is also a very important property of time series, we need to determine whether the time series can be described by the additive model, and then consider how to decompose the time series after determining the addition and attributes. Here is an example of the monthly sales data from January 1987 to December 1987 for the souvenir shop of the Queensland, State Of seaside resort in Australia. We first draw the sequence and find a sense of the whole:

Souvenir <-Scan ("Http://robjhyndman.com/tsdldata/data/fancy.dat") souvenirtimeseries <-ts (Souvenir, Frequency=12, Start=c (1987,1)) plot.ts (souvenirtimeseries)

The results are as follows:


The sequence does not look suitable for time because the seasonal volatility of the sequence and the size of the random fluctuations gradually rise with the time series. In order for the sequence to conform to the standard time series and to use the additive model description, we convert the raw data to the natural logarithm:

Logsouvenirtimeseries <-log (souvenirtimeseries) plot.ts (logsouvenirtimeseries)

The results are as follows:

We can see that seasonal fluctuations and the size of random changes in the logarithmic transformation of the time series, over time, the size of seasonal fluctuations and random fluctuations is roughly constant, and does not depend on the time series level. So the converted time series can be described by the additive model, and we decompose the changed sequence:

Logsouvenirtimeseriescomponents <-Decompose (logsouvenirtimeseries) plot (logsouvenirtimeseriescomponents)

Observing the graph, we can also see that random and seasonal fluctuations are stable over time, proving that our transformations are useful again.



Today and the small partners to share here, the following will introduce several commonly used time series prediction algorithm, please look forward to

R Language and Data Analysis VI: A brief introduction to 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.