Skip to content

Instantly share code, notes, and snippets.

View samclifford's full-sized avatar

Sam Clifford samclifford

View GitHub Profile
@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.

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 / 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) %>%
@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 / 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(.)) %>%