Skip to content

Instantly share code, notes, and snippets.

View bbolker's full-sized avatar

Ben Bolker bbolker

View GitHub Profile
@bbolker
bbolker / phylocov.R
Created May 26, 2022 00:43
phylocov
library(ape)
cat("(((Strix_aluco:4.2,Asio_otus:4.2):3.1,",
"Athene_noctua:7.3):6.3,Tyto_alba:13.5);",
file = "ex.tre", sep = "\n")
tree.owls <- read.tree("ex.tre")
set.seed(101)
dd <- data.frame(y = rnorm(4),
obs = factor(1:4),
g = factor(1))
library(lme4)
@bbolker
bbolker / glmmTMB_refit_hack.R
Last active May 22, 2022 00:23
Code sketching out a 'refit()` workflow for TMB objects
## example of updated refit, now incorporated in code
## * NO convergence testing/warnings/etc
## * won't work on weird binomial input (i.e. anything other than 0/1)
## * results seem only to be identical up to floating-point accuracy
## * doesn't yet do other clever things like resetting starting values
remotes::install_github("glmmTMB/glmmTMB/glmmTMB@smart_refit")
library(glmmTMB)
library(rbenchmark)
data("sleepstudy", package = "lme4")
@bbolker
bbolker / nest_contrast.R
Last active April 8, 2022 01:06
contrast example
dd <- data.frame(A = factor(c(1,2,2,2)),
B = factor(1:4),
y = 1:4)
dd$i <- with(dd, factor(sprintf("A%d.B%d",A,B)))
## set up *inverse* contrast matrix
invc <- matrix(c(
1/2, 1/6, 1/6, 1/6, ## A-weighted mean
-1, 1/3, 1/3, 1/3, ## A1 vs A2 (== B1 vs mean{B2, B3, B4})
0, 2/3, -1/3, -1/3, ## B2 vs mean(B2, B3, B4)
@bbolker
bbolker / morganstanley_cursed_mixed.R
Created March 23, 2022 23:39
mixed model for 'cursed' Morgan Stanley data
## setup from https://www.tjmahr.com/morgan-stanley-cursed-covid-plot/
library(tidyverse)
theme_set(theme_bw())
library(lme4)
library(ggeffects)
library(colorspace)
library(directlabels)
# a helper function to download the data from github
@bbolker
bbolker / nserc_dates.R
Created March 17, 2022 00:44
projecting dates of NSERC discovery grant results
library(tidyverse)
this_year <- 2022
(df <- read.table(text="
2021 April 14
2020 April 23
2019 April 11
2018 April 13
2017 April 12
2016 April 7
2015 April 1
library(emdbook)
set.seed(101)
dd <- rgamma(5, shape = 1, scale = 1)
ff <- function(x,y) -sum(dgamma(dd,shape=x, scale=y, log = TRUE))
ifun <- function(...) {
image(cc$x, cc$y, cc$z, ...)
contour(cc$x, cc$y, cc$z, level = min(cc$z) + 1.92, add = TRUE, label = "")
}
png("gamma_lik.png", width = 800, height = 400)
par(mfrow=c(1,2), las = 1, bty = "l")
@bbolker
bbolker / binnedplots2.R
Created February 4, 2022 01:32
illustrating binning for binary data/residuals from logistic regression
data("Contraception", package = "mlmRev")
library(lme4)
library(tidyverse)
library(magrittr)
## make sure we have a variable that's actually continuous
Contraception %<>% mutate(ja = jitter(age),
n_use = as.numeric(use) -1)
m1 <- glmer(use ~ urban * splines::ns(ja, 3) + (1|district), data = Contraception,
family = binomial)
aa <- broom.mixed::augment(m1, data = Contraception, type.residuals = "pearson")
@bbolker
bbolker / binnedplots.R
Created December 11, 2021 23:29
various machinery for binned plots
library(ggplot2); theme_set(theme_bw())
library(dplyr)
library(broom)
#' Summarize binary data à la arm::binnedplot
#' @param x continuous predictor variable
#' @param y 0/1 response variable
#' @param nclass number of categories for binning
#' @param cifun function for extracting confidence intervals
#' @return a data frame with columns "xbar" (centerpoint of bin),
library(MuMIn)
library(lme4)
library(glmmTMB)
library(emmeans)
set.seed(2) ## simulate some data...
dd <- expand.grid(f1 = factor(1:3), f2 = factor(1:3),
g = factor(1:10), rep = 1:2)
Z <- model.matrix(~g-1, dd)
b <- rnorm(10)
@bbolker
bbolker / facbench.R
Created October 22, 2021 22:42
factorial_benchmarks
library(microbenchmark)
library(Rcpp)
library(ggplot2); theme_set(theme_bw())
library(colorspace)
library(tidyverse)
## the most naive: a for loop, in pure R
f_forloop <- function(n) {
val = 0
for (i in 2:n) {