Skip to content

Instantly share code, notes, and snippets.

View cbrown5's full-sized avatar
🦈

Chris Brown cbrown5

🦈
View GitHub Profile
@cbrown5
cbrown5 / retransformation-bias.R
Created March 22, 2018 05:53
A simple demonstration of retransformation bias in log-linear models
#
# Simulate data
#
a <- 2
x <- seq(0, 1, length.out = 100)
n <- length(x)
sigma <- 0.5
set.seed(42)
lny <- rnorm(n, mean = a*x, sd = sigma)
y <- exp(lny)
@cbrown5
cbrown5 / gamma-priors-var-components.R
Last active November 25, 2021 01:12
Choosing gamma priors for variance components as per Fong et al. 2010 "Bayesian inference for generalized linear mixed models" Biostatistics 11
#Visualise priors for a variance component selected using the method of Fong et al. 2010 (see page 400 of that paper)
R <- log(10) #+-range. Use log for GLMs that have log-link functions(e.g. poisson) or no logs for a gaussian model with identity link
d <- 1 #Degrees of freedom for student t
quant <-0.95 #quantile you want random effect estimates to vary between
#Parameters of Gamma
a1 <- d/2
qtemp <- 1-(1-quant)/2
a2 <- (d/2) * (R^2)/(qt(qtemp, df = 1)^2)
@cbrown5
cbrown5 / semivariance-function.R
Created March 26, 2018 23:31
calculate semivariance for a given distance matrix
#Semivariance function for over water distance
semivariance <- function(xdists, yresp, ncats = NA){
#Function to calculate semivariance using a distance matrix
#
#Where:
#xdists is the distance matrix
#yresp is the response and rows of yresp correspond to sites in rows and columns of dists
#ncats is the resolution. Defaults to sturges rule for histogram classes (as per Legendre book)
@cbrown5
cbrown5 / inla-var-comp-pc-prior.R
Last active April 4, 2018 14:45
Visualise PC prior in INLA
library(INLA)
nprec <- 100000
prec <- seq(0.05, 1000, length.out = nprec) #precisions
sd <- 1/sqrt(prec)
U <- 1
alpha <- 0.025
lambda <- -log(alpha)/U #rate for exponential on sd
pr_prec <- inla.pc.dprec(prec, U, alpha) #Density for the precision.
pr_sd <- dexp(sd, rate = lambda) #Density for sd
#nb see inla.pc.dprec for how to get density for the precision.
@cbrown5
cbrown5 / SetUpRWindows10
Created June 26, 2018 05:02 — forked from amandamiotto/SetUpRWindows10
Tips for setting up R packages on windows 10
#Written by Chris Brown at Griffith Uni
#Tips for setting up R packages on windows 10
I recommend installing packages to your local drive, not the shared network drive. Though R will default to the network drive (which is "\\\\staff\..."), so some knowledge is required to overcome this.
You could also ask IT to bind a letter to your network drive. I've read that works (see here: https://github.com/r-lib/devtools/issues/353)
Here's the steps I followed to install packages:
Create a shortcut to R on desktop.
Right click it
@cbrown5
cbrown5 / st_as_raster
Created May 18, 2020 12:18
Convert stars raster to raster::raster
#Takes as an input a stars raster layer
#CJ Brown 2020-05-18
st_as_raster <- function(rstars){
rext <- st_bbox(rstars)
raster(t(rstars[[1]]), xmn = rext[1], xmx = rext[3],
ymn = rext[2], ymx=rext[4],
crs = st_crs(rstars)$proj4string)
}
# How to ggplot2 efficiently
# CJ Brown 2020-10-27
library(ggplot2)
#
# Make some data
#
n <- 20 #Sample size per group
# Why you should plot data before doing statistical tests
# CJ Brown 2020-11-06
#More at www.conservationhackers.org
library(ggplot2)
#
# Make some data
#
n <- 50 #Sample size per group
@cbrown5
cbrown5 / gamma_mode_sd.R
Created February 16, 2021 23:07
Parameterize gamma distribution with mode and SD
#Parameterize gamma distribution with mode and SD
#Choose summary stats for the gamma
xmode <- 1 #mode of distribution
xsd <- 0.5 # SD
#Calculate gamma parameters
beta <- (xmode + sqrt(xmode^2 + 4*xsd^2 ) ) / ( 2 * xsd^2 )
alpha <- 1 + xmode * beta
@cbrown5
cbrown5 / dist_from_cluster_membership.R
Created March 10, 2021 03:22
Turn a dataframe that indicates what cluster each sample belongs to into a distance matrix
#Function that converts dataframe of cluster identities
# to distance matrix representation of clusters (where rows/cols are samples/IDs and
# 0 indicates that pair are not in same cluster, 1 indicates they are in same cluster
as_dist_mat <- function(dtemp){
# dtemp is dataframe with two columns:
#ID is a unique cell ID
#Cluster is a variable giving cluster a cell belongs to
#It should be arranged so we are certain
#cells are in ascending order