Skip to content

Instantly share code, notes, and snippets.

View timcdlucas's full-sized avatar

Tim Lucas timcdlucas

View GitHub Profile
imp <- list()
for(i in 1:50){
r <- randomForest::randomForest(mpg ~ ., data = mtcars, importance = TRUE)
m <- lm(mpg ~ ., data = mtcars)
m %>% summary
imp[[i]] <- r$importance[,1]
@timcdlucas
timcdlucas / gist:f75b7e8f4ebb9b7fe9ff12d8ea454ba8
Created August 29, 2018 14:34
Comparing speed of Random Forest like algorithms
# sandbox
library(caret)
library(tictoc)
g1 <- data.frame(mtry = c(2, 5))
tic()
# Assume just the first 16 parameters vary, but can do the same thing.
df <-
data.frame(
ad.surv.intercept=rep(0.0002516054, 16 * 2),
ad.surv.temp=0.1702,
ad.surv.pack=-0.1654,
disp.intercept=0.0064262,
disp.pack.size=0.10594,
ad.surv.intercept.vec <- c(0.99 * ad.surv.intercept, 1.01 * ad.surv.intercept)
ad.surv.temp.vec <- c(0.99 * ad.surv.temp, 1.01 * ad.surv.temp)
# Going to end up being...
# 5 runs. 1 with everything at the mean, 2 new ad.surv.intercept and 2 new ad.surv.tmep
N <- 5
df <- data.frame(rep(ad.surv.intercept, N), rep(ad.surv.temp.vec, 5))
df[1:2, 1] <- ad.surv.intercept.vec
df[3:4, 2] <- ad.surv.temp.vec
# Assume just the first few parameters vary, but can do the same thing.
ad.surv.intercept.vec <- c(0.0002516054 , 0.0001)
ad.surv.temp.vec <- c(0.1702, 0.2)
tic()
results <-
lapply(1:2, function(x)
library(caret)
m <- train(mpg ~ .,
data = mtcars,
method = 'glmnet',
tuneLength = 10)
plot(m)
# 1 covariate and intercept
d <- as.matrix(data.frame(intercept = 1, x1 = rnorm(100)))
# Many responses
many_ys <- matrix(d[, 'x1'] * rep(0:10, each = 100) + rnorm(11 * 100, sd = 0.1), ncol = 11)
# Empty list to save our models into.
models <- list()
@timcdlucas
timcdlucas / gist:b255cc1da7e557d72c0cc10129effbfc
Last active February 8, 2018 10:21
hsv two way colour ramp
#load packages
require(raster)
require(colorplaner)
require(ggplot2)
#here's some dummy data
r1<- raster(ncol=10, nrow=10)
set.seed(0)
values(r1) <- runif(ncell(r1))
x <- 1:100
n <- sapply(x, function(x) rpois(1, x))
pos <- sapply(1:length(n), function(i) rbinom(1, n[i], plogis(x[i] - 50)))
p <- pos / n
d <- data.frame(p = p, x = x)
m <- glm(p ~ x, data = d, weights = n, family = binomial)
m
@timcdlucas
timcdlucas / gist:19d9bc6c7963357076ee05dbae77bcce
Created September 11, 2017 12:10
Replacement operators for zoon
# Run a workflow, specifying one module of each type.
work1 <- workflow(occurrence = UKAnophelesPlumbeus,
covariate = UKAir,
process = OneHundredBackground,
model = LogisticRegression,
output = PrintMap)
# Current form
work1 <- ChangeWorkflow(work1, model = RandomForest)