This analysis used the tidyverse,gridExtra,
plotly, fpp2 and stringi
packages. Every functions used in this analysis belongs to the creator
of these packages and as well as the creator of R.
# load data from FRED
usunemp <- read_csv("unempl.csv",show_col_types = FALSE)
The data I used is from the Unemployment Rate data set provided by FRED.
This is the raw data from the FRED data set.
## # A tibble: 895 × 2
## DATE UNRATE
## <date> <dbl>
## 1 1948-01-01 3.4
## 2 1948-02-01 3.8
## 3 1948-03-01 4
## 4 1948-04-01 3.9
## 5 1948-05-01 3.5
## 6 1948-06-01 3.6
## 7 1948-07-01 3.6
## 8 1948-08-01 3.9
## 9 1948-09-01 3.8
## 10 1948-10-01 3.7
## # … with 885 more rows
## # ℹ Use `print(n = ...)` to see more rows
Start the pipe with the data set, first use the stri_sub
function from the stringi package to extract the year value
of the Date column,use as.double to turn the
vector type Date to numerical vector Double.
Then use the filter function from the
tidyverse package to filter out the desired year (in this
case between 2016 and 2021), Lastly, select the UNRATE
column.
us_unemployment_1 <- usunemp |>
mutate(DATE = as.double(stri_sub(DATE,0,4))) |>
filter(DATE >= 2016 & DATE < 2021)|>
select(UNRATE)
The resulting tibble is shown below, which contains a total of 60
rows.
(5 years * 12 months/years)
## # A tibble: 60 × 1
## UNRATE
## <dbl>
## 1 4.8
## 2 4.9
## 3 5
## 4 5.1
## 5 4.8
## 6 4.9
## 7 4.8
## 8 4.9
## 9 5
## 10 4.9
## # … with 50 more rows
## # ℹ Use `print(n = ...)` to see more rows
Use ts function to translate the tibble into a
time-series object, note the frequency correspond with the pattern of
repetition within the data (months), and using the same start year as
set during the previous code chunk.
us_unemployment_g1 <-ts(us_unemployment_1,frequency=12,start = 2016)
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 2016 4.8 4.9 5.0 5.1 4.8 4.9 4.8 4.9 5.0 4.9 4.7 4.7
## 2017 4.7 4.6 4.4 4.4 4.4 4.3 4.3 4.4 4.3 4.2 4.2 4.1
## 2018 4.0 4.1 4.0 4.0 3.8 4.0 3.8 3.8 3.7 3.8 3.8 3.9
## 2019 4.0 3.8 3.8 3.6 3.6 3.6 3.7 3.7 3.5 3.6 3.6 3.6
## 2020 3.5 3.5 4.4 14.7 13.2 11.0 10.2 8.4 7.9 6.9 6.7 6.7
Lastly, use ggseasonplot to visualize the dataset, set
polar = TRUE will give us the cool polar coordinate in the
plot. Adjust the titles and texts with theme,
xlab, ylab, and ggtitle.
plot1 <- ggseasonplot(us_unemployment_g1, polar=TRUE)+
theme(text = element_text(family = "STHeiti"))+
theme(plot.title = element_text(hjust = 0.5))+
xlab("Month")+
ylab("Unemployment Rate") +
ggtitle("US Unemployment Rate")
Similarly, we can create the plot from 2020 to 2022 using the same technique.
us_unemployment_2 <- usunemp |>
mutate(DATE = as.double(stri_sub(DATE,0,4))) |>
filter(DATE >= 2020 & DATE <= 2022)|>
select(UNRATE)
us_unemployment_g2 <-ts(us_unemployment_2,frequency=12,start = 2020)
plot2 <- ggseasonplot(us_unemployment_g2, polar=TRUE)+
theme(text = element_text(family = "STHeiti"))+
theme(plot.title = element_text(hjust = 0.5))+
xlab("Month")+
ylab("Unemployment Rate") +
ggtitle("US Unemployment Rate")
Use grid.arrange from the gridExtra package
to put the two plots side by side.
grid.arrange(plot1, plot2, ncol=2)
The two plots represent the unemployment rate in the United States under the 45th President Donald Trump (Left), and the 46th President Joe Biden (Right). The plot at the bottom shows the historical US unemployment rate since January 1948.
The data have shown that the unemployment rate under the first three years of President Trump have gradually decreases, however, starting in January 2020, when Covid first hit the United States, the unemployment rate skyrocketed to an all time high of 14.7% in April 2022. After the peek in April, the unemployment rate decreases, and during the time of President Biden’s inauguration, the US unemployment rate was around 6.4%. After President Biden took office, the unemployment rate continues to drop, as of July 2022, the unemployment rate of the US was at it’s lowest point since 1960, at around 3.5%.