Skip to content

Instantly share code, notes, and snippets.

View mrecos's full-sized avatar

Matt Harris mrecos

View GitHub Profile
@mrecos
mrecos / brms_categorical.R
Created January 22, 2020 03:04
Example of brms model for fitting Bayesian (Stan) categorical model in 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 / Boxplot_compare.R
Last active May 6, 2023 19:28
Code for blog post: http://matthewdharris.com/2016/03/29/boxplot-or-not-to-boxplot-woe-ful-example/ A post to compare a bunch of visualizations against the boxplot.
library("data.table")
library("rowr")
library("dplyr")
library("ggplot2")
library("Information")
library("knitr")
library("ggrepel")
library("ggthemes")
library("ggalt")
library("xtable")
@mrecos
mrecos / model rank snippet.R
Created October 25, 2022 12:43
Code snippet to query O/T Pairs
## DR Set up
# predict on all 65M rows of DRPrimaryv4.csv
# using deployment [BOOKED predcitons] (634728e0ba6623c209179662)
# using model [eXtreme Gradient Boosted Trees Classifier with Early Stopping (learning rate =0.01) (Fast Feature Binning) M60 BP52] (63457d2fcc011ba8bee7ec78)
# from project Test1004 created by Adam
## Post-processing (R & SQL)
# load predictions (result-6356a08dabe595d288af9a3e.csv) into SQLite DB
sqlite_file <- "AMN.sqlite"
@mrecos
mrecos / Model Compare.R
Created October 3, 2022 18:38
Comparing models and MODEL RANK
library(readr)
library(tidyverse)
library(glue)
library(DataRobotColors)
pred <- read.csv("DATA/Predictions/[MDH]_DRPrimaryV3_10pcnt_SF_20221003_eXtreme_Gradient_Boosted_Trees_Classifier_with_Ear_(57)_79.93_IF_-_MDH_Reduced_v2_holdout.csv")
pred_rank <- pred %>%
group_by(TRAVELERID) %>%
arrange(TRAVELERID, desc(Cross.Validation.Prediction)) %>%
@mrecos
mrecos / google_dist_matrix.R
Created March 9, 2022 20:12
Example of using the Google API and {googleway} package to create time and distance matrices
# google distance example
library(tidyverse)
library(googleway)
api_key <- "GET A GOOGLE API KEY"
## set up a data.frame of locations
## can also use 'lat/lon' coordinates as the origin/destination
df_locations <- data.frame(
origin = c("Melbourne, Australia", "Sydney, Australia","Canberra, Australia")
@mrecos
mrecos / HW_EMAIL.r
Last active October 4, 2021 14:50
MUSA 508 HW email script
library(gmailr)
library(tidyverse)
library(glue)
library(kable)
library(knitr)
library(googledrive)
# need to get these values for your google drive account
gm_auth_configure(key = "LOTS_OF_NUMBERS_AND_LETTERS.apps.googleusercontent.com",secret = "SECRET_KEY")
gm_oauth_app()
dat <- data.frame(Response = c("Yes","No"), Value = c(0.32,0.68))
ggplot(dat, aes(x = Response, y = Value)) +
geom_bar(stat = "identity", width = 0.5) +
xlab("") +
ylab("") +
theme_bw() +
scale_y_continuous(limits = c(0,1)) +
geom_text(aes(label = paste0(Value*100,"%")), vjust = -1,
hjust = 0.5, face = "bold", size = 6) +
@mrecos
mrecos / precip_deviation_by_year.R
Created December 17, 2018 00:58
Code for downloading and plotting deviation in average precipitation for a given weather station. Using R and ggplot
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 / leaflet_flyTo_Shiny.r
Last active October 1, 2020 19:14
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
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)
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')