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 from an RW2 (random walk order 2) model. | |
# Useful for generating predictions from INLA RW2 models. | |
# When forecast, RW2 models predict continuation of trend, with | |
# deviations from the trend controlled by the sd parameter. | |
# | |
# See: https://inla.r-inla-download.org/r-inla.org/doc/latent/rw2.pdf | |
# Can intialize the RW2 at a given starting point, this needs | |
# needs to be two consecutive points, given we are working with | |
# second order differences. |
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
#Functions to simulate predictions and CIs from a GAM | |
# CJ Brown | |
# 2023-03-24 | |
# | |
# based on code in ?gam.predict and Wood, S.N. (2017) Generalized Additive Models: An | |
# Introduction with R (2nd edition). Chapman and | |
# Hall/CRC. | |
library(lazyeval) |
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
# Template for doing empirical Bayes prediction from a GAM with mgcv | |
# CJ Brown 2021-08-23 | |
# | |
# See Wood 2017 and ?predict.gam for more info | |
#First of all its often easiest to create your prediction dataframe with | |
# visreg, e.g. this makes a df across all values of NEOLI_Total | |
# and "has_fee": | |
xfit <- visreg(tour_mod, xvar = "NEOLI_Total", partial = FALSE, | |
plot = FALSE, by = "has_fee") |
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
#Demonstration of splines in R | |
#Can be used to add nonlinearity to linear models (e.g. GLMs) | |
library(splines) | |
x <- rnorm(10000) | |
n_df <- 5 | |
ns_design_mat <- ns(x, n_df) #n_df degrees of freedom | |
plot(x, ns_design_mat[,1], ylim = c(-1, 1)) | |
points(x, ns_design_mat[,2], col = "red") | |
points(x, ns_design_mat[,3], col = "blue") |
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
#Apply scale to just numeric variables | |
#keep all variables | |
dat2 <- dat %>% | |
mutate(across(where(is.numeric), scale)) | |
#keep just numerical variables, drop the others from the data.frame | |
dat2 <- dat %>% | |
transmute(across(where(is.numeric), scale)) |
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 |
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
# 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
# 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
#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) | |
} |
NewerOlder