Skip to content

Instantly share code, notes, and snippets.

View njtierney's full-sized avatar
👷‍♂️
Have some work availability from mid July

Nicholas Tierney njtierney

👷‍♂️
Have some work availability from mid July
View GitHub Profile
library(purrr)
set.seed(2020)
means <- 1:4
bp <- bench::press(
  n = c(100, 1000, 10000, 100000),
  {
    bench::mark(
  map = {set.seed(2020);  map(means, rnorm, n = n, sd = 1)},
  lapply = {set.seed(2020); lapply(means, rnorm, n = n, sd = 1)}
@njtierney
njtierney / crossprod-speed.md
Created June 5, 2025 03:24
crossprod vs t() %*%
pi_mat <- matrix(c(0.5, 0.5))
z_mat <- matrix(c(32, 1))  

bm <- bench::mark(
  t = t(pi_mat) %*% z_mat,
  crossprod = crossprod(pi_mat, z_mat)
)

bm
choice_set_hours <- function(hour_increments, n_hour_options) {
  choice_set <- seq(
    from = 0,
    to = (n_hour_options - 1) * hour_increments,
    by = hour_increments
  )
  choice_set
}
@njtierney
njtierney / ugh-optim.md
Last active May 30, 2025 07:03
ugh-optim
## requires pass by position?

fr_pos <- function(x) {   ## Rosenbrock Banana function
    x1 <- x[1]
    x2 <- x[2]
    100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}

pars <- list(a = -1.2, b = 1)
# example code from test_posteriors_geweke
devtools::load_all(".")
n <- 10
# mu1 <- rnorm(1, 0, 3)
# sd1 <- rlnorm(1)
# sd2 <- rlnorm(1)
mu1 <- 0
sd1 <- 2
sd2 <- 1
e <- new.env()
e$x <- 10
e$y <- 20

eval(quote(x + y), envir = e)
#> [1] 30

e2 <- new.env()
# Warming up the sampler and running extra samples on the model with the initial dataset (ie. not changing it each iteration) should tell us whether the sampler is correctly adapted.
library(greta)
#> 
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#> 
#>     binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#> 
library(rlang)

preserved_goods_none <- function(goods = c("cucumber", "lemon", "pear")) {
  goods
}

preserved_goods_rlang <- function(goods = c("cucumber", "lemon", "pear")) {
  goods <- rlang::arg_match(
    arg = goods
mat1 <- matrix(1:10, ncol = 2)
mat2 <- matrix(2:11, ncol = 2)

list_matrices <- list(mat1, mat2)
list_matrices
#> [[1]]
#>      [,1] [,2]
#> [1,]    1    6
#> [2,]    2    7
mat <- matrix(
  sample(x = c(1, 0), size = 9, replace = TRUE),
  nrow = 3,
  ncol = 3
)

mat
#>      [,1] [,2] [,3]
#> [1,]    1    1    0