Skip to content

Instantly share code, notes, and snippets.

View mrecos's full-sized avatar

Matt Harris mrecos

View GitHub Profile
@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) +
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 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)
@mrecos
mrecos / assignment loop.r
Created April 24, 2020 13:56
basic assignment loop over last name and case load
##### 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 March 11, 2020 02:13
reproducible Tidymodels workflow example
#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 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,