Skip to content

Instantly share code, notes, and snippets.

View DexGroves's full-sized avatar

DG DexGroves

  • London
View GitHub Profile
library("devtools")
install_github("DexGroves/gbm")
library("gbm")
# Data generation from gbm manual ----------------------------------------------
set.seed(112358)
N <- 1000
X1 <- runif(N)
X2 <- 2*runif(N)
X3 <- ordered(sample(letters[1:4],N,replace=TRUE),levels=letters[4:1])
library("gbm")
plot_gbm_contour <- function(gbm_obj, i.var, ...){
##############################################################################
# Plot a 3D contour plot for a gbm's partial dependencies. #
# Args: #
# gbm_obj: The gbm object. #
# i.var: Character vector of variables to show. #
# ...: Additional args passed to wireframe. #
# Returns: #
attempt_plot_gbm <- function(from_github){
# Try to make a simple partial dependency plot with a toy gbm
if (from_github){
devtools::install_github("gbm-developers/gbm")
}
else{
install.packages("gbm", repos = "http://cran.rstudio.com/")
}
library("gbm")
N <- 1000
benchmark_scoring <- function(repetitions, rows, n.trees){
# Score a n.trees gbm on rows, repetitions times, and time it.
library("gbm")
y <- seq(1, 2, length.out = rows)
number_x <- runif(rows)
factor_x <- sample(letters, replace = TRUE, size = rows)
factor_2 <- sample(LETTERS, replace = TRUE, size = rows)
g <- gbm(y ~ number_x + factor_x,
data = data.frame(y, number_x, factor_x),
handle_progressbar <- function(recurse_i, num) {
# Sort out the progress bar, and some messages while we're at it.
if (recurse_i == 1) {
if (num <= 10) {
message("Number is very large! This may take some time.")
} else {
message("Number is VERY large! This may take some time.")
}
pb <<- txtProgressBar(min = 0, max = num - 4, style = 3)
} else {
contrast_matrix <- function(object, data, ...) {
# Make a model matrix with one column per factor level, rather than base hacks
object_vars <- all.vars(object)
factor_vars <- colnames(data)[sapply(data, is.factor) &
colnames(data) %in% object_vars]
model.matrix(object = object,
...,
data = data,
contrasts.arg = lapply(data[, factor_vars],
contrasts,
library("Rcpp")
library("microbenchmark")
simulate_throws <- cppFunction('
double simulate_throw(NumericVector start, int nthrows, int trials) {
int trials_so_far = 0;
int final_throw_successes_so_far = 0;
int hits_so_far = 0;
int nstart = start.size();
@DexGroves
DexGroves / riddler_jan22.R
Created January 22, 2016 14:57
Stop ruining my Fridays fivethirtyeight.
library("igraph")
library("stringi")
library("magrittr")
generate_bridge_df <- function(nrow, ncol) {
this_layer <- generate_layer(ncol)
first_connections <- data.frame(from = "Northside",
to = get_layer_vertices(this_layer))
all_bridges <- rbind(first_connections, this_layer)
library("data.table")
library("xgboost")
library("Matrix")
generate_data <- function(N) {
data.table(
response = as.numeric(runif(N) > 0.8),
int1 = round(rnorm(N, 3, 3)),
int2 = round(rnorm(N, 3, 3)),
@DexGroves
DexGroves / xgboost_sparse_zeroes.R
Created March 1, 2016 10:37
xgboost sending sparse zeroes to missing node
library("data.table")
library("xgboost")
library("Matrix")
generate_data <- function(N) {
data.table(
response = as.numeric(runif(N) > 0.8),
int1 = round(rnorm(N, 3, 3))
)