Skip to content

Instantly share code, notes, and snippets.

View alexpghayes's full-sized avatar

alex hayes alexpghayes

View GitHub Profile
@ledell
ledell / h2oautoml_get_cv_metrics.R
Created April 9, 2020 22:34
How to get k-fold metrics for all the H2O AutoML models in R
# How to get k-fold metrics for all the H2O AutoML models in R
# Adapted from: http://docs.h2o.ai/h2o/latest-stable/h2o-docs/automl.html
library(h2o)
h2o.init()
# Import a sample binary outcome train/test set into H2O
train <- h2o.importFile("https://s3.amazonaws.com/erin-data/higgs/higgs_train_10k.csv")
test <- h2o.importFile("https://s3.amazonaws.com/erin-data/higgs/higgs_test_5k.csv")
@jnolis
jnolis / 3-legged-twitter-auth-lite.R
Last active August 7, 2021 19:30
Do 3-legged Twitter authentication for rtweet (lite version)
# _____ __ __
# |__ / / /__ ____ _____ ____ ____/ /
# /_ <______/ / _ \/ __ `/ __ `/ _ \/ __ /
# ___/ /_____/ / __/ /_/ / /_/ / __/ /_/ /
# /____/ /_/\___/\__, /\__, /\___/\__,_/
# /____//____/
# RTWEET + 3-LEGGED-AUTH DEMO (LITE)
# This code demonstrates how to do 3-legged authentication for Twitter
# using the {rtweet} package. Based heavily on code from Michael Kearney.
@eoppe1022
eoppe1022 / tweet_markov_bot.R
Created November 6, 2018 17:41
Tweet Markov Bot
library(tidyverse)
library(rtweet)
library(markovifyR)
## Get tweets with "rtweet"
## FYI you need to set up "rtweet" beforehand
yost_data <- get_timeline("@travisyost", n = 10000)
## Take out RTs and create Markov model
markov_model <- yost_data %>%
@Dpananos
Dpananos / gist:6d4a156fa45e58c12e44655594dd2a86
Created September 15, 2018 16:39
Confidence interval simulation
library(tidyverse)
n.samples = 25
set.seed(5)
rerun(20,rnorm(n.samples)) %>%
map_dfr(~data_frame(data = list(.x)), .id = 'samples') %>%
mutate(mu = map_dbl(data,mean),
se = map_dbl(data,~sd(.x)/sqrt(length(.x))),
top = mu +1.96*se,
library(furrr)
# localhost -> AWS EC2 linux -> Docker running on that -> R
# dm_create() and dm_ip() are from an unreleased R pkg I whipped up, dockermachinery
# https://github.com/DavisVaughan/dockermachinery
# Creates 1 t2.micro EC2 instance
dm_create("amazonec2", "dockertest")
library(rstanarm); library(tidyverse)
options(mc.cores = parallel::detectCores())
set.seed(42)
data("radon")
head(treatment_sample)
# Some levels have no variance in the outcomes, making likelihood estimates impossible
# Adding a tiny bit of noise fixes the problem
radon$log_uranium <- rnorm(nrow(radon), radon$log_uranium, 0.05)
@thomasp85
thomasp85 / trim_model.R
Created October 24, 2017 07:26
Trim all unnecessary data from model objects
library(future)
trim_model <- function(model, predictor = predict, ..., ignore_warnings = TRUE) {
# Cache the correct output
true_pred <- predictor(model, ...)
# Treat prediction warnings as errors?
if (!ignore_warnings) {
old_ops <- options(warn = 2)
on.exit(options(old_ops))
}
// [[Rcpp::depends(xtensor)]]
#include <numeric>
#include "xtensor/xmath.hpp"
#include "xtensor-r/rarray.hpp"
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp14)]]
@noamross
noamross / find_local_tweeps.R
Created August 14, 2017 23:43
A visit to Durham
library(rtweet) #rtweet API creds should already be set up
library(stringi)
library(dplyr)
friends = get_friends(user="noamross")
followers = get_followers("noamross")
tweeps_id = distinct(bind_rows(friends, followers))
tweeps_info = lookup_users(tweeps_id$user_id)
# A regex for a visit to Durham
@mike-lawrence
mike-lawrence / gp_regression.stan
Last active April 7, 2020 18:15
GP Regression example
functions{
// GP: computes noiseless Gaussian Process
vector GP(real volatility, real amplitude, vector normal01, int n_x, real[] x ) {
matrix[n_x,n_x] cov_mat ;
real amplitude_sq_plus_jitter ;
amplitude_sq_plus_jitter = amplitude^2 + 1e-6 ;
cov_mat = cov_exp_quad(x, amplitude, 1/volatility) ;
for(i in 1:n_x){
cov_mat[i,i] = amplitude_sq_plus_jitter ;
}