Skip to content

Instantly share code, notes, and snippets.

View timcdlucas's full-sized avatar

Tim Lucas timcdlucas

View GitHub Profile
@timcdlucas
timcdlucas / orth.R
Created July 9, 2019 08:33
Orthogonal polygnomials
library(ggplot2)
set.seed(1001)
start <- 2
end <- 20
N <- 100
x <- runif(N, start, end)
library(dplyr)
# If we don't have 'TOYOTA PRADO' in the set?
test <- c('ROVER', 'CRUISER', 'TOYOTA', 'TOYOTA PRADO', 'NISSAN')
old <- c('ROVER', 'CRUISER', 'TOYOTA')
new <- c('LandRover', 'LandRover', 'Toyota')
@timcdlucas
timcdlucas / dist_to_point.R
Created October 4, 2019 09:28
A markdown script for looking at geographic random forests.
#'---
#'output:
#' pdf_document:
#' number_sections: true
#' toc: true
#' toc_depth: 2
#'title: "RandomForest with distance to points"
#'author: Tim Lucas
#'fontsize: 8pt
@timcdlucas
timcdlucas / pca_log.R
Created October 4, 2019 18:47
pca with log + eps
# pca
d <- data.frame(x = runif(100))
d$y <- d$x + runif(100, -0.01, 0.01)
plot(y ~ x, data = d)
@timcdlucas
timcdlucas / pals.R
Created November 25, 2019 14:16
palettes and patchwork
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
library(paletteer)
library(ggplot2)
ggplot(mtcars, aes(mpg, disp, colour = factor(cyl))) +
geom_point() +
scale_colour_paletteer_d('palettetown', 'spearow') +
ggtitle('Spearow loves mtcars') +
@timcdlucas
timcdlucas / fct_reorder.R
Created November 26, 2019 11:00
Use fct_reorder in facet_wrap
library(forcats)
library(ggplot2)
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point() +
facet_wrap(~ factor(gear))
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point() +
@timcdlucas
timcdlucas / alpha_ribbon.R
Created January 7, 2020 14:11
alpa ribbons
head(mtcars)
a <- 0.1
ggplot(mtcars, aes(mpg, disp)) +
geom_smooth(method = 'lm', level = 0.99, alpha = a) +
geom_smooth(method = 'lm', level = 0.97, alpha = a) +
geom_smooth(method = 'lm', level = 0.94, alpha = a) +
geom_smooth(method = 'lm', level = 0.92, alpha = a) +
#'---
#'output:
#' pdf_document:
#' number_sections: true
#' toc: true
#' toc_depth: 2
#' keep_tex: true
#'title: "test"
#'author: Tim Lucas
@timcdlucas
timcdlucas / get_preds.R
Created February 14, 2020 16:07
Get the out of sample predictions from the best set of hyperparameters
get_preds <- function(t){
stopifnot(inherits(t, 'train'))
oos <- t$pred %>% arrange(rowIndex)
row_matches <- sapply(1:length(t$bestTune), function(x) oos[, names(t$bestTune)[x]] == t$bestTune[[x]])
best_rows <- rowMeans(row_matches) == 1
d <- oos[best_rows, ]
set.seed(17022020)
# "inside" people and countries
names_in <- c('lisa', 'suzanne', 'rohan')
countries_in <- c('germany', 'austria-hungary', 'italy', 'france')
# "outside"
names_out <- c('tim', 'jen')