Skip to content

Instantly share code, notes, and snippets.

View plnnr's full-sized avatar

Nick Kobel plnnr

View GitHub Profile
@plnnr
plnnr / tidymodels-workflow-and-plotting-utilities.Rmd
Last active March 16, 2023 03:48
Workflow examples of using tidymodels as well as two utility functions — generate simple table of multiple model metrics and effect plot for a logistic regression model.
---
title: "Workflow Examples"
output: html_notebook
---
## Train-test workflow examples
Two examples presented (2/2 is still TBD). The first is linear regression. The second is logistic regression.
### Linear regression `tidymodel` workflow
@plnnr
plnnr / stl_od_explorer_shiny_app.R
Last active April 13, 2023 16:29
Shiny dashboard app that loads OD data from StreetLight and allows users to explore origin-destination pairs. Has some bugs with the class breaks but mostly functions. Will update periodically.
library(shiny)
library(shinydashboard)
library(dplyr)
library(tidyr)
library(sf)
library(mapview)
library(DT)
# library(tigris)
library(leaflet)
# library(leafsync)
@plnnr
plnnr / interpolatepw_multiple_inputs.R
Last active July 24, 2022 04:02
Demonstration on how to use interpolate_pw() from tidycensus on multiple input tables with different input years and different weighting mechanisms.
## Load libraries, set options, source scripts ---------------------------------
library(tidyverse)
library(tidycensus)
library(sf)
library(tigris)
library(furrr)
options(
scipen = 999,
digits = 4,
@plnnr
plnnr / count_monthly_weekdays.R
Last active February 3, 2022 19:28
Return an integer of the number of weekdays in a month before or after a specified input date. Helpful for transit planning and NTD reporting when a sign-up period is in a split month. I.E. when the operator and route schedules change in the middle of a month and you must compute a weighted average based on the number of days within a signup per…
## Returns an integer of the number of weekdays in a month before or after a specified input date
## Counts weekdays (M-F), weekends (Sat and Sun), Sat only or Sun only
## Borrowed code from @Thomas at link below:
## https://stackoverflow.com/questions/29252520/using-r-to-count-number-of-specific-days-in-a-specific-month
count_monthly_weekdays <- function(x, direction, weekday = "weekend", inclusive = FALSE){
## x is the date from which to calculate remaining weekend days
## direction is whether you count before or after input date x
## weekday is the weekdays to count (saturday, sunday, both, weekday)
## inclusive T/F is whether to include the input date x in the days remaining
@plnnr
plnnr / long_to_wide_acs.R
Last active December 21, 2020 22:29
Summarize long ACS data and make into wide format, retaining the margins of error
## Helpful web pages:
# https://dcl-wrangle.stanford.edu/census.html
# https://tarakc02.github.io/dot-density/
## Run "install.packages("pacman")" in R console if you do not have pacman installed
pacman::p_load(tidyverse, tidycensus, tigris, sf, mapview)
## Optional if you want to write to a file geodatabase, uncomment and run code
# library(arcgisbinding)
# arc.check_product()
@plnnr
plnnr / spatial_aggregation.R
Last active March 23, 2020 17:01
Calculate spatially aggregated estimate and margin of error using k nearest neighbor matrix
pacman::p_load(tidyverse, tigris, tidycensus, sf, sp, mapview, spdep, spdplyr, leaflet, RColorBrewer)
options(tigris_use_cache = T); options(tigris_class = "sf")
pdx_msa_definition <- c('41005', '41009', '41051', '41067', '41071', '53011', '53059')
pdxcnty <- substr(pdx_msa_definition, start = 3, stop = 5)
lep_languages <- c('lep_spanish' = 'C16001_005', 'lep_french_hatian' = 'C16001_008',
'lep_germanic' = 'C16001_011', 'lep_slavic' = 'C16001_014',
'lep_oth_indoeuro' = 'C16001_017', 'lep_korean' = 'C16001_020',
'lep_chinese' = 'C16001_023', 'lep_vietnamese' = 'C16001_026',
@plnnr
plnnr / areal_interpolation_clean.R
Last active September 1, 2019 01:57
Areal interpolation with ACS data removing water and rights-of-way and clipping to filtered zoning area
if(!require(pacman)){install.packages("pacman");library(pacman)}
p_load(tidyverse, tidycensus, tigris, areal, sf, mapview, geojsonsf, rmapshaper, lwgeom)
options(tigris_use_cache = T); options(tigris_class = "sf")
## Download Portland zoning
zoning <- geojson_sf(readLines("https://opendata.arcgis.com/datasets/6b26f2ccb71d431f9ce8f34fd8ec1558_16.geojson")) %>%
st_transform(2913) %>% group_by(ZONE) %>% summarize()
## Download water featuers
water <- tigris::area_water("OR", "Multnomah", 2017) %>% summarize() %>% st_transform(2913)
@plnnr
plnnr / get_hh_income_by_race_2000.R
Last active August 22, 2018 18:14
Get household income by race, feeding in a vector of FIPS codes, a state and a county
get_hh_income_by_race_2000 <- function(vector_of_tract_fips, dec_state, dec_county) {
# Load essential libraries for function
if(!require(pacman)){install.packages(pacman);library(pacman)}
p_load(tidycensus, tidyverse)
# Name variables to gather
race_income_vars_00 <- c('P151A001', 'P151A002', 'P151A003', 'P151A004', 'P151A005', 'P151A006', 'P151A007', 'P151A008',
'P151A009', 'P151A010', 'P151A011', 'P151A012', 'P151A013', 'P151A014', 'P151A015', 'P151A016',
'P151A017', 'P151B001', 'P151B002', 'P151B003', 'P151B004', 'P151B005', 'P151B006', 'P151B007',
@plnnr
plnnr / get_median_hh_income_by_race_acs.R
Last active August 20, 2018 19:36
Get household income by race for ACS data (at least 2010 onward) by passing a list of tract FIPS codes to the function.
## Get median HH income by race for 2010 and 2016
# Returns an error that function parameter "acs_year" is not found.
get_median_hh_income_by_race_acs <- function(vector_of_tract_fips, compatibility_mode = "1990", acs_year = 2016, acs_state=c("OR", "WA"), acs_county = c("051", "005", "009", "067", "071", "011", "059")) {
if(!require(pacman)){install.packages(pacman);library(pacman)}
p_load(tidycensus, tidyverse)
race_income_vars_1016 <- c('B19001A_001', 'B19001A_002', 'B19001A_003', 'B19001A_004', 'B19001A_005', 'B19001A_006', 'B19001A_007',
'B19001A_008', 'B19001A_009', 'B19001A_010', 'B19001A_011', 'B19001A_012', 'B19001A_013', 'B19001A_014',
'B19001A_015', 'B19001A_016', 'B19001A_017', 'B19001B_001', 'B19001B_002', 'B19001B_003', 'B19001B_004',
'B19001B_005', 'B19001B_006', 'B19001B_007', 'B19001B_008', 'B19001B_009', 'B19001B_010', 'B19001B_011',
@plnnr
plnnr / get_median_hh_income_by_race_2000.R
Last active August 20, 2018 17:29
Get median income by race for the year 2000.
if(!require(pacman)){install.packages(pacman);library(pacman)}
p_load(tidyverse, tidycensus, rio, viridis, sf, leaflet, ggthemes)
# This function does not work with passing vectors to function parameters (e.g., dec_state and dec_county)
get_median_hh_income_by_race_2000 <- function(vector_of_tract_fips, compatibility_mode = "1990", dec_state=c("OR", "WA"), dec_county = c("051", "005", "009", "067", "071", "011", "059")) {
if(!require(pacman)){install.packages(pacman);library(pacman)}
p_load(tidycensus, tidyverse)
race_income_vars_00 <- c('P151A001', 'P151A002', 'P151A003', 'P151A004', 'P151A005', 'P151A006', 'P151A007', 'P151A008',