Skip to content

Instantly share code, notes, and snippets.

View njtierney's full-sized avatar
☀️
Back at work loving summer

Nicholas Tierney njtierney

☀️
Back at work loving summer
View GitHub Profile
library(tidyverse)
# example lagging code
n <- 100

grid_cov <- expand_grid(
  covariates = c("rainfall", "temperature"),
  years = 2000:2022,
  row = seq_len(n)
) |>
@njtierney
njtierney / year-lag.md
Created November 14, 2024 03:06
exploring creating year lag
# example lagging code
library(tidyverse)

n <- 100

grid_cov <- expand_grid(
  covariates = c("rainfall", "temperature"),
  years = 2000:2022,
  row = seq_len(n)
elev <-  terra::rast(system.file("ex", "elev.tif", package = "terra"))
elev
#> class       : SpatRaster 
#> dimensions  : 90, 95, 1  (nrow, ncol, nlyr)
#> resolution  : 0.008333333, 0.008333333  (x, y)
#> extent      : 5.741667, 6.533333, 49.44167, 50.19167  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source      : elev.tif 
#> name        : elevation 
@njtierney
njtierney / compare-git-commits.R
Created October 30, 2024 06:52
Helper for comparing git commits
compare_git_commits <- function(repo, sha1, sha2){
# https://github.com/github-linguist/linguist/compare/f75c570..3391dcc
glue::glue("{repo}/compare/{sha1}..{sha2}")
}
compare_greta_commits <- function(sha1, sha2){
compare_git_commits(repo = "https://github.com/greta-dev/greta/",
sha1 = sha1,
sha2 = sha2)
}
library(distributional)
library(tidyverse)
tibble(
  dist = c(dist_normal(0,1), dist_normal(4,7), dist_normal(2,8))
) |> 
  mutate(
    sample = generate(dist, 10),
    params = parameters(dist)
  )
# General process would be:
# 1. Simulate a wishart draw x with rWish()
# 2. Transform x to the equivalent free_state, using the bijector but running it in reverse
# 3. Run the log_prob() function on free_state
# 4. Run dWish(..., log = TRUE) on x, and compare with result of step 3
devtools::load_all(".")
#> ℹ Loading greta
#> ℹ Initialising python and checking dependencies, this may take a moment.
#> 
library(tidyverse)
txt <- "day 0 dis influenza host 1111 age 19.19day 0 dis influenza host 2222 age 5.55day 0 dis influenza host 333 age 11.11"

# age has to have two \\d+ as it abuts the next column, "day"
pattern <- "day (\\w+) dis (\\w+) host (\\w+) age (\\d+\\.\\d+)"

# Use str_match_all to extract all occurrences
matches <- str_match_all(txt, pattern)[[1]]

Demonstrating how %>% differs to |> in R. One is a function call, the other parses the code into nested functions.

library(magrittr)
library(lobstr)
1:10 |> sum()
#> [1] 55
1:10 %>% sum()
#> [1] 55

Just to build on https://gist.github.com/DavisVaughan/294b6934ae1f291634534ac952dcfa02, here is what we did in order to get this to work with Devel R.

We created a C file, named zzz.c in src/main/zzz.c, and also had to have the following header files in the below order. Then we also had to add a line to the (already existing) makefile, Makefile.in in src/main/Makefile.in, where we added a line zzz.c under SOURCES_C = \.

Then we did the regular debug stuff:

  1. Sys.getpid()
  2. Copy pid into LLDB debugger
  3. Add breakpoint in C code
  4. Run code to trigger breakpoints
sample.int <- function(n, size = n, replace = FALSE, prob = NULL,
prob_method = c("sequential", "marginal", "poisson"),
useHash = (n > 1e7 && !replace && is.null(prob) && size <= n/2)){
stopifnot(length(n) == 1L)
replace_or_no_prob <- is.null(prob) || replace
if (replace_or_no_prob){
size <- size %||% n
if (useHash) {
## will work with size > n/2 but may be slow.