This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to ggplot2 efficiently | |
# CJ Brown 2020-10-27 | |
library(ggplot2) | |
# | |
# Make some data | |
# | |
n <- 20 #Sample size per group |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
OlderNewer