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
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] |>
library(tidyverse)
example <- 
  tibble::tribble(
    ~token, ~surprisal,                          ~sentence,
    "them",     15.291,      "them eat them hotdogs soon.",
    "eat",      6.539,      "them eat them hotdogs soon.",
    "them",      2.739,      "them eat them hotdogs soon.",
    "hot",      2.164,      "them eat them hotdogs soon.",
    "##dog",     11.064,      "them eat them hotdogs soon.",
@tjmahr
tjmahr / glmmTMB-splines.md
Created August 2, 2022 18:57
weird lil bug in glmmTMB
library(glmmTMB)
library(splines)


x <- glmmTMB(
  am ~ ns(wt, df = 3), 
  dispformula = ~ ns(wt, df = 2), 
  data = mtcars
)
path_uncommon <- function(paths) {
# How long is the common part when we split it up?
common_lengths <- paths |>
fs::path_common() |>
fs::path_split() |>
unlist()
if (length(common_lengths)) {
paths |>
fs::path_split() |>
@tjmahr
tjmahr / brms-formula-wrapping.Rmd
Last active April 13, 2022 17:35
looks for brms summary() output and tries to add line-breaks at sweet spots in the formula
---
title: "Untitled"
output: pdf_document
date: '2022-04-13'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
@tjmahr
tjmahr / demo.md
Created March 25, 2022 19:31
tar_file() works on a directory
library(targets)
dir <- tempdir()
setwd(dir)
tar_script() 

dir.create("data")
write.csv(mtcars, "data/mtcars.csv")
library(tidyverse)
library(ordinal)
#> 
#> Attaching package: 'ordinal'
#> The following object is masked from 'package:dplyr':
#> 
#>     slice
wine <- as_tibble(wine)
wine
library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
#> Warning: package 'readr' was built under R version 4.1.2
d <- readr::read_tsv("C:/Users/Tristan/Downloads/dogs.txt")
#> Rows: 30 Columns: 26
#> -- Column specification --------------------------------------------------------
#> Delimiter: "\t"
#> dbl (26): Dog, T.0, T.1, T.2, T.3, T.4, T.5, T.6, T.7, T.8, T.9, T.10, T.11,...
#> 
library(tidyverse)

mtcars2 <- mtcars %>% 
  tibble::rownames_to_column()

# create a list of dataframe with a shared id column and 
# a random subset of rows
tables_to_combine <- names(mtcars2)[-1] %>% 
  lapply(function(x) mtcars2[c("rowname", x)]) %>% 
library(nlme)
library(ggplot2)
CO2$is_qc1 <- CO2$Plant == "Qc1"
ggplot(subset(CO2, Type == "Quebec" & Treatment == "chilled")) + 
  aes(x = conc, y = uptake) + 
  stat_smooth(
    method = nls, 
    formula = y ~ SSasymp(x, Asym, lrc, c0),
    se = FALSE,