Skip to content

Instantly share code, notes, and snippets.

View DexGroves's full-sized avatar

DG DexGroves

  • London
View GitHub Profile
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,
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 {
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),
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
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: #
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])