Skip to content

Instantly share code, notes, and snippets.

View padpadpadpad's full-sized avatar

Daniel Padfield padpadpadpad

View GitHub Profile
@padpadpadpad
padpadpadpad / check_rstanarm_install.R
Created April 15, 2024 08:44
quick code to check the installation of rstanarm (survival branch)
library(rstanarm)
library(simsurv)
?stan_surv
data("frail")
m_frail <- stan_surv(
formula = Surv(eventtime, status) ~ trt + (1 | site),
data = frail[1:40,],
# set colours for plot
colours <- RColorBrewer::brewer.pal(10, 'Spectral')
# function to make points lighter but not transparent
alpha_hex <- function(hex_code, alpha = 1){
# split by ',' and extract number
temp <- monochromeR::hex_to_rgb(hex_code) %>%
str_split(., ',') %>%
.[[1]] %>%
parse_number()
@padpadpadpad
padpadpadpad / dirty_TPC_censor_code.R
Last active December 11, 2023 14:38
R code for looking at how removing data points impacts a Sharpe-Schoolfield model fit.
# have a look at whether having multiple 0 values impacts the results of model fitting to TPCs
# load libraries
librarian::shelf(rTPC, nls.multstart, tidyverse, broom)
# simulate data using Sharpe Schoolfield model
d_simulate <- tibble(temp = seq(from = 10, to = 55, length.out = 10),
rate = sharpeschoolhigh_1981(temp = temp, r_tref = 0.5, e = 0.6, eh = 5, th = 33, tref = 17.5)) %>%
# add noise
mutate(noise = rnorm(n(), 0, 0.1),
# try write script to extract data/code accessibility statement
# try with BES journals first. The trick is in grabbing the correct node (whatever that is)
mee_url <- 'https://doi.org/10.1111/2041-210X.13585'
functionalecology_url <- 'https://doi.org/10.1111/1365-2435.14422'
animalecology_url <- 'https://doi.org/10.1111/1365-2656.13983'
appliedecology_url <- 'https://doi.org/10.1111/1365-2664.14474'
# library
library(rvest)
# random flextable example to put a symbol in a column
librarian::shelf(tidyverse, flextable, officer)
data(mpg)
# copyright symbol
copyright_symbol <- "\U00A9"
black_dot <- "\U2B24"
@padpadpadpad
padpadpadpad / tidybayes_nimble_example.R
Last active September 5, 2023 13:44
Very simple example of using nimble, ggdist, and tidybayes.
# nimble and tidybayes example
# load packages
librarian::shelf(tidybayes, nimble, tidyverse, bayesplot, ggdist, nlist)
# run example from runMCMC
code <- nimbleCode({
mu ~ dnorm(0, sd = 1000)
sigma ~ dunif(0, 1000)
@padpadpadpad
padpadpadpad / patchwork_cowplot_example.R
Created July 28, 2023 08:15
Example of using patchwork and cowplot to arrange legends in a four panel plot.
# example of using cowplot and patchwork to make a plot with legends in the bottom right of a four panel plot
# load in packages
librarian::shelf(tidyverse, patchwork, cowplot)
# make a random plot with colour
p1 <- ggplot(mpg, aes(hwy, cty, col = class)) +
geom_point()
# make other plot with colour on
@padpadpadpad
padpadpadpad / weighted_bootstrap_many_curves.R
Last active July 13, 2023 06:53
Runs an example of doing weighted bootstrap regression on many curves using rTPC
# try weighted bootstrapping of many curves
# combines approach from weighted bootstrapping and bootstrapping many curves
# https://padpadpadpad.github.io/rTPC/articles/bootstrapping_many_curves.html
# https://padpadpadpad.github.io/rTPC/articles/weighted_bootstrapping.html
# load in packages
librarian::shelf(tidyverse, rTPC, nls.multstart, broom, minpack.lm, car)
# load in data
data("chlorella_tpc")
@padpadpadpad
padpadpadpad / patchwork_legend_example.R
Last active April 20, 2023 07:57
An example of aligning plots using patchwork and having better control of the legend position.
# example of using cowplot and patchwork to make a plot with legends in the bottom right of a four panel plot
# load in packages
librarian::shelf(tidyverse, patchwork, cowplot)
# make a random plot with colour
p1 <- ggplot(mpg, aes(hwy, cty, col = class)) +
geom_point()
# make other plot with colour on
@padpadpadpad
padpadpadpad / map2_example.R
Created March 22, 2023 11:04
Example of using map2 to pass arguments to a model.
# random modelling example for Stephen
# load in packages
librarian::shelf(rTPC,tidyverse, nls.multstart)
# create data
d <- chlorella_tpc %>%
filter(curve_id < 4) %>%
nest(data = c(everything(), -curve_id))