0% found this document useful (0 votes)
4 views7 pages

MBAS921 Week 3 Tutorial Solution

The tutorial focuses on descriptive analytics through data plotting using R and Excel, specifically analyzing Monthly Retail Turnover data from April 1982 to June 2025. Key tasks include reading and cleaning data, creating various plots (bar charts, histograms, time series), and interpreting trends and seasonality in the data. Additionally, the tutorial introduces model fitting using STL decomposition to analyze the components of the time series data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

MBAS921 Week 3 Tutorial Solution

The tutorial focuses on descriptive analytics through data plotting using R and Excel, specifically analyzing Monthly Retail Turnover data from April 1982 to June 2025. Key tasks include reading and cleaning data, creating various plots (bar charts, histograms, time series), and interpreting trends and seasonality in the data. Additionally, the tutorial introduces model fitting using STL decomposition to analyze the components of the time series data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MBAS 921: Week 3 Tutorial

This week’s tutorial deals with the very first step in working with data: plotting it
(descriptive analytics)! In particular, we do the following:

i. An Excel file named “Tutorial_2.xlsx” is given. The file contains Monthly Retail
Turnover by state[1], for all months from April 1982 to June 2025. The data
includes the turnover for NSW, VIC, QLD, SA, WA, TAS, NT, ACT, and a Total value.
Open the file in Excel and check for any missing values.

ii. Read the file using RStudio (you can install the package “readxl”, and then use
“read_excel”). Store the file into a variable named “raw_data”.

iii. Store the raw data in a new variable named “data”, while blank rows are removed
(you can use “[Link]”).

iv. Plot a bar chart for the average trade value of each state: 8 bars in total (you can
calculate the average values first, using “colMeans”, and then plot the bar chart
using “geom_col” from “ggplot”).

For practice only – go to part vii for in-class questions


--------------------------------------------------------------------------------------------------
v. What do you observe from the plot? In what ways readability of the plot can be
improved?

To sort from high to low, we use the following code. Note that “means_df$Avg”
contains the average value for each state, “order(means_df$Avg)” would sort the
rows from lowest to highest, and the minus sign “-“ reverses this order. So, an
ordered set of mean values will be stored in “means_df”.
The second line sets the order of the states for the bar chart, as by default the
order is alphabetical. Here, “factor()” is used to treat the states as a categorical
variable, and the order of the categories (levels) is set based on their current
order in means_df.

means_df <- means_df[order(-means_df$Avg), ]


means_df$State <- factor(means_df$State, levels = means_df$State)

ggplot(means_df, aes(x = State, y = Avg)) +


geom_col() +
labs(title = "Plot 2", y = "Average turnover")

Next, we apply colours and labels. “means_df” is the dataset used for plotting, “x
= State” places each state on the horizontal axis, ‘y = Avg” places the
corresponding average turnover on the vertical axis, “fill = State” tells “ggplot” to
colour each bar based on the state. In addition, “round(Avg, 0)” rounds the
average value to the nearest whole number before displaying it, “vjust = -0.3”
moves the label slightly above the bar, “size = 3” controls the size of the text.
“ylim” extends the vertical axis up to 110% of the maximum value, “labs” sets the
labels, and “theme” is removing the legend.

ggplot(means_df, aes(x = State, y = Avg, fill = State)) +


geom_col() +
geom_text(aes(label = round(Avg, 1)), vjust = -0.3, size = 3) +
ylim(0, max(means_df$Avg) * 1.1) +
labs(title = "Plot 3", x = "State", y = "Average turnover") +
theme([Link] = "none")

vi. Plot a histogram of the trade values of NSW (you can use “geom_histogram” from
“ggplot”). Add a distribution curve to the plot (use “geom_density” from
“ggplot”).

ggplot(df, aes(x = NSW)) +


geom_histogram() +
labs(title = "Plot 4", x = "NSW turnover", y = "Frequency")

ggplot(df, aes(x = NSW)) +


geom_histogram(aes(y = after_stat(density)), ) +
geom_density() +
labs(title = "Plot 5", x = "NSW turnover", y = "Density")

Try changing the y axis tick labels by the following:

scale_y_continuous(breaks = seq(0, 100, by = 20))

Now, try setting the number of bins, setting a colour for the bins, and a colour for
the curve.

ggplot(df, aes(x = NSW)) +


geom_histogram(aes(y = after_stat(density)), bins = 12, fill = "lightblue",
color = "black") +
geom_density(color = "red", linewidth = 1) +
labs(title = "Plot 5", x = "NSW turnover", y = "Density")
--------------------------------------------------------------------------------------------------
vii. Plot a time series of the NSW trade values over time (use “geom_line”). Note that
the variable Month-year contains “-” in the name, which means we should put it
inside backticks.

We have introduced ggplot in the previous session. Here we are using it to plot a
line chart. One point to notice is using backtick “`” when setting the column for
x, i.e., x = `Month-year`.

ggplot(df, aes(x = `Month-year`, y = NSW)) +


geom_line() +
labs(title = "Plot 1", x = "Month", y = "NSW turnover")

Note that we can change the y axis labels by adding the following line after
“geom_line()+” :

scale_y_continuous(breaks = seq(0, 14000, by = 2000)) +

This makes the y axis stretch from 0 to 14,000, and a label every 2,000.

ggplot(df, aes(x = `Month-year`, y = NSW)) +


geom_line() +
scale_y_continuous(breaks = seq(0, 14000, by = 2000)) +
labs(title = "Plot 1", x = "Month", y = "NSW turnover")

We can also change the frequency of labels on the x axis. For that, we cannot use
scale_x_continuous, the variable on the x axis is not numerical (continuous). For
our case having a date variable on the x axis, we can use:

ggplot(df, aes(x = `Month-year`, y = NSW)) +


geom_line() +
scale_y_continuous(breaks = seq(0, 14000, by = 2000)) +
scale_x_date(date_breaks = "5 years", date_labels = "%Y") +
labs(title = "Plot 1", x = "Month", y = "NSW turnover")

This is creating a label for every 5 years, and writing the year only ("%Y").

viii. Now, store the data as a “tsibble” and try “autoplot”. Compare with the line chart
used in the previous part.

We first create a new column in “df” using “mutate”, and then we store the new
data in “df2”. This is needed as when we want to create a tsibble, a column
should be set for “index”, and that column can only take specific formats.
df2 <- df %>%
mutate(Month = yearmonth(`Month-year`))

We may print df2 and observe the new column. Note that running

df2

prints only 8 columns and 10 rows, while the new column is stored as the last
column. To print all columns, we can write:

print(df2, width = Inf)

To print more rows, we write:

print(df2, n = 100, width = Inf)

Note that “width = Inf” does not mean infinite number of columns, it means
infinite characters (so, width = 20 prints columns up to having 20 characters in
total, rather than 20 columns).

The next part selects two columns NSW and Month from “df2”, and set it as a
tsibble, which is a type of data in R, and stores it in “new_ts”. The operator “%>%”
is called a pipe operator.

nsw_ts <- df2 %>%


select(Month, NSW) %>%
as_tsibble(index = Month)

Then, we use “autoplot” to plot the time series. The result will be same as the
line chart above. Though, if we have a “tsibble”, “autoplot” will select the correct
plot type automatically.

autoplot(nsw_ts, NSW) +
scale_y_continuous(breaks = seq(0, 14000, by = 2000)) +
labs(title = "Plot 2", x = "Month", y = "NSW turnover")

ix. Redo part (viii) where data is filtered from 2015 onward (use “filter”).

Sometimes we need filtering data. Here we see how to filter data from the
beginning of 2015/.

nsw_ts %>%
filter(Month >= yearmonth("2015 Jan")) %>%
autoplot(NSW) + labs(title = "Plot 3", x = "Month", y = "NSW turnover")
Now, filter it between Jan 2015 and Dec 2023.

Once we practiced “from 2015”, we use the following for the second part:

nsw_ts %>%
filter(Month >= yearmonth("2015 Jan") & Month <= yearmonth("2023 Dec"))
%>%
autoplot(NSW) +
labs(title = "Plot 3", x = "Month", y = "NSW turnover")

x. Interpret the time series. In particular, discuss whether you see trend and
seasonality in each of the states.

The charts show a clear upward trend. It also shows a clear seasonality where on
specific months the value goes down, and on others goes up with the same
pattern on each year. We can also observe that the seasonality is getting bigger
as we go to the right hand side, that is, the difference between the high and low
values in each year is getting larger.

xi. The following code fits a model to NSW time series (given it is sored in
“nsw_ts”). In particular, we are fitting a STL (Seasonal and Trend decomposition
using Loess) model to the time series. This is similar to what we do in regression,
where we fit a model in the form of 𝒚 = 𝒂 + 𝒃𝒙. However, STL fits a model in
the form of:

The three components of time series (trend, seasonal, remainder) can be joined in
different ways. This one below is one way to do it and is called additive
decomposition. Note that cycle, if present, will be part of the trend. The assumption
in this decomposition is that the observed values can be modelled by adding these
three components.
The code below fits the model based on NSW data, and extracts the components.
The resulting plot below shows all components. We can also verify our earlier
findings based on this plots. In particular, the seasonal component, where the
difference in seasons is increasing over time is now clearly visible. The additive
model is suitable for the case that this difference remains constant over time, so
this means the model we used (additive decomposition) may not be the suitable
model for this data. We learn multiplicative decomposition next session, which
does a better job for this data.
Observed = Trend + Seasonal + Remainder

That means, STL model decomposes the time series into:

o Trend (long-term movement)


o Seasonal (repeating pattern)
o Remainder (random noise)
Using the decomposed series, verify your interpretation of the time series.

nsw_dc <- nsw_ts %>%


model(STL(NSW)) %>%
components() %>%
autoplot()

nsw_dc
How can we save the plot?
Here is the code to do it:
ggsave("Week_3/plot_additive.png", plot = nsw_dc, width = 6, height = 4)
[1] Sorce: [Link]
trade-australia/latest-release#analysis-by-industry
Further practice:

i. Redo part (viii) where the time series plot includes NSW, VIC, and QLD.

all_ts <- df2 %>%


select(Month, NSW, VIC, QLD) %>%
pivot_longer(-Month, names_to = "State", values_to = "Value") %>%
as_tsibble(key = State, index = Month)

autoplot(all_ts, Value) +
labs(title = "Plot 9", x = "Month", y = "Turnover")

ii. Compare the three states based on the findings (for your practice).

You might also like