You will use the `tidyverse` package and the dataset called `ip_and_unemployment.csv` that we used in lectures. Start with setting up your workspace:

```{r setup}
# load in libraries:
suppressMessages(library(tidyverse))
# or just library(tidyverse)

# read ip_and_unemployment.csv
ip_and_unemployment <- read.csv("ip_and_unemployment.csv")
head(ip_and_unemployment)

```
    
What are the highest unemployment rates for France and Spain during the time of the sample?  What are the lowest values for monthly percentage changes of industrial production for the two countries? Make sure to delete NA values in only the time series of interest. (_Optional_: can you create a function that would do this for any country?)

```{r q1}
# Q1 --------------------------------------------------------------------------
# data processing
ipu_clean <- ip_and_unemployment %>%
  pivot_wider(id_cols = c("country", "date"), 
              names_from = "series", values_from = "value")  # long to wide

# France
ipu_clean %>%
  filter(country == "france") %>%
  filter(unemployment == max(unemployment, na.rm = TRUE) | 
           ip == min(ip, na.rm = TRUE)) 
    # need to add na.rm = TRUE, because ip contains NA and min() will return NA
    # if there is one NA, unless na.rm = TRUE

# Spain
ipu_clean %>%
  filter(country == "spain") %>%
  filter(unemployment == max(unemployment, na.rm = TRUE) | 
           ip == min(ip, na.rm = TRUE))

# Optional --------------------------------------------------------------------
filter_worst_months <- function(x) {
  filtered <- ipu_clean %>%
    filter(country == x) %>%
    filter(unemployment == max(unemployment, na.rm = TRUE) | 
             ip == min(ip, na.rm = TRUE))
  return(filtered)
}

filter_worst_months("germany")



```

-----

How many non-NA monthly observations of industrial production exist for the countries here. Can you  determine this with the group_by and summarise functions? (_Optional_: can you calculate the % of values that are non-NA?)

```{r q2} 
# Q2 --------------------------------------------------------------------------
# Non-NA group and summarise
ipu_clean %>%
  group_by(country) %>%
  summarise(nonNA_ip = sum(!is.na(ip)),
            nonNA_ue = sum(!is.na(unemployment)))

# Optional --------------------------------------------------------------------
ipu_clean %>%
  group_by(country) %>%
  summarise(nonNA_ip = sum(!is.na(ip)),
            nonNA_ue = sum(!is.na(unemployment)),
            nonNA_ip_pct = nonNA_ip / length(ip),
            nonNA_ue_pct = nonNA_ue / length(unemployment))
```

-----

In data science and machine learning, it can sometimes increase the predictive power of models to add transformations of existing variables. This is usually done in the modelling step, but to practice using the `mutate` function, let's do it here. Add three new columns to the dataframe: 

  1. the square of the industrial production percentage change, 
  2. the natural logarithm of the unemployment rate, and 
  3. the interaction (i.e. the product) of industrial production percentage change and unemployment rate.

(_Optional_: Calculate the year-to-year difference in each of unemployment rate and industrial production for those months with available data for both time series in 2019 and 2020. Note that there are several ways to do this computation, some can be simpler than the solution sketch here)

```{r q3}
# Q3 --------------------------------------------------------------------------
# Data transformations with mutate
ipu_clean %>%
  mutate(ip_sq = ip ^ 2,
         unemployment_ln = log(unemployment),
         ip_unemployment = ip * unemployment) %>%
  head()

# Optional --------------------------------------------------------------------
library(lubridate)
(yeartoyear <- ipu_clean %>%
  mutate(yr = year(dmy(date)),
         mth = month(dmy(date))) %>%
  select(-date) %>%
  pivot_wider(id_cols = c("country", "mth"),
              names_from = yr, names_prefix = "yr",
              values_from = c("ip", "unemployment")) %>%
  mutate(ip_yty = ip_yr2020 - ip_yr2019,
         unemployment_yty = unemployment_yr2020 - unemployment_yr2019) %>%
  select(country, mth, ip_yty, unemployment_yty) %>%
  drop_na())

head(yeartoyear)

```
