Skip to content

Instantly share code, notes, and snippets.

View bbolker's full-sized avatar

Ben Bolker bbolker

View GitHub Profile
@bbolker
bbolker / xmeta.R
Last active December 21, 2023 20:01
attempt at matching metafor results with glmmTMB
library(brms)
library(metafor)
library(broom)
library(broom.mixed)
set.seed(101)
y <- rnorm(10)
sei <- rgamma(10, shape = 1)
## metafor: fixed-effect meta-analysis
@bbolker
bbolker / find_data_arrays.R
Last active November 27, 2023 16:38
search installed packages for data of specified types
i1 <- installed.packages()
null <- matrix(nrow=0, ncol = 2,
dimnames=list(NULL, c("Item", "type")))
get_data <- function(pkg) {
cat(pkg,"\n")
dd <- data(package=pkg)$results
## library(pkg, character.only = TRUE)
## on.exit(try(detach(paste0("package:", pkg))))
if (nrow(dd) == 0) return(NULL)
FUN <- function(x) {
@bbolker
bbolker / bayes_tails.R
Last active September 12, 2023 22:20
bayes tails examples
## original by Richard McElreath at https://twitter.com/rlmcelreath/status/1701165075493470644,
## https://gist.github.com/rmcelreath/39dd410fc6bb758e54d79249b11eeb2f
## originally based on https://doi.org/10.1006/jmva.1999.1820
## with improvements from https://gist.github.com/murphyk/94205bcf335837108ff0e6d51331785a
post_prior <- function(
m_pr = 10, m_lik = 0, ## mean values for prior and likelihood
sd_pr = 1, sd_lik = 1, ## standard deviations
df_pr = 1000, df_lik = 1000, ## dfs (df = 1000 is effectively Gaussian)
xlim = c(-5, 15), n = 1001, ## range and delta for x-vector
@bbolker
bbolker / binom_p.R
Created July 8, 2023 17:53
distribution of p-values under the null for a binomial test via GLM
## adapted from Richard McElreath's code at
## https://gist.github.com/rmcelreath/4f7010e8d5688c69bbeb7008f0aabe65
binom_p <- function(N=10, M=5, p=0.25, b=0,
test = c("Wald", "LRT")) {
test <- match.arg(test)
x <- rep(0:1, each = N/2)
p <- plogis(qlogis(p)+b*x)
y <- rbinom(N, size=M, prob=p)
z <- glm( cbind(y, M-y) ~ x , family=binomial )
@bbolker
bbolker / analemma.R
Created June 22, 2023 22:59
compute sunrise/sunset differences
start_date <- "2023-06-21"
end_date <- "2023-07-01"
refdate <- "2023-06-21"
season <- "summer"
library(ggplot2); theme_set(theme_bw())
library(colorspace)
library(purrr)
library(dplyr)
## need archived package for sunrise/sunset calcs
@bbolker
bbolker / cbrm.R
Created May 22, 2023 20:36 — forked from derekpowell/cbrm.R
Wrapper for brm() that supports caching of BRMS models
cbrm <- function(formula,
data,
family = gaussian(),
prior = NULL,
autocor = NULL,
cov_ranef = NULL,
sample_prior = c("no", "yes", "only"),
sparse = FALSE,
knots = NULL,
stan_funs = NULL,
@bbolker
bbolker / coxme_condsd.R
Last active May 16, 2023 16:26
Attempt to compute conditional SDs for coxme fits
library(coxme)
library(Matrix)
#' @param fit a fitted \code{coxme} model
#' @param data the data frame for which to evaluate conditional SDs
#' @param grpvar (character) grouping variable (should be able to
#' extract this from the model, but it's a nuisance)
#' @param rform random-effects term (ditto)
#' @param sd.only return only sqrt(diag(V))? (otherwise return full covariance matrix)
coxme.condsd <- function(fit,
@bbolker
bbolker / twitter_badREML.R
Created May 14, 2023 15:12
Exploring REML edge cases
library(lme4)
library(nlme)
library(glmmTMB)
library(Matrix)
## what do various R packages do when confronted with an
## undefined REML problem (fixed effects *and* random effects
## for every subject?
## Twitter thread: https://twitter.com/ten_photos/status/1657399290166222850
@bbolker
bbolker / misconduct_investigations.R
Created May 10, 2023 22:14
data and graphics on academic misconduct investigations
#https://dynamicecology.wordpress.com/2021/03/15/how-long-do-institutional-investigations-into-accusations-of-serious-scientific-misconduct-typically-take-heres-some-data/
## https://secretariat.mcmaster.ca/app/uploads/Research-Integrity-Policy.pdf
## https://www-nature-com.libaccess.lib.mcmaster.ca/articles/d41586-020-00287-y
library(tidyverse)
dd <- read.table(header=TRUE, text="
subject duration year
Fuji 24 2010
Fuji 3 2012
Boldt 21 2010
@bbolker
bbolker / drop_scalar_singular.R
Created May 3, 2023 21:13
illustration that dropping scalar singular terms makes no difference
library(lme4)
set.seed(101)
nn <- 100
ng <- 10
dd <- data.frame(x = rnorm(nn),
f = factor(rep(1:(nn/ng), ng)),
g = factor(rep(1:ng, each = ng)))
dd$y1 <- simulate( ~ x + (1|f),
newdata = dd,
newparams = list(beta = c(1,1),