This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(shinydashboard) | |
library(dplyr) | |
library(tidyr) | |
library(sf) | |
library(mapview) | |
library(DT) | |
# library(tigris) | |
library(leaflet) | |
# library(leafsync) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Load libraries, set options, source scripts --------------------------------- | |
library(tidyverse) | |
library(tidycensus) | |
library(sf) | |
library(tigris) | |
library(furrr) | |
options( | |
scipen = 999, | |
digits = 4, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
NewerOlder