Skip to content

Instantly share code, notes, and snippets.

View bbolker's full-sized avatar

Ben Bolker bbolker

View GitHub Profile
@bbolker
bbolker / zigamma_pred.R
Created April 4, 2025 16:28
example of simulation ("parametric Bayes")-based confidence intervals for Z-I gamma
library(glmmTMB)
set.seed(101)
dd <- data.frame(x = rnorm(1000))
dd$y0 <- simulate_new( ~ 1,
seed = 101,
ziformula = ~ 1,
family = ziGamma(link = "log"),
newdata = dd,
newparams = list(
download.file("https://www.census.gov/foreign-trade/balance/country.xlsx", dest = "balance.xlsx")
library(readxl)
library(dplyr)
library(ggplot2); theme_set(theme_bw())
library(ggrepel)
bdat <- read_excel("balance.xlsx") |>
filter(year == 2024) |>
select(countrycode=CTY_CODE, country=CTYNAME,imports=IYR,exports=EYR)
@bbolker
bbolker / 3d_kriging_cgs.R
Created February 25, 2025 16:26 — forked from mikebirdgeneau/3d_kriging_cgs.R
3D Kriging with Conditional Gaussian Simulation in R
library(gstat)
library(lattice)
# Create Data Points (Random)
n <- 50
data3D <- data.frame(x = runif(n), y = runif(n), z = runif(n), v = rnorm(n))
coordinates(data3D) = ~x+y+z
# Create empty grid to krige
range1D <- seq(from = 0, to = 1, length = 20)
@bbolker
bbolker / softmax_regression.R
Created November 7, 2024 17:01
fitting a regression with coefficients constrained to sum to 1
## https://fediscience.org/@[email protected]/113439584737810431
library(MASS) # for eqscplot
library(RTMB)
set.seed(101)
n <- 1000
X <- cbind(
x1 = rnorm(n , 10, 2),
x2 = rnorm(n, 7, 1),
@bbolker
bbolker / constrained_cor.R
Created August 24, 2024 22:59
attempt to impose arbitrary equality constraints on correlations in a glmmTMB fit
## simulate example (NOT satisfying constraints, but large and close enough that we
## probably won't hit singular fits etc. etc.)
library(glmmTMB)
set.seed(1001)
nx <- 3
N <- 1000
ng <- 20
dd <- replicate(nx, rnorm(N), simplify = FALSE) |> as.data.frame() |> setNames(paste0("x", 1:nx))
dd$g <- factor(sample(1:ng, replace = TRUE, size = N))
@bbolker
bbolker / brightstar_scrape.R
Created July 23, 2024 22:34
code to scrape the list of bright stars from Wikipedia and count by hemisphere
## https://ivelasq.rbind.io/blog/politely-scraping/index.html
## To clean data
library(tidyverse)
library(lubridate)
library(janitor)
# To scrape data
library(rvest)
library(httr)
library(polite)
@bbolker
bbolker / lme4_groupRE.R
Created June 23, 2024 23:32
reproduce example for CV question
## https://stats.stackexchange.com/questions/649052/selecting-the-correct-r-lme4-syntax-for-nested-models
library(lme4)
df <- data.frame(Subject = rep(factor(1:120), each = 200),
Group = rep(LETTERS[1:3], each = 40*200))
with(df, table(Group))
with(df, table(table(Subject)))
df$Accuracy <- simulate(~ Group + (1|Subject),
newdata = df,
@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