Skip to content

Instantly share code, notes, and snippets.

@noamross
noamross / ocat.R
Created May 31, 2023 00:18
Example code for an ordered categorical model
View ocat.R
library(mgcv)
library(ggplot2)
library(dplyr)
## Simulate data with two categories and outcomes in 1:10
## Outcomes must be positive nonzero integers, transform if necessary
set.seed(3);
dat$Group = as.factor(paste("Group", rep(1:2, each = 200)))
dat$y = as.integer(round(pmax(pmin(rnorm(400, ifelse(dat$Group == "Group 1", 4, 6), 3), 10), 1)))
@noamross
noamross / sample_gam_posterior.R
Created May 1, 2023 14:25
Parallel chains with gam.mh()
View sample_gam_posterior.R
sample_gam_posterior <- function(multinomial_model,
chains = 4,
cores = min(chains, parallel::detectCores()), ...) {
samps <-
purrr::transpose(parallel::mclapply(
X = seq_len(chains),
FUN = function(X) {
post <- gam.mh(multinomial_model, ...)
View xgb_to_code.R
library(xgboost)
library(reticulate)
#reticulate::py_install("m2cgen") # https://github.com/BayesWitnesses/m2cgen
#reticulate::py_install("xgboost")
#reticulate::py_install("sklearn")
# Fit an XGBoost Model (example from ?xgb.train)
data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')
@noamross
noamross / test-missing-mrf.R
Last active January 21, 2023 20:18
Testing reduced-rank missing data strategies with mgcv
View test-missing-mrf.R
# Testing missing data strategies with mgcv
library(mgcv)
library(MRFtools) # github.com/noamross/MRFtools
library(tidyverse)
# Simulate data with missing parts
n <- 350;set.seed(2)
dat <- gamSim(1,n=n,scale=3) ## 1 or 7
drop <- sample(1:n,300) ## to
for (i in 2:5) dat[drop[1:75+(i-2)*75],i] <- NA
@noamross
noamross / _targets.R
Created December 13, 2022 12:48
A {targets} workflow to nuke all your twitter data
View _targets.R
# A Targets workflow to nuke your Twitter data
# Still working around some rate limits,
# I suggest building each of the deletion targets
# individually, e.g., "tar_make(blockunblock)"
# Load packages required to define the pipeline:
library(targets)
library(tarchetypes)
library(tidyverse)
library(rtweet)
library(jsonlite)
View eha_rsa_infrastructure_2022.md

Research Software Engineer, Infrastructure - EcoHealth Alliance

EcoHealth Alliance seeks a research software engineer to build and maintain technical infrastructure to support scientific research across our field, lab, and computational teams. We are seeking someone with the ability to learn and use a variety of system administration and development operations tools, and to work with a diverse, multi-disciplinary team that includes scientists from across the globe. This is a key position at a rapidly growing NGO with diverse funded scientific research programs around the world focused on conservation

View quantitative_ecologist_ecohealth_2022.txt
QUANTITATIVE ECOLOGY RESEARCH SCIENTIST
https://www.ecohealthalliance.org/career/quantitative-ecology-research-scientist
EcoHealth Alliance is seeking a quantitative ecologist to work closely with our team on multiple projects studying bat-borne zoonotic viruses. We are seeking someone with excellent statistical and computational skills, strong communication skills, and the ability to work with a diverse, multi-disciplinary team that includes scientists from across the globe. This is a key position at a rapidly growing NGO with diversely funded scientific research programs around the world focused on conservation and zoonotic disease emergence. This position requires experience with, and an ability to learn, a variety of ecological and epidemiological statistical techniques including generalized linear and additive, spatiotemporal, mixed-effect, Bayesian mixture and occupancy models, as well as proficiency with R and reproducible methods. Responsibilities include leading analysis of longitudinal and spatial
@noamross
noamross / coupling.R
Last active January 18, 2022 16:29
Calculating spatial correlations in a MRF
View coupling.R
library(mgcv)
library(spdep)
library(sf)
library(ggplot2)
# Columbus crime data from mgcv/spdep
example(columbus)
# Make a neighborhood object compatible with mgcv's MRF
nb <- poly2nb(columbus)
@noamross
noamross / logshift.R
Last active December 10, 2021 16:46
Custom log-shift scale in ggplot
View logshift.R
library(ggplot2)
dat <- data.frame(a = letters[1:10], b = exp(rnorm(10)))
ggplot(dat, aes(x = a, y = b)) + geom_col() + scale_y_log10() # Basic log scale transform
trans = scales::trans_new("logshift", \(x) log10(x*10), \(x) (10^x)/10) # Custom transformation
ggplot(dat, aes(x = a, y = b)) + geom_col() + scale_y_continuous(trans = trans) # Use custom transformation
ggplot(dat, aes(x = a, y = b)) + geom_col() + scale_y_continuous(trans = trans, breaks = c(0.25, 0.5, 1, 2, 4, 8)) # Add breaks
ggplot(dat, aes(x = a, y = b)) +
geom_col() +
@noamross
noamross / test-dolt.R
Created September 1, 2021 16:58
Reprex of trying to write data to Dolt with RMariaDB
View test-dolt.R
library(sys)
library(RMariaDB)
library(DBI)
library(withr)
# Clean up from previous session
unlink("doltdb", recursive = TRUE)
try(dbDisconnect(conn), silent = TRUE)