Skip to content

Instantly share code, notes, and snippets.

View andrewheiss's full-sized avatar
👨‍💻
#rstats-ing all the things

Andrew Heiss andrewheiss

👨‍💻
#rstats-ing all the things
View GitHub Profile
---
title: Panel tabset from list of plots
---
```{r}
#| warning: false
#| message: false
library(tidyverse)
library(glue)
library(tidyverse)

mean_sales <- c(dairy = 5364.5846, meat = 5059.6955, fish = 764.4324, deli = 1744.4206, cheese = 364.5226)
sd_sales <- c(dairy = 1192.3751, meat = 1560.7741, fish = 333.7008, deli = 509.8426, cheese = 127.2061)

# This is from the lesson---this is how you iterate over two things
map2(mean_sales, sd_sales, \(.x, .y) rnorm(30, .x, .y))
#> $dairy
#>  [1] 5260.738 5948.625 4823.229 5332.571 5087.384 3951.635 3381.243 4245.053
---
title: "Fancy causal quartet"
date: 2024-09-06
author: "Andrew Heiss"
---
```{r}
#| warning: false
#| message: false
library(tidyverse)
library(lme4)
library(marginaleffects)

model <- lmer(weight ~ Time + I(Time^2) + Diet*Time + (1 | Chick), data = ChickWeight)

# This is a shortcut for plotting predictions automatically
plot_predictions(model, condition = "Time")
library(MASS)
library(tidyverse)
library(marginaleffects)
library(palmerpenguins)

# Make a categorical weight column
penguins <- penguins |> 
  drop_na(sex) |> 
  mutate(weight_cat = cut(
library(tidyverse)
library(brms)
library(marginaleffects)
library(tidybayes)
library(ggh4x)
library(scales)
# Ordered logit model
ologit_priors <- c(
prior(student_t(1, 0, 3), class = Intercept),
---
title: "Testing with lots of plots"
---
```{r}
#| label: fun-generate-chunks
#| include: false
generate_chunk <- function(id) {
paste0(
---
title: "Testing"
---
```{r}
#| label: fun-generate-chunks
#| include: false
generate_chunk <- function(id) {
paste0(
library(tidyverse)
library(marginaleffects)
library(gapminder)

gapminder_2007 <- gapminder |> 
  filter(year == 2007)

# Use log() in the model formula
model <- lm(lifeExp ~ log(gdpPercap), data = gapminder_2007)
library(tidyverse)
library(mlogit)
library(dfidx)
library(marginaleffects)

chocolate <- read_csv("https://www.andrewheiss.com/blog/2023/08/12/conjoint-multilevel-multinomial-guide/data/choco_candy.csv") %>% 
  mutate(
    dark = case_match(dark, 0 ~ "Milk", 1 ~ "Dark"),
    dark = factor(dark, levels = c("Milk", "Dark")),