## START OF FILE ############################# # # Southern Methodist University - ECO 5375 # # This R code accompanies the first lecture # that motivates forecasting. # # Christoffer Koch # # August 28, 2017 ############################################## # clear your workspace rm(list = ls()) ## you might want to set your working directory # setwd('c:/your/path/') # getwd() ## install packages - only required once, comment out afterwards ## recall that in R-Studio CTRL+SHIFT+C ## comments out (Windows) # install.packages("TSA") # install.packages("pdfetch") # install.packages("forecast") # install.packages("fpp") # install.packages("Hmisc) # loading the libraries library(TSA) library(pdfetch) library(forecast) library(fpp) library(Hmis) # Graphics - Air Passengers plot(melsyd[,"Economy.Class"], main="Economy class passengers: Melbourne-Sydney", xlab="Year",ylab="Thousands",col="blue",las =1, lwd = 2) # Graphics - Drug Sales plot(a10, ylab="\$ million", las = 1, col = "blue", xlab="Year", main="Antidiabetic drug sales", lwd = 2) # Graphics - Seasonal Plot seasonplot(a10, ylab="\$ million", xlab="Year", main="Seasonal plot: antidiabetic drug sales", year.labels=TRUE, year.labels.left=TRUE, col=1:20, pch=19) # Univariate Statistics - Cars Example subset(fuel, Litres<2)[, c(1,3,5,6,8)] fuel2 <- fuel[fuel\$Litres<2,] summary(fuel2[,"Carbon"]) sd(fuel2[,"Carbon"]) # Bivariate Statistics - Correlation Coefficient cor(fuel2[,"Carbon"], fuel2[,"City"]) # Bivariate Statistics - Autocorrelation beer2 <- window(ausbeer, start=1992, end=2006-.1) lag.plot(beer2, lags=9, do.lines=FALSE) # Autocorrelation Function Acf(beer2, las = 1, lwd = 2, main = "Autocorrelation Function") # Discussing the Simple Methods beer2 <- window(ausbeer,start=1992,end=2006-.1) beerfit1 <- meanf(beer2, h=11) beerfit2 <- naive(beer2, h=11) beerfit3 <- snaive(beer2, h=11) plot(beerfit1,plot.conf=FALSE,main="Forecasts for quarterly beer production", las = 1, lwd = 2) legend("bottomleft",lty=1,col=c(4),lwd=c(2),inset = 0.02, box.col = "white", legend=c("1. Mean Method")) minor.tick(nx = 5, ny =5) ## Discussing the Simple Methods beer2 <- window(ausbeer,start=1992,end=2006-.1) beerfit1 <- meanf(beer2, h=11) beerfit2 <- naive(beer2, h=11) beerfit3 <- snaive(beer2, h=11) plot(beerfit1,plot.conf=FALSE,main="Forecasts for quarterly beer production", las = 1, lwd = 2) lines(beerfit2\$mean,col=2, lwd = 2) legend("bottomleft",lty=1,col=c(4,2),lwd=c(2,2),inset = 0.02, box.col = "white", legend=c("1. Mean Method","2. Naive Method")) minor.tick(nx = 5, ny =5) # Discussing the Simple Methods beer2 <- window(ausbeer,start=1992,end=2006-.1) beerfit1 <- meanf(beer2, h=11) beerfit2 <- naive(beer2, h=11) beerfit3 <- snaive(beer2, h=11) plot(beerfit1,plot.conf=FALSE,main="Forecasts for quarterly beer production", las = 1, lwd = 2, col = "black") lines(beerfit2\$mean,col=2,lwd = 2) lines(beerfit3\$mean,col=3, lwd = 2) legend("bottomleft",lty=1,col=c(4,2,3),lwd=c(2,2,2),inset = 0.02, box.col = "white", legend=c("1. Mean Method","2. Naive Method","3. Seasonal Naive Method")) minor.tick(nx = 5, ny =5) # Plano City Manager Planning Problem plano <- ts(read.table(file = "./data/plano.csv"),start = c(1990,2),freq = 12) plano = plano/1000 plot(plano, las = 1, type = "l", col = "blue", lwd = 2, main = "Plano Sales Tax Revenue (in \$1'000)", ylab="US\$") minor.tick(nx = 5, ny =5) ## END OF FILE ##