程序代写 ETW3420: Principles of Forecasting and Applications

ETW3420: Principles of Forecasting and Applications

Principles of

Copyright By PowCoder代写 加微信 powcoder

Forecasting and
Applications
Topic 2: Time Series Graphics

1 Time series in R

2 Time plots

3 Seasonal plots

4 Seasonal or cyclic?

5 Lag plots and autocorrelation

6 White noise

ts objects and ts function

A time series is stored in a ts object in R:
a list of numbers
information about times those numbers were recorded.

## Year Observation
## 1 2012 123
## 2 2013 39
## 3 2014 78
## 4 2015 52
## 5 2016 110

y <- ts(c(123,39,78,52,110), start=2012) ts objects and ts function For observations that are more frequent than once per year, add a frequency argument. E.g., monthly data stored as a numerical vector z: y <- ts(z, frequency=12, start=c(2003, 1)) ts objects and ts function ts(data, frequency, start) Type of data frequency start example Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 c(1995,9) Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 c(1995,9) Daily 7 or 365.25 Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 c(1995,9) Daily 7 or 365.25 1 or c(1995,234) Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 c(1995,9) Daily 7 or 365.25 1 or c(1995,234) Weekly 52.18 Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 c(1995,9) Daily 7 or 365.25 1 or c(1995,234) Weekly 52.18 c(1995,23) Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 c(1995,9) Daily 7 or 365.25 1 or c(1995,234) Weekly 52.18 c(1995,23) Hourly 24 or 168 or 8,766 Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 c(1995,9) Daily 7 or 365.25 1 or c(1995,234) Weekly 52.18 c(1995,23) Hourly 24 or 168 or 8,766 1 Half-hourly ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 c(1995,9) Daily 7 or 365.25 1 or c(1995,234) Weekly 52.18 c(1995,23) Hourly 24 or 168 or 8,766 1 Half-hourly 48 or 336 or 17,532 ts objects and ts function ts(data, frequency, start) Type of data frequency start example Annual 1 1995 Quarterly 4 c(1995,2) Monthly 12 c(1995,9) Daily 7 or 365.25 1 or c(1995,234) Weekly 52.18 c(1995,23) Hourly 24 or 168 or 8,766 1 Half-hourly 48 or 336 or 17,532 1 Australian GDP ausgdp <- ts(x, frequency=4, start=c(1971,3)) Class: “ts” Print and plotting methods available. ## Qtr1 Qtr2 Qtr3 Qtr4 ## 1971 4612 4651 ## 1972 4645 4615 4645 4722 ## 1973 4780 4830 4887 4933 ## 1974 4921 4875 4867 4905 ## 1975 4938 4934 4942 4979 ## 1976 5028 5079 5112 5127 ## 1977 5130 5101 5072 5069 ## 1978 5100 5166 5244 5312 ## 1979 5349 5370 5388 5396 ## 1980 5388 5403 5442 5482 ## 1981 5506 5531 5560 5583 ## 1982 5568 5524 5452 5358 ## 1983 5303 5320 5408 5531 ## 1984 5624 5669 5697 5736 ## 1985 5811 5894 5952 5965 ## 1986 5943 5924 5935 5979 ## 1987 6035 6097 6167 6227 ## 1988 6256 6272 6295 6345 ## 1989 6413 6468 6497 6511 ## 1990 6514 6512 6490 6442 ## 1991 6390 6346 6328 6340 ## 1992 6362 6389 6433 6491 ## 1993 6541 6566 6602 6671 ## 1994 6765 6847 6890 6918 ## 1995 6962 7018 7083 7134 ## 1996 7173 7212 7242 7276 ## 1997 7332 7400 7478 7550 ## 1998 7618 Australian GDP autoplot(ausgdp) 1975 1980 1985 1990 1995 Residential electricity sales ## Time Series: ## Start = 1989 ## End = 2008 ## Frequency = 1 ## [1] 2354.34 2379.71 2318.52 2468.99 2386.09 2569.47 2575.72 2762.72 ## [9] 2844.50 3000.70 3108.10 3357.50 3075.70 3180.60 3221.60 3176.20 ## [17] 3430.60 3527.48 3637.89 3655.00 Class package > library(fpp2)

This loads:

some data for use in examples and exercises
forecast package (for forecasting functions)
ggplot2 package (for graphics functions)
fma package (for lots of time series data)
expsmooth package (for more time series data)

Class package

> library(fpp2)

This loads:

some data for use in examples and exercises

forecast package (for forecasting functions)
ggplot2 package (for graphics functions)
fma package (for lots of time series data)
expsmooth package (for more time series data)

Class package

> library(fpp2)

This loads:

some data for use in examples and exercises
forecast package (for forecasting functions)
ggplot2 package (for graphics functions)
fma package (for lots of time series data)
expsmooth package (for more time series data)

1 Time series in R

2 Time plots

3 Seasonal plots

4 Seasonal or cyclic?

5 Lag plots and autocorrelation

6 White noise

Time plots

autoplot(melsyd[,”Economy.Class”])

1988 1989 1990 1991 1992 1993

Time plots

autoplot(a10) + ylab(“$ million”) + xlab(“Year”) +

ggtitle(“Antidiabetic drug sales”)

1995 2000 2005

Antidiabetic drug sales

Time plots

autoplot(elecdaily[,”Temperature”]) +

xlab(“Week”) + ylab(“Max temperature”)

0 10 20 30 40 50

Self-Practice

Create plots of the following time series: dole, bricksq,
lynx, goog
Use help() to find out about the data in each series.

1 Time series in R

2 Time plots

3 Seasonal plots

4 Seasonal or cyclic?

5 Lag plots and autocorrelation

6 White noise

Seasonal plots

ggseasonplot(a10, year.labels=TRUE, year.labels.left=TRUE) +
ylab(“$ million”) +
ggtitle(“Seasonal plot: antidiabetic drug sales”)

19931994 19941995

1996 19961997

Mar Apr Jul Aug Sep Oct Nov Dec

Seasonal plot: antidiabetic drug sales

Seasonal plots

Data plotted against the individual “seasons” in which the
data were observed. (In this case a “season” is a month.)
Something like a time plot except that the data from each
season are overlapped.
Enables the underlying seasonal pattern to be seen more
clearly, and also allows any substantial departures from the
seasonal pattern to be easily identified.
In R: ggseasonplot()

Seasonal polar plots

ggseasonplot(a10, polar=TRUE) + ylab(“$ million”)

Seasonal plot: a10

Seasonal subseries plots

ggsubseriesplot(a10) + ylab(“$ million”) +

ggtitle(“Subseries plot: antidiabetic drug sales”)

Mar Apr Jul Aug Sep Oct Nov Dec

Subseries plot: antidiabetic drug sales

Seasonal subseries plots

Data for each season collected together in time plot as
separate time series.
Enables the underlying seasonal pattern to be seen clearly,
and changes in seasonality over time to be visualized.
In R: ggsubseriesplot()

Quarterly Australian Beer Production

beer <- window(ausbeer,start=1992) autoplot(beer) 1995 2000 2005 2010 Quarterly Australian Beer Production ggseasonplot(beer,year.labels=TRUE) Q1 Q2 Q3 Q4 Seasonal plot: beer Quarterly Australian Beer Production ggsubseriesplot(beer) Q1 Q2 Q3 Q4 Self-Practice The arrivals data set comprises quarterly international arrivals (in thousands) to Australia from Japan, , UK and the Use autoplot() and ggseasonplot() to compare the differences between the arrivals from these four countries. Can you identify any unusual observations? 1 Time series in R 2 Time plots 3 Seasonal plots 4 Seasonal or cyclic? 5 Lag plots and autocorrelation 6 White noise Time series patterns Trend pattern exists when there is a long-term increase or decrease in the data. Seasonal pattern exists when a series is influenced by seasonal factors (e.g., the quarter of the year, the month, or day of the week). Cyclic pattern exists when data exhibit rises and falls that are not of fixed period (duration usually of at least 2 Time series components Differences between seasonal and cyclic patterns: seasonal pattern constant length; cyclic pattern variable average length of cycle longer than length of seasonal pattern magnitude of cycle more variable than magnitude of seasonal Time series patterns autoplot(window(elec, start=1980)) + ggtitle("Australian electricity production") + xlab("Year") + ylab("GWh") 1980 1985 1990 1995 Australian electricity production Time series patterns autoplot(bricksq) + ggtitle("Australian clay brick production") + xlab("Year") + ylab("million units") 1960 1970 1980 1990 Australian clay brick production Time series patterns autoplot(hsales) + ggtitle("Sales of new one-family houses, USA") + xlab("Year") + ylab("Total sales") 1975 1980 1985 1990 1995 Sales of new one−family houses, USA Time series patterns autoplot(ustreas) + ggtitle("US Treasury ") + xlab("Day") + ylab("price") 0 20 40 60 80 100 US Treasury Time series patterns autoplot(lynx) + ggtitle("Annual Canadian Lynx Trappings") + xlab("Year") + ylab("Number trapped") 1820 1840 1860 1880 1900 1920 Annual Canadian Lynx Trappings Seasonal or cyclic? Differences between seasonal and cyclic patterns: seasonal pattern constant length; cyclic pattern variable average length of cycle longer than length of seasonal pattern magnitude of cycle more variable than magnitude of seasonal The timing of peaks and troughs is predictable with seasonal data, but unpredictable in the long term with cyclic data. Seasonal or cyclic? Differences between seasonal and cyclic patterns: seasonal pattern constant length; cyclic pattern variable average length of cycle longer than length of seasonal pattern magnitude of cycle more variable than magnitude of seasonal The timing of peaks and troughs is predictable with seasonal data, but unpredictable in the long term with cyclic data. 1 Time series in R 2 Time plots 3 Seasonal plots 4 Seasonal or cyclic? 5 Lag plots and autocorrelation 6 White noise Example: Beer production beer <- window(ausbeer, start=1992) gglagplot(beer, do.lines = F) Example: Beer production lag 7 lag 8 lag 9 lag 4 lag 5 lag 6 lag 1 lag 2 lag 3 400 450 500 400 450 500 400 450 500 Lagged scatterplots Each graph shows yt plotted against yt−k for different values The autocorrelations are the correlations associated with these scatterplots. Autocorrelation Covariance and correlation: measure extent of linear relationship between two variables (y and X). Autocovariance and autocorrelation: measure linear relationship between lagged values of a time series y. We measure the relationship between: yt and yt−1 yt and yt−2 yt and yt−3 Autocorrelation Covariance and correlation: measure extent of linear relationship between two variables (y and X). Autocovariance and autocorrelation: measure linear relationship between lagged values of a time series y. We measure the relationship between: yt and yt−1 yt and yt−2 yt and yt−3 Autocorrelation Covariance and correlation: measure extent of linear relationship between two variables (y and X). Autocovariance and autocorrelation: measure linear relationship between lagged values of a time series y. We measure the relationship between: yt and yt−1 yt and yt−2 yt and yt−3 Autocorrelation We denote the sample autocovariance at lag k by ck and the sample autocorrelation at lag k by rk. Then define (yt − ȳ)(yt−k − ȳ) and rk = ck/c0 r1 indicates how successive values of y relate to each other r2 indicates how y values two periods apart relate to each other rk is almost the same as the sample correlation between yt and Autocorrelation We denote the sample autocovariance at lag k by ck and the sample autocorrelation at lag k by rk. Then define (yt − ȳ)(yt−k − ȳ) and rk = ck/c0 r1 indicates how successive values of y relate to each other r2 indicates how y values two periods apart relate to each other rk is almost the same as the sample correlation between yt and Autocorrelation Results for first 9 lags for beer data: ggAcf(beer) Series: beer Autocorrelation r4 higher than for the other lags. This is due to the seasonal pattern in the data: the peaks tend to be 4 quarters apart and the troughs tend to be 2 quarters apart. r2 is more negative than for the other lags because troughs tend to be 2 quarters behind peaks. Together, the autocorrelations at lags 1, 2, . . . , make up the autocorrelation or ACF. The plot is known as a correlogram ggAcf(beer) Series: beer Trend and seasonality in ACF plots When data have a trend, the autocorrelations for small lags tend to be large and positive. When data are seasonal, the autocorrelations will be larger at the seasonal lags (i.e., at multiples of the seasonal frequency) When data are trended and seasonal, you see a combination of these effects. Aus monthly electricity production elec2 <- window(elec, start=1980) autoplot(elec2) 1980 1985 1990 1995 Aus monthly electricity production ggAcf(elec2, lag.max=48) 0 12 24 36 48 Series: elec2 Aus monthly electricity production Time plot shows clear trend and seasonality. The same features are reflected in the ACF. The slowly decaying ACF indicates trend. The ACF peaks at lags 12, 24, 36, . . . , indicate seasonality of length 12. Google stock price autoplot(goog) 0 200 400 600 800 1000 Google stock price ggAcf(goog, lag.max=100) 0 20 40 60 80 100 Series: goog Self-Practice We have introduced the following graphics functions: Explore the following time series using these functions. Can you spot any seasonality, cyclicity and trend? What do you learn about the series? sunspotarea Which is which? 0 20 40 60 1. Daily temperature of cow 1974 1976 1978 2. Monthly accidental deaths 1950 1952 1954 1956 1958 1960 3. Monthly air passengers 1860 1880 1900 4. Annual mink trappings 1 Time series in R 2 Time plots 3 Seasonal plots 4 Seasonal or cyclic? 5 Lag plots and autocorrelation 6 White noise Example: White noise wn <- ts(rnorm(36)) autoplot(wn) 0 10 20 30 Example: White noise Sample autocorrelations for white noise series. We expect each autocorrelation to be close to zero. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Series: wn Sampling distribution of autocorrelations Sampling distribution of rk for white noise data is asymptotically 95% of all rk for white noise must lie within±1.96/ If this is not the case, the series is probably not WN. Common to plot lines at±1.96/ T when plotting ACF. These are the critical values. Sampling distribution of autocorrelations Sampling distribution of rk for white noise data is asymptotically 95% of all rk for white noise must lie within±1.96/ If this is not the case, the series is probably not WN. Common to plot lines at±1.96/ T when plotting ACF. These are the critical values. Autocorrelation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Series: wnExample: T = 36 and so critical values 36 = ±0.327. All autocorrelation coefficients lie within these limits, confirming that the data are white noise. (More precisely, the data cannot be distinguished from white noise.) Example: Pigs slaughtered pigs2 <- window(pigs, start=1990) autoplot(pigs2) + xlab("Year") + ylab("thousands") + ggtitle("Number of pigs slaughtered in Victoria") 1990 1991 1992 1993 1994 1995 Number of pigs slaughtered in Victoria Example: Pigs slaughtered ggAcf(pigs2) Series: pigs2 Example: Pigs slaughtered Monthly total number of pigs slaughtered in the state of Victoria, Australia, from January 1990 through August 1995. (Source: Australian Bureau of Statistics.) Difficult to detect pattern in time plot. ACF shows some significant autocorrelation at lags 1, 2, and 3. r12 relatively large although not significant. This may indicate some slight seasonality. These show the series is not a white noise series. Example: Pigs slaughtered Monthly total number of pigs slaughtered in the state of Victoria, Australia, from January 1990 through August 1995. (Source: Australian Bureau of Statistics.) Difficult to detect pattern in time plot. ACF shows some significant autocorrelation at lags 1, 2, and 3. r12 relatively large although not significant. This may indicate some slight seasonality. These show the series is not a white noise series. Example: Pigs slaughtered Monthly total number of pigs slaughtered in the state of Victoria, Australia, from January 1990 through August 1995. (Source: Australian Bureau of Statistics.) Difficult to detect pattern in time plot. ACF shows some significant autocorrelation at lags 1, 2, and 3. r12 relatively large although not significant. This may indicate some slight seasonality. 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com