Skip to content

Instantly share code, notes, and snippets.

View mjskay's full-sized avatar

Matthew Kay mjskay

View GitHub Profile

Bounded KDE for ggdist comparison

Matthew Kay 2023-04-21

library(ggplot2)
library(dplyr)
library(distributional)
library(ggdist)    # requires remotes::install_github("mjskay/ggdist@dev")

Simple example of pattern matching and rewriting of expressions in R with a bquote()-style syntax.

N.B. doesn't support splicing-style syntax (..()), but shouldn't be too hard to add.

match_exprs = function(expr, ...) {
  patterns = list(...)
  for (pattern in patterns) {
    tryCatch({
      match = match_expr(expr, pattern[[2]])

All combinations of right-hand-side terms in a formula

Given this formula…

fixed = y ~ x1 + x2 + x3 + z1 + x3:z1

We can extract all the terms as bare expressions (I prefer this to using

@mjskay
mjskay / continuous_interaction_ordinal_brms.md
Created April 1, 2022 21:32
example of a continuous interaction plot with an ordinal model in brms
library(brms)
library(tidybayes)
library(dplyr)
library(ggplot2)
library(modelr)

mtcars_clean = mtcars %>%
  mutate(cyl = ordered(cyl))
@mjskay
mjskay / blinky_dotplot.R
Last active November 15, 2019 23:42
Blinky quantile dotplot + hypothetical outcome plot
library(tidyverse)
library(gganimate)
library(tidybayes) # need latest vesion from github for geom_dotsh()
k = 50
draws = tibble(
x = qnorm(ppoints(k)),
draw = sample(1:k, k)
)