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
library(tidyverse)

# geom_bar() doesn't need a variable mapped to the y-axis; it calculates it on its own behind the scenes
ggplot(mpg, aes(x = drv, fill = factor(year))) +
  geom_bar()

library(tidyverse)
library(marginaleffects)
library(broom)
library(palmerpenguins)

penguins <- penguins %>% drop_na(sex)

# Marginal means in conjoint world are just the averages for each of the levels in a factor 
# variable included in a regression model (i.e. instead of using an omitted reference 
library(tidyverse)
library(broom)

# Model with a cateogorical predictor
example_model <- lm(hwy ~ displ + drv, data = mpg)

# Extract all the right-hand variables
rhs <- all.vars(stats::update(formula(example_model), 0 ~ .))
rhs
library(tidyverse)
library(sf)
library(rnaturalearth)
library(countrycode)
library(gapminder)

# rerturnclass = "sf" makes it so the resulting dataframe has the special
# sf-enabled geometry column
world_map <- ne_countries(scale = 50, returnclass = "sf") %>% 
library(tidyverse)
library(palmerpenguins)
library(patchwork)
library(rcartocolor)
library(scales)

# Clean up the data
penguins <- penguins |> 
  drop_na(sex) |>
library(tidyverse)
library(patchwork)
library(latex2exp)
logit_df <- tibble(x = seq(0, 100, length.out = 101),
logits = seq(-4, 4, length.out = 101)) |>
mutate(odds = exp(logits)) |>
mutate(probs = plogis(logits))
p1 <- ggplot(logit_df, aes(x = x, y = probs)) +
library(tidyverse)
library(ggdist)
library(ggpattern)

# Just look at a few categories
diamonds_small <- diamonds %>% 
  filter(cut %in% c("Fair", "Good", "Very Good"))

# Manually calculate summary statistics since we can't use stat_pointinterval()
library(tidyverse)
library(brms)
library(tidybayes)
library(marginaleffects)

# Example data with proportions
example_data <- tribble(
  ~answer,               ~n, ~total,
  "Very familiar",       88, 205,
library(tidyverse)
library(brms)
library(tidybayes)


data_thing <- tribble(
  ~answer,               ~regime_type,  ~n,
  "Very familiar",       "Democracy",   117,
  "Very familiar",       "Autocracy",   88,