Skip to content

Instantly share code, notes, and snippets.

View cbrown5's full-sized avatar
🦈

Chris Brown cbrown5

🦈
View GitHub Profile
@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 / 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 / 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 / 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 / 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)