Beer-Solution R Studio

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Solution: Beer supply data

Explorative analysis of beer supply data

Read in the monthly beer supply data from the book website, and save it as a vector called beerdata:

www <- "http://asta.math.aau.dk/eng/static/datasets?file=cbe.dat"


CBEdata <- read.table(www, header = TRUE)
beerdata <- CBEdata$beer

Now convert it to a time series object (ts) with the correct starting date (Jan. 1958) and frequency and call
it beer:

beer <- ts(beerdata, start = 1958, freq = 12)

Now perform an explorative analysis like in the lecture:

• Plot the data, plot the aggregated yearly data and make a boxplot of the observations for each of the
twelve months. Comment on the figures.

plot(beer)
200
150
beer

100

1960 1965 1970 1975 1980 1985 1990

Time

beer_avg <- aggregate(beer, FUN = mean)


plot(beer_avg)

1
160
140
beer_avg

120
100

1960 1965 1970 1975 1980 1985 1990

Time

beer_month <- factor(cycle(beer), labels = month.abb)


boxplot(beer ~ beer_month)
200
150
100

Jan Mar May Jul Aug Oct Dec

• Use decompose to decompose the series into trend, seasonal and random component, and make relevant
figures and comment on them. Try also to use ts.plot to plot the seasonal component on top of the
trend (you can set argument lty = c(1,2) to plot with different line styles for the two series).

d <- decompose(beer)
plot(d)

2
Decomposition of additive time series
200
observed
100
140
trend
100
random seasonal
20
20 −20 0
−40 −10

1960 1965 1970 1975 1980 1985 1990

Time
m <- d$trend
s <- d$season
ts.plot(m+s, m, lty = c(2,1), lwd = c(1,2), col = c("black", "red"))
200
160
120
80
60

1960 1965 1970 1975 1980 1985 1990

Time

• Repeat the steps above for a multiplicative model for the decomposition (use decompose(beer,
"mult")) and repeat the figures for that (now you should multiply trend, seasonal and random to
recover the original series).

3
d_mult <- decompose(beer, type = "mult")
plot(d_mult)

Decomposition of multiplicative time series


200
observed
100
140
trend
1.2 100
random seasonal
1.0
0.8
1.2
1.0
0.8

1960 1965 1970 1975 1980 1985 1990

Time
m_mult <- d_mult$trend
s_mult <- d_mult$season
ts.plot(m_mult*s_mult, m_mult, lty = c(2,1), lwd = c(1,2), col = c("black", "red"))
200
160
120
80

1960 1965 1970 1975 1980 1985 1990

Time

You might also like