## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval = FALSE-------------------------------------------------------------
# devtools::install()

## ----setup, message = FALSE, warning = FALSE----------------------------------
library(tripaccess)
library(tidyverse)

## ----message = FALSE, warning = FALSE, out.width = "70%"----------------------
#> Create readable labels for travel disability status and public transit use
person_transit <- person |>
  mutate(
    travel_disability_group = case_when(
      travel_disability == "No_disability" ~ "No Travel Disability",
      TRUE ~ "Travel Disability"
    ),
    public_transit_status = case_when(
      count_of_public_transit_usage > 0 ~ "Used Public Transit",
      TRUE ~ "Did Not Use Public Transit"
    )
  )

#> Sort labels
person_transit$travel_disability_sort_val <- factor(person_transit$travel_disability_group, levels = c("No Travel Disability", "Travel Disability"))
person_transit$public_transit_sort_val <- factor(person_transit$public_transit_status, levels = c("Did Not Use Public Transit", "Used Public Transit"))

#> Summary statistics of public transit use by travel disability status
transit_summary <- person_transit |>
  group_by(travel_disability_sort_val) |>
  summarize(
    people = n(),
    public_transit_users = sum(count_of_public_transit_usage > 0),
    public_transit_use_prop = mean(count_of_public_transit_usage > 0),
    public_transit_usage_median = median(count_of_public_transit_usage),
    public_transit_usage_mean = mean(count_of_public_transit_usage),
    public_transit_usage_sd = sd(count_of_public_transit_usage)
  )

transit_summary

#> Plot Public Transit Use by Travel Disability Status
person_transit |>
  count(travel_disability_sort_val, public_transit_sort_val) |>
  group_by(travel_disability_sort_val) |>
  mutate(public_transit_use_prop = n / sum(n)) |>
  filter(public_transit_sort_val == "Used Public Transit") |>
  ggplot(aes(x = travel_disability_sort_val,
             y = public_transit_use_prop,
             fill = travel_disability_sort_val)) +
  geom_col(width = 0.65) +
  scale_y_continuous(labels = function(x) paste0(round(100 * x), "%")) +
  labs(title = "Public Transit Use by Travel Disability Status",
       x = "Travel Disability Status",
       y = "Percent Who Used Public Transit",
       fill = "Travel Disability Status") +
  theme_bw() +
  theme(axis.text = element_text(size = 5),
        axis.title = element_text(size = 5),
        title = element_text(size = 5),
        legend.text = element_text(size = 4),
        legend.title = element_text(size = 4))

#> Test whether public transit use differs by travel disability status
prop.test(
  x = transit_summary$public_transit_users,
  n = transit_summary$people
)

