Do exercises 3.1, 3.2, 3.3, 3.4, 3.5, 3.7, 3.8 and 3.9 from the online Hyndman book. Please include your Rpubs link along with.pdf file of your run code
3.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(tidyverse)
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tibble' was built under R version 4.5.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'readr' was built under R version 4.5.2
Warning: package 'purrr' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.0 ✔ readr 2.1.6
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.2 ✔ tibble 3.3.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# A tibble: 58 × 3
# Groups: Year [58]
Year Country gdp_per_capita
<dbl> <fct> <dbl>
1 1960 United States 3007.
2 1961 United States 3067.
3 1962 United States 3244.
4 1963 United States 3375.
5 1964 United States 3574.
6 1965 Kuwait 4429.
7 1966 Kuwait 4556.
8 1967 United States 4336.
9 1968 United States 4696.
10 1969 United States 5032.
# ℹ 48 more rows
global_economy |>mutate(gdp_per_capita = GDP / Population) |>filter( Country %in%c("United States","Kuwait","Monaco","United Arab Emirates" ) ) |>ggplot(aes(x = Year,y = gdp_per_capita,color = Country )) +geom_line(na.rm =TRUE) +labs(title ="GDP Per Capita Over Time",x ="Year",y ="GDP per capita (USD)",color ="Country" )
##Interpretation
The country with the highest GDP per capita for most of the period is Monaco. Monaco shows a strong upward trend over time, although there are some periods of decline, especially around 1980’s, 2000 and 2010. Overall, Monaco’s GDP per capita increased significantly over time. The United Arab Emirates, the United States, and Kuwait also show an overall upward trend. The United States has a more stable trend, while Kuwait, Monaco, and the United Arab Emirates show more fluctuations and several periods of decline.
3.2 .For each of the following series, make a graph of the data. If transforming seems appropriate, do so and describe the effect.
The original U.S. GDP series shows a strong upward trend, with the magnitude of the changes increasing as GDP increases. The log transformation compresses the larger values and makes the growth pattern more nearly linear, helping to stabilize the variation over time.
global_economy |>filter(Country =="United States") |>autoplot(log(GDP)) +labs(title ="Log of United States GDP",x ="Year",y ="GDP (USD)")
Slaughter of Victorian “Bulls, bullocks and steers” in aus_livestock.
aus_livestock |>filter(State =="Victoria", Animal =="Bulls, bullocks and steers") |>ggplot(aes(x = Month, y = Count)) +geom_line() +labs(title ="Slaughter of Victorian Bulls, bullocks and steers", x ="Month", y ="Count")
victoria_bulls <- aus_livestock |>filter( State =="Victoria", Animal =="Bulls, bullocks and steers" )dcmp <- victoria_bulls |>model(stl =STL(Count))components(dcmp)
we could use a log transformation for aus_livestock observation over time, and by using a log transformation we see that the original series ranges differ from the transformed series ranges, but the overall pattern and variability remain similar to the original series.
we could use a log transformation for vic_elec observation over time, and by using a log transformation we see that the original series ranges differ from the transformed series ranges, but the overall pattern and variability remain similar to the original series.
aus_production |>autoplot(Gas) +labs(title ="Australia Gas Production in Quarter")
##Interpretation
The original gas production series shows an upward trend and seasonal fluctuations whose size changes with the level of the series. The log transformation compresses the larger values and makes the variation more stable over time.
aus_production |>autoplot(log(Gas)) +labs(title ="Australia Gas Production in Quarter")
3.3. Consider the last five years of the Gas data from aus_production.
let explore the canadian_gas data with a plot
canadian_gas |>autoplot(Volume)
explore with Box - Cox
lambda <- canadian_gas |>features(Volume, features = guerrero) |>pull(lambda_guerrero)canadian_gas |>autoplot(box_cox(Volume, lambda)) +labs(y ="",title = latex2exp::TeX(paste0( "Canada Transformed Gas Production with $\\lambda$ = ", round(lambda, 2))))
##Interpretation
Applying a Box–Cox transformation to the Canadian gas production data is not particularly helpful. Although the Guerrero method gives λ = 0.58 and the transformation changes the scale of the data, the overall pattern and variability remain similar to the original series. Therefore, the Box–Cox transformation does not provide a substantial improvement in stabilizing the variance.
3.4. What Box-Cox transformation would you select for your retail data (from Exercise 7 in Section 2.10)?
The box-cox I would select for my retail data from Exercise 7 in section 2.10 is automatically calculating the lambda = 0.083 using features guerrero that will generate a lambda value we will use.
3.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.
To find an appropriate Box-Cox transformation in order to stabilise the variance. we use the features guerrero which provide a lambda that we apply to plot the graph therefor we can observe the graph shap for Australian tobacco production.
ansett_mel_syd <- ansett |>filter( Class =="Economy", Airports =="MEL-SYD" )lambda <- ansett_mel_syd |>features(Passengers, features = guerrero) |>pull(lambda_guerrero)lambda
To find an appropriate Box-Cox transformation in order to stabilize the variance. we use the features guerrero which provide a lambda that we apply to plot the graph therefor we can observe the graph shape for ansett Economy flights. so we can concluded using box-cox is suitable.
The series shows a repeating quarterly seasonal pattern over the five-year period. Gas production rises and falls at similar times each year, indicating strong seasonality. There is also an overall upward trend in gas production.
b. Use classical_decomposition with type=multiplicative to calculate the trend-cycle and seasonal indices.
gas |>model(classical_decomposition(Gas, type ="multiplicative")) |>components() |>autoplot(Gas, na.rm =TRUE) +labs(title ="Classical multiplicative decomposition of Gas Production in Australia")
c. Do the results support the graphical interpretation from part a?
The results support the graphical interpretation from part (a). The classical decomposition shows a clear upward trend and a strong repeating quarterly seasonal pattern.
d. Compute and plot the seasonally adjusted data.
gas_dcmp <- gas |>model(classical_decomposition(Gas, type ="multiplicative") ) |>components()gas_dcmp
gas_dcmp |>autoplot(season_adjust) +labs(title ="Seasonally Adjusted Australian Gas Production",x ="Quarter",y ="Gas" )
e. 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?
gas_outlier <- gas |>mutate(Gas =if_else( Quarter ==yearquarter("2008 Q1"), Gas +300, Gas ) )gas_outlier_dcmp <- gas_outlier |>model(classical_decomposition(Gas, type ="multiplicative") ) |>components()gas_outlier_dcmp |>autoplot(season_adjust) +labs(title ="Seasonally Adjusted Gas Production with Outlier",x ="Quarter",y ="Gas" )
Interpretation
Adding 300 to one observation creates a large spike in the seasonally adjusted data. The outlier has a strong effect on that observation and can also affect the estimated components used in the classical decomposition.
f. Does it make any difference if the outlier is near the end rather than in the middle of the time series?
gas_outlier <- gas |>mutate(Gas =if_else( Quarter ==yearquarter("2010 Q1"), Gas +300, Gas ) )gas_outlier_dcmp <- gas_outlier |>model(classical_decomposition(Gas, type ="multiplicative") ) |>components()gas_outlier_dcmp |>autoplot(season_adjust) +labs(title ="Seasonally Adjusted Gas Production with Outlier",x ="Quarter",y ="Gas" )
Interpretation
Yes, it makes a difference when the outlier is near the end rather than in the middle of the time series. An outlier in the middle can affect the estimated trend-cycle around it because there are observations on both sides. Near the end, the trend-cycle cannot be estimated in the same way because there are not enough observations after the outlier. Therefore, the position of the outlier affects the classical decomposition and the seasonally adjusted data.
3.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?
#install.packages("seasonal")library(seasonal)
Warning: package 'seasonal' was built under R version 4.5.2
Attaching package: 'seasonal'
The following object is masked from 'package:tibble':
view
Observing the remainder component, we can see some unusual positive and negative spikes that were not as noticeable in the original time series. These large deviations represent observations that are not explained by the trend or seasonal components and may indicate unusual events or outliers.
3.9. Figures 3.19 and 3.20 show the result of decomposing the number of persons in the civilian labor force in Australia each month from February 1978 to August 1995.
a. Write about 3–5 sentences describing the results of the decomposition. Pay particular attention to the scales of the graphs in making your interpretation.
The decomposition shows a smooth upward trend in the Australian civilian labor force from 1978 to 1995. The seasonal component ranges approximately from −100 to +100 and shows a consistent seasonal pattern. December and March have relatively large positive seasonal effects, while January and August have large negative seasonal effects. Compared with the overall level of the series, which increases from approximately 6,500 to 9,000, the seasonal fluctuations are relatively small.
b. Is the recession of 1991/1992 visible in the estimated components?
Yes, the recession of 1991/1992 is visible in the decomposition. The trend component shows that the growth of the labor force slows considerably around 1991–1992. The remainder also shows unusually large negative values around 1991, including a decline of approximately −400.