Skip to content

Instantly share code, notes, and snippets.

Avatar

Matt Harris mrecos

View GitHub Profile
View file load.r
lancCounty <- st_read("C:/Users/matthew.d.harris/Documents/GitHub/Public-Policy-Analytics-Landing/DATA/Chapter2/LancasterCountyBoundary.geojson") %>%
st_transform('ESRI:102728')
uga <- st_read("C:/Users/matthew.d.harris/Documents/GitHub/Public-Policy-Analytics-Landing/DATA/Chapter2/Urban_Growth_Boundary.geojson") %>%
st_transform('ESRI:102728')
studyAreaTowns <- st_read("C:/Users/matthew.d.harris/Documents/GitHub/Public-Policy-Analytics-Landing/DATA/Chapter2/StudyAreaTowns.geojson") %>%
st_transform('ESRI:102728')
buildings <- st_read("C:/Users/matthew.d.harris/Documents/GitHub/Public-Policy-Analytics-Landing/DATA/Chapter2/LancasterCountyBuildings.geojson") %>% st_transform('ESRI:102728')
@mrecos
mrecos / leaflet_flyTo_Shiny.r
Last active Aug 14, 2020
A (mostly) minimal example of using sidebar drop downs to 1) filter counties within states, and 2) `flyTo` the centroid of the selected state in shiny + leaflet
View leaflet_flyTo_Shiny.r
library(shiny)
library(shinydashboard)
# devtools::install_github("nik01010/dashboardthemes")
library(dashboardthemes)
library(tidyverse)
library(sf)
library(leaflet)
library(usmap) # us counties and states as table
library(leaflet)
@mrecos
mrecos / assignment loop.r
Created Apr 24, 2020
basic assignment loop over last name and case load
View assignment loop.r
##### Your code leading up to this point ######
# initialize active cases
SWDetail$active_cases <- 0
SWDetail[1,"active_cases"] <- 1
# initialize most recent cases
SWDetail$last_case_given <- 0
SWDetail[1,"last_case_given"] <- 1
# resorting this by date
cps_test_assignment <- cps_test_assignment[order(cps_test_assignment$assignment_date),]
@mrecos
mrecos / tidymodels.R
Last active Mar 11, 2020
reproducible Tidymodels workflow example
View tidymodels.R
#Package installs -------------------------------------------------------------
load.fun <- function(x) {
x <- as.character(x)
if(isTRUE(x %in% .packages(all.available=TRUE))) {
eval(parse(text=paste("require(", x, ")", sep="")))
print(paste(c(x, " : already installed; requiring"), collapse=''))
} else {
#update.packages()
print(paste(c(x, " : not installed; installing"), collapse=''))
eval(parse(text=paste("install.packages('", x, "')", sep="")))
@mrecos
mrecos / brms_categorical.R
Created Jan 22, 2020
Example of brms model for fitting Bayesian (Stan) categorical model in R
View brms_categorical.R
library(brms)
library(tidyverse)
library(caret)
rstan_options(auto_write=TRUE)
options(mc.cores=parallel::detectCores ()) # Run on multiple cores
set.seed(3875)
ir <- data.frame (scale (iris[, -5]), Species=iris[, 5])
system.time (b2 <- brm (Species ~ Petal.Length + Petal.Width + Sepal.Length + Sepal.Width,
@mrecos
mrecos / sf point_in_poly.r
Last active Dec 4, 2019
A repro example to get data, and aggregate points into polygons over a list with purrr::map and then animate with ggplot
View sf point_in_poly.r
library(corrplot)
library(viridis)
library(stargazer)
library(tidyverse)
library(dplyr)
library(sf)
library(tigris)
library(ggplot2)
library(rgdal)
library(maptools)
@mrecos
mrecos / purrr_example_iris.r
Created Dec 3, 2019
Quick example of purrr::nest analysis
View purrr_example_iris.r
library(tidyverse)
g <- glimpse
g(iris)
dat <- iris %>%
nest(data = c(-Species))
dat$data[[1]]
@mrecos
mrecos / multiclass_confusion_matrix.R
Last active Mar 6, 2019
Reproducible example for the ggplot design and approach to making a mulitclass ggplot confusion matrix
View multiclass_confusion_matrix.R
### Example of ggplot code for multiclass confusion matrix with caret::confusionMatrix and ggplot
### `Example_plot1` is the result of applying `caret::confusionMatrix()` to the outcome ...
### of a model that included a reference class and a predicted class; both as factors
### calling `as.data.frame(Example_plot1$table)` casts the predicted class frequency table from the ...
### `caret::confusionMatrix()` object into a nice long format table of columns `Reference`, `Prediction`, and `Freq`.
### Do this for a bunch of models, and then use `cowplot::plot_grid()` to arrange them.
library(tidyverse)
library(cowplot)
library(caret)
@mrecos
mrecos / precip_deviation_by_year.R
Created Dec 17, 2018
Code for downloading and plotting deviation in average precipitation for a given weather station. Using R and ggplot
View precip_deviation_by_year.R
library('rnoaa')
library("tidyverse")
library("lubridate")
library("ggrepel")
token = 'GET YOUR API KEY at: http://www.ncdc.noaa.gov/cdo-web/token'
locs <- ncdc_locs(locationcategoryid='CITY', sortfield='name', sortorder='desc', token = token, limit = 800)
loc_data <- locs$data
dplyr::filter(loc_data, grepl(", PA",loc_data$name))
@mrecos
mrecos / Beach not Beach NN Loop.r
Last active Jan 11, 2018
R stats code for building NN and looping over hidden layer node density for animated gif output. NN code attributed to David Selby; http://selbydavid.com/2018/01/09/neural-network/
View Beach not Beach NN Loop.r
########################################################################
### Bespoke Neural Network R code attributed to: David Selby
### From blog post: http://selbydavid.com/2018/01/09/neural-network/
### Adapted here for making animated GIF of node density
### output gifs compiled at gifmaker.me for final output
### output tweeted here:
### https://twitter.com/Md_Harris/status/951257342418608128
########################################################################
two_spirals <- function(N = 200,
You can’t perform that action at this time.