Homework 2
Do exercises 3.1, 3.2, 3.3, 3.4, 3.5, 3.7, 3.8 and 3.9 from the online Hyndman book.
library(fpp3)
## Registered S3 method overwritten by 'tsibble':
## method from
## as_tibble.grouped_df dplyr
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.0 ──
## ✔ tibble 3.2.1 ✔ tsibble 1.1.5
## ✔ dplyr 1.1.4 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.3.1 ✔ feasts 0.3.2
## ✔ lubridate 1.9.3 ✔ fable 0.3.4
## ✔ ggplot2 3.5.1 ✔ fabletools 0.4.2
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ✖ tsibble::setdiff() masks base::setdiff()
## ✖ tsibble::union() masks base::union()
In 2017, the country with the highest GDP per capita was Luxembourg
The ggplot is misleading and also looks like Luxembourg was the highest through out most of time.
I also reviewed the top countries in the year 1980, while Luxembourg was eighth highest. In 1908 Monaco was the highest
I looked at some online resources and finally made the autoplot work; it’s pretty clear that the GDP per cap was a lot lower in the past than in the recent 20 years.
data("global_economy")
GDP_per_cap <- global_economy %>%
mutate(per_cap = GDP/Population) %>%
select(Country, Year, per_cap) %>%
as_tsibble(index = Year, key = Country)
# autoplot(GDP_per_cap, per_cap) not running...facet_wrap(~ Country, scales = "free_y") also not helping...15k+ observations might impact that too....trying to names to country also not working.
ggplot(GDP_per_cap, aes(x=Year, y=per_cap, colour = Country)) + geom_line(stat = "identity", show.legend = F)
## Warning: Removed 3242 rows containing missing values or values outside the scale range
## (`geom_line()`).
GDP_per_cap %>%
filter(Year == 1980) %>%
arrange(desc(per_cap))
GDP_per_cap %>%
filter(Year == 2017) %>%
arrange(desc(per_cap))
gdp1 <- global_economy %>%
mutate(GDP_Per_Capital = GDP/Population) %>%
filter(GDP_Per_Capital > 45000) %>%# adjusting the legend
autoplot(GDP_Per_Capital) + labs(title= "GDP per capital", y = "Currency in US Dollars")
gdp1
Transforming is needed ot prepare for decomposition to balance the fluctuations; consider if variation of cycle trend/seasonality is proportional to the level of the time series
United States GDP from global_economy.
Slaughter of Victorian “Bulls, bullocks and steers” in aus_livestock.
Victorian Electricity Demand from vic_elec.
Gas production from aus_production.
global_economy |>
filter(Country == "United States") |>
autoplot(GDP/Population)
glimpse(aus_livestock)
## Rows: 29,364
## Columns: 4
## Key: Animal, State [54]
## $ Month <mth> 1976 Jul, 1976 Aug, 1976 Sep, 1976 Oct, 1976 Nov, 1976 Dec, 197…
## $ Animal <fct> "Bulls, bullocks and steers", "Bulls, bullocks and steers", "Bu…
## $ State <fct> Australian Capital Territory, Australian Capital Territory, Aus…
## $ Count <dbl> 2300, 2100, 2100, 1900, 2100, 1800, 1800, 1900, 2700, 2300, 250…
vic_livestock <- aus_livestock %>%
filter(State == "Victoria",
Animal %in% c("Bulls, bullocks and steers"))
vic_livestock
vic_livestock <- aus_livestock %>%
filter(State == "Victoria",
Animal %in% c("Bulls, bullocks and steers"))
autoplot(vic_livestock, Count)
autoplot(aus_production, Gas)
#hard to see with the default 30 mins; kept getting errors when trying to change it to quarterly/ monthly
autoplot(vic_elec, Demand)
autoplot(aus_production, Gas)
autoplot(canadian_gas, Volume)
lambda <- canadian_gas %>%
features(Volume, features = guerrero) %>%
pull(lambda_guerrero)
canadian_gas %>%
autoplot(box_cox(Volume, lambda))
set.seed(123788)
myseries <- aus_retail |>
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
autoplot(myseries)
## Plot variable not specified, automatically selected `.vars = Turnover`
lambda <- myseries %>%
features(Turnover, features = guerrero) %>%
pull(lambda_guerrero)
myseries %>%
autoplot(box_cox(Turnover, lambda))
print(lambda)
## [1] 0.2053157
aus_production %>%
autoplot(Tobacco)
## Warning: Removed 24 rows containing missing values or values outside the scale range
## (`geom_line()`).
library(latex2exp)
lambda <- aus_production %>%
features(Tobacco, features = guerrero) %>%
pull(lambda_guerrero)
aus_production %>%
autoplot(box_cox(Tobacco, lambda)) +
labs(y = "", title = TeX(paste0("Transformed Tobacco Production with $\\lambda$ = ",
round(lambda,2))))
## Warning: Removed 24 rows containing missing values or values outside the scale range
## (`geom_line()`).
#something is wrong here since lambda should be between 0&1
mel_syd <- ansett %>%
filter(Class == "Economy",
Airports == "MEL-SYD")
autoplot(mel_syd, Passengers)+
labs(title = "Economy class Passengers Between Melbourne and Sydney")
lambda <- mel_syd %>%
features(Passengers, features = guerrero) %>%
pull(lambda_guerrero)
mel_syd %>%
autoplot(box_cox(Passengers, lambda)) +
labs(y = "", title = TeX(paste0("Transformed Number of Passengers with $\\lambda$ = ",
round(lambda,2))))
7.Consider the last five years of the Gas data from aus_production.
gas <- tail(aus_production, 5*4) |> select(Gas) a. Plot the time series. Can you identify seasonal fluctuations and/or a trend-cycle? There is an increasing pattern over the years. Seasonal pattern present lowest at Q1 and highest at Q3.
gas <- tail(aus_production, 5*4) |> select(Gas)
autoplot(gas, Gas)
as_tsibble(gas) %>%
model(classical_decomposition(type ="multiplicative") )%>%
components() %>%
autoplot()
## Model not specified, defaulting to automatic modelling of the `Gas` variable.
## Override this using the model formula.
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_line()`).
c. Do the results support the graphical interpretation from part a? Yes,
seasonal and trend being accurate to my interpretation. Compute and plot
the seasonally adjusted data.
as_tsibble(gas) %>%
model(classical_decomposition(type ="multiplicative") )%>%
components() %>%
as_tsibble() %>%
autoplot() + geom_line(aes(y=season_adjust), colour = "red")
## Model not specified, defaulting to automatic modelling of the `Gas` variable. Override this using the model formula.
## Plot variable not specified, automatically selected `.vars = Gas`
gas$Gas[19]<-gas$Gas[19]+300
as_tsibble(gas) %>%
model(classical_decomposition(type ="multiplicative") )%>%
components() %>%
as_tsibble() %>%
autoplot() +geom_line()
## Model not specified, defaulting to automatic modelling of the `Gas` variable. Override this using the model formula.
## Plot variable not specified, automatically selected `.vars = Gas`
gas$Gas[11]<-gas$Gas[11]+300
as_tsibble(gas) %>%
model(classical_decomposition(type ="multiplicative") )%>%
components() %>%
as_tsibble() %>%
autoplot() +geom_line()
## Model not specified, defaulting to automatic modelling of the `Gas` variable. Override this using the model formula.
## Plot variable not specified, automatically selected `.vars = Gas`
library(seasonal)
##
## Attaching package: 'seasonal'
## The following object is masked from 'package:tibble':
##
## view
x11 <- myseries %>%
model(x11 = X_13ARIMA_SEATS(Turnover ~ x11())) %>%
components()
autoplot(x11)
Decomposition of the number of persons in the civilian labour force in Australia each month from February 1978 to August 1995.
Write about 3–5 sentences describing the results of the decomposition. Pay particular attention to the scales of the graphs in making your interpretation. It shows a upward trend, which is significant when considering the scale of the graphs. The dip during the 1991-1992 recession is evident but relatively minor compared to the overall growth trajectory, indicating that while it impacted the labor force, it did not derail the long-term trend. Seasonality is present, but its scale is much smaller than the trend, suggesting it has less influence on labor force changes.
Is the recession of 1991/1992 visible in the estimated components? Even though remainder is noise that is where I see it most; there is also a drip in the first graph ‘value’ during those years. The trend does not decrease but looks like a bit of a plateau during those years.