---
title: "Avondale: A Local Geography Case Study"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Avondale: A Local Geography Case Study}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", 
  echo = FALSE, 
  message = FALSE, 
  warning = FALSE
)
```

```{r setup}
library(cincy)
library(dplyr)
library(tmap)
library(ggplot2)
tmap_mode("view")
```

## Neighborhood

When trying to describe local, place-based characteristics, the typical geographic unit of interest is [neighborhood](vignettes/geographies.html#neighborhoods). Neighborhoods are where people live and interact and tend to have their own identities. However, neighborhoods are not always well-defined and few data products are available directly at the neighborhood level. 

Here we will use the Avondale neighborhood in Cincinnati, Ohio to examine potential issues when working with local geographies. In Cincinnati, neighborhoods are defined by neighborhood community councils. However, other definitions (such as Statistical Neighborhood Approximations) have modified neighborhood boundaries to align with Census tract boundaries and are therefore more commonly used in research. 

```{r}
avondale_tract_aligned <- cincy::neigh_cchmc_2010 |>
  filter(neighborhood_2010 == "Avondale") |>
  mutate(neighborhood = "Avondale - Tract Aligned")

avondale_community_council <- cincy::neigh_ccc |>
  filter(neighborhood == "Avondale") |>
  mutate(neighborhood = "Avondale - Community Council")

avondale_tracts_2010 <- cincy::tract_tigris_2010 |>
    filter(census_tract_id_2010 %in% c("39061006900", 
                                "39061006800", 
                                "39061006600", 
                                "39061027000"))

tmap_mode("view")

tm_basemap(leaflet::providers$CartoDB.Positron) +
  tm_shape(avondale_community_council) +
  tm_polygons(alpha = 0.5,
              col = "red", 
              border.col = "gray") +
  tm_shape(avondale_tract_aligned) +
  tm_polygons(alpha = 0.3, 
              col = "blue") +
  tm_shape(avondale_tracts_2010) +
  tm_borders(col = "black") +
  tm_text("census_tract_id_2010", 
          col = "black")
```

## Approximating neighborhood using tracts

[Census tracts](vignettes/geographies.html#census-tracts) are often used to approximate neighborhoods. As a common unit of area defined by the U.S. Census Bureau, tracts are relatively stable over time and have an average population of 4,000 residents. Although not based on governmental neighborhood boundaries, tracts are meant to describe contextually similar areas. 

In Hamilton County, there were 230 tracts in 2000, 222 in 2010, and 226 in 2020. Tracts can be merged or split every ten years by the Census Bureau, but tract identifiers are always unique. For example, the 2000 tracts `39061006700` and `39061003400` are merged in 2010, and the new tract is given the distinct identifier `39061027000`. This means that Avondale is comprised of five vintage 2000 tracts, but only four vintage 2010 tracts. The tracts that make up Avondale did not change from 2010 to 2020. 

```{r}
avondale_tracts_2000 <- cincy::tract_tigris_2000 |>
  filter(census_tract_id_2000 %in% c("39061006900", 
                                "39061006800", 
                                "39061006600", 
                                "39061006700", 
                                "39061003400"))

p1 <- ggplot() +
  geom_sf(data = avondale_tracts_2000, aes(fill = census_tract_id_2000)) +
  ggsflabel::geom_sf_label(data = avondale_tracts_2000, 
                           aes(label = census_tract_id_2000)) +
  ggthemes::theme_map() +
  theme(legend.position = "none")

p2 <- ggplot() +
  geom_sf(data = avondale_tracts_2010, aes(fill = census_tract_id_2010)) +
  ggsflabel::geom_sf_label(data = avondale_tracts_2010, 
                           aes(label = census_tract_id_2010)) +
  ggthemes::theme_map() +
  theme(legend.position = "none")

cowplot::plot_grid(p1, p2, nrow = 1, labels = c("2000", "2010"))
```

## Approximating neighborhood using ZCTA

ZIP codes and their corresponding [Zip Code Tabulation Areas](vignettes/geographies.html#zip-codes) (ZCTAs) are also used to approximate neighborhood due to their accessibility. A ZIP code is a component of postal addresses and is available when neighborhood and/or tract may not be. ZIP codes can also sometimes be shared when full addresses cannot. However, ZCTA boundaries are based on postal routes rather than contextual neighborhood characteristics, and therefore are not always good neighborhood approximations. Here we show how Avondale aligns with the surrounding ZCTAs.

```{r}
zcta <- cincy::zcta_tigris_2010 |>
  filter(zcta_2010 %in% c("45229",
                     "45217",
                     "45220",
                     "45219",
                     "45206",
                     "45207")) |>
  mutate(main = ifelse(zcta_2010 == "45229", TRUE, FALSE))

tm_basemap(leaflet::providers$CartoDB.Positron) +
  tm_shape(zcta) +
  tm_polygons(alpha = 0.5,
              col = "main",
              palette = c("gray90", "gray50"),
              legend.show = FALSE) +
  tm_text("zcta_2010") +
  tm_shape(avondale_tract_aligned) +
  tm_polygons(alpha = 0.3,
              col = "blue")
```

The 45229 ZCTA is most commonly used to represent Avondale, but includes North Avondale and excludes the most Western, Eastern, and Southern parts of the neighborhood. 

```{r}
neigh_int <- cincy::interpolate(dep_index, to = cincy::neigh_cchmc_2010, "pop") |>
  sf::st_drop_geometry() |>
  filter(neighborhood_2010 == "Avondale") |>
  group_by(neighborhood_2010) |>
  transmute(`Percent Poverty` = glue::glue("{round(fraction_poverty * 100)}%"), 
         `Percent Assisted Income` = glue::glue("{round(fraction_assisted_income * 100)}%"),
         `Percent High School Education` = glue::glue("{round(fraction_high_school_edu * 100)}%"),
         `Percent No Health Insurance` = glue::glue("{round(fraction_no_health_ins * 100)}%"),
         `Percent Vacant Housing` = glue::glue("{round(fraction_vacant_housing * 100)}%"),
         `Median Income` = glue::glue("${substr(median_income, 1, 2)}k"),
         `Deprivation Index` = glue::glue("{round(dep_index,2)}"), 
         `Percent Black Non-Hispanic/Latino` = "78%") |>
  rename(geography = neighborhood_2010)

zcta_int <- cincy::interpolate(dep_index, to = cincy::zcta_tigris_2010, "pop") |>
  sf::st_drop_geometry() |>
  filter(zcta_2010 == "45229") |>
  group_by(zcta_2010) |>
  transmute(`Percent Poverty` = glue::glue("{round(fraction_poverty * 100)}%"), 
         `Percent Assisted Income` = glue::glue("{round(fraction_assisted_income * 100)}%"),
         `Percent High School Education` = glue::glue("{round(fraction_high_school_edu * 100)}%"),
         `Percent No Health Insurance` = glue::glue("{round(fraction_no_health_ins * 100)}%"),
         `Percent Vacant Housing` = glue::glue("{round(fraction_vacant_housing * 100)}%"),
         `Median Income` = glue::glue("${substr(median_income, 1, 2)}k"),
         `Deprivation Index` = glue::glue("{round(dep_index,2)}"), 
         `Percent Black Non-Hispanic/Latino` = "65%") |>
  rename(geography = zcta_2010)

# hc_pov <- tibble::tibble(geography = "Hamilton County", 
#                          `Percent Poverty` = "16%")

knitr::kable(bind_rows(neigh_int, zcta_int))
```
In this case, using ZCTA to approximate neighborhood would result in underestimating the percent of the population living in poverty by about 7%, and the percent of Black Non-Hispanic/Latino residents by about 13%. The estimates in the table above are based on census tract-level measures from the American Community Survey.

