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 / ordinal.md
Created April 30, 2024 03:55
ordinal-plot-code

How I made the ordinal plot for this paper https://onlinelibrary.wiley.com/doi/10.1111/dmcn.15198

Local .Rprofile detected at C:\Users\Tristan\Documents\WiscRepos\2021-10-hl-vss\.Rprofile

library(tidyverse)

if (FALSE) {
  # Read in the models and make the tribbles for the demo
  draws_epred_means <- targets::tar_read("draws_epred_means")
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)
  )
@tjmahr
tjmahr / pixels-to-degrees.R
Created February 6, 2018 21:13
quick script to convert pixel screen distances to degrees for our lab's eyetracker
pixels_to_degrees <- function(px_distance, mm_from_screen) {
# Work in millimeters
pixels_per_mm <- 3.71
mm_distance <- px_distance / pixels_per_mm
# Trigonometry. Eye-to-screen is adjacent edge of triangle. Subtended portion
# of screen is opposite edge. Solve for angle by using opp/adj =
# tangent(angle)
tangent <- mm_distance / mm_from_screen
radian <- atan(tangent)
@tjmahr
tjmahr / ggpreview.R
Created October 2, 2017 12:32
ggpreview - preview ggsave output in system image viewer
library(ggplot2)
# preview a file that would be created by ggsave()
ggpreview <- function(...) {
fname <- tempfile(fileext = ".png")
ggsave(filename = fname, ...)
system2("open", fname)
invisible(NULL)
}
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