---
title: "Week 1"
subtitle: "PA 470 Spring 2023"
author: "Eric Langowski"
output:
  html_document:
    df_print: paged
    theme: sandstone
    code_folding: hide
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```

# Welcome and Schedule

### 6:30pm Introductions

- Preferred Name & Pronouns
- Recent book or something you learned

### 7:00pm Course Introduction

### 7:45pm Break + Debugging (15 minutes)

### 8:00pm Coding Review

# Course Introduction

- Syllabus

- [Course Website](https://pa470spring2023.netlify.app/)

- Tech 'Stack' R/RStudio/RMarkdown

## Course Motivation

What should the relationship between the public sector and ML/AI be? Think of a few words and put them in the chat. 

## Optional Readings

- [Ask Delphi](https://delphi.allenai.org/?a1=Women+are+better+than+men.)
- [Crime Prediction Software Promised to Be Free of Biases. New Data Shows It Perpetuates Them](https://themarkup.org/prediction-bias/2021/12/02/crime-prediction-software-promised-to-be-free-of-biases-new-data-shows-it-perpetuates-them)
- [Crime Prediction Keeps Society Stuck in the Past](https://www.wired.com/story/crime-prediction-racist-history/)

- ![](https://mrkp-static-production.themarkup.org/graphics/2021-predpol_graphics-2021-predpol__national-map/1638323971066/assets/predpol_national_map-01.svg){width=50%}

## A framework

<div class='columns-2'>
![](https://inteng-storage.s3.amazonaws.com/img/iea/bjOLnW046e/uspus2.jpeg){width=75%}
![](https://1000logos.net/wp-content/uploads/2021/04/UPS-logo.png){width=75%}
</div>

## Coding Philosophy

- Reproducibility ** 2
- Iterative
- tidy
  - "the bottleneck in most data analysis is thinking time, not computing time." ~tidyverse manifesto
  
Review [Coding Warmup 0](https://pa470spring2023.netlify.app/coding-warmup-0/)

## Break

## Coding Review/EDA

- 1) Read in [Assessor [Archived 05-11-2022] - Residential Modeling Characteristics (Chicago)](https://datacatalog.cookcountyil.gov/Property-Taxation/Assessor-Archived-05-11-2022-Residential-Modeling-/8f9d-wy2d)


```{r, }

library(tidyverse)

file_loc <- '~/../Downloads/'

ccao <- read_csv(str_glue(file_loc, 
                          'Assessor__Archived_05-11-2022__-_Residential_Modeling_Characteristics__Chicago_.csv'))

str(ccao)

ccao %>% glimpse()
```


- 2) Explore/describe
- 3) Count property classes
- 4) Top Five Classes

```{r, }
# count property classes
ccao %>% count(`Property Class`)

ccao %>% group_by(`Property Class`) %>%
  summarize(n = n())

ccao %>% count(`Property Class`, sort=TRUE)

count(ccao, `Property Class`)

# top five classes

ccao %>% count(`Property Class`) %>%
  slice_max(order_by = `n`, n=5)
```

- 5) Keep only townships with Chicago in the name
- 6) Average rooms by property class

```{r, }
# keep only townships with Chicago in the name

ccao %>% distinct(`Township Name`)

ccao %>% filter(str_detect(`Township Name`, '(?<=g)o')) %>%
  count(`Township Name`)


# average rooms by property class

ccao %>% group_by(`Property Class`) %>%
  summarize(avg_rooms = mean(Rooms),
            count = n()) %>% view()
```


- 7) understand dates with lubridate
- 8) count sales in January 2019
- 9) count sales by month

```{r, }
# lubridate

library(lubridate)

ccao %>% select(`Sale Date`)

ccao %>% select(contains('Date'))

ccao %>% count(`Sale Date`, sort=TRUE)

ccao <- ccao %>%
  mutate(
    `Sale Date` = mdy_hms(`Sale Date`)
  )

ccao %>% count(`Sale Date`)

# count sales in January 2019

ccao %>% filter(
  month(`Sale Date`) == 1 & year(`Sale Date`) == 2019
)

ccao %>% mutate(
  new_date = floor_date(`Sale Date`, 'month')
) %>% select(new_date) %>% filter(
  !is.na(new_date),
  new_date == ymd_hms('2019-01-01 00:00:00'))

# count sales by month

ccao %>% group_by(
  month = floor_date(`Sale Date`, 'month')
) %>% summarize(
  count = n()
)
```


- 10) ggplot2
- 11) sales by week plot
- 12) sales price density

```{r, }
# ggplot2
library(ggplot2)

theme_set(theme_bw())

# sales by week

gdata <- ccao %>% 
  filter(!is.na(`Sale Date`)) %>%
  group_by(week = floor_date(`Sale Date`, 'week')) %>%
  summarize(count = n())

ggplot(gdata, aes(x=week, y=count)) +
  geom_point() +
  geom_line()

ggplot(gdata, aes(x=week, y=count)) +
  geom_point(size=2) + geom_line(linewidth=1.5) +
  scale_x_datetime(date_breaks = '6 months', date_labels='%m/%d/%y') +
  labs(x='Week of', y='Sales', title='Sales in Chicago by Week')

# sale price density

library(scales)

ggplot(ccao, aes(x=`Sale Price`)) +
  geom_density(fill='navy')

ggplot(ccao, aes(x=`Sale Price`)) +
  geom_density(fill='navy') +
  scale_x_log10(labels=dollar_format())

ggplot(ccao, aes(x=`Township Name`, y=`Sale Price`)) +
  geom_boxplot(fill='navy') +
  scale_y_log10(labels=dollar_format()) +
  coord_flip()
```

