Please complete each problem below to the best of your ability. Where plain text is required, you can type directly into the .RMD file. Where code and output is required, be sure to include all code in the code chunks provided. The assignment must be submitted, via email, as both the .RMD file and the knitted file (whether .html or .pdf, whichever is best for yourself)
Run through the “Decomposition in R” section from Lecture 1.Rmd
Copy and paste each code block from that lecture below. Run each code chunk and ensure they run correctly. Include the output here (the output will be automatically produced when you knit this document)
# Set your working directory below if it is not already set to the current 'Lecture Notes' directory.
# Your directory must be set to tell R where to find the data file for Maine monthly unemployment
#setwd()
Elec = read.csv("C:/Users/Wanth/OneDrive/Documents/CS-637-Time-Series-and-Forecasting--main/Lecture Notes/data/elec.csv")
Elec.ts <- ts(Elec, start = 1958, freq = 12)
Next, we visualize its additive decomposition
plot(decompose(Elec.ts))
Next, its multiplicative decomposition
Elec.decom <- decompose(Elec.ts, type = "mult")
plot(Elec.decom)
Finally, the trend multiplied by the seasonal, which does not account for randon noise
Trend <- Elec.decom$trend
Seasonal <- Elec.decom$seasonal
ts.plot(cbind(Trend, Trend * Seasonal), lty = 1:2)
Given the additive and the multiplicative decomposition models, and what we know about each (reference both the textbook and notes for more infor between the two), which decomposition model seems to be the best fit for the data?
##multiplicative, because the seasonal variation seems to increase over time.
Complete Chapter 1, Exercise 1 from the textbook. You can find both the beer.csv and the choc.csv files in the Lecture Notes/data folders.
##Carry out the following exploratory time series analysis in R using either the chocolate or the beer production data from §1.4.3.
#a) Produce a time plot of the data. Plot the aggregated annual series and a boxplot that summarises the observed values for each season, and comment on the plots.
Beer = read.csv("C:/CS-637-Time-Series-and-Forecasting--main/Lecture Notes/data/beer.csv")
Beer.ts <- ts(Beer, freq = 12)
plot(aggregate(Beer.ts))
boxplot(Beer.ts ~ cycle(Beer.ts))
##Beer production increases in the final quarter of the year
#### Part 1B
#b) Decompose the series into the components trend, seasonal effect, and residuals, and plot the decomposed series. Produce a plot of the trend with a superimposed seasonal effect.
#additive decomposition
plot(decompose(Beer.ts))
#multiplicative decomposition
Beer.decom <- decompose(Beer.ts, type = "mult")
plot(Elec.decom)
#trend multiplied by seasonal
Trend <- Beer.decom$trend
Seasonal <- Beer.decom$seasonal
ts.plot(cbind(Trend, Trend * Seasonal), lty = 1:2)
``