Skip to content

Instantly share code, notes, and snippets.

View tjmahr's full-sized avatar
🍍
sampling

TJ Mahr tjmahr

🍍
sampling
View GitHub Profile
@tjmahr
tjmahr / demo.md
Created September 16, 2026 15:15
tidyselect on posterior draws
d <- posterior::example_draws()
library(posterior)
#> Warning: package 'posterior' was built under R version 4.6.1
#> This is posterior version 1.7.1
#> 
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#> 
#>     mad, sd, var
t <- tempfile(fileext = ".R")
writeLines(con = t, text = r"(
#' Get a function's source code and any preceding comment lines
#' @param fun a function with a `srcref` attribute
#' @return a character vector of source code for the function
get_function_source_with_comment_block <- function(fun) {
 stopifnot(!is.null(attr(fun, "srcref")))

 source_lines <- fun |>
@tjmahr
tjmahr / marginalize_2_random_effects.R
Created February 12, 2026 20:27
Marginal means for `~ (control_file_type | subject_num) + (phase | listener)`
library(tidyverse)
library(brms)
targets::tar_load(model_wtocs_fast_phase_type_interaction_slopes)
model <- model_wtocs_fast_phase_type_interaction_slopes
data_marg <- compute_marginal_means_child_listener_slopes(model)
# Compute marginal means for the model with random effects:
# ~ (control_file_type | subject_num) + (phase | listener)
compute_marginal_means_child_listener_slopes <- function(
withr::local_temp_libpaths()
pak::pkg_install("ggplot2@3.5.2")
#> ℹ Loading metadata database✔ Loading metadata database ... done
#>  
#> → Will install 20 packages.
#> → Will download 18 CRAN packages (11.47 MB), cached: 2 (1.51 MB).
#> + cli            3.6.5  [dl] (1.40 MB)
#> + farver         2.1.2  [dl] (1.52 MB)
#> + ggplot2        3.5.2  [bld]
library(tidyverse)

TLDR: probability functions like pt() can provide log values so that you can avoid numerical problems. (This is why we sum log likelihoods instead of multiplying likelihoods.) So, we can identify the FLOATING-POINT BREAKERS of p-values if we stick to the log scale throughout the pipeline.

@tjmahr
tjmahr / ga.md
Last active October 18, 2024 18:10
library(tidyverse)
# d <- readr::read_tsv("test.tsv") |>
#   janitor::clean_names()
#
# d$total <- d$biden + d$trump + d$other
# d$prop_votes_in <- d$ballots_accepted / d$total
# d$prop_biden <- d$biden / d$total
# datapasta::tribble_paste(d)
@tjmahr
tjmahr / my-bad-geom.md
Created October 9, 2024 20:29
my-bad-geom
library(ggplot2)

geom_crossrange <- function(
    mapping = NULL,
    data = NULL,
    stat = "identity",
    position = "identity",
    ...,
    na.rm = FALSE,
print.file_check_log <- function(x, ...) str(x, ...)
as_file_check_log <- function(x) UseMethod("as_file_check_log")
as_file_check_log.default <- function(x) {
  structure(list(x = x, notes = list()), class = "file_check_log")
}

apply_to_file_check_log <- function(x, fn, ...) {
  results <- fn(x$x, ...)
  l <- list(results)
@tjmahr
tjmahr / unbrace-yourself.md
Created August 19, 2024 20:51
struct_pack() in duckdb
# Make a csv to read in
db1 <- DBI::dbConnect(duckdb::duckdb())
f <- tempfile("mtcars", fileext = ".csv")
write.csv(mtcars, f)

library(tidyverse)

# Read in the csv.
# Here two column types are hard-coded using a struct
@tjmahr
tjmahr / ragged-tsv.md
Last active July 25, 2024 19:45
ragged tsv

Some of the rows have more columns than the first row of column names.

writeLines(
"a\tb\tc\td\te\tf
1\t2\t3\t4\t5\t6\t
1\t2\t3\t4\t5\t6\t7\t
1\t2\t3\t4\t5\t6\t7\t8
1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11
1\t2\t3\t4\t5\t6