Due 10/11/2024

Author

Ava Graf

Published

October 10, 2024

Setup

  • Create a new chunk and include setup code from instructions here.

  • This is Chunk 1.

Code
```{r}
#|label: setup

# this line specifies options for default options for all R Chunks
knitr::opts_chunk$set(echo=T, highlight=T)

# suppress scientific notation
options(scipen=100)

# install helper package (pacman) if needed
if (!require("pacman")) install.packages("pacman", repos = "http://lib.stat.cmu.edu/R/CRAN/")
```
Loading required package: pacman
Code
```{r}
# install and load required packages
# pacman should be first package in parentheses and then list others
pacman::p_load(pacman, tidyverse, gridExtra, magrittr, 
               ggthemes)

# verify packages (comment out in finished documents)
p_loaded()
```
 [1] "ggthemes"  "magrittr"  "gridExtra" "lubridate" "forcats"   "stringr"  
 [7] "dplyr"     "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"  
[13] "tidyverse" "pacman"   

HW 4 - Part 1

Chunk 2: Create bls_tidy function from provided code.

Code
```{r}
#|label: create bls_tidy function

# import and clean bls data
unemp_orig <- read_csv("data/bls_unemp_rate.csv", skip = 11, show_col_types = F) |> 
  pivot_longer(cols = Jan:Dec,                      # reshapes data
               names_to = "month", 
               values_to = "value") |>
  filter(!is.na(value)) |>                          # removes missing values
  rename("unemp_rate" = "value")                    # renames quantitative var 


bls_tidy <- function(data_file, skip_num, var_name){
  read_csv(data_file, skip = skip_num, show_col_types = F) |> 
  pivot_longer(cols = Jan:Dec,                      # reshapes data
               names_to = "month", 
               values_to = "value") |>
  filter(!is.na(value)) |>                          # removes missing values
  rename({{var_name}} := "value")
}

unemp <- bls_tidy(data_file="data/bls_unemp_rate.csv", skip_num=11, var_name="unemp_rate")
```

HW 4 - Part 2

Chunk 3: Use function to import two datasets.

Code
```{r}
#|label: import export_index and import_index datasets

export_index <- bls_tidy(data_file="data/bls_export_index.csv",
                         skip_num = 10,
                         var_name = "exp_indx")

import_index <- bls_tidy(data_file="data/bls_import_index.csv",
                         skip_num = 10, 
                         var_name = "imp_indx")

summary(export_index$exp_indx)

summary(import_index$imp_indx)
```
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  118.1   123.0   126.9   132.8   145.2   166.7 
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  117.2   123.3   126.7   130.3   139.4   148.5 

HW 4 - Part 3

Chunk 4: Join datasets and create date variable.

Code
```{r}
#|label: join datasets and create date variable

export_import <- full_join(export_index, import_index) |>
  mutate(date = ym(paste(Year, month)))
```
Joining with `by = join_by(Year, month)`

HW 4 - Part 4

Chunk 5: Reshape, format and plot data

Code
```{r}
#|label: reshape and format plot data

exp_imp_plt <- export_import |>
  select(date, exp_indx, imp_indx) |>
  pivot_longer(cols = exp_indx:imp_indx, names_to = "type", values_to = "value") |>
  mutate(type=factor(type,
                       levels = c("exp_indx", "imp_indx"),
                       labels = c("Export", "Import"))) |> glimpse()
```
Rows: 252
Columns: 3
$ date  <date> 2014-01-01, 2014-01-01, 2014-02-01, 2014-02-01, 2014-03-01, 201…
$ type  <fct> Export, Import, Export, Import, Export, Import, Export, Import, …
$ value <dbl> 132.7, 138.3, 133.7, 139.8, 134.9, 140.5, 133.5, 139.7, 133.7, 1…

HW 4 - Part 5

Chunk 6: Create Formatted Plot

Code
```{r}
#|label: create and save plot

(exp_imp_lineplot <- exp_imp_plt |>     # replace blank with name of dataset
  ggplot() +
  geom_line(aes(x = date, y = value, color = type), linewidth = 1 ) +
   
# can opt for different theme
  theme_classic() +                                           

# moves legend to bottom
  theme(legend.position="bottom") +                         

# specifies colors (many other options vailable)
  scale_color_manual(values=c("lightblue","blue")) +        
                                                        
# formats x axis to show each year as 4 digits                                       
  scale_x_date(date_breaks = "year", date_labels = "%Y") +
  
   
  labs(x = "Date", 
       y = "Index", 
       color = "",
       title = "Import and Export Price Indices", 
       subtitle = "January 2014 - June 2024" ,
       caption = "Data Source - www.bls.gov" ))
```