Skip to content

Instantly share code, notes, and snippets.

View seananderson's full-sized avatar

Sean Anderson seananderson

View GitHub Profile
@seananderson
seananderson / lm.R
Created June 11, 2024 20:29
Example of predicting on newdata by rebuilding TMB object
library(TMB)
compile("lm1.cpp")
dyn.load(dynlib("lm1")) # can't call it 'lm'!
set.seed(123)
x <- seq(0, 10, length.out = 10)
data <- list(Y = rnorm(length(x)) + x, x = x)
parameters <- list(a = 0, b = 0, logSigma = 0)
obj <- MakeADFun(data, parameters, DLL = "lm1")
opt <- nlminb(obj$par, obj$fn, obj$gr)
sdr <- sdreport(obj)
@seananderson
seananderson / OpenBLAS-Windows-user.md
Last active May 31, 2024 18:36
OpenBLAS on in Windows without admin privileges

If you work on a Windows computer without admin privileges to modify system files (e.g., many government employees), you can likely still install R in your user folder and replace the default BLAS library there.* Instructions:

  1. Download the latest R installer.
  2. Run the installer and select a user folder for installation when prompted, e.g. C:\Users\USERNAME\SUBFOLDER.
  3. In RStudio go to 'Tools' > 'Global Options' > 'General' > 'R version' to change the R installation being used to the one you just installed in your user folder.
  4. Download OpenBLAS following these instructions. A summarized version of this and subsequent steps follows.
  5. Extract libopenblas.dll from the OpenBLAS zip file.
@seananderson
seananderson / sdmTMB-RCA-sim.R
Last active October 20, 2023 22:38
Example of simulating with sdmTMB
library(sdmTMB)
library(dplyr)
library(ggplot2)
mesh <- make_mesh(pcod_2011, c("X", "Y"), cutoff = 10)
fit <- sdmTMB(
density ~ poly(depth, 2),
data = pcod_2011, mesh = mesh,
family = tweedie(link = "log")
)
@seananderson
seananderson / spatial-binning.qmd
Created March 24, 2023 02:13
Spatial binning vs. individual-level observations
---
title: "Individual vs. blocked spatial data modelling"
format:
html:
embed-resources: true
editor: source
execute:
echo: true
eval: true
cache: true
@seananderson
seananderson / lme.R
Last active February 15, 2019 20:43
Example mixed-effects model integration and optimization in R
set.seed(138593)
N <- 100
x <- runif(N, -2, 2)
a <- 1
b <- 1.5
sigma <- 0.3
tau <- 0.4
re_levels <- gl(10, 10)
re <- rnorm(10, 0, tau)
y_true <- a + re[re_levels] + b * x
@seananderson
seananderson / smith-bayes-workshop.md
Created May 7, 2018 23:13
Bayesian workshop instructions

Thanks for filling in the survey on your experience levels and interests in an introductory workshop on Bayesian statistics in R. I'll do my best to tailor the content based on your responses.

I'm going to assume you know the basics of ggplot and dplyr — the content will make a lot more sense if you understand the elements of a ggplot plot and the main dplyr verbs: mutate, filter, summarise, select, and group_by. If you need to brush up on these, I'd recommend these chapters from 'R for Data Science': http://r4ds.had.co.nz/data-visualisation.html http://r4ds.had.co.nz/transform.html

We can talk about textbooks at the workshop, but my favorite textbook on Bayesian modelling is 'Bayesian Models: A Statistical Primer for Ecologists' by Hobbs and Hooten. https://www.amazon.com/Bayesian-Models-Statistical-Primer-Ecologists/dp/0691159289

@seananderson
seananderson / smith-bayes-workshop.md
Created May 7, 2018 23:13
Bayesian workshop instructions

Thanks for filling in the survey on your experience levels and interests in an introductory workshop on Bayesian statistics in R. I'll do my best to tailor the content based on your responses.

I'm going to assume you know the basics of ggplot and dplyr — the content will make a lot more sense if you understand the elements of a ggplot plot and the main dplyr verbs: mutate, filter, summarise, select, and group_by. If you need to brush up on these, I'd recommend these chapters from 'R for Data Science': http://r4ds.had.co.nz/data-visualisation.html http://r4ds.had.co.nz/transform.html

We can talk about textbooks at the workshop, but my favorite textbook on Bayesian modelling is 'Bayesian Models: A Statistical Primer for Ecologists' by Hobbs and Hooten. https://www.amazon.com/Bayesian-Models-Statistical-Primer-Ecologists/dp/0691159289

library(tidyverse)
d <- tibble(group = letters[1:5], x = 1:5, y = rnorm(5))
ggplot(d, aes(x, y, colour = group)) + geom_point(size = 3)
# lose group
d <- d[-2,]
ggplot(d, aes(x, y, colour = group)) + geom_point(size = 3)
# fix by joining in all known groups to create NAs:
all_groups <- tibble(group = letters[1:5])
@seananderson
seananderson / multi.R
Last active January 19, 2023 10:28
Multivariate regression in Stan (based on 9.15 Multivariate Outcomes manual section)
set.seed(42)
N <- 400
x <- runif(N, -1, 1)
Omega <- rbind( # correlation matrix
c(1, 0.9),
c(0.9, 1)
)
sigma <- c(0.6, 0.4) # residual SDs
Sigma <- diag(sigma) %*% Omega %*% diag(sigma) # covariance matrix
# compare the datalimited::prm function to the original fits
# start with setwd("analysis") from the ensembles root folder
library("datalimited")
library("dplyr")
library("ggplot2")
d <- dplyr::inner_join(datalimited::ram_ts, datalimited::spp_categories,
by = "scientificname")
ram_prm_dat <- plyr::ddply(d, "stockid", function(x) {