---
title: "Week 9"
author: "[CSDE Student](mailto:???)"
date: '`r format(Sys.time(), "%Y-%m-%d %H:%M")`'
header-includes: #allows you to add in your own Latex packages
- \usepackage{float} #use the 'float' package
- \floatplacement{figure}{H} #make every figure with caption = h
output: 
    html_document:
        number_sections: true
        self_contained: true
        code_folding: hide
        toc: true
        toc_float:
            collapsed: true
            smooth_scroll: false
    pdf_document:
        number_sections: true
        toc: true
        fig_cap: yes
        keep_tex: yes
urlcolor: blue        
---

<!--these following settings control width and also make a DIV tag for black outlines for figures-->
<style type="text/css">
.main-container {
  max-width: 2400px !important;
  margin-left: auto;
  margin-right: auto;
}
</style>

<style>
.outline {
   background-color: #000000;
   padding:1px;
   display: inline-block;
}
</style>


```{r setup, message=FALSE}
# key setups
pacman::p_load(tidyverse, sf, tidyverse, magrittr, mapview, knitr, kableExtra, captioner)

# captions
library(captioner)
table_nums <- captioner(prefix = "Table")
figure_nums <- captioner(prefix = "Figure")

# for having DIV tags
knitr::knit_hooks$set(class = function(before, options, envir) {
    if (before) {
        sprintf("<div class = '%s'>", options$class)
    } else {
        "</div>"
    }
})

# chunk options
knitr::opts_chunk$set(warning = FALSE, message = FALSE)

# path to this file name
if (!interactive()) {
    fnamepath <- as.character(sys.call(1))[2]
}
```

# Read the data

```{r}
# layers in the GDB
st_layers("H:/geocoding_2022/geocoding_data_20220310.gdb")

# read the coffee shops layer
coffee_run1 <- st_read(
    dsn = "H:/geocoding_2022/geocoding_data_20220310.gdb",
    layer = "BeanBox_Top_24_Coffee_Shops_2022_03_07_run1"
)

coffee_run2 <- st_read(
    dsn = "H:/geocoding_2022/geocoding_data_20220310.gdb",
    layer = "BeanBox_Top_24_Coffee_Shops_2022_03_07_run2"
)

# lowercase the names
names(coffee_run1) %<>% str_to_lower()
names(coffee_run2) %<>% str_to_lower()

# geometry columns
st_geometry(coffee_run1) <- "shape"
st_geometry(coffee_run2) <- "shape"
```

# A simple plot

```{r}
plot(coffee_run1$shape)
plot(coffee_run2$shape, pch = 2, add = TRUE)
```

# A `mapview` graphic

```{r}
mapview(coffee_run1)
```

# Descriptive statistics

## Coffee shops, run 1
### Summarize by locator
```{r}
# locator
coffee_run1 %>%
    st_set_geometry(NULL) %>%
    group_by(loc_name) %>%
    summarize(
        n = n(),
        score_mean = mean(score),
        score_sd = sd(score)
    ) %>%
    kable() %>%
    kable_styling(
        bootstrap_options = c("striped", "hover", "condensed"),
        full_width = FALSE,
        position = "left"
    )
```

### Summarize by status

```{r}
# status
# mean and SD of score
coffee_run1 %>%
    st_set_geometry(NULL) %>%
    group_by(status) %>%
    summarise(
        n = n(),
        score_mean = mean(score),
        score_sd = sd(score),
        .groups = "drop"
    ) %>%
    kable() %>%
    kable_styling(
        bootstrap_options = c("striped", "hover", "condensed"),
        full_width = FALSE,
        position = "left"
    )
```

# Summarize by status and match type

```{r}
# status, match type
# mean and SD of score
coffee_run1 %>%
    st_set_geometry(NULL) %>%
    group_by(status, match_type) %>%
    summarise(
        n = n(),
        score_mean = mean(score),
        score_sd = sd(score),
        .groups = "drop"
    ) %>%
    kable() %>%
    kable_styling(
        bootstrap_options = c("striped", "hover", "condensed"),
        full_width = FALSE,
        position = "left"
    )
```


## Coffee shops, run 2

### Summarize by locator name

```{r}
# locator
coffee_run2 %>%
    st_set_geometry(NULL) %>%
    group_by(loc_name) %>%
    summarize(
        n = n(),
        score_mean = mean(score),
        score_sd = sd(score),
        .groups = "drop"
    ) %>%
    kable() %>%
    kable_styling(
        bootstrap_options = c("striped", "hover", "condensed"),
        full_width = FALSE,
        position = "left"
    )
```

### Summarize by status and match type

```{r}
# status
# mean and SD of score
coffee_run2 %>%
    st_set_geometry(NULL) %>%
    group_by(status, match_type) %>%
    summarise(
        n = n(),
        score_mean = mean(score),
        score_sd = sd(score),
        .groups = "drop"
    ) %>%
    kable() %>%
    kable_styling(
        bootstrap_options = c("striped", "hover", "condensed"),
        full_width = FALSE,
        position = "left"
    )
```

## Rat sightings
Rat sightings were downloaded from the [NYC open data portal](https://data.cityofnewyork.us/Social-Services/Rat-Sightings/3q43-55fe).

After an initial geocoding pass, many locations did not geocode. Examination of the data showed that some of the locations were cross streets that had not value in the `Incident.Address` column. The code here concatenated the two streets with an ampersand (`&`) where the original `Intersection.Address` was blank. A new CSV was created for geocoding.

```{r}
rats_in <- read.csv("H:/geocoding_2022/data/Rat_Sightings_20220308.csv")

# cross streets
rats_in %>% 
    mutate(
        case_when(
            Incident.Address == "" ~ paste(Intersection.Street.1, "&", Intersection.Street.2),
            TRUE ~ Incident.Address
            )) %>% 
    write.csv("H:/geocoding_2022/data/Rat_Sightings_20220309.csv", row.names = FALSE)
```

