library(dplyr)
library(fpp3)
library(seasonal)
global_economy
. Plot
the GDP per capita for each country over time. Which country has the
highest GDP per capita? How has this changed over time?per_cap <- global_economy %>%
group_by(Country) %>%
mutate(percap = GDP/Population) %>%
select(Country, Year, percap)
ggplot(per_cap, aes(x=Year, y=percap, color = Country)) +
geom_line() +
theme(legend.position = 'none') +
labs(title = 'All countries per capita', y='$USD')
The above plot has too much data to display the legend. So i used another code below to extract the country with the highest GDP per capita. And that country is Monanco in year 2014
global_economy %>%
group_by(Country) %>%
summarise(per_cap_avg = mean(GDP / Population, na.rm = TRUE)) %>%
slice_max(per_cap_avg)
## # A tsibble: 1 x 3 [1Y]
## # Key: Country [1]
## Country Year per_cap_avg
## <fct> <dbl> <dbl>
## 1 Monaco 2014 185153.
For each of the following series, make a graph of the data. If transforming seems appropriate, do so and describe the effect.
global_economy
.aus_livestock
.vic_elec
.aus_production
.#global_economy for USA:
global_economy %>%
filter(Code == 'USA') %>%
index_by(Year) %>%
mutate(GDP_per_capita = GDP/Population) %>%
pivot_longer(c(GDP, GDP_per_capita),
values_to='wealth',
names_to = 'GDP_type') %>%
ggplot(aes(x=Year, y=wealth)) +
geom_line()+
facet_grid(GDP_type~., scales = 'free_y') +
labs(title='USA GDP and GDP/capita', y='USD$')
library(latex2exp)
livestock_og<- aus_livestock %>%
filter(Animal == 'Bulls, bullocks and steers', State=='Victoria')
livestock_lambda<- aus_livestock %>%
filter(Animal == 'Bulls, bullocks and steers', State=='Victoria') %>%
features(Count, features = guerrero) %>%
pull(lambda_guerrero)
livestock_og %>%
autoplot(Count)+
labs(title = 'Victoria State Bulls, bullocks, and steers slaugther number',
y='numbers slaugthered')
livestock_og %>%
autoplot(box_cox(Count, livestock_lambda)) +
labs(title = latex2exp::TeX(paste0('Victoria State Bulls, bullocks, and steers slaugther number. lambda =', round(livestock_lambda, 2))),
y='Transformed number of slaugther')
lambda <- vic_elec %>%
features(Demand, features = guerrero) %>%
pull(lambda_guerrero)
vic_elec %>%
autoplot(box_cox(Demand, lambda))
gas<- aus_production %>%
select(Quarter, Gas)
gas_lambda<- aus_production %>%
select(Quarter, Gas) %>%
features(Gas, features = guerrero) %>%
pull(lambda_guerrero)
gas %>%
autoplot(Gas)+
labs(title = 'AUS Quarterly Gas production')
gas %>%
autoplot(box_cox(Gas, gas_lambda))+
labs(title = latex2exp::TeX(paste0('Transformed AUS Quarterly Gas production. Lambda =', round(gas_lambda, 2), y='Transformed gas production')))
Why is a Box-Cox transformation unhelpful for the
canadian_gas
data?
Ans: This could be that the original data already has a well spread of variance and further transformation will yield similar graph.
cgas_lambda<- canadian_gas %>%
features(Volume, features = guerrero) %>%
pull(lambda_guerrero)
canadian_gas %>%
autoplot()+labs(title = 'original plot')
## Plot variable not specified, automatically selected `.vars = Volume`
canadian_gas %>%
autoplot(box_cox(Volume, cgas_lambda))+
labs(title = 'Transformed plot')
What Box-Cox transformation would you select for your retail data (from Exercise 7 in Section 2.10)?
Ans: Lambda of 0.215 is likely the most optimal Box Cox lambda value.
set.seed(123)
myseries <- aus_retail |>
filter(`Series ID` == sample(aus_retail$`Series ID`,1)) %>%
features(Turnover, features = guerrero) %>%
pull(lambda_guerrero)
myseries
## [1] 0.2151641
Ans: Lambda = 0.926
aus_production %>%
select(Quarter, Tobacco) %>%
features(Tobacco, features = guerrero) %>%
pull(lambda_guerrero)
## [1] 0.9264636
Economy class passengers between Melbourne and Sydney
from ansett
Ans: lambda = 1.99
ansett %>%
filter(Class=='Economy', Airports == 'MEL-SYD') %>% features(Passengers, features = guerrero) %>%
pull(lambda_guerrero)
## [1] 1.999927
Pedestrian counts at Southern Cross Station from pedestrian.
Ans: lambda = -0.25
pedestrian %>%
filter(Sensor == 'Southern Cross Station') %>%
features(Count, features = guerrero) %>%
pull(lambda_guerrero)
## [1] -0.2501616
Recall your retail time series data (from Exercise 7 in Section 2.10). Decompose the series using X-11. Does it reveal any outliers, or unusual features that you had not noticed previously?
Ans: The decomposition reveals that the seasonality is slowly shifting to a lesser turnover pattern as time goes by despite the overall growing trend. There seems to be a potential outlier highlighted a little pass January of 2000, where an unusual peak is observed in the remainder series, but the scale of it is very small.
set.seed(123)
x11_dcmp<- aus_retail |>
filter(`Series ID` == sample(aus_retail$`Series ID`,1)) %>%
model(x11 = X_13ARIMA_SEATS(Turnover ~ x11())) %>%
components()
autoplot(x11_dcmp) +
labs(title = 'Aus Retail decomposition using x11')
Figures 3.19 and 3.20 show the result of decomposing the number of persons in the civilian labour force in Australia each month from February 1978 to August 1995.
Ans: There is a general growth of labor force as the year went on. The seasonality decomposition reveals that around December the demand for labor force has slightly increased over time. Similarly, the trough of labor force, around January is also dipped more when compared to previous years. Seasonality, however, as shown by the scale on the left side, is the smallest factor amount the entire decomposition series.
Ans: The recession of 1991/1992 is very visible as shown in the remainder series, where a sudden decline is observed.