---
title: "Exercise: Dates, Factors, and More EDA"
author: "Rian DeFaccio, for BH Analytics LLC"
date: "`r Sys.Date()`"
output: html_document
---

To begin, load any libraries you would like to use.

```{r}

```

### 1. Import Data

------------------------------------------------------------------------

Import the *data/Customer.csv* file. This has information on customers from a simulated banking system. Do a quick check of the data after you import it.

```{r}
# import data


# do a quick initial look

```

```{r}
# try another way to do an initial look at the data

```

### 2. Credit Score as Factor

------------------------------------------------------------------------

In the next code chunk, we create a discrete version of the `CreditScore` variable and plot the distribution of it's values.

```{r}
# build discrete credit score variable
customer <- customer %>%
  mutate(
    CreditScoreCat = case_when(
      CreditScore >= 300 & CreditScore <= 579 ~ 'Poor',
      CreditScore >= 580 & CreditScore <= 669 ~ 'Fair',
      CreditScore >= 670 & CreditScore <= 739 ~ 'Good',
      CreditScore >= 740 & CreditScore <= 799 ~ 'Very Good',
      CreditScore >= 800 & CreditScore <= 850 ~ 'Excellent',
      TRUE                                    ~ NA
    )
  )

# plot it's distribution
ggplot(customer, aes(CreditScoreCat)) +
  geom_bar()
```

If we look at the above plot, we can see that the ordering along the x-axis is not ideal. Can you think of a couple of ways to get the x-axis to display in order of **Poor** to **Excellent** credit score? You'll want to create a factor variable somehow and try making the plot again.

```{r}

```

### 3. `UrbanRural` as Factor

------------------------------------------------------------------------

We can also use factors as a quick way to clean up dirty variables. The `UrbanRural` variable has missing data represented in several ways, and we'd prefer for all those representations of missingness to be `NA`s.

```{r}
table(customer$UrbanRural, useNA = 'ifany')
```

A quick way to consolidate the representations of missing data is to create a factor variable and specify the levels you'd like to keep. Everything else will be converted to `NA`. Do this now, creating a new variable called `UrbanRuralFactor`. Additionally, change the labels in this new variable to [`City`, `Country`]. As always, check your work after you are done building a new variable.

```{r}
# build new variable


# check your work

```

### 4. Calculate Age

------------------------------------------------------------------------

Next we would like to calculate the ages of all customers as of today. You will need to do this in a couple of steps:

1.  Convert the `Birthdate` column into a proper date data type variable called `BirthDateDt`.
2.  Calculate `Age` as of today from `BirthDateDt`. This should be in units of years.
3.  Examine the distribution of `Age`, both **numerically** and **visually**

```{r}
# convert Birthdate to date variable


# calculate age as of today


# examine age numerically


# examine age visually


```

It would appear we have a problem. Can you see what it is in your previous visualization of `Age`?

Try sub-setting to only ages that are above 110 and get some summary statistics on that subset of the data.

```{r}
# summary stats of just the highest ages


```

From looking at the distribution of ages that were above 110, can you think of a possible data cleaning solution to our problem? If not, ask your instructor for a hint. If you've got it, implement that now, creating a new variable called `AgeClean`. Now look at the distribution of `AgeClean`.

```{r}
# create AgeClean


# examine the distribution of AgeClean


```

**BONUS:** We might want to also go back and clean up the `BirthDateDt` column in a similar way as how we just cleaned up `Age`. Use the appropriate lubridate time span variable type to add an appropriate amount of time to "bad" birth dates to create a new variable called `BirthDateDtClean`.

**Hint:** Use `case_when()` here instead of `ifelse()`. You need to be a little explicit in telling R how to deal with the `NA`s in this case.

```{r}


```

**BONUS:** Let's say you wanted to track which customers in your data set are either Baby Boomers or members of Generation X. Baby Boomers were born between 1946 and 1964 and members of Generation X were born between 1965 and 1980 (according to Wikipedia). Create a new variable called `Generation` that maps customers to either `Boomer`, `Gen X`, or `Other` based on their cleaned birth date. You can do this two ways:

1.  Directly do a comparison of dates: `BirthDateDt >= ymd('some_other_date')`
2.  Use an appropriate lubridate time span variable. Your options are: **Duration, Period, Interval**. Refer to the lesson notebook to review these types of variables.

If you can, demonstrate this both ways. After you have built your variables, think of a way to show that your variable is as expected. You could produce a numeric or visual display here.

```{r}


```

### 5. Exploring Two Continuous Variables

------------------------------------------------------------------------

Finally, let's examine the relationship between two continuous variables in this data set: `AgeClean` and `CreditScore`. Examine this relationship **numerically** and **visually**.

**Hint:** You will need to remove any rows from the data that are missing values in these two variables in order to calculate the appropriate summary statistic.

```{r}
# numeric assessment


# visual assessment


```

Can you describe any relationship between `AgeClean` and `CreditScore`?

------------------------------------------------------------------------

That's it for now!
