Skip to content

Instantly share code, notes, and snippets.

View timcdlucas's full-sized avatar

Tim Lucas timcdlucas

View GitHub Profile
@timcdlucas
timcdlucas / diffusion.R
Last active April 8, 2016 15:56
Diffusion based on direction and velocity raster
# Edit: whoops, radians
library(raster)
direction <- raster(nrows=100, ncols=100, xmn=0, xmx=10, , ymn=0, ymx=10)
values(direction) <- seq(0, 2 * pi, length.out = 100^2) + rnorm(100^2)
velocity <- raster(nrows=100, ncols=100, xmn=0, xmx=10, , ymn=0, ymx=10)
values(velocity) <- runif(100^2, 0, 0.1)
x <- c(NA, 5:1)
y <- c(5:1, NA)
psum <- function(x, y){
x[is.na(x)] <- 0
y[is.na(y)] <- 0
x + y
}
@timcdlucas
timcdlucas / gist:d303b70eb3140c10bfc96cd24e3f7004
Created September 23, 2016 09:09
bivariate colour palette
# Code by Dave Redding
library(raster)
zzz=100
colsX<-raster(nrow=zzz,ncol=zzz)
values(colsX)<-1:ncell(colsX)
extent(colsX)<-c(-100,100,-100,100)
cols<-c("#00FF00","#FFFFFF")
@timcdlucas
timcdlucas / gist:cdb52b68c8c01968e438f9dc1871771d
Created October 4, 2016 09:01
Extract homogenous subsets from tukeyHSD object.
library(ggplot2)
library(igraph)
set.seed(1)
# Make some data
x <- data.frame(y = c(rnorm(200), rnorm(200, 3), rnorm(200, 6)),
x = rep(letters[1:20], each = 30))
d <- data.frame(year = rep(2012:2016, each = 30),
y = rnorm(5 * 30))
ggplot(d, aes(y = y)) +
geom_boxplot(aes(x = factor(year))) +
geom_smooth(aes(x = as.numeric(factor(year))))
df1 <- data.frame(y = rnorm(100), x = letters[1:2])
df2 <- data.frame(aggregate(. ~ x, df1, mean),
upperCI = aggregate(. ~ x, df1, function(x) quantile(x, 0.025))[, 2],
lowerCI = aggregate(. ~ x, df1, function(x) quantile(x, 0.975))[, 2])
ggplot() +
geom_violin(data = df1, aes(x = x, y = y)) +
geom_errorbar(data = df2, aes(x = x, ymax = upperCI, ymin = lowerCI), width = 0.5) +
geom_point(data = df2, aes(x, y))
@timcdlucas
timcdlucas / gist:4e371eda4269910f2542d9e93b778f97
Created February 27, 2017 14:42
Violins vs bars with proportion data.
library(dplyr)
library(ggplot2)
d <- data.frame(x = rep(c('a', 'b'), 50), y = rbinom(100, 1, 0.4))
summary <- d %>%
group_by(x) %>%
summarise(prop = mean(y), CI = 0.95 * sqrt((sum(y)/50 * (50 - sum(y))/50) / 50))
library(dplyr)
library(glmnet)
library(splines)
library(tidyr)
library(ggplot2)
library(magrittr)
d <- data_frame(cov1 = rnorm(100),
cov2 = rnorm(100),
@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)
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