Skip to content

Instantly share code, notes, and snippets.

View samclifford's full-sized avatar

Sam Clifford samclifford

View GitHub Profile
@samclifford
samclifford / confint.gam.R
Last active February 25, 2022 10:31
A method for extracting confidence intervals for parametric terms from an mgcv gamObject and returning a tidy data frame
confint.gam <- function(mod, level = 0.95) {
# a method for extracting confidence intervals and returning a tidy data frame
require(mgcv)
require(dplyr)
mod.s <- summary(mod)
E <- data.frame(Estimate = mod.s$p.coeff) %>%
mutate(Term = row.names(.)) %>%
@samclifford
samclifford / get.nearest.neighbour.R
Created March 1, 2017 01:39
A function for snapping points to a grid. The grid doesn't even have to be regular!
library(raster)
library(tidyverse)
get.nearest.neighbour <- function(point_x,
point_y,
grid){
dists <- pointDistance(
matrix(c(point_x,point_y), nrow = 1),
as.matrix(grid[, c('longitude','latitude')]),
lonlat = F)
@samclifford
samclifford / adjacencyplot
Created April 18, 2017 05:26
Plotting an adjacency matrix
library(igraph)
library(tidyverse)
library(magrittr)
dat <- data.frame(A = c(1,1,1,2,3),
B = c(2,3,4,5,7))
Adj <- dat %>%
as.matrix %>%
graph.edgelist(directed=FALSE) %>%
samvif <- function(mod){
# mod is an mgcv object
# this function calculates the variance inflation factors for GAM as no one else has written code to do it properly
# this is used to summarise how well the GAM performed
mod.sum <- summary(mod)
s2 <- mod$sig2 # estimate of standard deviation of residuals
X <- mod$model # data used to fit the model
n <- nrow(X) # how many observations were used in fitting?
@samclifford
samclifford / brag-r-pack-hack.md
Last active May 2, 2017 09:05 — forked from njtierney/brag-r-pack-hack.md
Details for the BRAG R Pack Hack

R pack hack

Do you want to learn more about R packages and hang out from the nice people at BRAG?

Great! Me too.

Let's hang out this afternoon and discuss R packages from 5-7 in Y801.

We aren't aiming to put anything on CRAN, or make packages that are going to change the world and fabric of the universe.

@samclifford
samclifford / geom_qq_conf.R
Last active May 18, 2017 00:43
ggplot2 quantile-quantile plot that contains confidence bounds
# code copied and amended from http://stackoverflow.com/questions/4357031/qqnorm-and-qqline-in-ggplot2/
library(ggplot2)
gg_qq_conf <- function(x, distribution = "norm",
...,
line.estimate = NULL,
conf = 0.95,
labels = names(x)){
q.function <- eval(parse(text = paste0("q", distribution)))
@samclifford
samclifford / geom_wheat
Created June 25, 2017 13:58
Wheat plot
library(tidyverse)
tibble(x = rnorm(n=100)) %>%
arrange(x) %>%
mutate(bin = base::cut(x, breaks=pretty(x,n=20))) %>%
group_by(bin) %>%
mutate(count = 1:n()) %>%
ggplot(data=., aes(x=x, y=count)) +
geom_path(aes(group=bin)) +
geom_point(size=0.5) +
library(tidyverse)
library(magrittr)
library(purrr)
library(forcats)
base <- expand.grid(chem = c("Th", "Ta", "Nb", "La", "Ce", "P", "Zr", "Hf", "Sm", "Ti", "Y", "Yb", "Lu"),
id=1:7)
grouping <- data.frame(id=1:7,
@samclifford
samclifford / string_triangle_problem.R
Last active January 13, 2018 00:09
Discussing how often a string with two cuts will form a triangle
n_sims <- 1e5
breaking_convention <- "all_at_once" # or stick_breaking
if (breaking_convention == "stick_breaking"){
break_locations <- matrix(runif(n = n_sims), ncol=1)
sorted_break_locations <- cbind(break_locations, matrix(runif(n = n_sims, min = break_locations, max=1), ncol=1))
} else {
break_locations <- matrix(runif(n = 2*n_sims), ncol=2)
sorted_break_locations <- t(apply(break_locations, 1, sort))
@samclifford
samclifford / tuition.R
Last active April 4, 2018 05:37
#tidytuesday for US tuition data
library(tidyverse)
# download from https://github.com/rfordatascience/tidytuesday/blob/master/data/us_avg_tuition.xlsx
dat <- read_xlsx("us_avg_tuition.xlsx") %>%
gather(Year, Tuition, -State) %>%
separate(col = Year, into = c("Start", "End"), sep = "-") %>%
mutate_at(.vars = c("Start", "End"), .funs = parse_number) %>%
mutate(End = End + 2000)