Skip to content

Instantly share code, notes, and snippets.

View njtierney's full-sized avatar
🌴
On vacation

Nicholas Tierney njtierney

🌴
On vacation
View GitHub Profile
library(tidyverse)
# example structure of the data
# some of the columns are:
# Months (1 2 3, 13, 14 15), Arm (control, intervention)
# Before_After (beofre, after), Village ( 1:8), household (1:8), weight (number?)
# this is painful...
tibble(
month = c(1, 1, 2, 2, 3, 3, 13, 13, 14, 14, 15, 15),
library(tidyverse)

# building the bones of the data
# Specify the ranges of each of the data
# 
household_design <- expand_grid(
  month = c(1,2,3, 13, 14, 15),
  arm = c("C", "I"),
  b_a = c("b", "a"),
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