{"id":663,"date":"2014-07-15T09:53:03","date_gmt":"2014-07-15T16:53:03","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=663"},"modified":"2014-07-15T09:53:03","modified_gmt":"2014-07-15T16:53:03","slug":"using-r-for-time-series-analysis","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/07\/15\/using-r-for-time-series-analysis\/","title":{"rendered":"Using R for Time Series Analysis"},"content":{"rendered":"<div id=\"time-series-analysis\" class=\"section\" style=\"color: #000000;\">\n<h2>Time Series Analysis<\/h2>\n<p>This booklet itells you how to use the R statistical software to carry out some simple analyses that are common in analysing time series data.<\/p>\n<p>This booklet assumes that the reader has some basic knowledge of time series analysis, and the principal focus of the booklet is not to explain time series analysis, but rather to explain how to carry out these analyses using R.<\/p>\n<p>If you are new to time series analysis, and want to learn more about any of the concepts presented here, I would highly recommend the Open University book \u201cTime series\u201d (product code M249\/02), available from from<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/www.ouw.co.uk\/store\/\">the Open University Shop<\/a>.<\/p>\n<p>In this booklet, I will be using time series data sets that have been kindly made available by Rob Hyndman in his Time Series Data Library at\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/robjhyndman.com\/TSDL\/\">http:\/\/robjhyndman.com\/TSDL\/<\/a>.<\/p>\n<p>There is a pdf version of this booklet available at\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"https:\/\/media.readthedocs.org\/pdf\/a-little-book-of-r-for-time-series\/latest\/a-little-book-of-r-for-time-series.pdf\">https:\/\/media.readthedocs.org\/pdf\/a-little-book-of-r-for-time-series\/latest\/a-little-book-of-r-for-time-series.pdf<\/a>.<\/p>\n<p>If you like this booklet, you may also like to check out my booklet on using R for biomedical statistics,\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/a-little-book-of-r-for-biomedical-statistics.readthedocs.org\/\">http:\/\/a-little-book-of-r-for-biomedical-statistics.readthedocs.org\/<\/a>, and my booklet on using R for multivariate analysis,\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/little-book-of-r-for-multivariate-analysis.readthedocs.org\/\">http:\/\/little-book-of-r-for-multivariate-analysis.readthedocs.org\/<\/a>.<\/p>\n<\/div>\n<div id=\"reading-time-series-data\" class=\"section\" style=\"color: #000000;\">\n<h2>Reading Time Series Data<\/h2>\n<p>The first thing that you will want to do to analyse your time series data will be to read it into R, and to plot the time series. You can read data into R using the scan() function, which assumes that your data for successive time points is in a simple text file with one column.<\/p>\n<p>For example, the file\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/robjhyndman.com\/tsdldata\/misc\/kings.dat\">http:\/\/robjhyndman.com\/tsdldata\/misc\/kings.dat<\/a>\u00a0contains data on the age of death of successive kings of England, starting with William the Conqueror (original source: Hipel and Mcleod, 1994).<\/p>\n<p>The data set looks like this:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre>Age of Death of Successive Kings of England\n<span class=\"c1\" style=\"font-style: italic; color: #408090;\">#starting with William the Conqueror<\/span>\n<span class=\"c1\" style=\"font-style: italic; color: #408090;\">#Source: McNeill, \"Interactive Data Analysis\"<\/span>\n<span class=\"m\" style=\"color: #208050;\">60<\/span>\n<span class=\"m\" style=\"color: #208050;\">43<\/span>\n<span class=\"m\" style=\"color: #208050;\">67<\/span>\n<span class=\"m\" style=\"color: #208050;\">50<\/span>\n<span class=\"m\" style=\"color: #208050;\">56<\/span>\n<span class=\"m\" style=\"color: #208050;\">42<\/span>\n<span class=\"m\" style=\"color: #208050;\">50<\/span>\n<span class=\"m\" style=\"color: #208050;\">65<\/span>\n<span class=\"m\" style=\"color: #208050;\">68<\/span>\n<span class=\"m\" style=\"color: #208050;\">43<\/span>\n<span class=\"m\" style=\"color: #208050;\">65<\/span>\n<span class=\"m\" style=\"color: #208050;\">34<\/span>\n<span class=\"kc\" style=\"font-weight: bold; color: #007020;\">...<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>Only the first few lines of the file have been shown. The first three lines contain some comment on the data, and we want to ignore this when we read the data into R. We can use this by using the \u201cskip\u201d parameter of the scan() function, which specifies how many lines at the top of the file to ignore. To read the file into R, ignoring the first three lines, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kings <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> scan<span class=\"p\">(<\/span><span class=\"s\" style=\"color: #4070a0;\">\"http:\/\/robjhyndman.com\/tsdldata\/misc\/kings.dat\"<\/span><span class=\"p\">,<\/span>skip<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">3<\/span><span class=\"p\">)<\/span>\n  Read <span class=\"m\" style=\"color: #208050;\">42<\/span> items\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kings\n  <span class=\"p\">[<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">]<\/span> <span class=\"m\" style=\"color: #208050;\">60<\/span> <span class=\"m\" style=\"color: #208050;\">43<\/span> <span class=\"m\" style=\"color: #208050;\">67<\/span> <span class=\"m\" style=\"color: #208050;\">50<\/span> <span class=\"m\" style=\"color: #208050;\">56<\/span> <span class=\"m\" style=\"color: #208050;\">42<\/span> <span class=\"m\" style=\"color: #208050;\">50<\/span> <span class=\"m\" style=\"color: #208050;\">65<\/span> <span class=\"m\" style=\"color: #208050;\">68<\/span> <span class=\"m\" style=\"color: #208050;\">43<\/span> <span class=\"m\" style=\"color: #208050;\">65<\/span> <span class=\"m\" style=\"color: #208050;\">34<\/span> <span class=\"m\" style=\"color: #208050;\">47<\/span> <span class=\"m\" style=\"color: #208050;\">34<\/span> <span class=\"m\" style=\"color: #208050;\">49<\/span> <span class=\"m\" style=\"color: #208050;\">41<\/span> <span class=\"m\" style=\"color: #208050;\">13<\/span> <span class=\"m\" style=\"color: #208050;\">35<\/span> <span class=\"m\" style=\"color: #208050;\">53<\/span> <span class=\"m\" style=\"color: #208050;\">56<\/span> <span class=\"m\" style=\"color: #208050;\">16<\/span> <span class=\"m\" style=\"color: #208050;\">43<\/span> <span class=\"m\" style=\"color: #208050;\">69<\/span> <span class=\"m\" style=\"color: #208050;\">59<\/span> <span class=\"m\" style=\"color: #208050;\">48<\/span>\n  <span class=\"p\">[<\/span><span class=\"m\" style=\"color: #208050;\">26<\/span><span class=\"p\">]<\/span> <span class=\"m\" style=\"color: #208050;\">59<\/span> <span class=\"m\" style=\"color: #208050;\">86<\/span> <span class=\"m\" style=\"color: #208050;\">55<\/span> <span class=\"m\" style=\"color: #208050;\">68<\/span> <span class=\"m\" style=\"color: #208050;\">51<\/span> <span class=\"m\" style=\"color: #208050;\">33<\/span> <span class=\"m\" style=\"color: #208050;\">49<\/span> <span class=\"m\" style=\"color: #208050;\">67<\/span> <span class=\"m\" style=\"color: #208050;\">77<\/span> <span class=\"m\" style=\"color: #208050;\">81<\/span> <span class=\"m\" style=\"color: #208050;\">67<\/span> <span class=\"m\" style=\"color: #208050;\">71<\/span> <span class=\"m\" style=\"color: #208050;\">81<\/span> <span class=\"m\" style=\"color: #208050;\">68<\/span> <span class=\"m\" style=\"color: #208050;\">70<\/span> <span class=\"m\" style=\"color: #208050;\">77<\/span> <span class=\"m\" style=\"color: #208050;\">56<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>In this case the age of death of 42 successive kings of England has been read into the variable \u2018kings\u2019.<\/p>\n<p>Once you have read the time series data into R, the next step is to store the data in a time series object in R, so that you can use R\u2019s many functions for analysing time series data. To store the data in a time series object, we use the ts() function in R. For example, to store the data in the variable \u2018kings\u2019 as a time series object in R, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kingstimeseries <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> ts<span class=\"p\">(<\/span>kings<span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kingstimeseries\n  Time Series<span class=\"o\" style=\"color: #666666;\">:<\/span>\n  Start <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">1<\/span>\n  End <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">42<\/span>\n  Frequency <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">1<\/span>\n  <span class=\"p\">[<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">]<\/span> <span class=\"m\" style=\"color: #208050;\">60<\/span> <span class=\"m\" style=\"color: #208050;\">43<\/span> <span class=\"m\" style=\"color: #208050;\">67<\/span> <span class=\"m\" style=\"color: #208050;\">50<\/span> <span class=\"m\" style=\"color: #208050;\">56<\/span> <span class=\"m\" style=\"color: #208050;\">42<\/span> <span class=\"m\" style=\"color: #208050;\">50<\/span> <span class=\"m\" style=\"color: #208050;\">65<\/span> <span class=\"m\" style=\"color: #208050;\">68<\/span> <span class=\"m\" style=\"color: #208050;\">43<\/span> <span class=\"m\" style=\"color: #208050;\">65<\/span> <span class=\"m\" style=\"color: #208050;\">34<\/span> <span class=\"m\" style=\"color: #208050;\">47<\/span> <span class=\"m\" style=\"color: #208050;\">34<\/span> <span class=\"m\" style=\"color: #208050;\">49<\/span> <span class=\"m\" style=\"color: #208050;\">41<\/span> <span class=\"m\" style=\"color: #208050;\">13<\/span> <span class=\"m\" style=\"color: #208050;\">35<\/span> <span class=\"m\" style=\"color: #208050;\">53<\/span> <span class=\"m\" style=\"color: #208050;\">56<\/span> <span class=\"m\" style=\"color: #208050;\">16<\/span> <span class=\"m\" style=\"color: #208050;\">43<\/span> <span class=\"m\" style=\"color: #208050;\">69<\/span> <span class=\"m\" style=\"color: #208050;\">59<\/span> <span class=\"m\" style=\"color: #208050;\">48<\/span>\n  <span class=\"p\">[<\/span><span class=\"m\" style=\"color: #208050;\">26<\/span><span class=\"p\">]<\/span> <span class=\"m\" style=\"color: #208050;\">59<\/span> <span class=\"m\" style=\"color: #208050;\">86<\/span> <span class=\"m\" style=\"color: #208050;\">55<\/span> <span class=\"m\" style=\"color: #208050;\">68<\/span> <span class=\"m\" style=\"color: #208050;\">51<\/span> <span class=\"m\" style=\"color: #208050;\">33<\/span> <span class=\"m\" style=\"color: #208050;\">49<\/span> <span class=\"m\" style=\"color: #208050;\">67<\/span> <span class=\"m\" style=\"color: #208050;\">77<\/span> <span class=\"m\" style=\"color: #208050;\">81<\/span> <span class=\"m\" style=\"color: #208050;\">67<\/span> <span class=\"m\" style=\"color: #208050;\">71<\/span> <span class=\"m\" style=\"color: #208050;\">81<\/span> <span class=\"m\" style=\"color: #208050;\">68<\/span> <span class=\"m\" style=\"color: #208050;\">70<\/span> <span class=\"m\" style=\"color: #208050;\">77<\/span> <span class=\"m\" style=\"color: #208050;\">56<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>Sometimes the time series data set that you have may have been collected at regular intervals that were less than one year, for example, monthly or quarterly. In this case, you can specify the number of times that data was collected per year by using the \u2018frequency\u2019 parameter in the ts() function. For monthly time series data, you set frequency=12, while for quarterly time series data, you set frequency=4.<\/p>\n<p>You can also specify the first year that the data was collected, and the first interval in that year by using the \u2018start\u2019 parameter in the ts() function. For example, if the first data point corresponds to the second quarter of 1986, you would set start=c(1986,2).<\/p>\n<p>An example is a data set of the number of births per month in New York city, from January 1946 to December 1959 (originally collected by Newton). This data is available in the file<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/robjhyndman.com\/tsdldata\/data\/nybirths.dat\">http:\/\/robjhyndman.com\/tsdldata\/data\/nybirths.dat<\/a>\u00a0We can read the data into R, and store it as a time series object, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> births <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> scan<span class=\"p\">(<\/span><span class=\"s\" style=\"color: #4070a0;\">\"http:\/\/robjhyndman.com\/tsdldata\/data\/nybirths.dat\"<\/span><span class=\"p\">)<\/span>\n  Read <span class=\"m\" style=\"color: #208050;\">168<\/span> items\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> birthstimeseries <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> ts<span class=\"p\">(<\/span>births<span class=\"p\">,<\/span> frequency<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">12<\/span><span class=\"p\">,<\/span> start<span class=\"o\" style=\"color: #666666;\">=<\/span>c<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">1946<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">))<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> birthstimeseries\n    Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct    Nov    Dec\n  <span class=\"m\" style=\"color: #208050;\">1946<\/span> <span class=\"m\" style=\"color: #208050;\">26.663<\/span> <span class=\"m\" style=\"color: #208050;\">23.598<\/span> <span class=\"m\" style=\"color: #208050;\">26.931<\/span> <span class=\"m\" style=\"color: #208050;\">24.740<\/span> <span class=\"m\" style=\"color: #208050;\">25.806<\/span> <span class=\"m\" style=\"color: #208050;\">24.364<\/span> <span class=\"m\" style=\"color: #208050;\">24.477<\/span> <span class=\"m\" style=\"color: #208050;\">23.901<\/span> <span class=\"m\" style=\"color: #208050;\">23.175<\/span> <span class=\"m\" style=\"color: #208050;\">23.227<\/span> <span class=\"m\" style=\"color: #208050;\">21.672<\/span> <span class=\"m\" style=\"color: #208050;\">21.870<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1947<\/span> <span class=\"m\" style=\"color: #208050;\">21.439<\/span> <span class=\"m\" style=\"color: #208050;\">21.089<\/span> <span class=\"m\" style=\"color: #208050;\">23.709<\/span> <span class=\"m\" style=\"color: #208050;\">21.669<\/span> <span class=\"m\" style=\"color: #208050;\">21.752<\/span> <span class=\"m\" style=\"color: #208050;\">20.761<\/span> <span class=\"m\" style=\"color: #208050;\">23.479<\/span> <span class=\"m\" style=\"color: #208050;\">23.824<\/span> <span class=\"m\" style=\"color: #208050;\">23.105<\/span> <span class=\"m\" style=\"color: #208050;\">23.110<\/span> <span class=\"m\" style=\"color: #208050;\">21.759<\/span> <span class=\"m\" style=\"color: #208050;\">22.073<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1948<\/span> <span class=\"m\" style=\"color: #208050;\">21.937<\/span> <span class=\"m\" style=\"color: #208050;\">20.035<\/span> <span class=\"m\" style=\"color: #208050;\">23.590<\/span> <span class=\"m\" style=\"color: #208050;\">21.672<\/span> <span class=\"m\" style=\"color: #208050;\">22.222<\/span> <span class=\"m\" style=\"color: #208050;\">22.123<\/span> <span class=\"m\" style=\"color: #208050;\">23.950<\/span> <span class=\"m\" style=\"color: #208050;\">23.504<\/span> <span class=\"m\" style=\"color: #208050;\">22.238<\/span> <span class=\"m\" style=\"color: #208050;\">23.142<\/span> <span class=\"m\" style=\"color: #208050;\">21.059<\/span> <span class=\"m\" style=\"color: #208050;\">21.573<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1949<\/span> <span class=\"m\" style=\"color: #208050;\">21.548<\/span> <span class=\"m\" style=\"color: #208050;\">20.000<\/span> <span class=\"m\" style=\"color: #208050;\">22.424<\/span> <span class=\"m\" style=\"color: #208050;\">20.615<\/span> <span class=\"m\" style=\"color: #208050;\">21.761<\/span> <span class=\"m\" style=\"color: #208050;\">22.874<\/span> <span class=\"m\" style=\"color: #208050;\">24.104<\/span> <span class=\"m\" style=\"color: #208050;\">23.748<\/span> <span class=\"m\" style=\"color: #208050;\">23.262<\/span> <span class=\"m\" style=\"color: #208050;\">22.907<\/span> <span class=\"m\" style=\"color: #208050;\">21.519<\/span> <span class=\"m\" style=\"color: #208050;\">22.025<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1950<\/span> <span class=\"m\" style=\"color: #208050;\">22.604<\/span> <span class=\"m\" style=\"color: #208050;\">20.894<\/span> <span class=\"m\" style=\"color: #208050;\">24.677<\/span> <span class=\"m\" style=\"color: #208050;\">23.673<\/span> <span class=\"m\" style=\"color: #208050;\">25.320<\/span> <span class=\"m\" style=\"color: #208050;\">23.583<\/span> <span class=\"m\" style=\"color: #208050;\">24.671<\/span> <span class=\"m\" style=\"color: #208050;\">24.454<\/span> <span class=\"m\" style=\"color: #208050;\">24.122<\/span> <span class=\"m\" style=\"color: #208050;\">24.252<\/span> <span class=\"m\" style=\"color: #208050;\">22.084<\/span> <span class=\"m\" style=\"color: #208050;\">22.991<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1951<\/span> <span class=\"m\" style=\"color: #208050;\">23.287<\/span> <span class=\"m\" style=\"color: #208050;\">23.049<\/span> <span class=\"m\" style=\"color: #208050;\">25.076<\/span> <span class=\"m\" style=\"color: #208050;\">24.037<\/span> <span class=\"m\" style=\"color: #208050;\">24.430<\/span> <span class=\"m\" style=\"color: #208050;\">24.667<\/span> <span class=\"m\" style=\"color: #208050;\">26.451<\/span> <span class=\"m\" style=\"color: #208050;\">25.618<\/span> <span class=\"m\" style=\"color: #208050;\">25.014<\/span> <span class=\"m\" style=\"color: #208050;\">25.110<\/span> <span class=\"m\" style=\"color: #208050;\">22.964<\/span> <span class=\"m\" style=\"color: #208050;\">23.981<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1952<\/span> <span class=\"m\" style=\"color: #208050;\">23.798<\/span> <span class=\"m\" style=\"color: #208050;\">22.270<\/span> <span class=\"m\" style=\"color: #208050;\">24.775<\/span> <span class=\"m\" style=\"color: #208050;\">22.646<\/span> <span class=\"m\" style=\"color: #208050;\">23.988<\/span> <span class=\"m\" style=\"color: #208050;\">24.737<\/span> <span class=\"m\" style=\"color: #208050;\">26.276<\/span> <span class=\"m\" style=\"color: #208050;\">25.816<\/span> <span class=\"m\" style=\"color: #208050;\">25.210<\/span> <span class=\"m\" style=\"color: #208050;\">25.199<\/span> <span class=\"m\" style=\"color: #208050;\">23.162<\/span> <span class=\"m\" style=\"color: #208050;\">24.707<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1953<\/span> <span class=\"m\" style=\"color: #208050;\">24.364<\/span> <span class=\"m\" style=\"color: #208050;\">22.644<\/span> <span class=\"m\" style=\"color: #208050;\">25.565<\/span> <span class=\"m\" style=\"color: #208050;\">24.062<\/span> <span class=\"m\" style=\"color: #208050;\">25.431<\/span> <span class=\"m\" style=\"color: #208050;\">24.635<\/span> <span class=\"m\" style=\"color: #208050;\">27.009<\/span> <span class=\"m\" style=\"color: #208050;\">26.606<\/span> <span class=\"m\" style=\"color: #208050;\">26.268<\/span> <span class=\"m\" style=\"color: #208050;\">26.462<\/span> <span class=\"m\" style=\"color: #208050;\">25.246<\/span> <span class=\"m\" style=\"color: #208050;\">25.180<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1954<\/span> <span class=\"m\" style=\"color: #208050;\">24.657<\/span> <span class=\"m\" style=\"color: #208050;\">23.304<\/span> <span class=\"m\" style=\"color: #208050;\">26.982<\/span> <span class=\"m\" style=\"color: #208050;\">26.199<\/span> <span class=\"m\" style=\"color: #208050;\">27.210<\/span> <span class=\"m\" style=\"color: #208050;\">26.122<\/span> <span class=\"m\" style=\"color: #208050;\">26.706<\/span> <span class=\"m\" style=\"color: #208050;\">26.878<\/span> <span class=\"m\" style=\"color: #208050;\">26.152<\/span> <span class=\"m\" style=\"color: #208050;\">26.379<\/span> <span class=\"m\" style=\"color: #208050;\">24.712<\/span> <span class=\"m\" style=\"color: #208050;\">25.688<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1955<\/span> <span class=\"m\" style=\"color: #208050;\">24.990<\/span> <span class=\"m\" style=\"color: #208050;\">24.239<\/span> <span class=\"m\" style=\"color: #208050;\">26.721<\/span> <span class=\"m\" style=\"color: #208050;\">23.475<\/span> <span class=\"m\" style=\"color: #208050;\">24.767<\/span> <span class=\"m\" style=\"color: #208050;\">26.219<\/span> <span class=\"m\" style=\"color: #208050;\">28.361<\/span> <span class=\"m\" style=\"color: #208050;\">28.599<\/span> <span class=\"m\" style=\"color: #208050;\">27.914<\/span> <span class=\"m\" style=\"color: #208050;\">27.784<\/span> <span class=\"m\" style=\"color: #208050;\">25.693<\/span> <span class=\"m\" style=\"color: #208050;\">26.881<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1956<\/span> <span class=\"m\" style=\"color: #208050;\">26.217<\/span> <span class=\"m\" style=\"color: #208050;\">24.218<\/span> <span class=\"m\" style=\"color: #208050;\">27.914<\/span> <span class=\"m\" style=\"color: #208050;\">26.975<\/span> <span class=\"m\" style=\"color: #208050;\">28.527<\/span> <span class=\"m\" style=\"color: #208050;\">27.139<\/span> <span class=\"m\" style=\"color: #208050;\">28.982<\/span> <span class=\"m\" style=\"color: #208050;\">28.169<\/span> <span class=\"m\" style=\"color: #208050;\">28.056<\/span> <span class=\"m\" style=\"color: #208050;\">29.136<\/span> <span class=\"m\" style=\"color: #208050;\">26.291<\/span> <span class=\"m\" style=\"color: #208050;\">26.987<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1957<\/span> <span class=\"m\" style=\"color: #208050;\">26.589<\/span> <span class=\"m\" style=\"color: #208050;\">24.848<\/span> <span class=\"m\" style=\"color: #208050;\">27.543<\/span> <span class=\"m\" style=\"color: #208050;\">26.896<\/span> <span class=\"m\" style=\"color: #208050;\">28.878<\/span> <span class=\"m\" style=\"color: #208050;\">27.390<\/span> <span class=\"m\" style=\"color: #208050;\">28.065<\/span> <span class=\"m\" style=\"color: #208050;\">28.141<\/span> <span class=\"m\" style=\"color: #208050;\">29.048<\/span> <span class=\"m\" style=\"color: #208050;\">28.484<\/span> <span class=\"m\" style=\"color: #208050;\">26.634<\/span> <span class=\"m\" style=\"color: #208050;\">27.735<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1958<\/span> <span class=\"m\" style=\"color: #208050;\">27.132<\/span> <span class=\"m\" style=\"color: #208050;\">24.924<\/span> <span class=\"m\" style=\"color: #208050;\">28.963<\/span> <span class=\"m\" style=\"color: #208050;\">26.589<\/span> <span class=\"m\" style=\"color: #208050;\">27.931<\/span> <span class=\"m\" style=\"color: #208050;\">28.009<\/span> <span class=\"m\" style=\"color: #208050;\">29.229<\/span> <span class=\"m\" style=\"color: #208050;\">28.759<\/span> <span class=\"m\" style=\"color: #208050;\">28.405<\/span> <span class=\"m\" style=\"color: #208050;\">27.945<\/span> <span class=\"m\" style=\"color: #208050;\">25.912<\/span> <span class=\"m\" style=\"color: #208050;\">26.619<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1959<\/span> <span class=\"m\" style=\"color: #208050;\">26.076<\/span> <span class=\"m\" style=\"color: #208050;\">25.286<\/span> <span class=\"m\" style=\"color: #208050;\">27.660<\/span> <span class=\"m\" style=\"color: #208050;\">25.951<\/span> <span class=\"m\" style=\"color: #208050;\">26.398<\/span> <span class=\"m\" style=\"color: #208050;\">25.565<\/span> <span class=\"m\" style=\"color: #208050;\">28.865<\/span> <span class=\"m\" style=\"color: #208050;\">30.000<\/span> <span class=\"m\" style=\"color: #208050;\">29.261<\/span> <span class=\"m\" style=\"color: #208050;\">29.012<\/span> <span class=\"m\" style=\"color: #208050;\">26.992<\/span> <span class=\"m\" style=\"color: #208050;\">27.897<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>Similarly, the file\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/robjhyndman.com\/tsdldata\/data\/fancy.dat\">http:\/\/robjhyndman.com\/tsdldata\/data\/fancy.dat<\/a>\u00a0contains monthly sales for a souvenir shop at a beach resort town in Queensland, Australia, for January 1987-December 1993 (original data from Wheelwright and Hyndman, 1998). We can read the data into R by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> souvenir <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> scan<span class=\"p\">(<\/span><span class=\"s\" style=\"color: #4070a0;\">\"http:\/\/robjhyndman.com\/tsdldata\/data\/fancy.dat\"<\/span><span class=\"p\">)<\/span>\n  Read <span class=\"m\" style=\"color: #208050;\">84<\/span> items\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> souvenirtimeseries <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> ts<span class=\"p\">(<\/span>souvenir<span class=\"p\">,<\/span> frequency<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">12<\/span><span class=\"p\">,<\/span> start<span class=\"o\" style=\"color: #666666;\">=<\/span>c<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">1987<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">))<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> souvenirtimeseries\n  Jan       Feb       Mar       Apr       May       Jun       Jul       Aug       Sep       Oct       Nov       Dec\n  <span class=\"m\" style=\"color: #208050;\">1987<\/span>   <span class=\"m\" style=\"color: #208050;\">1664.81<\/span>   <span class=\"m\" style=\"color: #208050;\">2397.53<\/span>   <span class=\"m\" style=\"color: #208050;\">2840.71<\/span>   <span class=\"m\" style=\"color: #208050;\">3547.29<\/span>   <span class=\"m\" style=\"color: #208050;\">3752.96<\/span>   <span class=\"m\" style=\"color: #208050;\">3714.74<\/span>   <span class=\"m\" style=\"color: #208050;\">4349.61<\/span>   <span class=\"m\" style=\"color: #208050;\">3566.34<\/span>   <span class=\"m\" style=\"color: #208050;\">5021.82<\/span>   <span class=\"m\" style=\"color: #208050;\">6423.48<\/span>   <span class=\"m\" style=\"color: #208050;\">7600.60<\/span>  <span class=\"m\" style=\"color: #208050;\">19756.21<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1988<\/span>   <span class=\"m\" style=\"color: #208050;\">2499.81<\/span>   <span class=\"m\" style=\"color: #208050;\">5198.24<\/span>   <span class=\"m\" style=\"color: #208050;\">7225.14<\/span>   <span class=\"m\" style=\"color: #208050;\">4806.03<\/span>   <span class=\"m\" style=\"color: #208050;\">5900.88<\/span>   <span class=\"m\" style=\"color: #208050;\">4951.34<\/span>   <span class=\"m\" style=\"color: #208050;\">6179.12<\/span>   <span class=\"m\" style=\"color: #208050;\">4752.15<\/span>   <span class=\"m\" style=\"color: #208050;\">5496.43<\/span>   <span class=\"m\" style=\"color: #208050;\">5835.10<\/span>  <span class=\"m\" style=\"color: #208050;\">12600.08<\/span>  <span class=\"m\" style=\"color: #208050;\">28541.72<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1989<\/span>   <span class=\"m\" style=\"color: #208050;\">4717.02<\/span>   <span class=\"m\" style=\"color: #208050;\">5702.63<\/span>   <span class=\"m\" style=\"color: #208050;\">9957.58<\/span>   <span class=\"m\" style=\"color: #208050;\">5304.78<\/span>   <span class=\"m\" style=\"color: #208050;\">6492.43<\/span>   <span class=\"m\" style=\"color: #208050;\">6630.80<\/span>   <span class=\"m\" style=\"color: #208050;\">7349.62<\/span>   <span class=\"m\" style=\"color: #208050;\">8176.62<\/span>   <span class=\"m\" style=\"color: #208050;\">8573.17<\/span>   <span class=\"m\" style=\"color: #208050;\">9690.50<\/span>  <span class=\"m\" style=\"color: #208050;\">15151.84<\/span>  <span class=\"m\" style=\"color: #208050;\">34061.01<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1990<\/span>   <span class=\"m\" style=\"color: #208050;\">5921.10<\/span>   <span class=\"m\" style=\"color: #208050;\">5814.58<\/span>  <span class=\"m\" style=\"color: #208050;\">12421.25<\/span>   <span class=\"m\" style=\"color: #208050;\">6369.77<\/span>   <span class=\"m\" style=\"color: #208050;\">7609.12<\/span>   <span class=\"m\" style=\"color: #208050;\">7224.75<\/span>   <span class=\"m\" style=\"color: #208050;\">8121.22<\/span>   <span class=\"m\" style=\"color: #208050;\">7979.25<\/span>   <span class=\"m\" style=\"color: #208050;\">8093.06<\/span>   <span class=\"m\" style=\"color: #208050;\">8476.70<\/span>  <span class=\"m\" style=\"color: #208050;\">17914.66<\/span>  <span class=\"m\" style=\"color: #208050;\">30114.41<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1991<\/span>   <span class=\"m\" style=\"color: #208050;\">4826.64<\/span>   <span class=\"m\" style=\"color: #208050;\">6470.23<\/span>   <span class=\"m\" style=\"color: #208050;\">9638.77<\/span>   <span class=\"m\" style=\"color: #208050;\">8821.17<\/span>   <span class=\"m\" style=\"color: #208050;\">8722.37<\/span>  <span class=\"m\" style=\"color: #208050;\">10209.48<\/span>  <span class=\"m\" style=\"color: #208050;\">11276.55<\/span>  <span class=\"m\" style=\"color: #208050;\">12552.22<\/span>  <span class=\"m\" style=\"color: #208050;\">11637.39<\/span>  <span class=\"m\" style=\"color: #208050;\">13606.89<\/span>  <span class=\"m\" style=\"color: #208050;\">21822.11<\/span>  <span class=\"m\" style=\"color: #208050;\">45060.69<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1992<\/span>   <span class=\"m\" style=\"color: #208050;\">7615.03<\/span>   <span class=\"m\" style=\"color: #208050;\">9849.69<\/span>  <span class=\"m\" style=\"color: #208050;\">14558.40<\/span>  <span class=\"m\" style=\"color: #208050;\">11587.33<\/span>   <span class=\"m\" style=\"color: #208050;\">9332.56<\/span>  <span class=\"m\" style=\"color: #208050;\">13082.09<\/span>  <span class=\"m\" style=\"color: #208050;\">16732.78<\/span>  <span class=\"m\" style=\"color: #208050;\">19888.61<\/span>  <span class=\"m\" style=\"color: #208050;\">23933.38<\/span>  <span class=\"m\" style=\"color: #208050;\">25391.35<\/span>  <span class=\"m\" style=\"color: #208050;\">36024.80<\/span>  <span class=\"m\" style=\"color: #208050;\">80721.71<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1993<\/span>  <span class=\"m\" style=\"color: #208050;\">10243.24<\/span>  <span class=\"m\" style=\"color: #208050;\">11266.88<\/span>  <span class=\"m\" style=\"color: #208050;\">21826.84<\/span>  <span class=\"m\" style=\"color: #208050;\">17357.33<\/span>  <span class=\"m\" style=\"color: #208050;\">15997.79<\/span>  <span class=\"m\" style=\"color: #208050;\">18601.53<\/span>  <span class=\"m\" style=\"color: #208050;\">26155.15<\/span>  <span class=\"m\" style=\"color: #208050;\">28586.52<\/span>  <span class=\"m\" style=\"color: #208050;\">30505.41<\/span>  <span class=\"m\" style=\"color: #208050;\">30821.33<\/span>  <span class=\"m\" style=\"color: #208050;\">46634.38<\/span> <span class=\"m\" style=\"color: #208050;\">104660.67<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"plotting-time-series\" class=\"section\" style=\"color: #000000;\">\n<h2>Plotting Time Series<\/h2>\n<p>Once you have read a time series into R, the next step is usually to make a plot of the time series data, which you can do with the plot.ts() function in R.<\/p>\n<p>For example, to plot the time series of the age of death of 42 successive kings of England, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>kingstimeseries<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image1.png\" alt=\"image1\" \/><\/p>\n<p>We can see from the time plot that this time series could probably be described using an additive model, since the random fluctuations in the data are roughly constant in size over time.<\/p>\n<p>Likewise, to plot the time series of the number of births per month in New York city, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>birthstimeseries<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image2.png\" alt=\"image2\" \/><\/p>\n<p>We can see from this time series that there seems to be seasonal variation in the number of births per month: there is a peak every summer, and a trough every winter. Again, it seems that this time series could probably be described using an additive model, as the seasonal fluctuations are roughly constant in size over time and do not seem to depend on the level of the time series, and the random fluctuations also seem to be roughly constant in size over time.<\/p>\n<p>Similarly, to plot the time series of the monthly sales for the souvenir shop at a beach resort town in Queensland, Australia, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>souvenirtimeseries<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image4.png\" alt=\"image4\" \/><\/p>\n<p>In this case, it appears that an additive model is not appropriate for describing this time series, since the size of the seasonal fluctuations and random fluctuations seem to increase with the level of the time series. Thus, we may need to transform the time series in order to get a transformed time series that can be described using an additive model. For example, we can transform the time series by calculating the natural log of the original data:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> logsouvenirtimeseries <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> log<span class=\"p\">(<\/span>souvenirtimeseries<span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>logsouvenirtimeseries<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image5.png\" alt=\"image5\" \/><\/p>\n<p>Here we can see that the size of the seasonal fluctuations and random fluctuations in the log-transformed time series seem to be roughly constant over time, and do not depend on the level of the time series. Thus, the log-transformed time series can probably be described using an additive model.<\/p>\n<\/div>\n<div id=\"decomposing-time-series\" class=\"section\" style=\"color: #000000;\">\n<h2>Decomposing Time Series<\/h2>\n<p>Decomposing a time series means separating it into its constituent components, which are usually a trend component and an irregular component, and if it is a seasonal time series, a seasonal component.<\/p>\n<div id=\"decomposing-non-seasonal-data\" class=\"section\">\n<h3>Decomposing Non-Seasonal Data<\/h3>\n<p>A non-seasonal time series consists of a trend component and an irregular component. Decomposing the time series involves trying to separate the time series into these components, that is, estimating the the trend component and the irregular component.<\/p>\n<p>To estimate the trend component of a non-seasonal time series that can be described using an additive model, it is common to use a smoothing method, such as calculating the simple moving average of the time series.<\/p>\n<p>The SMA() function in the \u201cTTR\u201d R package can be used to smooth time series data using a simple moving average. To use this function, we first need to install the \u201cTTR\u201d R package (for instructions on how to install an R package, see\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/src\/installr.html#how-to-install-an-r-package\">How to install an R package<\/a>). Once you have installed the \u201cTTR\u201d R package, you can load the \u201cTTR\u201d R package by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> library<span class=\"p\">(<\/span><span class=\"s\" style=\"color: #4070a0;\">\"TTR\"<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>You can then use the \u201cSMA()\u201d function to smooth time series data. To use the SMA() function, you need to specify the order (span) of the simple moving average, using the parameter \u201cn\u201d. For example, to calculate a simple moving average of order 5, we set n=5 in the SMA() function.<\/p>\n<p>For example, as discussed above, the time series of the age of death of 42 successive kings of England appears is non-seasonal, and can probably be described using an additive model, since the random fluctuations in the data are roughly constant in size over time:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image1.png\" alt=\"image1\" \/><\/p>\n<p>Thus, we can try to estimate the trend component of this time series by smoothing using a simple moving average. To smooth the time series using a simple moving average of order 3, and plot the smoothed time series data, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kingstimeseriesSMA3 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> SMA<span class=\"p\">(<\/span>kingstimeseries<span class=\"p\">,<\/span>n<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">3<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>kingstimeseriesSMA3<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image6.png\" alt=\"image6\" \/><\/p>\n<p>There still appears to be quite a lot of random fluctuations in the time series smoothed using a simple moving average of order 3. Thus, to estimate the trend component more accurately, we might want to try smoothing the data with a simple moving average of a higher order. This takes a little bit of trial-and-error, to find the right amount of smoothing. For example, we can try using a simple moving average of order 8:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kingstimeseriesSMA8 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> SMA<span class=\"p\">(<\/span>kingstimeseries<span class=\"p\">,<\/span>n<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">8<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>kingstimeseriesSMA8<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image7.png\" alt=\"image7\" \/><\/p>\n<p>The data smoothed with a simple moving average of order 8 gives a clearer picture of the trend component, and we can see that the age of death of the English kings seems to have decreased from about 55 years old to about 38 years old during the reign of the first 20 kings, and then increased after that to about 73 years old by the end of the reign of the 40th king in the time series.<\/p>\n<\/div>\n<div id=\"decomposing-seasonal-data\" class=\"section\">\n<h3>Decomposing Seasonal Data<\/h3>\n<p>A seasonal time series consists of a trend component, a seasonal component and an irregular component. Decomposing the time series means separating the time series into these three components: that is, estimating these three components.<\/p>\n<p>To estimate the trend component and seasonal component of a seasonal time series that can be described using an additive model, we can use the \u201cdecompose()\u201d function in R. This function estimates the trend, seasonal, and irregular components of a time series that can be described using an additive model.<\/p>\n<p>The function \u201cdecompose()\u201d returns a list object as its result, where the estimates of the seasonal component, trend component and irregular component are stored in named elements of that list objects, called \u201cseasonal\u201d, \u201ctrend\u201d, and \u201crandom\u201d respectively.<\/p>\n<p>For example, as discussed above, the time series of the number of births per month in New York city is seasonal with a peak every summer and trough every winter, and can probably be described using an additive model since the seasonal and random fluctuations seem to be roughly constant in size over time:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image2.png\" alt=\"image2\" \/><\/p>\n<p>To estimate the trend, seasonal and irregular components of this time series, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> birthstimeseriescomponents <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> decompose<span class=\"p\">(<\/span>birthstimeseries<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>The estimated values of the seasonal, trend and irregular components are now stored in variables birthstimeseriescomponents$seasonal, birthstimeseriescomponents$trend and birthstimeseriescomponents$random. For example, we can print out the estimated values of the seasonal component by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> birthstimeseriescomponents<span class=\"o\" style=\"color: #666666;\">$<\/span>seasonal <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># get the estimated values of the seasonal component<\/span>\n       Jan        Feb        Mar        Apr        May        Jun        Jul        Aug        Sep        Oct        Nov        Dec\n <span class=\"m\" style=\"color: #208050;\">1946<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1947<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1948<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1949<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1950<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1951<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1952<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1953<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1954<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1955<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1956<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1957<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1958<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n <span class=\"m\" style=\"color: #208050;\">1959<\/span> <span class=\"m\" style=\"color: #208050;\">-0.6771947<\/span> <span class=\"m\" style=\"color: #208050;\">-2.0829607<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8625232<\/span> <span class=\"m\" style=\"color: #208050;\">-0.8016787<\/span>  <span class=\"m\" style=\"color: #208050;\">0.2516514<\/span> <span class=\"m\" style=\"color: #208050;\">-0.1532556<\/span>  <span class=\"m\" style=\"color: #208050;\">1.4560457<\/span>  <span class=\"m\" style=\"color: #208050;\">1.1645938<\/span>  <span class=\"m\" style=\"color: #208050;\">0.6916162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.7752444<\/span> <span class=\"m\" style=\"color: #208050;\">-1.1097652<\/span> <span class=\"m\" style=\"color: #208050;\">-0.3768197<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>The estimated seasonal factors are given for the months January-December, and are the same for each year. The largest seasonal factor is for July (about 1.46), and the lowest is for February (about -2.08), indicating that there seems to be a peak in births in July and a trough in births in February each year.<\/p>\n<p>We can plot the estimated trend, seasonal, and irregular components of the time series by using the \u201cplot()\u201d function, for example:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot<span class=\"p\">(<\/span>birthstimeseriescomponents<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image8.png\" alt=\"image8\" \/><\/p>\n<p>The plot above shows the original time series (top), the estimated trend component (second from top), the estimated seasonal component (third from top), and the estimated irregular component (bottom). We see that the estimated trend component shows a small decrease from about 24 in 1947 to about 22 in 1948, followed by a steady increase from then on to about 27 in 1959.<\/p>\n<\/div>\n<div id=\"seasonally-adjusting\" class=\"section\">\n<h3>Seasonally Adjusting<\/h3>\n<p>If you have a seasonal time series that can be described using an additive model, you can seasonally adjust the time series by estimating the seasonal component, and subtracting the estimated seasonal component from the original time series. We can do this using the estimate of the seasonal component calculated by the \u201cdecompose()\u201d function.<\/p>\n<p>For example, to seasonally adjust the time series of the number of births per month in New York city, we can estimate the seasonal component using \u201cdecompose()\u201d, and then subtract the seasonal component from the original time series:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> birthstimeseriescomponents <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> decompose<span class=\"p\">(<\/span>birthstimeseries<span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> birthstimeseriesseasonallyadjusted <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> birthstimeseries <span class=\"o\" style=\"color: #666666;\">-<\/span> birthstimeseriescomponents<span class=\"o\" style=\"color: #666666;\">$<\/span>seasonal\n<\/pre>\n<\/div>\n<\/div>\n<p>We can then plot the seasonally adjusted time series using the \u201cplot()\u201d function, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot<span class=\"p\">(<\/span>birthstimeseriesseasonallyadjusted<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image9.png\" alt=\"image9\" \/><\/p>\n<p>You can see that the seasonal variation has been removed from the seasonally adjusted time series. The seasonally adjusted time series now just contains the trend component and an irregular component.<\/p>\n<\/div>\n<\/div>\n<div id=\"forecasts-using-exponential-smoothing\" class=\"section\" style=\"color: #000000;\">\n<h2>Forecasts using Exponential Smoothing<\/h2>\n<p>Exponential smoothing can be used to make short-term forecasts for time series data.<\/p>\n<div id=\"simple-exponential-smoothing\" class=\"section\">\n<h3>Simple Exponential Smoothing<\/h3>\n<p>If you have a time series that can be described using an additive model with constant level and no seasonality, you can use simple exponential smoothing to make short-term forecasts.<\/p>\n<p>The simple exponential smoothing method provides a way of estimating the level at the current time point. Smoothing is controlled by the parameter alpha; for the estimate of the level at the current time point. The value of alpha; lies between 0 and 1. Values of alpha that are close to 0 mean that little weight is placed on the most recent observations when making forecasts of future values.<\/p>\n<p>For example, the file\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/robjhyndman.com\/tsdldata\/hurst\/precip1.dat\">http:\/\/robjhyndman.com\/tsdldata\/hurst\/precip1.dat<\/a>\u00a0contains total annual rainfall in inches for London, from 1813-1912 (original data from Hipel and McLeod, 1994). We can read the data into R and plot it by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> rain <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> scan<span class=\"p\">(<\/span><span class=\"s\" style=\"color: #4070a0;\">\"http:\/\/robjhyndman.com\/tsdldata\/hurst\/precip1.dat\"<\/span><span class=\"p\">,<\/span>skip<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">)<\/span>\n  Read <span class=\"m\" style=\"color: #208050;\">100<\/span> items\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> rainseries <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> ts<span class=\"p\">(<\/span>rain<span class=\"p\">,<\/span>start<span class=\"o\" style=\"color: #666666;\">=<\/span>c<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">1813<\/span><span class=\"p\">))<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>rainseries<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image10.png\" alt=\"image10\" \/><\/p>\n<p>You can see from the plot that there is roughly constant level (the mean stays constant at about 25 inches). The random fluctuations in the time series seem to be roughly constant in size over time, so it is probably appropriate to describe the data using an additive model. Thus, we can make forecasts using simple exponential smoothing.<\/p>\n<p>To make forecasts using simple exponential smoothing in R, we can fit a simple exponential smoothing predictive model using the \u201cHoltWinters()\u201d function in R. To use HoltWinters() for simple exponential smoothing, we need to set the parameters beta=FALSE and gamma=FALSE in the HoltWinters() function (the beta and gamma parameters are used for Holt\u2019s exponential smoothing, or Holt-Winters exponential smoothing, as described below).<\/p>\n<p>The HoltWinters() function returns a list variable, that contains several named elements.<\/p>\n<p>For example, to use simple exponential smoothing to make forecasts for the time series of annual rainfall in London, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> rainseriesforecasts <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> HoltWinters<span class=\"p\">(<\/span>rainseries<span class=\"p\">,<\/span> beta<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">,<\/span> gamma<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> rainseriesforecasts\n  Smoothing parameters<span class=\"o\" style=\"color: #666666;\">:<\/span>\n  alpha<span class=\"o\" style=\"color: #666666;\">:<\/span>  <span class=\"m\" style=\"color: #208050;\">0.02412151<\/span>\n  beta <span class=\"o\" style=\"color: #666666;\">:<\/span>  <span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span>\n  gamma<span class=\"o\" style=\"color: #666666;\">:<\/span>  <span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span>\n  Coefficients<span class=\"o\" style=\"color: #666666;\">:<\/span>\n    <span class=\"p\">[,<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">]<\/span>\n  a <span class=\"m\" style=\"color: #208050;\">24.67819<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>The output of HoltWinters() tells us that the estimated value of the alpha parameter is about 0.024. This is very close to zero, telling us that the forecasts are based on both recent and less recent observations (although somewhat more weight is placed on recent observations).<\/p>\n<p>By default, HoltWinters() just makes forecasts for the same time period covered by our original time series. In this case, our original time series included rainfall for London from 1813-1912, so the forecasts are also for 1813-1912.<\/p>\n<p>In the example above, we have stored the output of the HoltWinters() function in the list variable \u201crainseriesforecasts\u201d. The forecasts made by HoltWinters() are stored in a named element of this list variable called \u201cfitted\u201d, so we can get their values by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> rainseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>fitted\n  Time Series<span class=\"o\" style=\"color: #666666;\">:<\/span>\n  Start <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">1814<\/span>\n  End <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">1912<\/span>\n  Frequency <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">1<\/span>\n     xhat    level\n  <span class=\"m\" style=\"color: #208050;\">1814<\/span> <span class=\"m\" style=\"color: #208050;\">23.56000<\/span> <span class=\"m\" style=\"color: #208050;\">23.56000<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1815<\/span> <span class=\"m\" style=\"color: #208050;\">23.62054<\/span> <span class=\"m\" style=\"color: #208050;\">23.62054<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1816<\/span> <span class=\"m\" style=\"color: #208050;\">23.57808<\/span> <span class=\"m\" style=\"color: #208050;\">23.57808<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1817<\/span> <span class=\"m\" style=\"color: #208050;\">23.76290<\/span> <span class=\"m\" style=\"color: #208050;\">23.76290<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1818<\/span> <span class=\"m\" style=\"color: #208050;\">23.76017<\/span> <span class=\"m\" style=\"color: #208050;\">23.76017<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1819<\/span> <span class=\"m\" style=\"color: #208050;\">23.76306<\/span> <span class=\"m\" style=\"color: #208050;\">23.76306<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1820<\/span> <span class=\"m\" style=\"color: #208050;\">23.82691<\/span> <span class=\"m\" style=\"color: #208050;\">23.82691<\/span>\n  <span class=\"kc\" style=\"font-weight: bold; color: #007020;\">...<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1905<\/span> <span class=\"m\" style=\"color: #208050;\">24.62852<\/span> <span class=\"m\" style=\"color: #208050;\">24.62852<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1906<\/span> <span class=\"m\" style=\"color: #208050;\">24.58852<\/span> <span class=\"m\" style=\"color: #208050;\">24.58852<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1907<\/span> <span class=\"m\" style=\"color: #208050;\">24.58059<\/span> <span class=\"m\" style=\"color: #208050;\">24.58059<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1908<\/span> <span class=\"m\" style=\"color: #208050;\">24.54271<\/span> <span class=\"m\" style=\"color: #208050;\">24.54271<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1909<\/span> <span class=\"m\" style=\"color: #208050;\">24.52166<\/span> <span class=\"m\" style=\"color: #208050;\">24.52166<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1910<\/span> <span class=\"m\" style=\"color: #208050;\">24.57541<\/span> <span class=\"m\" style=\"color: #208050;\">24.57541<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1911<\/span> <span class=\"m\" style=\"color: #208050;\">24.59433<\/span> <span class=\"m\" style=\"color: #208050;\">24.59433<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1912<\/span> <span class=\"m\" style=\"color: #208050;\">24.59905<\/span> <span class=\"m\" style=\"color: #208050;\">24.59905<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>We can plot the original time series against the forecasts by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot<span class=\"p\">(<\/span>rainseriesforecasts<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image11.png\" alt=\"image11\" \/><\/p>\n<p>The plot shows the original time series in black, and the forecasts as a red line. The time series of forecasts is much smoother than the time series of the original data here.<\/p>\n<p>As a measure of the accuracy of the forecasts, we can calculate the sum of squared errors for the in-sample forecast errors, that is, the forecast errors for the time period covered by our original time series. The sum-of-squared-errors is stored in a named element of the list variable \u201crainseriesforecasts\u201d called \u201cSSE\u201d, so we can get its value by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> rainseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>SSE\n  <span class=\"p\">[<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">]<\/span> <span class=\"m\" style=\"color: #208050;\">1828.855<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>That is, here the sum-of-squared-errors is 1828.855.<\/p>\n<p>It is common in simple exponential smoothing to use the first value in the time series as the initial value for the level. For example, in the time series for rainfall in London, the first value is 23.56 (inches) for rainfall in 1813. You can specify the initial value for the level in the HoltWinters() function by using the \u201cl.start\u201d parameter. For example, to make forecasts with the initial value of the level set to 23.56, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> HoltWinters<span class=\"p\">(<\/span>rainseries<span class=\"p\">,<\/span> beta<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">,<\/span> gamma<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">,<\/span> l.start<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">23.56<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>As explained above, by default HoltWinters() just makes forecasts for the time period covered by the original data, which is 1813-1912 for the rainfall time series. We can make forecasts for further time points by using the \u201cforecast.HoltWinters()\u201d function in the R \u201cforecast\u201d package. To use the forecast.HoltWinters() function, we first need to install the \u201cforecast\u201d R package (for instructions on how to install an R package, see\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/src\/installr.html#how-to-install-an-r-package\">How to install an R package<\/a>).<\/p>\n<p>Once you have installed the \u201cforecast\u201d R package, you can load the \u201cforecast\u201d R package by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> library<span class=\"p\">(<\/span><span class=\"s\" style=\"color: #4070a0;\">\"forecast\"<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>When using the forecast.HoltWinters() function, as its first argument (input), you pass it the predictive model that you have already fitted using the HoltWinters() function. For example, in the case of the rainfall time series, we stored the predictive model made using HoltWinters() in the variable \u201crainseriesforecasts\u201d. You specify how many further time points you want to make forecasts for by using the \u201ch\u201d parameter in forecast.HoltWinters(). For example, to make a forecast of rainfall for the years 1814-1820 (8 more years) using forecast.HoltWinters(), we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> rainseriesforecasts2 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> forecast.HoltWinters<span class=\"p\">(<\/span>rainseriesforecasts<span class=\"p\">,<\/span> h<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">8<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> rainseriesforecasts2\n Point     Forecast    Lo <span class=\"m\" style=\"color: #208050;\">80<\/span>    Hi <span class=\"m\" style=\"color: #208050;\">80<\/span>    Lo <span class=\"m\" style=\"color: #208050;\">95<\/span>    Hi <span class=\"m\" style=\"color: #208050;\">95<\/span>\n <span class=\"m\" style=\"color: #208050;\">1913<\/span>       <span class=\"m\" style=\"color: #208050;\">24.67819<\/span> <span class=\"m\" style=\"color: #208050;\">19.17493<\/span> <span class=\"m\" style=\"color: #208050;\">30.18145<\/span> <span class=\"m\" style=\"color: #208050;\">16.26169<\/span> <span class=\"m\" style=\"color: #208050;\">33.09470<\/span>\n <span class=\"m\" style=\"color: #208050;\">1914<\/span>       <span class=\"m\" style=\"color: #208050;\">24.67819<\/span> <span class=\"m\" style=\"color: #208050;\">19.17333<\/span> <span class=\"m\" style=\"color: #208050;\">30.18305<\/span> <span class=\"m\" style=\"color: #208050;\">16.25924<\/span> <span class=\"m\" style=\"color: #208050;\">33.09715<\/span>\n <span class=\"m\" style=\"color: #208050;\">1915<\/span>       <span class=\"m\" style=\"color: #208050;\">24.67819<\/span> <span class=\"m\" style=\"color: #208050;\">19.17173<\/span> <span class=\"m\" style=\"color: #208050;\">30.18465<\/span> <span class=\"m\" style=\"color: #208050;\">16.25679<\/span> <span class=\"m\" style=\"color: #208050;\">33.09960<\/span>\n <span class=\"m\" style=\"color: #208050;\">1916<\/span>       <span class=\"m\" style=\"color: #208050;\">24.67819<\/span> <span class=\"m\" style=\"color: #208050;\">19.17013<\/span> <span class=\"m\" style=\"color: #208050;\">30.18625<\/span> <span class=\"m\" style=\"color: #208050;\">16.25434<\/span> <span class=\"m\" style=\"color: #208050;\">33.10204<\/span>\n <span class=\"m\" style=\"color: #208050;\">1917<\/span>       <span class=\"m\" style=\"color: #208050;\">24.67819<\/span> <span class=\"m\" style=\"color: #208050;\">19.16853<\/span> <span class=\"m\" style=\"color: #208050;\">30.18785<\/span> <span class=\"m\" style=\"color: #208050;\">16.25190<\/span> <span class=\"m\" style=\"color: #208050;\">33.10449<\/span>\n <span class=\"m\" style=\"color: #208050;\">1918<\/span>       <span class=\"m\" style=\"color: #208050;\">24.67819<\/span> <span class=\"m\" style=\"color: #208050;\">19.16694<\/span> <span class=\"m\" style=\"color: #208050;\">30.18945<\/span> <span class=\"m\" style=\"color: #208050;\">16.24945<\/span> <span class=\"m\" style=\"color: #208050;\">33.10694<\/span>\n <span class=\"m\" style=\"color: #208050;\">1919<\/span>       <span class=\"m\" style=\"color: #208050;\">24.67819<\/span> <span class=\"m\" style=\"color: #208050;\">19.16534<\/span> <span class=\"m\" style=\"color: #208050;\">30.19105<\/span> <span class=\"m\" style=\"color: #208050;\">16.24701<\/span> <span class=\"m\" style=\"color: #208050;\">33.10938<\/span>\n <span class=\"m\" style=\"color: #208050;\">1920<\/span>       <span class=\"m\" style=\"color: #208050;\">24.67819<\/span> <span class=\"m\" style=\"color: #208050;\">19.16374<\/span> <span class=\"m\" style=\"color: #208050;\">30.19265<\/span> <span class=\"m\" style=\"color: #208050;\">16.24456<\/span> <span class=\"m\" style=\"color: #208050;\">33.11182<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>The forecast.HoltWinters() function gives you the forecast for a year, a 80% prediction interval for the forecast, and a 95% prediction interval for the forecast. For example, the forecasted rainfall for 1920 is about 24.68 inches, with a 95% prediction interval of (16.24, 33.11).<\/p>\n<p>To plot the predictions made by forecast.HoltWinters(), we can use the \u201cplot.forecast()\u201d function:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.forecast<span class=\"p\">(<\/span>rainseriesforecasts2<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image12.png\" alt=\"image12\" \/><\/p>\n<p>Here the forecasts for 1913-1920 are plotted as a blue line, the 80% prediction interval as an orange shaded area, and the 95% prediction interval as a yellow shaded area.<\/p>\n<p>The \u2018forecast errors\u2019 are calculated as the observed values minus predicted values, for each time point. We can only calculate the forecast errors for the time period covered by our original time series, which is 1813-1912 for the rainfall data. As mentioned above, one measure of the accuracy of the predictive model is the sum-of-squared-errors (SSE) for the in-sample forecast errors.<\/p>\n<p>The in-sample forecast errors are stored in the named element \u201cresiduals\u201d of the list variable returned by forecast.HoltWinters(). If the predictive model cannot be improved upon, there should be no correlations between forecast errors for successive predictions. In other words, if there are correlations between forecast errors for successive predictions, it is likely that the simple exponential smoothing forecasts could be improved upon by another forecasting technique.<\/p>\n<p>To figure out whether this is the case, we can obtain a correlogram of the in-sample forecast errors for lags 1-20. We can calculate a correlogram of the forecast errors using the \u201cacf()\u201d function in R. To specify the maximum lag that we want to look at, we use the \u201clag.max\u201d parameter in acf().<\/p>\n<p>For example, to calculate a correlogram of the in-sample forecast errors for the London rainfall data for lags 1-20, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> acf<span class=\"p\">(<\/span>rainseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image13.png\" alt=\"image13\" \/><\/p>\n<p>You can see from the sample correlogram that the autocorrelation at lag 3 is just touching the significance bounds. To test whether there is significant evidence for non-zero correlations at lags 1-20, we can carry out a Ljung-Box test. This can be done in R using the \u201cBox.test()\u201d, function. The maximum lag that we want to look at is specified using the \u201clag\u201d parameter in the Box.test() function. For example, to test whether there are non-zero autocorrelations at lags 1-20, for the in-sample forecast errors for London rainfall data, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> Box.test<span class=\"p\">(<\/span>rainseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> type<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"s\" style=\"color: #4070a0;\">\"Ljung-Box\"<\/span><span class=\"p\">)<\/span>\n    Box<span class=\"o\" style=\"color: #666666;\">-<\/span>Ljung test\n  data<span class=\"o\" style=\"color: #666666;\">:<\/span>  rainseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals\n  X<span class=\"o\" style=\"color: #666666;\">-<\/span>squared <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">17.4008<\/span><span class=\"p\">,<\/span> df <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> p<span class=\"o\" style=\"color: #666666;\">-<\/span>value <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">0.6268<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>Here the Ljung-Box test statistic is 17.4, and the p-value is 0.6, so there is little evidence of non-zero autocorrelations in the in-sample forecast errors at lags 1-20.<\/p>\n<p>To be sure that the predictive model cannot be improved upon, it is also a good idea to check whether the forecast errors are normally distributed with mean zero and constant variance. To check whether the forecast errors have constant variance, we can make a time plot of the in-sample forecast errors:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>rainseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image18.png\" alt=\"image18\" \/><\/p>\n<p>The plot shows that the in-sample forecast errors seem to have roughly constant variance over time, although the size of the fluctuations in the start of the time series (1820-1830) may be slightly less than that at later dates (eg. 1840-1850).<\/p>\n<p>To check whether the forecast errors are normally distributed with mean zero, we can plot a histogram of the forecast errors, with an overlaid normal curve that has mean zero and the same standard deviation as the distribution of forecast errors. To do this, we can define an R function \u201cplotForecastErrors()\u201d, below:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plotForecastErrors <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> <span class=\"kr\" style=\"font-weight: bold; color: #007020;\">function<\/span><span class=\"p\">(<\/span>forecasterrors<span class=\"p\">)<\/span>\n  <span class=\"p\">{<\/span>\n     <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make a histogram of the forecast errors:<\/span>\n     mybinsize <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> IQR<span class=\"p\">(<\/span>forecasterrors<span class=\"p\">)<\/span><span class=\"o\" style=\"color: #666666;\">\/<\/span><span class=\"m\" style=\"color: #208050;\">4<\/span>\n     mysd   <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> sd<span class=\"p\">(<\/span>forecasterrors<span class=\"p\">)<\/span>\n     mymin  <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> min<span class=\"p\">(<\/span>forecasterrors<span class=\"p\">)<\/span> <span class=\"o\" style=\"color: #666666;\">-<\/span> mysd<span class=\"o\" style=\"color: #666666;\">*<\/span><span class=\"m\" style=\"color: #208050;\">5<\/span>\n     mymax  <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> max<span class=\"p\">(<\/span>forecasterrors<span class=\"p\">)<\/span> <span class=\"o\" style=\"color: #666666;\">+<\/span> mysd<span class=\"o\" style=\"color: #666666;\">*<\/span><span class=\"m\" style=\"color: #208050;\">3<\/span>\n     <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># generate normally distributed data with mean 0 and standard deviation mysd<\/span>\n     mynorm <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> rnorm<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">10000<\/span><span class=\"p\">,<\/span> mean<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">0<\/span><span class=\"p\">,<\/span> sd<span class=\"o\" style=\"color: #666666;\">=<\/span>mysd<span class=\"p\">)<\/span>\n     mymin2 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> min<span class=\"p\">(<\/span>mynorm<span class=\"p\">)<\/span>\n     mymax2 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> max<span class=\"p\">(<\/span>mynorm<span class=\"p\">)<\/span>\n     <span class=\"kr\" style=\"font-weight: bold; color: #007020;\">if<\/span> <span class=\"p\">(<\/span>mymin2 <span class=\"o\" style=\"color: #666666;\">&lt;<\/span> mymin<span class=\"p\">)<\/span> <span class=\"p\">{<\/span> mymin <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> mymin2 <span class=\"p\">}<\/span>\n     <span class=\"kr\" style=\"font-weight: bold; color: #007020;\">if<\/span> <span class=\"p\">(<\/span>mymax2 <span class=\"o\" style=\"color: #666666;\">&gt;<\/span> mymax<span class=\"p\">)<\/span> <span class=\"p\">{<\/span> mymax <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> mymax2 <span class=\"p\">}<\/span>\n     <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make a red histogram of the forecast errors, with the normally distributed data overlaid:<\/span>\n     mybins <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> seq<span class=\"p\">(<\/span>mymin<span class=\"p\">,<\/span> mymax<span class=\"p\">,<\/span> mybinsize<span class=\"p\">)<\/span>\n     hist<span class=\"p\">(<\/span>forecasterrors<span class=\"p\">,<\/span> col<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"s\" style=\"color: #4070a0;\">\"red\"<\/span><span class=\"p\">,<\/span> freq<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">,<\/span> breaks<span class=\"o\" style=\"color: #666666;\">=<\/span>mybins<span class=\"p\">)<\/span>\n     <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># freq=FALSE ensures the area under the histogram = 1<\/span>\n     <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># generate normally distributed data with mean 0 and standard deviation mysd<\/span>\n     myhist <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> hist<span class=\"p\">(<\/span>mynorm<span class=\"p\">,<\/span> plot<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">,<\/span> breaks<span class=\"o\" style=\"color: #666666;\">=<\/span>mybins<span class=\"p\">)<\/span>\n     <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># plot the normal curve as a blue line on top of the histogram of forecast errors:<\/span>\n     points<span class=\"p\">(<\/span>myhist<span class=\"o\" style=\"color: #666666;\">$<\/span>mids<span class=\"p\">,<\/span> myhist<span class=\"o\" style=\"color: #666666;\">$<\/span>density<span class=\"p\">,<\/span> type<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"s\" style=\"color: #4070a0;\">\"l\"<\/span><span class=\"p\">,<\/span> col<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"s\" style=\"color: #4070a0;\">\"blue\"<\/span><span class=\"p\">,<\/span> lwd<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">2<\/span><span class=\"p\">)<\/span>\n  <span class=\"p\">}<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>You will have to copy the function above into R in order to use it. You can then use plotForecastErrors() to plot a histogram (with overlaid normal curve) of the forecast errors for the rainfall predictions:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plotForecastErrors<span class=\"p\">(<\/span>rainseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image19.png\" alt=\"image19\" \/><\/p>\n<p>The plot shows that the distribution of forecast errors is roughly centred on zero, and is more or less normally distributed, although it seems to be slightly skewed to the right compared to a normal curve. However, the right skew is relatively small, and so it is plausible that the forecast errors are normally distributed with mean zero.<\/p>\n<p>The Ljung-Box test showed that there is little evidence of non-zero autocorrelations in the in-sample forecast errors, and the distribution of forecast errors seems to be normally distributed with mean zero. This suggests that the simple exponential smoothing method provides an adequate predictive model for London rainfall, which probably cannot be improved upon. Furthermore, the assumptions that the 80% and 95% predictions intervals were based upon (that there are no autocorrelations in the forecast errors, and the forecast errors are normally distributed with mean zero and constant variance) are probably valid.<\/p>\n<\/div>\n<div id=\"holt-s-exponential-smoothing\" class=\"section\">\n<h3>Holt\u2019s Exponential Smoothing<\/h3>\n<p>If you have a time series that can be described using an additive model with increasing or decreasing trend and no seasonality, you can use Holt\u2019s exponential smoothing to make short-term forecasts.<\/p>\n<p>Holt\u2019s exponential smoothing estimates the level and slope at the current time point. Smoothing is controlled by two parameters, alpha, for the estimate of the level at the current time point, and beta for the estimate of the slope b of the trend component at the current time point. As with simple exponential smoothing, the paramters alpha and beta have values between 0 and 1, and values that are close to 0 mean that little weight is placed on the most recent observations when making forecasts of future values.<\/p>\n<p>An example of a time series that can probably be described using an additive model with a trend and no seasonality is the time series of the annual diameter of women\u2019s skirts at the hem, from 1866 to 1911. The data is available in the file\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/robjhyndman.com\/tsdldata\/roberts\/skirts.dat\">http:\/\/robjhyndman.com\/tsdldata\/roberts\/skirts.dat<\/a>\u00a0(original data from Hipel and McLeod, 1994).<\/p>\n<p>We can read in and plot the data in R by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> skirts <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> scan<span class=\"p\">(<\/span><span class=\"s\" style=\"color: #4070a0;\">\"http:\/\/robjhyndman.com\/tsdldata\/roberts\/skirts.dat\"<\/span><span class=\"p\">,<\/span>skip<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">5<\/span><span class=\"p\">)<\/span>\n  Read <span class=\"m\" style=\"color: #208050;\">46<\/span> items\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> skirtsseries <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> ts<span class=\"p\">(<\/span>skirts<span class=\"p\">,<\/span>start<span class=\"o\" style=\"color: #666666;\">=<\/span>c<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">1866<\/span><span class=\"p\">))<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>skirtsseries<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image14.png\" alt=\"image14\" \/><\/p>\n<p>We can see from the plot that there was an increase in hem diameter from about 600 in 1866 to about 1050 in 1880, and that afterwards the hem diameter decreased to about 520 in 1911.<\/p>\n<p>To make forecasts, we can fit a predictive model using the HoltWinters() function in R. To use HoltWinters() for Holt\u2019s exponential smoothing, we need to set the parameter gamma=FALSE (the gamma parameter is used for Holt-Winters exponential smoothing, as described below).<\/p>\n<p>For example, to use Holt\u2019s exponential smoothing to fit a predictive model for skirt hem diameter, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> skirtsseriesforecasts <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> HoltWinters<span class=\"p\">(<\/span>skirtsseries<span class=\"p\">,<\/span> gamma<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> skirtsseriesforecasts\n  Smoothing parameters<span class=\"o\" style=\"color: #666666;\">:<\/span>\n  alpha<span class=\"o\" style=\"color: #666666;\">:<\/span>  <span class=\"m\" style=\"color: #208050;\">0.8383481<\/span>\n  beta <span class=\"o\" style=\"color: #666666;\">:<\/span>  <span class=\"m\" style=\"color: #208050;\">1<\/span>\n  gamma<span class=\"o\" style=\"color: #666666;\">:<\/span>  <span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span>\n  Coefficients<span class=\"o\" style=\"color: #666666;\">:<\/span>\n    <span class=\"p\">[,<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">]<\/span>\n  a <span class=\"m\" style=\"color: #208050;\">529.308585<\/span>\n  b   <span class=\"m\" style=\"color: #208050;\">5.690464<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> skirtsseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>SSE\n  <span class=\"p\">[<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">]<\/span> <span class=\"m\" style=\"color: #208050;\">16954.18<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>The estimated value of alpha is 0.84, and of beta is 1.00. These are both high, telling us that both the estimate of the current value of the level, and of the slope b of the trend component, are based mostly upon very recent observations in the time series. This makes good intuitive sense, since the level and the slope of the time series both change quite a lot over time. The value of the sum-of-squared-errors for the in-sample forecast errors is 16954.<\/p>\n<p>We can plot the original time series as a black line, with the forecasted values as a red line on top of that, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot<span class=\"p\">(<\/span>skirtsseriesforecasts<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image15.png\" alt=\"image15\" \/><\/p>\n<p>We can see from the picture that the in-sample forecasts agree pretty well with the observed values, although they tend to lag behind the observed values a little bit.<\/p>\n<p>If you wish, you can specify the initial values of the level and the slope b of the trend component by using the \u201cl.start\u201d and \u201cb.start\u201d arguments for the HoltWinters() function. It is common to set the initial value of the level to the first value in the time series (608 for the skirts data), and the initial value of the slope to the second value minus the first value (9 for the skirts data). For example, to fit a predictive model to the skirt hem data using Holt\u2019s exponential smoothing, with initial values of 608 for the level and 9 for the slope b of the trend component, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> HoltWinters<span class=\"p\">(<\/span>skirtsseries<span class=\"p\">,<\/span> gamma<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">,<\/span> l.start<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">608<\/span><span class=\"p\">,<\/span> b.start<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">9<\/span><span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>As for simple exponential smoothing, we can make forecasts for future times not covered by the original time series by using the forecast.HoltWinters() function in the \u201cforecast\u201d package. For example, our time series data for skirt hems was for 1866 to 1911, so we can make predictions for 1912 to 1930 (19 more data points), and plot them, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> skirtsseriesforecasts2 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> forecast.HoltWinters<span class=\"p\">(<\/span>skirtsseriesforecasts<span class=\"p\">,<\/span> h<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">19<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.forecast<span class=\"p\">(<\/span>skirtsseriesforecasts2<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image16.png\" alt=\"image16\" \/><\/p>\n<p>The forecasts are shown as a blue line, with the 80% prediction intervals as an orange shaded area, and the 95% prediction intervals as a yellow shaded area.<\/p>\n<p>As for simple exponential smoothing, we can check whether the predictive model could be improved upon by checking whether the in-sample forecast errors show non-zero autocorrelations at lags 1-20. For example, for the skirt hem data, we can make a correlogram, and carry out the Ljung-Box test, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> acf<span class=\"p\">(<\/span>skirtsseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> Box.test<span class=\"p\">(<\/span>skirtsseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> type<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"s\" style=\"color: #4070a0;\">\"Ljung-Box\"<\/span><span class=\"p\">)<\/span>\n    Box<span class=\"o\" style=\"color: #666666;\">-<\/span>Ljung test\n  data<span class=\"o\" style=\"color: #666666;\">:<\/span>  skirtsseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals\n  X<span class=\"o\" style=\"color: #666666;\">-<\/span>squared <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">19.7312<\/span><span class=\"p\">,<\/span> df <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> p<span class=\"o\" style=\"color: #666666;\">-<\/span>value <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">0.4749<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image17.png\" alt=\"image17\" \/><\/p>\n<p>Here the correlogram shows that the sample autocorrelation for the in-sample forecast errors at lag 5 exceeds the significance bounds. However, we would expect one in 20 of the autocorrelations for the first twenty lags to exceed the 95% significance bounds by chance alone. Indeed, when we carry out the Ljung-Box test, the p-value is 0.47, indicating that there is little evidence of non-zero autocorrelations in the in-sample forecast errors at lags 1-20.<\/p>\n<p>As for simple exponential smoothing, we should also check that the forecast errors have constant variance over time, and are normally distributed with mean zero. We can do this by making a time plot of forecast errors, and a histogram of the distribution of forecast errors with an overlaid normal curve:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>skirtsseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span>            <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make a time plot<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plotForecastErrors<span class=\"p\">(<\/span>skirtsseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span> <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make a histogram<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image20.png\" alt=\"image20\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image21.png\" alt=\"image21\" \/><\/p>\n<p>The time plot of forecast errors shows that the forecast errors have roughly constant variance over time. The histogram of forecast errors show that it is plausible that the forecast errors are normally distributed with mean zero and constant variance.<\/p>\n<p>Thus, the Ljung-Box test shows that there is little evidence of autocorrelations in the forecast errors, while the time plot and histogram of forecast errors show that it is plausible that the forecast errors are normally distributed with mean zero and constant variance. Therefore, we can conclude that Holt\u2019s exponential smoothing provides an adequate predictive model for skirt hem diameters, which probably cannot be improved upon. In addition, it means that the assumptions that the 80% and 95% predictions intervals were based upon are probably valid.<\/p>\n<\/div>\n<div id=\"holt-winters-exponential-smoothing\" class=\"section\">\n<h3>Holt-Winters Exponential Smoothing<\/h3>\n<p>If you have a time series that can be described using an additive model with increasing or decreasing trend and seasonality, you can use Holt-Winters exponential smoothing to make short-term forecasts.<\/p>\n<p>Holt-Winters exponential smoothing estimates the level, slope and seasonal component at the current time point. Smoothing is controlled by three parameters: alpha, beta, and gamma, for the estimates of the level, slope b of the trend component, and the seasonal component, respectively, at the current time point. The parameters alpha, beta and gamma all have values between 0 and 1, and values that are close to 0 mean that relatively little weight is placed on the most recent observations when making forecasts of future values.<\/p>\n<p>An example of a time series that can probably be described using an additive model with a trend and seasonality is the time series of the log of monthly sales for the souvenir shop at a beach resort town in Queensland, Australia (discussed above):<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image5.png\" alt=\"image5\" \/><\/p>\n<p>To make forecasts, we can fit a predictive model using the HoltWinters() function. For example, to fit a predictive model for the log of the monthly sales in the souvenir shop, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> logsouvenirtimeseries <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> log<span class=\"p\">(<\/span>souvenirtimeseries<span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> souvenirtimeseriesforecasts <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> HoltWinters<span class=\"p\">(<\/span>logsouvenirtimeseries<span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> souvenirtimeseriesforecasts\n  Holt<span class=\"o\" style=\"color: #666666;\">-<\/span>Winters exponential smoothing with trend and additive seasonal component.\n  Smoothing parameters<span class=\"o\" style=\"color: #666666;\">:<\/span>\n  alpha<span class=\"o\" style=\"color: #666666;\">:<\/span>  <span class=\"m\" style=\"color: #208050;\">0.413418<\/span>\n  beta <span class=\"o\" style=\"color: #666666;\">:<\/span>  <span class=\"m\" style=\"color: #208050;\">0<\/span>\n  gamma<span class=\"o\" style=\"color: #666666;\">:<\/span>  <span class=\"m\" style=\"color: #208050;\">0.9561275<\/span>\n  Coefficients<span class=\"o\" style=\"color: #666666;\">:<\/span>\n       <span class=\"p\">[,<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">]<\/span>\n   a   <span class=\"m\" style=\"color: #208050;\">10.37661961<\/span>\n   b    <span class=\"m\" style=\"color: #208050;\">0.02996319<\/span>\n   s1  <span class=\"m\" style=\"color: #208050;\">-0.80952063<\/span>\n   s2  <span class=\"m\" style=\"color: #208050;\">-0.60576477<\/span>\n   s3   <span class=\"m\" style=\"color: #208050;\">0.01103238<\/span>\n   s4  <span class=\"m\" style=\"color: #208050;\">-0.24160551<\/span>\n   s5  <span class=\"m\" style=\"color: #208050;\">-0.35933517<\/span>\n   s6  <span class=\"m\" style=\"color: #208050;\">-0.18076683<\/span>\n   s7   <span class=\"m\" style=\"color: #208050;\">0.07788605<\/span>\n   s8   <span class=\"m\" style=\"color: #208050;\">0.10147055<\/span>\n   s9   <span class=\"m\" style=\"color: #208050;\">0.09649353<\/span>\n   s10  <span class=\"m\" style=\"color: #208050;\">0.05197826<\/span>\n   s11  <span class=\"m\" style=\"color: #208050;\">0.41793637<\/span>\n   s12  <span class=\"m\" style=\"color: #208050;\">1.18088423<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> souvenirtimeseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>SSE\n  <span class=\"m\" style=\"color: #208050;\">2.011491<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>The estimated values of alpha, beta and gamma are 0.41, 0.00, and 0.96, respectively. The value of alpha (0.41) is relatively low, indicating that the estimate of the level at the current time point is based upon both recent observations and some observations in the more distant past. The value of beta is 0.00, indicating that the estimate of the slope b of the trend component is not updated over the time series, and instead is set equal to its initial value. This makes good intuitive sense, as the level changes quite a bit over the time series, but the slope b of the trend component remains roughly the same. In contrast, the value of gamma (0.96) is high, indicating that the estimate of the seasonal component at the current time point is just based upon very recent observations.<\/p>\n<p>As for simple exponential smoothing and Holt\u2019s exponential smoothing, we can plot the original time series as a black line, with the forecasted values as a red line on top of that:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot<span class=\"p\">(<\/span>souvenirtimeseriesforecasts<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image22.png\" alt=\"image22\" \/><\/p>\n<p>We see from the plot that the Holt-Winters exponential method is very successful in predicting the seasonal peaks, which occur roughly in November every year.<\/p>\n<p>To make forecasts for future times not included in the original time series, we use the \u201cforecast.HoltWinters()\u201d function in the \u201cforecast\u201d package. For example, the original data for the souvenir sales is from January 1987 to December 1993. If we wanted to make forecasts for January 1994 to December 1998 (48 more months), and plot the forecasts, we would type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> souvenirtimeseriesforecasts2 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> forecast.HoltWinters<span class=\"p\">(<\/span>souvenirtimeseriesforecasts<span class=\"p\">,<\/span> h<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">48<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.forecast<span class=\"p\">(<\/span>souvenirtimeseriesforecasts2<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image23.png\" alt=\"image23\" \/><\/p>\n<p>The forecasts are shown as a blue line, and the orange and yellow shaded areas show 80% and 95% prediction intervals, respectively.<\/p>\n<p>We can investigate whether the predictive model can be improved upon by checking whether the in-sample forecast errors show non-zero autocorrelations at lags 1-20, by making a correlogram and carrying out the Ljung-Box test:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> acf<span class=\"p\">(<\/span>souvenirtimeseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> Box.test<span class=\"p\">(<\/span>souvenirtimeseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> type<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"s\" style=\"color: #4070a0;\">\"Ljung-Box\"<\/span><span class=\"p\">)<\/span>\n  Box<span class=\"o\" style=\"color: #666666;\">-<\/span>Ljung test\n  data<span class=\"o\" style=\"color: #666666;\">:<\/span>  souvenirtimeseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals\n  X<span class=\"o\" style=\"color: #666666;\">-<\/span>squared <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">17.5304<\/span><span class=\"p\">,<\/span> df <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> p<span class=\"o\" style=\"color: #666666;\">-<\/span>value <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">0.6183<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image24.png\" alt=\"image24\" \/><\/p>\n<p>The correlogram shows that the autocorrelations for the in-sample forecast errors do not exceed the significance bounds for lags 1-20. Furthermore, the p-value for Ljung-Box test is 0.6, indicating that there is little evidence of non-zero autocorrelations at lags 1-20.<\/p>\n<p>We can check whether the forecast errors have constant variance over time, and are normally distributed with mean zero, by making a time plot of the forecast errors and a histogram (with overlaid normal curve):<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>souvenirtimeseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span>            <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make a time plot<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plotForecastErrors<span class=\"p\">(<\/span>souvenirtimeseriesforecasts2<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span> <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make a histogram<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image25.png\" alt=\"image25\" \/>\u00a0<img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image26.png\" alt=\"image26\" \/><\/p>\n<p>From the time plot, it appears plausible that the forecast errors have constant variance over time. From the histogram of forecast errors, it seems plausible that the forecast errors are normally distributed with mean zero.<\/p>\n<p>Thus,there is little evidence of autocorrelation at lags 1-20 for the forecast errors, and the forecast errors appear to be normally distributed with mean zero and constant variance over time. This suggests that Holt-Winters exponential smoothing provides an adequate predictive model of the log of sales at the souvenir shop, which probably cannot be improved upon. Furthermore, the assumptions upon which the prediction intervals were based are probably valid.<\/p>\n<\/div>\n<\/div>\n<div id=\"arima-models\" class=\"section\" style=\"color: #000000;\">\n<h2>ARIMA Models<\/h2>\n<p>Exponential smoothing methods are useful for making forecasts, and make no assumptions about the correlations between successive values of the time series. However, if you want to make prediction intervals for forecasts made using exponential smoothing methods, the prediction intervals require that the forecast errors are uncorrelated and are normally distributed with mean zero and constant variance.<\/p>\n<p>While exponential smoothing methods do not make any assumptions about correlations between successive values of the time series, in some cases you can make a better predictive model by taking correlations in the data into account. Autoregressive Integrated Moving Average (ARIMA) models include an explicit statistical model for the irregular component of a time series, that allows for non-zero autocorrelations in the irregular component.<\/p>\n<div id=\"differencing-a-time-series\" class=\"section\">\n<h3>Differencing a Time Series<\/h3>\n<p>ARIMA models are defined for stationary time series. Therefore, if you start off with a non-stationary time series, you will first need to \u2018difference\u2019 the time series until you obtain a stationary time series. If you have to difference the time series d times to obtain a stationary series, then you have an ARIMA(p,d,q) model, where d is the order of differencing used.<\/p>\n<p>You can difference a time series using the \u201cdiff()\u201d function in R. For example, the time series of the annual diameter of women\u2019s skirts at the hem, from 1866 to 1911 is not stationary in mean, as the level changes a lot over time:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image14.png\" alt=\"image14\" \/><\/p>\n<p>We can difference the time series (which we stored in \u201cskirtsseries\u201d, see above) once, and plot the differenced series, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> skirtsseriesdiff1 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> diff<span class=\"p\">(<\/span>skirtsseries<span class=\"p\">,<\/span> differences<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>skirtsseriesdiff1<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image27.png\" alt=\"image27\" \/><\/p>\n<p>The resulting time series of first differences (above) does not appear to be stationary in mean. Therefore, we can difference the time series twice, to see if that gives us a stationary time series:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> skirtsseriesdiff2 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> diff<span class=\"p\">(<\/span>skirtsseries<span class=\"p\">,<\/span> differences<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">2<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>skirtsseriesdiff2<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image28.png\" alt=\"image28\" \/><\/p>\n<div class=\"sidebar\">\n<p class=\"first sidebar-title\" style=\"font-weight: bold;\">Formal tests for stationarity<\/p>\n<p class=\"last\">Formal tests for stationarity called \u201cunit root tests\u201d are available in the fUnitRoots package, available on CRAN, but will not be discussed here.<\/p>\n<\/div>\n<p>The time series of second differences (above) does appear to be stationary in mean and variance, as the level of the series stays roughly constant over time, and the variance of the series appears roughly constant over time. Thus, it appears that we need to difference the time series of the diameter of skirts twice in order to achieve a stationary series.<\/p>\n<p>If you need to difference your original time series data d times in order to obtain a stationary time series, this means that you can use an ARIMA(p,d,q) model for your time series, where d is the order of differencing used. For example, for the time series of the diameter of women\u2019s skirts, we had to difference the time series twice, and so the order of differencing (d) is 2. This means that you can use an ARIMA(p,2,q) model for your time series. The next step is to figure out the values of p and q for the ARIMA model.<\/p>\n<p>Another example is the time series of the age of death of the successive kings of England (see above):<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image1.png\" alt=\"image1\" \/><\/p>\n<p>From the time plot (above), we can see that the time series is not stationary in mean. To calculate the time series of first differences, and plot it, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kingtimeseriesdiff1 <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> diff<span class=\"p\">(<\/span>kingstimeseries<span class=\"p\">,<\/span> differences<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>kingtimeseriesdiff1<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image29.png\" alt=\"image29\" \/><\/p>\n<p>The time series of first differences appears to be stationary in mean and variance, and so an ARIMA(p,1,q) model is probably appropriate for the time series of the age of death of the kings of England. By taking the time series of first differences, we have removed the trend component of the time series of the ages at death of the kings, and are left with an irregular component. We can now examine whether there are correlations between successive terms of this irregular component; if so, this could help us to make a predictive model for the ages at death of the kings.<\/p>\n<\/div>\n<div id=\"selecting-a-candidate-arima-model\" class=\"section\">\n<h3>Selecting a Candidate ARIMA Model<\/h3>\n<p>If your time series is stationary, or if you have transformed it to a stationary time series by differencing d times, the next step is to select the appropriate ARIMA model, which means finding the values of most appropriate values of p and q for an ARIMA(p,d,q) model. To do this, you usually need to examine the correlogram and partial correlogram of the stationary time series.<\/p>\n<p>To plot a correlogram and partial correlogram, we can use the \u201cacf()\u201d and \u201cpacf()\u201d functions in R, respectively. To get the actual values of the autocorrelations and partial autocorrelations, we set \u201cplot=FALSE\u201d in the \u201cacf()\u201d and \u201cpacf()\u201d functions.<\/p>\n<div id=\"example-of-the-ages-at-death-of-the-kings-of-england\" class=\"section\">\n<h4>Example of the Ages at Death of the Kings of England<\/h4>\n<p>For example, to plot the correlogram for lags 1-20 of the once differenced time series of the ages at death of the kings of England, and to get the values of the autocorrelations, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> acf<span class=\"p\">(<\/span>kingtimeseriesdiff1<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">)<\/span>             <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># plot a correlogram<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> acf<span class=\"p\">(<\/span>kingtimeseriesdiff1<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> plot<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">)<\/span> <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># get the autocorrelation values<\/span>\n  Autocorrelations of series <span class=\"s\" style=\"color: #4070a0;\">'kingtimeseriesdiff1'<\/span><span class=\"p\">,<\/span> by lag\n     <span class=\"m\" style=\"color: #208050;\">0<\/span>      <span class=\"m\" style=\"color: #208050;\">1<\/span>      <span class=\"m\" style=\"color: #208050;\">2<\/span>      <span class=\"m\" style=\"color: #208050;\">3<\/span>      <span class=\"m\" style=\"color: #208050;\">4<\/span>      <span class=\"m\" style=\"color: #208050;\">5<\/span>      <span class=\"m\" style=\"color: #208050;\">6<\/span>      <span class=\"m\" style=\"color: #208050;\">7<\/span>      <span class=\"m\" style=\"color: #208050;\">8<\/span>      <span class=\"m\" style=\"color: #208050;\">9<\/span>     <span class=\"m\" style=\"color: #208050;\">10<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1.000<\/span> <span class=\"m\" style=\"color: #208050;\">-0.360<\/span> <span class=\"m\" style=\"color: #208050;\">-0.162<\/span> <span class=\"m\" style=\"color: #208050;\">-0.050<\/span>  <span class=\"m\" style=\"color: #208050;\">0.227<\/span> <span class=\"m\" style=\"color: #208050;\">-0.042<\/span> <span class=\"m\" style=\"color: #208050;\">-0.181<\/span>  <span class=\"m\" style=\"color: #208050;\">0.095<\/span>  <span class=\"m\" style=\"color: #208050;\">0.064<\/span> <span class=\"m\" style=\"color: #208050;\">-0.116<\/span> <span class=\"m\" style=\"color: #208050;\">-0.071<\/span>\n     <span class=\"m\" style=\"color: #208050;\">11<\/span>     <span class=\"m\" style=\"color: #208050;\">12<\/span>     <span class=\"m\" style=\"color: #208050;\">13<\/span>     <span class=\"m\" style=\"color: #208050;\">14<\/span>     <span class=\"m\" style=\"color: #208050;\">15<\/span>     <span class=\"m\" style=\"color: #208050;\">16<\/span>     <span class=\"m\" style=\"color: #208050;\">17<\/span>     <span class=\"m\" style=\"color: #208050;\">18<\/span>     <span class=\"m\" style=\"color: #208050;\">19<\/span>     <span class=\"m\" style=\"color: #208050;\">20<\/span>\n  <span class=\"m\" style=\"color: #208050;\">0.206<\/span> <span class=\"m\" style=\"color: #208050;\">-0.017<\/span> <span class=\"m\" style=\"color: #208050;\">-0.212<\/span>  <span class=\"m\" style=\"color: #208050;\">0.130<\/span>  <span class=\"m\" style=\"color: #208050;\">0.114<\/span> <span class=\"m\" style=\"color: #208050;\">-0.009<\/span> <span class=\"m\" style=\"color: #208050;\">-0.192<\/span>  <span class=\"m\" style=\"color: #208050;\">0.072<\/span>  <span class=\"m\" style=\"color: #208050;\">0.113<\/span> <span class=\"m\" style=\"color: #208050;\">-0.093<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image30.png\" alt=\"image30\" \/><\/p>\n<p>We see from the correlogram that the autocorrelation at lag 1 (-0.360) exceeds the significance bounds, but all other autocorrelations between lags 1-20 do not exceed the significance bounds.<\/p>\n<p>To plot the partial correlogram for lags 1-20 for the once differenced time series of the ages at death of the English kings, and get the values of the partial autocorrelations, we use the \u201cpacf()\u201d function, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> pacf<span class=\"p\">(<\/span>kingtimeseriesdiff1<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">)<\/span>             <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># plot a partial correlogram<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> pacf<span class=\"p\">(<\/span>kingtimeseriesdiff1<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> plot<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">)<\/span> <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># get the partial autocorrelation values<\/span>\n  Partial autocorrelations of series <span class=\"s\" style=\"color: #4070a0;\">'kingtimeseriesdiff1'<\/span><span class=\"p\">,<\/span> by lag\n    <span class=\"m\" style=\"color: #208050;\">1<\/span>      <span class=\"m\" style=\"color: #208050;\">2<\/span>      <span class=\"m\" style=\"color: #208050;\">3<\/span>      <span class=\"m\" style=\"color: #208050;\">4<\/span>      <span class=\"m\" style=\"color: #208050;\">5<\/span>      <span class=\"m\" style=\"color: #208050;\">6<\/span>      <span class=\"m\" style=\"color: #208050;\">7<\/span>      <span class=\"m\" style=\"color: #208050;\">8<\/span>      <span class=\"m\" style=\"color: #208050;\">9<\/span>     <span class=\"m\" style=\"color: #208050;\">10<\/span>     <span class=\"m\" style=\"color: #208050;\">11<\/span>\n  <span class=\"m\" style=\"color: #208050;\">-0.360<\/span> <span class=\"m\" style=\"color: #208050;\">-0.335<\/span> <span class=\"m\" style=\"color: #208050;\">-0.321<\/span>  <span class=\"m\" style=\"color: #208050;\">0.005<\/span>  <span class=\"m\" style=\"color: #208050;\">0.025<\/span> <span class=\"m\" style=\"color: #208050;\">-0.144<\/span> <span class=\"m\" style=\"color: #208050;\">-0.022<\/span> <span class=\"m\" style=\"color: #208050;\">-0.007<\/span> <span class=\"m\" style=\"color: #208050;\">-0.143<\/span> <span class=\"m\" style=\"color: #208050;\">-0.167<\/span>  <span class=\"m\" style=\"color: #208050;\">0.065<\/span>\n    <span class=\"m\" style=\"color: #208050;\">12<\/span>     <span class=\"m\" style=\"color: #208050;\">13<\/span>     <span class=\"m\" style=\"color: #208050;\">14<\/span>     <span class=\"m\" style=\"color: #208050;\">15<\/span>     <span class=\"m\" style=\"color: #208050;\">16<\/span>     <span class=\"m\" style=\"color: #208050;\">17<\/span>     <span class=\"m\" style=\"color: #208050;\">18<\/span>     <span class=\"m\" style=\"color: #208050;\">19<\/span>     <span class=\"m\" style=\"color: #208050;\">20<\/span>\n   <span class=\"m\" style=\"color: #208050;\">0.034<\/span> <span class=\"m\" style=\"color: #208050;\">-0.161<\/span>  <span class=\"m\" style=\"color: #208050;\">0.036<\/span>  <span class=\"m\" style=\"color: #208050;\">0.066<\/span>  <span class=\"m\" style=\"color: #208050;\">0.081<\/span> <span class=\"m\" style=\"color: #208050;\">-0.005<\/span> <span class=\"m\" style=\"color: #208050;\">-0.027<\/span> <span class=\"m\" style=\"color: #208050;\">-0.006<\/span> <span class=\"m\" style=\"color: #208050;\">-0.037<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image31.png\" alt=\"image31\" \/><\/p>\n<p>The partial correlogram shows that the partial autocorrelations at lags 1, 2 and 3 exceed the significance bounds, are negative, and are slowly decreasing in magnitude with increasing lag (lag 1: -0.360, lag 2: -0.335, lag 3:-0.321). The partial autocorrelations tail off to zero after lag 3.<\/p>\n<p>Since the correlogram is zero after lag 1, and the partial correlogram tails off to zero after lag 3, this means that the following ARMA (autoregressive moving average) models are possible for the time series of first differences:<\/p>\n<ul class=\"simple\">\n<li>an ARMA(3,0) model, that is, an autoregressive model of order p=3, since the partial autocorrelogram is zero after lag 3, and the autocorrelogram tails off to zero (although perhaps too abruptly for this model to be appropriate)<\/li>\n<li>an ARMA(0,1) model, that is, a moving average model of order q=1, since the autocorrelogram is zero after lag 1 and the partial autocorrelogram tails off to zero<\/li>\n<li>an ARMA(p,q) model, that is, a mixed model with p and q greater than 0, since the autocorrelogram and partial correlogram tail off to zero (although the correlogram probably tails off to zero too abruptly for this model to be appropriate)<\/li>\n<\/ul>\n<p>We use the principle of parsimony to decide which model is best: that is, we assume that the model with the fewest parameters is best. The ARMA(3,0) model has 3 parameters, the ARMA(0,1) model has 1 parameter, and the ARMA(p,q) model has at least 2 parameters. Therefore, the ARMA(0,1) model is taken as the best model.<\/p>\n<p>An ARMA(0,1) model is a moving average model of order 1, or MA(1) model. This model can be written as: X_t &#8211; mu = Z_t &#8211; (theta * Z_t-1), where X_t is the stationary time series we are studying (the first differenced series of ages at death of English kings), mu is the mean of time series X_t, Z_t is white noise with mean zero and constant variance, and theta is a parameter that can be estimated.<\/p>\n<p>A MA (moving average) model is usually used to model a time series that shows short-term dependencies between successive observations. Intuitively, it makes good sense that a MA model can be used to describe the irregular component in the time series of ages at death of English kings, as we might expect the age at death of a particular English king to have some effect on the ages at death of the next king or two, but not much effect on the ages at death of kings that reign much longer after that.<\/p>\n<div class=\"sidebar\">\n<p class=\"first sidebar-title\" style=\"font-weight: bold;\">Shortcut: the auto.arima() function<\/p>\n<p class=\"last\">The auto.arima() function can be used to find the appropriate ARIMA model, eg., type \u201clibrary(forecast)\u201d, then \u201cauto.arima(kings)\u201d. The output says an appropriate model is ARIMA(0,1,1).<\/p>\n<\/div>\n<p>Since an ARMA(0,1) model (with p=0, q=1) is taken to be the best candidate model for the time series of first differences of the ages at death of English kings, then the original time series of the ages of death can be modelled using an ARIMA(0,1,1) model (with p=0, d=1, q=1, where d is the order of differencing required).<\/p>\n<\/div>\n<div id=\"example-of-the-volcanic-dust-veil-in-the-northern-hemisphere\" class=\"section\">\n<h4>Example of the Volcanic Dust Veil in the Northern Hemisphere<\/h4>\n<p>Let\u2019s take another example of selecting an appropriate ARIMA model. The file file<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/robjhyndman.com\/tsdldata\/annual\/dvi.dat\">http:\/\/robjhyndman.com\/tsdldata\/annual\/dvi.dat<\/a>\u00a0contains data on the volcanic dust veil index in the northern hemisphere, from 1500-1969 (original data from Hipel and Mcleod, 1994). This is a measure of the impact of volcanic eruptions\u2019 release of dust and aerosols into the environment. We can read it into R and make a time plot by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> volcanodust <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> scan<span class=\"p\">(<\/span><span class=\"s\" style=\"color: #4070a0;\">\"http:\/\/robjhyndman.com\/tsdldata\/annual\/dvi.dat\"<\/span><span class=\"p\">,<\/span> skip<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">)<\/span>\n  Read <span class=\"m\" style=\"color: #208050;\">470<\/span> items\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> volcanodustseries <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> ts<span class=\"p\">(<\/span>volcanodust<span class=\"p\">,<\/span>start<span class=\"o\" style=\"color: #666666;\">=<\/span>c<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">1500<\/span><span class=\"p\">))<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>volcanodustseries<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image32.png\" alt=\"image32\" \/><\/p>\n<p>From the time plot, it appears that the random fluctuations in the time series are roughly constant in size over time, so an additive model is probably appropriate for describing this time series.<\/p>\n<p>Furthermore, the time series appears to be stationary in mean and variance, as its level and variance appear to be roughly constant over time. Therefore, we do not need to difference this series in order to fit an ARIMA model, but can fit an ARIMA model to the original series (the order of differencing required, d, is zero here).<\/p>\n<p>We can now plot a correlogram and partial correlogram for lags 1-20 to investigate what ARIMA model to use:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> acf<span class=\"p\">(<\/span>volcanodustseries<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">)<\/span>             <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># plot a correlogram<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> acf<span class=\"p\">(<\/span>volcanodustseries<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> plot<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">)<\/span> <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># get the values of the autocorrelations<\/span>\n  Autocorrelations of series <span class=\"s\" style=\"color: #4070a0;\">'volcanodustseries'<\/span><span class=\"p\">,<\/span> by lag\n    <span class=\"m\" style=\"color: #208050;\">0<\/span>      <span class=\"m\" style=\"color: #208050;\">1<\/span>      <span class=\"m\" style=\"color: #208050;\">2<\/span>      <span class=\"m\" style=\"color: #208050;\">3<\/span>      <span class=\"m\" style=\"color: #208050;\">4<\/span>      <span class=\"m\" style=\"color: #208050;\">5<\/span>      <span class=\"m\" style=\"color: #208050;\">6<\/span>      <span class=\"m\" style=\"color: #208050;\">7<\/span>      <span class=\"m\" style=\"color: #208050;\">8<\/span>      <span class=\"m\" style=\"color: #208050;\">9<\/span>     <span class=\"m\" style=\"color: #208050;\">10<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1.000<\/span>  <span class=\"m\" style=\"color: #208050;\">0.666<\/span>  <span class=\"m\" style=\"color: #208050;\">0.374<\/span>  <span class=\"m\" style=\"color: #208050;\">0.162<\/span>  <span class=\"m\" style=\"color: #208050;\">0.046<\/span>  <span class=\"m\" style=\"color: #208050;\">0.017<\/span> <span class=\"m\" style=\"color: #208050;\">-0.007<\/span>  <span class=\"m\" style=\"color: #208050;\">0.016<\/span>  <span class=\"m\" style=\"color: #208050;\">0.021<\/span>  <span class=\"m\" style=\"color: #208050;\">0.006<\/span>  <span class=\"m\" style=\"color: #208050;\">0.010<\/span>\n    <span class=\"m\" style=\"color: #208050;\">11<\/span>     <span class=\"m\" style=\"color: #208050;\">12<\/span>     <span class=\"m\" style=\"color: #208050;\">13<\/span>     <span class=\"m\" style=\"color: #208050;\">14<\/span>     <span class=\"m\" style=\"color: #208050;\">15<\/span>     <span class=\"m\" style=\"color: #208050;\">16<\/span>     <span class=\"m\" style=\"color: #208050;\">17<\/span>     <span class=\"m\" style=\"color: #208050;\">18<\/span>     <span class=\"m\" style=\"color: #208050;\">19<\/span>     <span class=\"m\" style=\"color: #208050;\">20<\/span>\n  <span class=\"m\" style=\"color: #208050;\">0.004<\/span>  <span class=\"m\" style=\"color: #208050;\">0.024<\/span>  <span class=\"m\" style=\"color: #208050;\">0.075<\/span>  <span class=\"m\" style=\"color: #208050;\">0.082<\/span>  <span class=\"m\" style=\"color: #208050;\">0.064<\/span>  <span class=\"m\" style=\"color: #208050;\">0.039<\/span>  <span class=\"m\" style=\"color: #208050;\">0.005<\/span>  <span class=\"m\" style=\"color: #208050;\">0.028<\/span>  <span class=\"m\" style=\"color: #208050;\">0.108<\/span>  <span class=\"m\" style=\"color: #208050;\">0.182<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image33.png\" alt=\"image33\" \/><\/p>\n<p>We see from the correlogram that the autocorrelations for lags 1, 2 and 3 exceed the significance bounds, and that the autocorrelations tail off to zero after lag 3. The autocorrelations for lags 1, 2, 3 are positive, and decrease in magnitude with increasing lag (lag 1: 0.666, lag 2: 0.374, lag 3: 0.162).<\/p>\n<p>The autocorrelation for lags 19 and 20 exceed the significance bounds too, but it is likely that this is due to chance, since they just exceed the significance bounds (especially for lag 19), the autocorrelations for lags 4-18 do not exceed the signifiance bounds, and we would expect 1 in 20 lags to exceed the 95% significance bounds by chance alone.<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> pacf<span class=\"p\">(<\/span>volcanodustseries<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> pacf<span class=\"p\">(<\/span>volcanodustseries<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> plot<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"kc\" style=\"font-weight: bold; color: #007020;\">FALSE<\/span><span class=\"p\">)<\/span>\n  Partial autocorrelations of series <span class=\"s\" style=\"color: #4070a0;\">'volcanodustseries'<\/span><span class=\"p\">,<\/span> by lag\n    <span class=\"m\" style=\"color: #208050;\">1<\/span>      <span class=\"m\" style=\"color: #208050;\">2<\/span>      <span class=\"m\" style=\"color: #208050;\">3<\/span>      <span class=\"m\" style=\"color: #208050;\">4<\/span>      <span class=\"m\" style=\"color: #208050;\">5<\/span>      <span class=\"m\" style=\"color: #208050;\">6<\/span>      <span class=\"m\" style=\"color: #208050;\">7<\/span>      <span class=\"m\" style=\"color: #208050;\">8<\/span>      <span class=\"m\" style=\"color: #208050;\">9<\/span>     <span class=\"m\" style=\"color: #208050;\">10<\/span>     <span class=\"m\" style=\"color: #208050;\">11<\/span>\n  <span class=\"m\" style=\"color: #208050;\">0.666<\/span> <span class=\"m\" style=\"color: #208050;\">-0.126<\/span> <span class=\"m\" style=\"color: #208050;\">-0.064<\/span> <span class=\"m\" style=\"color: #208050;\">-0.005<\/span>  <span class=\"m\" style=\"color: #208050;\">0.040<\/span> <span class=\"m\" style=\"color: #208050;\">-0.039<\/span>  <span class=\"m\" style=\"color: #208050;\">0.058<\/span> <span class=\"m\" style=\"color: #208050;\">-0.016<\/span> <span class=\"m\" style=\"color: #208050;\">-0.025<\/span>  <span class=\"m\" style=\"color: #208050;\">0.028<\/span> <span class=\"m\" style=\"color: #208050;\">-0.008<\/span>\n    <span class=\"m\" style=\"color: #208050;\">12<\/span>     <span class=\"m\" style=\"color: #208050;\">13<\/span>     <span class=\"m\" style=\"color: #208050;\">14<\/span>     <span class=\"m\" style=\"color: #208050;\">15<\/span>     <span class=\"m\" style=\"color: #208050;\">16<\/span>     <span class=\"m\" style=\"color: #208050;\">17<\/span>     <span class=\"m\" style=\"color: #208050;\">18<\/span>     <span class=\"m\" style=\"color: #208050;\">19<\/span>     <span class=\"m\" style=\"color: #208050;\">20<\/span>\n  <span class=\"m\" style=\"color: #208050;\">0.036<\/span>  <span class=\"m\" style=\"color: #208050;\">0.082<\/span> <span class=\"m\" style=\"color: #208050;\">-0.025<\/span> <span class=\"m\" style=\"color: #208050;\">-0.014<\/span>  <span class=\"m\" style=\"color: #208050;\">0.008<\/span> <span class=\"m\" style=\"color: #208050;\">-0.025<\/span>  <span class=\"m\" style=\"color: #208050;\">0.073<\/span>  <span class=\"m\" style=\"color: #208050;\">0.131<\/span>  <span class=\"m\" style=\"color: #208050;\">0.063<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image34.png\" alt=\"image34\" \/><\/p>\n<p>From the partial autocorrelogram, we see that the partial autocorrelation at lag 1 is positive and exceeds the significance bounds (0.666), while the partial autocorrelation at lag 2 is negative and also exceeds the significance bounds (-0.126). The partial autocorrelations tail off to zero after lag 2.<\/p>\n<p>Since the correlogram tails off to zero after lag 3, and the partial correlogram is zero after lag 2, the following ARMA models are possible for the time series:<\/p>\n<ul class=\"simple\">\n<li>an ARMA(2,0) model, since the partial autocorrelogram is zero after lag 2, and the correlogram tails off to zero after lag 3, and the partial correlogram is zero after lag 2<\/li>\n<li>an ARMA(0,3) model, since the autocorrelogram is zero after lag 3, and the partial correlogram tails off to zero (although perhaps too abruptly for this model to be appropriate)<\/li>\n<li>an ARMA(p,q) mixed model, since the correlogram and partial correlogram tail off to zero (although the partial correlogram perhaps tails off too abruptly for this model to be appropriate)<\/li>\n<\/ul>\n<div class=\"sidebar\">\n<p class=\"first sidebar-title\" style=\"font-weight: bold;\">Shortcut: the auto.arima() function<\/p>\n<p class=\"last\">Again, we can use auto.arima() to find an appropriate model, by typing \u201cauto.arima(volcanodust)\u201d, which gives us ARIMA(1,0,2), which has 3 parameters. However, different criteria can be used to select a model (see auto.arima() help page). If we use the \u201cbic\u201d criterion, which penalises the number of parameters, we get ARIMA(2,0,0), which is ARMA(2,0): \u201cauto.arima(volcanodust,ic=\u201dbic\u201d)\u201d.<\/p>\n<\/div>\n<p>The ARMA(2,0) model has 2 parameters, the ARMA(0,3) model has 3 parameters, and the ARMA(p,q) model has at least 2 parameters. Therefore, using the principle of parsimony, the ARMA(2,0) model and ARMA(p,q) model are equally good candidate models.<\/p>\n<p>An ARMA(2,0) model is an autoregressive model of order 2, or AR(2) model. This model can be written as: X_t &#8211; mu = (Beta1 * (X_t-1 &#8211; mu)) + (Beta2 * (Xt-2 &#8211; mu)) + Z_t, where X_t is the stationary time series we are studying (the time series of volcanic dust veil index), mu is the mean of time series X_t, Beta1 and Beta2 are parameters to be estimated, and Z_t is white noise with mean zero and constant variance.<\/p>\n<p>An AR (autoregressive) model is usually used to model a time series which shows longer term dependencies between successive observations. Intuitively, it makes sense that an AR model could be used to describe the time series of volcanic dust veil index, as we would expect volcanic dust and aerosol levels in one year to affect those in much later years, since the dust and aerosols are unlikely to disappear quickly.<\/p>\n<p>If an ARMA(2,0) model (with p=2, q=0) is used to model the time series of volcanic dust veil index, it would mean that an ARIMA(2,0,0) model can be used (with p=2, d=0, q=0, where d is the order of differencing required). Similarly, if an ARMA(p,q) mixed model is used, where p and q are both greater than zero, than an ARIMA(p,0,q) model can be used.<\/p>\n<\/div>\n<\/div>\n<div id=\"forecasting-using-an-arima-model\" class=\"section\">\n<h3>Forecasting Using an ARIMA Model<\/h3>\n<p>Once you have selected the best candidate ARIMA(p,d,q) model for your time series data, you can estimate the parameters of that ARIMA model, and use that as a predictive model for making forecasts for future values of your time series.<\/p>\n<p>You can estimate the parameters of an ARIMA(p,d,q) model using the \u201carima()\u201d function in R.<\/p>\n<div id=\"id2\" class=\"section\">\n<h4>Example of the Ages at Death of the Kings of England<\/h4>\n<p>For example, we discussed above that an ARIMA(0,1,1) model seems a plausible model for the ages at deaths of the kings of England. You can specify the values of p, d and q in the ARIMA model by using the \u201corder\u201d argument of the \u201carima()\u201d function in R. To fit an ARIMA(p,d,q) model to this time series (which we stored in the variable \u201ckingstimeseries\u201d, see above), we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kingstimeseriesarima <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> arima<span class=\"p\">(<\/span>kingstimeseries<span class=\"p\">,<\/span> order<span class=\"o\" style=\"color: #666666;\">=<\/span>c<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">0<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">))<\/span> <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># fit an ARIMA(0,1,1) model<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kingstimeseriesarima\n  ARIMA<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">0<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">1<\/span><span class=\"p\">)<\/span>\n  Coefficients<span class=\"o\" style=\"color: #666666;\">:<\/span>\n          ma1\n        <span class=\"m\" style=\"color: #208050;\">-0.7218<\/span>\n  s.e.   <span class=\"m\" style=\"color: #208050;\">0.1208<\/span>\n  sigma<span class=\"o\" style=\"color: #666666;\">^<\/span><span class=\"m\" style=\"color: #208050;\">2<\/span> estimated as <span class=\"m\" style=\"color: #208050;\">230.4<\/span><span class=\"o\" style=\"color: #666666;\">:<\/span>  log likelihood <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">-170.06<\/span>\n  AIC <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">344.13<\/span>   AICc <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">344.44<\/span>   BIC <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">347.56<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>As mentioned above, if we are fitting an ARIMA(0,1,1) model to our time series, it means we are fitting an an ARMA(0,1) model to the time series of first differences. An ARMA(0,1) model can be written X_t &#8211; mu = Z_t &#8211; (theta * Z_t-1), where theta is a parameter to be estimated. From the output of the \u201carima()\u201d R function (above), the estimated value of theta (given as \u2018ma1\u2019 in the R output) is -0.7218 in the case of the ARIMA(0,1,1) model fitted to the time series of ages at death of kings.<\/p>\n<div class=\"sidebar\">\n<p class=\"first sidebar-title\" style=\"font-weight: bold;\">Specifying the confidence level for prediction intervals<\/p>\n<p class=\"last\">You can specify the confidence level for prediction intervals in forecast.Arima() by using the \u201clevel\u201d argument. For example, to get a 99.5% prediction interval, we would type \u201cforecast.Arima(kingstimeseriesarima, h=5, level=c(99.5))\u201d.<\/p>\n<\/div>\n<p>We can then use the ARIMA model to make forecasts for future values of the time series, using the \u201cforecast.Arima()\u201d function in the \u201cforecast\u201d R package. For example, to forecast the ages at death of the next five English kings, we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> library<span class=\"p\">(<\/span><span class=\"s\" style=\"color: #4070a0;\">\"forecast\"<\/span><span class=\"p\">)<\/span> <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># load the \"forecast\" R library<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kingstimeseriesforecasts <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> forecast.Arima<span class=\"p\">(<\/span>kingstimeseriesarima<span class=\"p\">,<\/span> h<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">5<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> kingstimeseriesforecasts\n     Point Forecast    Lo <span class=\"m\" style=\"color: #208050;\">80<\/span>    Hi <span class=\"m\" style=\"color: #208050;\">80<\/span>    Lo <span class=\"m\" style=\"color: #208050;\">95<\/span>     Hi <span class=\"m\" style=\"color: #208050;\">95<\/span>\n  <span class=\"m\" style=\"color: #208050;\">43<\/span>       <span class=\"m\" style=\"color: #208050;\">67.75063<\/span> <span class=\"m\" style=\"color: #208050;\">48.29647<\/span> <span class=\"m\" style=\"color: #208050;\">87.20479<\/span> <span class=\"m\" style=\"color: #208050;\">37.99806<\/span>  <span class=\"m\" style=\"color: #208050;\">97.50319<\/span>\n  <span class=\"m\" style=\"color: #208050;\">44<\/span>       <span class=\"m\" style=\"color: #208050;\">67.75063<\/span> <span class=\"m\" style=\"color: #208050;\">47.55748<\/span> <span class=\"m\" style=\"color: #208050;\">87.94377<\/span> <span class=\"m\" style=\"color: #208050;\">36.86788<\/span>  <span class=\"m\" style=\"color: #208050;\">98.63338<\/span>\n  <span class=\"m\" style=\"color: #208050;\">45<\/span>       <span class=\"m\" style=\"color: #208050;\">67.75063<\/span> <span class=\"m\" style=\"color: #208050;\">46.84460<\/span> <span class=\"m\" style=\"color: #208050;\">88.65665<\/span> <span class=\"m\" style=\"color: #208050;\">35.77762<\/span>  <span class=\"m\" style=\"color: #208050;\">99.72363<\/span>\n  <span class=\"m\" style=\"color: #208050;\">46<\/span>       <span class=\"m\" style=\"color: #208050;\">67.75063<\/span> <span class=\"m\" style=\"color: #208050;\">46.15524<\/span> <span class=\"m\" style=\"color: #208050;\">89.34601<\/span> <span class=\"m\" style=\"color: #208050;\">34.72333<\/span> <span class=\"m\" style=\"color: #208050;\">100.77792<\/span>\n  <span class=\"m\" style=\"color: #208050;\">47<\/span>       <span class=\"m\" style=\"color: #208050;\">67.75063<\/span> <span class=\"m\" style=\"color: #208050;\">45.48722<\/span> <span class=\"m\" style=\"color: #208050;\">90.01404<\/span> <span class=\"m\" style=\"color: #208050;\">33.70168<\/span> <span class=\"m\" style=\"color: #208050;\">101.79958<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>The original time series for the English kings includes the ages at death of 42 English kings. The forecast.Arima() function gives us a forecast of the age of death of the next five English kings (kings 43-47), as well as 80% and 95% prediction intervals for those predictions. The age of death of the 42nd English king was 56 years (the last observed value in our time series), and the ARIMA model gives the forecasted age at death of the next five kings as 67.8 years.<\/p>\n<p>We can plot the observed ages of death for the first 42 kings, as well as the ages that would be predicted for these 42 kings and for the next 5 kings using our ARIMA(0,1,1) model, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.forecast<span class=\"p\">(<\/span>kingstimeseriesforecasts<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image35.png\" alt=\"image35\" \/><\/p>\n<p>As in the case of exponential smoothing models, it is a good idea to investigate whether the forecast errors of an ARIMA model are normally distributed with mean zero and constant variance, and whether the are correlations between successive forecast errors.<\/p>\n<p>For example, we can make a correlogram of the forecast errors for our ARIMA(0,1,1) model for the ages at death of kings, and perform the Ljung-Box test for lags 1-20, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> acf<span class=\"p\">(<\/span>kingstimeseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> Box.test<span class=\"p\">(<\/span>kingstimeseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> type<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"s\" style=\"color: #4070a0;\">\"Ljung-Box\"<\/span><span class=\"p\">)<\/span>\n  Box<span class=\"o\" style=\"color: #666666;\">-<\/span>Ljung test\n  data<span class=\"o\" style=\"color: #666666;\">:<\/span>  kingstimeseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals\n  X<span class=\"o\" style=\"color: #666666;\">-<\/span>squared <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">13.5844<\/span><span class=\"p\">,<\/span> df <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> p<span class=\"o\" style=\"color: #666666;\">-<\/span>value <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">0.851<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image36.png\" alt=\"image36\" \/><\/p>\n<p>Since the correlogram shows that none of the sample autocorrelations for lags 1-20 exceed the significance bounds, and the p-value for the Ljung-Box test is 0.9, we can conclude that there is very little evidence for non-zero autocorrelations in the forecast errors at lags 1-20.<\/p>\n<p>To investigate whether the forecast errors are normally distributed with mean zero and constant variance, we can make a time plot and histogram (with overlaid normal curve) of the forecast errors:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>kingstimeseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span>            <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make time plot of forecast errors<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plotForecastErrors<span class=\"p\">(<\/span>kingstimeseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span> <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make a histogram<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image37.png\" alt=\"image37\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image38.png\" alt=\"image38\" \/><\/p>\n<p>The time plot of the in-sample forecast errors shows that the variance of the forecast errors seems to be roughly constant over time (though perhaps there is slightly higher variance for the second half of the time series). The histogram of the time series shows that the forecast errors are roughly normally distributed and the mean seems to be close to zero. Therefore, it is plausible that the forecast errors are normally distributed with mean zero and constant variance.<\/p>\n<p>Since successive forecast errors do not seem to be correlated, and the forecast errors seem to be normally distributed with mean zero and constant variance, the ARIMA(0,1,1) does seem to provide an adequate predictive model for the ages at death of English kings.<\/p>\n<\/div>\n<div id=\"id3\" class=\"section\">\n<h4>Example of the Volcanic Dust Veil in the Northern Hemisphere<\/h4>\n<p>We discussed above that an appropriate ARIMA model for the time series of volcanic dust veil index may be an ARIMA(2,0,0) model. To fit an ARIMA(2,0,0) model to this time series, we can type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> volcanodustseriesarima <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> arima<span class=\"p\">(<\/span>volcanodustseries<span class=\"p\">,<\/span> order<span class=\"o\" style=\"color: #666666;\">=<\/span>c<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">2<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">0<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">0<\/span><span class=\"p\">))<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> volcanodustseriesarima\n  ARIMA<span class=\"p\">(<\/span><span class=\"m\" style=\"color: #208050;\">2<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">0<\/span><span class=\"p\">,<\/span><span class=\"m\" style=\"color: #208050;\">0<\/span><span class=\"p\">)<\/span> with non<span class=\"o\" style=\"color: #666666;\">-<\/span>zero mean\n  Coefficients<span class=\"o\" style=\"color: #666666;\">:<\/span>\n     ar1      ar2  intercept\n  <span class=\"m\" style=\"color: #208050;\">0.7533<\/span>  <span class=\"m\" style=\"color: #208050;\">-0.1268<\/span>    <span class=\"m\" style=\"color: #208050;\">57.5274<\/span>\n  s.e.  <span class=\"m\" style=\"color: #208050;\">0.0457<\/span>   <span class=\"m\" style=\"color: #208050;\">0.0458<\/span>     <span class=\"m\" style=\"color: #208050;\">8.5958<\/span>\n  sigma<span class=\"o\" style=\"color: #666666;\">^<\/span><span class=\"m\" style=\"color: #208050;\">2<\/span> estimated as <span class=\"m\" style=\"color: #208050;\">4870<\/span><span class=\"o\" style=\"color: #666666;\">:<\/span>  log likelihood <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">-2662.54<\/span>\n  AIC <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">5333.09<\/span>   AICc <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">5333.17<\/span>   BIC <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">5349.7<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>As mentioned above, an ARIMA(2,0,0) model can be written as: written as: X_t &#8211; mu = (Beta1 * (X_t-1 &#8211; mu)) + (Beta2 * (Xt-2 &#8211; mu)) + Z_t, where Beta1 and Beta2 are parameters to be estimated. The output of the arima() function tells us that Beta1 and Beta2 are estimated as 0.7533 and -0.1268 here (given as ar1 and ar2 in the output of arima()).<\/p>\n<p>Now we have fitted the ARIMA(2,0,0) model, we can use the \u201cforecast.ARIMA()\u201d model to predict future values of the volcanic dust veil index. The original data includes the years 1500-1969. To make predictions for the years 1970-2000 (31 more years), we type:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> volcanodustseriesforecasts <span class=\"o\" style=\"color: #666666;\">&lt;-<\/span> forecast.Arima<span class=\"p\">(<\/span>volcanodustseriesarima<span class=\"p\">,<\/span> h<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">31<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> volcanodustseriesforecasts\n  Point      Forecast     Lo <span class=\"m\" style=\"color: #208050;\">80<\/span>    Hi <span class=\"m\" style=\"color: #208050;\">80<\/span>     Lo <span class=\"m\" style=\"color: #208050;\">95<\/span>    Hi <span class=\"m\" style=\"color: #208050;\">95<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1970<\/span>       <span class=\"m\" style=\"color: #208050;\">21.48131<\/span> <span class=\"m\" style=\"color: #208050;\">-67.94860<\/span> <span class=\"m\" style=\"color: #208050;\">110.9112<\/span> <span class=\"m\" style=\"color: #208050;\">-115.2899<\/span> <span class=\"m\" style=\"color: #208050;\">158.2526<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1971<\/span>       <span class=\"m\" style=\"color: #208050;\">37.66419<\/span> <span class=\"m\" style=\"color: #208050;\">-74.30305<\/span> <span class=\"m\" style=\"color: #208050;\">149.6314<\/span> <span class=\"m\" style=\"color: #208050;\">-133.5749<\/span> <span class=\"m\" style=\"color: #208050;\">208.9033<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1972<\/span>       <span class=\"m\" style=\"color: #208050;\">47.13261<\/span> <span class=\"m\" style=\"color: #208050;\">-71.57070<\/span> <span class=\"m\" style=\"color: #208050;\">165.8359<\/span> <span class=\"m\" style=\"color: #208050;\">-134.4084<\/span> <span class=\"m\" style=\"color: #208050;\">228.6737<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1973<\/span>       <span class=\"m\" style=\"color: #208050;\">52.21432<\/span> <span class=\"m\" style=\"color: #208050;\">-68.35951<\/span> <span class=\"m\" style=\"color: #208050;\">172.7881<\/span> <span class=\"m\" style=\"color: #208050;\">-132.1874<\/span> <span class=\"m\" style=\"color: #208050;\">236.6161<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1974<\/span>       <span class=\"m\" style=\"color: #208050;\">54.84241<\/span> <span class=\"m\" style=\"color: #208050;\">-66.22681<\/span> <span class=\"m\" style=\"color: #208050;\">175.9116<\/span> <span class=\"m\" style=\"color: #208050;\">-130.3170<\/span> <span class=\"m\" style=\"color: #208050;\">240.0018<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1975<\/span>       <span class=\"m\" style=\"color: #208050;\">56.17814<\/span> <span class=\"m\" style=\"color: #208050;\">-65.01872<\/span> <span class=\"m\" style=\"color: #208050;\">177.3750<\/span> <span class=\"m\" style=\"color: #208050;\">-129.1765<\/span> <span class=\"m\" style=\"color: #208050;\">241.5327<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1976<\/span>       <span class=\"m\" style=\"color: #208050;\">56.85128<\/span> <span class=\"m\" style=\"color: #208050;\">-64.37798<\/span> <span class=\"m\" style=\"color: #208050;\">178.0805<\/span> <span class=\"m\" style=\"color: #208050;\">-128.5529<\/span> <span class=\"m\" style=\"color: #208050;\">242.2554<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1977<\/span>       <span class=\"m\" style=\"color: #208050;\">57.18907<\/span> <span class=\"m\" style=\"color: #208050;\">-64.04834<\/span> <span class=\"m\" style=\"color: #208050;\">178.4265<\/span> <span class=\"m\" style=\"color: #208050;\">-128.2276<\/span> <span class=\"m\" style=\"color: #208050;\">242.6057<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1978<\/span>       <span class=\"m\" style=\"color: #208050;\">57.35822<\/span> <span class=\"m\" style=\"color: #208050;\">-63.88124<\/span> <span class=\"m\" style=\"color: #208050;\">178.5977<\/span> <span class=\"m\" style=\"color: #208050;\">-128.0615<\/span> <span class=\"m\" style=\"color: #208050;\">242.7780<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1979<\/span>       <span class=\"m\" style=\"color: #208050;\">57.44283<\/span> <span class=\"m\" style=\"color: #208050;\">-63.79714<\/span> <span class=\"m\" style=\"color: #208050;\">178.6828<\/span> <span class=\"m\" style=\"color: #208050;\">-127.9777<\/span> <span class=\"m\" style=\"color: #208050;\">242.8634<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1980<\/span>       <span class=\"m\" style=\"color: #208050;\">57.48513<\/span> <span class=\"m\" style=\"color: #208050;\">-63.75497<\/span> <span class=\"m\" style=\"color: #208050;\">178.7252<\/span> <span class=\"m\" style=\"color: #208050;\">-127.9356<\/span> <span class=\"m\" style=\"color: #208050;\">242.9059<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1981<\/span>       <span class=\"m\" style=\"color: #208050;\">57.50627<\/span> <span class=\"m\" style=\"color: #208050;\">-63.73386<\/span> <span class=\"m\" style=\"color: #208050;\">178.7464<\/span> <span class=\"m\" style=\"color: #208050;\">-127.9145<\/span> <span class=\"m\" style=\"color: #208050;\">242.9271<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1982<\/span>       <span class=\"m\" style=\"color: #208050;\">57.51684<\/span> <span class=\"m\" style=\"color: #208050;\">-63.72330<\/span> <span class=\"m\" style=\"color: #208050;\">178.7570<\/span> <span class=\"m\" style=\"color: #208050;\">-127.9040<\/span> <span class=\"m\" style=\"color: #208050;\">242.9376<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1983<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52212<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71802<\/span> <span class=\"m\" style=\"color: #208050;\">178.7623<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8987<\/span> <span class=\"m\" style=\"color: #208050;\">242.9429<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1984<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52476<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71538<\/span> <span class=\"m\" style=\"color: #208050;\">178.7649<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8960<\/span> <span class=\"m\" style=\"color: #208050;\">242.9456<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1985<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52607<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71407<\/span> <span class=\"m\" style=\"color: #208050;\">178.7662<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8947<\/span> <span class=\"m\" style=\"color: #208050;\">242.9469<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1986<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52673<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71341<\/span> <span class=\"m\" style=\"color: #208050;\">178.7669<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8941<\/span> <span class=\"m\" style=\"color: #208050;\">242.9475<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1987<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52706<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71308<\/span> <span class=\"m\" style=\"color: #208050;\">178.7672<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8937<\/span> <span class=\"m\" style=\"color: #208050;\">242.9479<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1988<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52723<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71291<\/span> <span class=\"m\" style=\"color: #208050;\">178.7674<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8936<\/span> <span class=\"m\" style=\"color: #208050;\">242.9480<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1989<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52731<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71283<\/span> <span class=\"m\" style=\"color: #208050;\">178.7674<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8935<\/span> <span class=\"m\" style=\"color: #208050;\">242.9481<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1990<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52735<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71279<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9481<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1991<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52737<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71277<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1992<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52738<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71276<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1993<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52739<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71275<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1994<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52739<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71275<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1995<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52739<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71275<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1996<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52739<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71275<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1997<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52739<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71275<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1998<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52739<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71275<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n  <span class=\"m\" style=\"color: #208050;\">1999<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52739<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71275<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n  <span class=\"m\" style=\"color: #208050;\">2000<\/span>       <span class=\"m\" style=\"color: #208050;\">57.52739<\/span> <span class=\"m\" style=\"color: #208050;\">-63.71275<\/span> <span class=\"m\" style=\"color: #208050;\">178.7675<\/span> <span class=\"m\" style=\"color: #208050;\">-127.8934<\/span> <span class=\"m\" style=\"color: #208050;\">242.9482<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>We can plot the original time series, and the forecasted values, by typing:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.forecast<span class=\"p\">(<\/span>volcanodustseriesforecasts<span class=\"p\">)<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image39.png\" alt=\"image39\" \/><\/p>\n<p>One worrying thing is that the model has predicted negative values for the volcanic dust veil index, but this variable can only have positive values! The reason is that the arima() and forecast.Arima() functions don\u2019t know that the variable can only take positive values. Clearly, this is not a very desirable feature of our current predictive model.<\/p>\n<p>Again, we should investigate whether the forecast errors seem to be correlated, and whether they are normally distributed with mean zero and constant variance. To check for correlations between successive forecast errors, we can make a correlogram and use the Ljung-Box test:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> acf<span class=\"p\">(<\/span>volcanodustseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag.max<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">)<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> Box.test<span class=\"p\">(<\/span>volcanodustseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">,<\/span> lag<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> type<span class=\"o\" style=\"color: #666666;\">=<\/span><span class=\"s\" style=\"color: #4070a0;\">\"Ljung-Box\"<\/span><span class=\"p\">)<\/span>\n  Box<span class=\"o\" style=\"color: #666666;\">-<\/span>Ljung test\n  data<span class=\"o\" style=\"color: #666666;\">:<\/span>  volcanodustseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals\n  X<span class=\"o\" style=\"color: #666666;\">-<\/span>squared <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">24.3642<\/span><span class=\"p\">,<\/span> df <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">20<\/span><span class=\"p\">,<\/span> p<span class=\"o\" style=\"color: #666666;\">-<\/span>value <span class=\"o\" style=\"color: #666666;\">=<\/span> <span class=\"m\" style=\"color: #208050;\">0.2268<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image40.png\" alt=\"image40\" \/><\/p>\n<p>The correlogram shows that the sample autocorrelation at lag 20 exceeds the significance bounds. However, this is probably due to chance, since we would expect one out of 20 sample autocorrelations to exceed the 95% significance bounds. Furthermore, the p-value for the Ljung-Box test is 0.2, indicating that there is little evidence for non-zero autocorrelations in the forecast errors for lags 1-20.<\/p>\n<p>To check whether the forecast errors are normally distributed with mean zero and constant variance, we make a time plot of the forecast errors, and a histogram:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plot.ts<span class=\"p\">(<\/span>volcanodustseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span>            <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make time plot of forecast errors<\/span>\n<span class=\"o\" style=\"color: #666666;\">&gt;<\/span> plotForecastErrors<span class=\"p\">(<\/span>volcanodustseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span> <span class=\"c1\" style=\"font-style: italic; color: #408090;\"># make a histogram<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image41.png\" alt=\"image41\" \/><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/a-little-book-of-r-for-time-series.readthedocs.org\/en\/latest\/_images\/image42.png\" alt=\"image42\" \/><\/p>\n<p>The time plot of forecast errors shows that the forecast errors seem to have roughly constant variance over time. However, the time series of forecast errors seems to have a negative mean, rather than a zero mean. We can confirm this by calculating the mean forecast error, which turns out to be about -0.22:<\/p>\n<div class=\"highlight-r\">\n<div class=\"highlight\">\n<pre><span class=\"o\" style=\"color: #666666;\">&gt;<\/span> mean<span class=\"p\">(<\/span>volcanodustseriesforecasts<span class=\"o\" style=\"color: #666666;\">$<\/span>residuals<span class=\"p\">)<\/span>\n  <span class=\"m\" style=\"color: #208050;\">-0.2205417<\/span>\n<\/pre>\n<\/div>\n<\/div>\n<p>The histogram of forecast errors (above) shows that although the mean value of the forecast errors is negative, the distribution of forecast errors is skewed to the right compared to a normal curve. Therefore, it seems that we cannot comfortably conclude that the forecast errors are normally distributed with mean zero and constant variance! Thus, it is likely that our ARIMA(2,0,0) model for the time series of volcanic dust veil index is not the best model that we could make, and could almost definitely be improved upon!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div id=\"links-and-further-reading\" class=\"section\" style=\"color: #000000;\">\n<h2>Links and Further Reading<\/h2>\n<p>Here are some links for further reading.<\/p>\n<p>For a more in-depth introduction to R, a good online tutorial is available on the \u201cKickstarting R\u201d website,<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/cran.r-project.org\/doc\/contrib\/Lemon-kickstart\/\">cran.r-project.org\/doc\/contrib\/Lemon-kickstart<\/a>.<\/p>\n<p>There is another nice (slightly more in-depth) tutorial to R available on the \u201cIntroduction to R\u201d website,\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/cran.r-project.org\/doc\/manuals\/R-intro.html\">cran.r-project.org\/doc\/manuals\/R-intro.html<\/a>.<\/p>\n<p>You can find a list of R packages for analysing time series data on the\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/cran.r-project.org\/web\/views\/TimeSeries.html\">CRAN Time Series Task View webpage<\/a>.<\/p>\n<p>To learn about time series analysis, I would highly recommend the book \u201cTime series\u201d (product code M249\/02) by the Open University, available from\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/www.ouw.co.uk\/store\/\">the Open University Shop<\/a>.<\/p>\n<p>There are two books available in the \u201cUse R!\u201d series on using R for time series analyses, the first is\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/www.springer.com\/statistics\/statistical+theory+and+methods\/book\/978-0-387-88697-8\">Introductory Time Series with R<\/a>\u00a0by Cowpertwait and Metcalfe, and the second is\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/www.springer.com\/statistics\/statistical+theory+and+methods\/book\/978-0-387-75966-1\">Analysis of Integrated and Cointegrated Time Series with R<\/a>\u00a0by Pfaff.<\/p>\n<\/div>\n<div id=\"acknowledgements\" class=\"section\" style=\"color: #000000;\">\n<h2>Acknowledgements<\/h2>\n<p>I am grateful to\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/robjhyndman.com\/\">Professor Rob Hyndman<\/a>, for kindly allowing me to use the time series data sets from his\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/robjhyndman.com\/TSDL\/\">Time Series Data Library (TSDL)<\/a>\u00a0in the examples in this booklet.<\/p>\n<p>Many of the examples in this booklet are inspired by examples in the excellent Open University book, \u201cTime series\u201d (product code M249\/02), available from\u00a0<a class=\"reference external\" style=\"color: #ca7900;\" href=\"http:\/\/www.ouw.co.uk\/store\/\">the Open University Shop<\/a>.<\/p>\n<p>Thank you to Ravi Aranke for bringing auto.arima() to my attention, and Maurice Omane-Adjepong for bringing unit root tests to my attention, and Christian Seubert for noticing a small bug in plotForecastErrors(). Thank you for other comments to Antoine Binard and Bill Johnston.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Time Series Analysis This booklet itells you how to use the R statistical software to carry out some simple analyses that are common in analysing&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-663","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=663"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/663\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}