###1. Consider the GDP information in 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?

library(fpp3)
library(magrittr)
# Install the forecast package if you haven't already


# Load the forecast package
library(forecast)

global_economy %>% autoplot(GDP / Population, show.legend =  FALSE)

global_economy %>%
  mutate(GDP_per_capita = GDP / Population) %>%
  filter(GDP_per_capita == max(GDP_per_capita, na.rm = TRUE)) %>%
  select(Country, GDP_per_capita)
## # A tsibble: 1 x 3 [1Y]
## # Key:       Country [1]
##   Country GDP_per_capita  Year
##   <fct>            <dbl> <dbl>
## 1 Monaco         185153.  2014
global_economy %>%
  filter(Country == "Monaco") %>%
  autoplot(GDP/Population)

###2.For each of the following series, make a graph of the data. If transforming seems appropriate, do so and describe the effect. #### United States GDP from global_economy. sqrt transformation makes sense.

global_economy %>% filter(Country == "United States")%>% autoplot(GDP/Population)

global_economy %>% filter(Country == "United States")%>% autoplot(sqrt(GDP/Population))

#### Slaughter of Victorian “Bulls, bullocks and steers” in aus_livestock.

aus_livestock %>% filter(Animal == "Bulls, bullocks and steers") %>% filter(State == "Victoria") %>% autoplot((Count))

#### Victorian Electricity Demand from vic_elec.

vic_elec %>% autoplot((Demand))

vic_elec %>%
  group_by(Date) %>%
  mutate(Demand = sum(Demand)) %>% autoplot((Demand))

Gas production from aus_production.

aus_production %>%
  autoplot(Gas)

aus_production_transformed <- aus_production$Gas
lambda <- BoxCox.lambda(aus_production_transformed)
aus_production_boxcox <- BoxCox(aus_production_transformed, lambda)

3. Why is a Box-Cox transformation unhelpful for the canadian_gas data?

Becasue it decreases linearity and doesnot stabilize the variance and deviates from normality

canadian_gas %>%
  autoplot(Volume) +
  labs(title = "Non-Transformed Gas Production")

lambda <- canadian_gas %>%
  features(Volume, features = guerrero) %>%
  pull(lambda_guerrero)

canadian_gas %>%
  autoplot(box_cox(Volume, lambda))

### 4. What Box-Cox transformation would you select for your retail data (from Exercise 7 in Section 2.10)?

set.seed(1234)
series_df <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`,1)) 
autoplot(series_df, Turnover)

lambda <- series_df %>%
  features(Turnover, features = guerrero) %>%
  pull(lambda_guerrero)

series_df %>%
  autoplot(box_cox(Turnover, lambda))

### 5. For the following series, find an appropriate Box-Cox transformation in order to stabilise the variance. Tobacco from aus_production, Economy class passengers between Melbourne and Sydney from ansett, and Pedestrian counts at Southern Cross Station from pedestrian.

autoplot(aus_production, Tobacco)

lambda <- aus_production %>%
  features(Tobacco, features = guerrero) %>%
  pull(lambda_guerrero)

aus_production %>%
  autoplot(box_cox(Tobacco, lambda))

7. Consider the last five years of the Gas data from aus_production.

Plot the time series. Can you identify seasonal fluctuations and/or a trend-cycle?

gas <- tail(aus_production, 5*4) |> select(Gas)
gas %>% autoplot(Gas)

Use classical_decomposition with type=multiplicative to calculate the trend-cycle and seasonal indices.

decomp <- gas %>% model(
    classical_decomposition(Gas, type = "multiplicative")
  ) %>%
  components()
decomp %>% autoplot()
## Warning: Removed 2 rows containing missing values (`geom_line()`).

as_tsibble(decomp) %>%
  autoplot(season_adjust) +
  labs(title = "Seasonally Adjusted Data")

gas %>%
  mutate(Gas = if_else(Quarter==yearquarter("2007Q2"), Gas + 300, Gas)) %>%
  model(classical_decomposition(Gas, type = "multiplicative")) %>%
  components() %>%
  as_tsibble() %>%
  autoplot(season_adjust) +
  labs(title = 'Seasonally Adjusted Data with 300 added to "2007 Q2"')

Do the results support the graphical interpretation from part a? Compute and plot the seasonally adjusted data. Change one observation to be an outlier (e.g., add 300 to one observation), and recompute the seasonally adjusted data. What is the effect of the outlier? Does it make any difference if the outlier is near the end rather than in the middle of the time series?

8. 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?

library(seasonal)
## Warning: package 'seasonal' was built under R version 4.3.2
## 
## Attaching package: 'seasonal'
## The following object is masked from 'package:tibble':
## 
##     view
decom <- series_df %>% model(x11 = X_13ARIMA_SEATS(Turnover ~ x11())) %>% components()
autoplot(decom)

### 9. 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. The civilian labor force in Australia shows a rising trend over time, with noticeable seasonality that, while present, appears to be less impactful compared to other factors. A distinct decline in the early 1990s is evident, attributed to an economic recession, primarily reflected in the remainder component. The recession of 1991/1992 is clearly discernible in the estimated components, particularly in the sharp decrease observed in the remainder component.