Skip to content

Instantly share code, notes, and snippets.

View tjmahr's full-sized avatar
🍍
sampling

TJ Mahr tjmahr

🍍
sampling
View GitHub Profile
x <- rnorm(250, 5, 2)
i <- sample.int(250, 20)

ranks <- rank(x)[i]
percentiles <- ppoints(250)[ranks]
points <- x[i]

f <- function(args, points, percentiles) {
  test_quantiles <- qnorm(percentiles, args[1], args[2])
@tjmahr
tjmahr / update.md
Created April 15, 2024 16:15
update() is bad
data <- mtcars
m <- lm(mpg ~ wt, data)

data <- mtcars[-c(1:10), ]
m_update <- update(m, . ~ . + disp)

nobs(m)
#> [1] 32
@tjmahr
tjmahr / file-history.R
Created April 9, 2024 20:15
some r code i made to dredge up older git versions of a file into a tibble
#' @param path current path of format
#' @param date_format date format to request from git. This format is given to
#' git to set the format of the dates it returns and then given to readr to
#' tell it how to parse the dates into date-times.
#' @param f_split optional function to help reduce `system()` calls. By default
#' (`NULL`), one `system()` call is made for each version of the file.
#' Alternatively, we can ask git to give the contents of the all files at once
#' with one `system()` call. Then `f_split()` creates a list of file contents.
#' @return a tibble with one row commit with commit, commit date, historical
fct_regex_levels <- function(xs, patterns) {
  default_levels <- unique(xs)
  which_matches <- default_levels |> 
    lapply(stringr::str_which, patterns) |> 
    unlist()
  stopifnot(
    "Each factor level should have exactly one pattern" =
      all(seq_along(default_levels) %in% which_matches)
  )
is_even <- function(n) {
if (n == 0) list(TRUE, sys.nframe()) else Tailcall(is_odd, n - 1)
}
is_odd <- function(n) {
if (n == 0) list(FALSE, sys.nframe()) else Tailcall(is_even, n - 1)
}
is_odd(30)
naive_is_even <- function(n) {
@tjmahr
tjmahr / bib-glance.R
Last active December 14, 2023 22:06
bib_glance()
bib_glance <- function(sort = TRUE) {
path <- "./notebook/refs.bib"
refs <- bibtex::read.bib(path)
if (sort) {
refs <- refs[sort(names(refs))]
}
titles <- purrr::map_chr(refs, purrr::pluck, "title") |>
stringr::str_replace_all("\\{|\\}", "") |>
@tjmahr
tjmahr / label-hints.md
Last active November 30, 2023 20:05
hints for labels
library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.3.2
#> Warning: package 'dplyr' was built under R version 4.3.2
#> Warning: package 'stringr' was built under R version 4.3.2
#> Warning: package 'lubridate' was built under R version 4.3.2

Suppose we have a dataframe with labels and their locations

@tjmahr
tjmahr / demo.md
Last active June 27, 2023 17:26
demo of ordering constraint via priors
library(tidyverse)
library(brms)
#> Loading required package: Rcpp
#> Loading 'brms' package (version 2.19.0). Useful instructions
#> can be found by typing help('brms'). A more detailed introduction
#> to the package is available through vignette('brms_overview').
#> 
#> Attaching package: 'brms'
#> The following object is masked from 'package:stats':
@tjmahr
tjmahr / gist:b76680e5d1395c3fbae5ee78e538754c
Last active May 31, 2023 18:44
i don't have any references for this technique tho
library(gamlss)
#> Loading required package: splines
#> Loading required package: gamlss.data
#> 
#> Attaching package: 'gamlss.data'
#> The following object is masked from 'package:datasets':
#> 
#>     sleep
#> Loading required package: gamlss.dist
@tjmahr
tjmahr / demo.md
Last active May 19, 2023 01:14
drawing a spectrogram outputted by praat with ggplot2
read_spectogram_file <- function(path) {
  lines <- readLines(path)
  powers <- lines |>
    stringr::str_match("z \\[(\\d+)\\] \\[(\\d+)\\] = (\\S+) ") |>
    as.data.frame() |>
    stats::setNames(c("line", "y", "x", "power"))

  # ugly bc i removed dplyr functions
  powers <- powers[!is.na(powers$line), 2:4] |>