Skip to content

Instantly share code, notes, and snippets.

View tjmahr's full-sized avatar
🍍
sampling

TJ Mahr tjmahr

🍍
sampling
View GitHub Profile
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
``` r
A <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), nrow = 3, ncol = 4)
B <- matrix(c(13, 14, 15, 16, 17, 18, 19, 20), nrow = 4, ncol = 2)
dot_product <- function(x, y) {
mapply(\(x, y) sum(x * y), x, y)
}
rows_of <- function(m) purrr::array_tree(m, 1)
cols_of <- function(m) purrr::array_tree(m, 2)
@tjmahr
tjmahr / rvar-marginals.md
Last active May 13, 2024 13:58
using rvar to compute marginal means from a mixed effects model

Local .Rprofile detected at C:\Users\mahr\Documents\WiscRepos\2021-04-kh-td-intel-and-rate\.Rprofile

library(faux)
#> 
#> ************
#> Welcome to faux. For support and examples visit:
#> https://debruine.github.io/faux/
#> - Get and set global package options with: faux_options()
#> ************
library(splines)
model <- lm(mpg ~ ns(wt, df = 3), mtcars)

# make and plot a basis
basis <- ns(sort(mtcars$wt), df = 3)
matplot(basis, type = "l")

@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