Skip to content

Instantly share code, notes, and snippets.

View GarrettMooney's full-sized avatar

Garrett Mooney GarrettMooney

View GitHub Profile
@GarrettMooney
GarrettMooney / get_electricity_data.R
Last active February 26, 2018 03:11
script to get electricity data from pjm.com
library(purrr)
data_dir <- here::here()
base_url <- "http://www.pjm.com"
url <- sprintf("%s/pub/operations/hist-meter-load", base_url)
years <- 1993L:2018L
# check for robots.txt
robotstxt::get_robotstxt(base_url)
@GarrettMooney
GarrettMooney / h2o_auc.R
Created February 28, 2018 04:07
function to get AUC from H2O models
#' description:
#' Get the AUC for the {validation,test} data set for a given model.
#' usage:
#' list(best_dl, best_gbm) %>% map_dbl(h2o_auc_v) %>% set_names('DL AUC', 'GBM AUC')
#' list(best_dl, best_gbm) %>% map_dbl(h2o_auc_t) %>% set_names('DL AUC', 'GBM AUC')
library(h2o)
library(purrr)
h2o_auc_t <- compose(h2o.auc, partial(h2o.performance, newdata = test))
h2o_auc_v <- compose(h2o.auc, partial(h2o.performance, newdata = valid))
@GarrettMooney
GarrettMooney / multi_class_logistic_regression.Rmd
Last active April 30, 2018 00:18
multi-class logistic regression
Multiclass classifier
----
```{r, include=FALSE}
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
cache = TRUE,
echo = FALSE,
out.width = "70%",
@GarrettMooney
GarrettMooney / spike_and_slab_prior.Rmd
Created March 4, 2018 08:51
plots of spike and slab prior
Spike and Slab Prior
----
```{r, message=FALSE}
library(magrittr)
```
```{r}
prior_plot <- function(lower, upper, sigma, ...) {
seq(lower, upper, l=100) %>% plot(., dnorm(., 0, sigma))
abline(v= 0, lty = 2, col = "red")
@GarrettMooney
GarrettMooney / rustinr.R
Last active March 4, 2018 13:29
fibonacci comparison of rust vs. compiled R vs. R
compiler::enableJIT(0)
compiler::enableJIT(0)
rustinr::rust(code = '
// #[rustr_export]
pub fn fib_rust(a : i32) -> RResult<f64>{
let mut first = 0.0;
let mut second = 1.0;
let mut third = 0.0;
for _ in 0..a {
@GarrettMooney
GarrettMooney / get_code.R
Last active March 8, 2018 01:41
Get code from blog
library(rvest)
library(purrr)
library(stringr)
url <- "http://ellisp.github.io/blog/2016/11/06/forecastxgb"
robotstxt::get_robotstxt(url)
url %>%
read_html() %>% html_nodes("span") %>% html_text() %>%
reduce(., str_c) %>%
@GarrettMooney
GarrettMooney / prediction_by_matrix_multiplication.R
Created August 30, 2018 01:06
Predict for multiple models using linear algebra.
library(purrr)
# two regressions
lm_fit <- lm(mpg ~ wt + cyl, data = mtcars)
bayes_fit <- rstanarm::stan_glm(mpg ~ wt + cyl, data = mtcars)
# design matrix [32 x 3]
X <- model.matrix(lm_fit)
# coefficient matrix [3 x 2]
@GarrettMooney
GarrettMooney / create_gist.R
Created August 30, 2018 01:08
Create gist via R.
library(gistr)
# create
gist_create(files='../create_gist.R',
description='Create gist via R.')
# update
gists(what = "minepublic")[[1]] %>%
update_files('../create_gist.R') %>%
update()
@GarrettMooney
GarrettMooney / closed_form_ridge_regression.R
Created September 5, 2018 03:46
Closed form ridge regression.
library(tidyverse)
y <- as.matrix(mtcars$mpg)
X <- model.matrix(mpg ~ ., mtcars)
# linear regression
solve(crossprod(X)) %*% t(X) %*% y
solve(crossprod(X) + 0 * diag(rep(1, ncol(X)))) %*% t(X) %*% y
# ridge regression
@GarrettMooney
GarrettMooney / .Rprofile
Last active September 20, 2018 03:04
.Rprofile
.First <- function() {
# set TZ if unset
if (is.na(Sys.getenv("TZ", unset = NA)))
Sys.setenv(TZ = "America/New_York")
# bail if revo R
if (exists("Revo.version"))
return()