## Loading required package: ggplot2
## Loading required package: forecast
## Registered S3 method overwritten by 'xts':
## method from
## as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
## method from
## fitted.fracdiff fracdiff
## residuals.fracdiff fracdiff
## Loading required package: fma
## Loading required package: expsmooth
Please submit exercises 2.1, 2.2, 2.3 and 2.6 from the Hyndman online Forecasting book.
Please submit both your Rpubs link as well as attach the .rmd file with your code.
gold, woolyrnq and gas represent.## starting httpd help server ... done
gold.title <- "Daily morning gold prices in US dollars. 1 January 1985 – 31 March 1989."
### Woolyrnq
help(woolyrnq)
woolyrnq.title <- "Quarterly production of woollen yarn in Australia: tonnes. Mar 1965 – Sep 1994."
### Gas
help(gas)
gas.title <- "Australian monthly gas production: 1956–1995."gold series represents Daily morning gold prices in US dollars. 1 January 1985 – 31 March 1989.woolyrnq series represents Quarterly production of woollen yarn in Australia: tonnes. Mar 1965 – Sep 1994.gas series represents Australian monthly gas production: 1956–1995.autoplot() to plot each of these in separate plots.## [1] 1
## [1] 4
## [1] 12
gold is stored with frequency = 1, which suggests “Annual”, but the frequency is actually daily(Weekday), i.e, 5 days per week, or 260 observations per year.woolyrnq is 4, which corresponds to Quarterly.gas is 12, which corresponds to Monthly.## [1] 770
## [1] 593.7
NA values for holidays such as New Years Day, Good Friday, Easter Monday, Christmas, Boxing Day, and the Bank Holiday which occur on Mondays at the beginning and end of May, and at the end of August.ts requires data to be equally spaced.gold data set, of which 34 are NA .NA .)gold.days <- seq( as.Date("1985/1/2", format="%Y/%m/%d"),
as.Date("1989/3/31", format="%Y/%m/%d"),"days")
gold.weekdays <- gold.days[ ! weekdays(gold.days) %in% c("Saturday", "Sunday") ]
plot(x=gold.weekdays,y=gold,type="l",col="darkgreen",
xlab="Date(weekdays only)",main=gold.title)ts object while specifying 260 observations per year:gold.ts260 <- ts(gold,start=c(1985,2),frequency = 260)
autoplot(gold.ts260) + ggtitle(gold.title) + geom_line(color="blue")ts) would require expanding the dataset to include NA values for every Saturday and Sunday during the above time period, and would require 1551 rather than 1108 “observations”.tute1.csv from the book website, open it in Excel (or some other spreadsheet application), and review its contents. You should find four columns of information.Sales, AdBudget and GDP.Sales contains the quarterly sales for a small company over the period 1981-2005.AdBudget is the advertising budget andGDP is the gross domestic product.All series have been adjusted for inflation.
(The [,-1] removes the first column which contains the quarters as we don’t need them now.)
facets=TRUE.If omitting “facets=TRUE”, then all three series are plotted on the same axes. In this case because the values of the data do not intersect each other, the results can still be clearly observed. However, for other data sets with data of very different magnitudes, the result could be a graph which may not allow for easy viewing of the smallest data set.
#### readxl does not read straight from URL without local download
####retaildata <- readxl::read_excel("https://otexts.com/fpp2/extrafiles/retail.xlsx", skip=1)
retaildata <- readxl::read_excel("retail.xlsx", skip=1)
View(retaildata)The second argument (skip=1) is required because the Excel sheet has two header rows.
autoplot(), ggseasonplot(), ggsubseriesplot(), gglagplot(), ggAcf()
ggseasonplot(x=myts, year.labels=TRUE, year.labels.left=TRUE) +
ylab("A$ million") +
ggtitle(mymain)ggseasonplot(x=myts, polar=TRUE) +
ylab("A$ million") +
ggtitle(paste("Polar season plot: ",mycode))ggsubseriesmain <- paste(mycode, ": " , "Monthly from 1981 to 2005")
ggsubseriesplot(x=myts) +
ggtitle(ggsubseriesmain)gglagmain <- paste(mycode, ": ", "monthly lags")
gglagplot(x=myts,set.lags = 0:24)+
ylab("A$ million") +
ggtitle(gglagmain)autoplot(), ggseasonplot(), ggsubseriesplot(), gglagplot(), ggAcf()hsales, usdeaths, bricksq, sunspotarea, gasoline.hsales - Monthly sales of new one-family houses sold in the USA since 1973.usdeaths - Monthly accidental deaths in USA (1973-1978).mytitle <- "usdeaths: Monthly accidental deaths in USA (1973-1978)"
autoplot(usdeaths) + ggtitle(mytitle)bricksq - Australian quarterly clay brick production: 1956–1994.mytitle <- "bricksq: Australian quarterly clay brick production: 1956–1994"
autoplot(bricksq) + ggtitle(mytitle)sunspotarea - Annual average sunspot area (1875-2015).mytitle <- "sunspotarea: Annual average sunspot area (1875-2015)"
autoplot(sunspotarea) + ggtitle(mytitle)##ggseasonplot(sunspotarea, year.labels=TRUE, year.labels.left=TRUE) + ggtitle(mytitle)
## Error in ggseasonplot(sunspotarea, year.labels = TRUE, year.labels.left = TRUE) :
## Data are not seasonal
##ggsubseriesplot(sunspotarea) + ggtitle(mytitle)
## Error in ggsubseriesplot(sunspotarea) : Data are not seasonal
gglagplot(sunspotarea,set.lags = 0:24) + ggtitle(mytitle)ggseasonplot and ggsubseriesplot functions return errors.gasoline - US finished motor gasoline product supplied: 2/2/1991-1/20/2017 (weekly).mytitle <- "gasoline: US finished motor gasoline product supplied: 2/2/1991-1/20/2017 (weekly)"
autoplot(gasoline) + ggtitle(mytitle)## ggsubseriesplot(gasoline) + ggtitle(mytitle)
## Error in ggsubseriesplot(gasoline) : Each season requires at least 2 observations.
## This may be caused from specifying a time-series with non-integer frequency.
gglagplot(gasoline) + ggtitle(mytitle)ts object, it appears that the software is unable to map the \(n^{th}\) week of each calendar year, as it easily does with monthly or quarterly data.ts object indicates that the frequency is 52.1785714 . If adjustments were made, it might be possible to have the software treat the frequency as 52 rather than 52.1785714 .