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)
library(palmerpenguins)
library(broom)
# Get rid of missing values
penguins <- penguins %>% drop_na(sex)
# Scatterplot
ggplot(penguins, aes(x = bill_length_mm, y = body_mass_g)) +
geom_point() +
$$
\begin{align}
&\ \textbf{Registered provinces for INGO } i \\
\text{Count of provinces}\ \sim&\ \operatorname{Ordered\,Beta}(\mu_{i_j}, \phi_y, k_{0_y}, k_{1_y}) \\[8pt]
&\ \textbf{Model of outcome average} \\
% Put the huge equation in a nested \begin{aligned}[t] environment so that
% \mathrlap{} can go around it so that the annotations in the priors can be
% aligned closer to the math
\mu_i =&\
\mathrlap{\begin{aligned}[t]
library(tidyverse)

# Example data
df <- tibble(org_id = 1:10,
  outcome = 1:10,
  issue_1 = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),
  issue_2 = c(NA, NA, "X", NA, "Y", NA, NA, NA, NA, "Z"))
df
#> # A tibble: 10 × 4
library(tidyverse)
library(kableExtra)
library(gt)
library(palmerpenguins)
penguins <- penguins %>% drop_na(sex)
# With kableExtra
penguins %>%
group_by(species, island, sex) %>%
---
title: "Example article that is actually a single normal .qmd file"
author:
- name: Jane Doe
affiliation: Big State University
- name: John Smith
affiliation: Liberal Arts College
abstract: |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
date: February 28, 2023
library(tidyverse)
library(gapminder)
# Using ifelse() with just one condition (good)
gapminder_new <- gapminder %>%
mutate(is_africa = ifelse(continent == "Africa", "Africa", "Not Africa"))
# Using ifelse() with nested conditions (ew)
gapminder_new <- gapminder %>%
mutate(
@andrewheiss
andrewheiss / ggplot_cairo.R
Created May 7, 2015 04:04
Custom fonts in ggplot2 with Cairo
library(ggplot2)
library(Cairo) # MAGIC PACKAGE
fake.data <- data.frame(x = rnorm(100), y = rnorm(100), z = rbinom(100, 1, 0.5))
p <- ggplot(fake.data, aes(x=x, y=y)) +
geom_line() +
labs(x="Science", y="More science", title="Here's a title") +
facet_wrap(~ z) +
theme_bw() +
data_to_plug_in_cat <- expand.grid(
sex = c("female", "male"),
bill_length_mm = c(40, 44, 48),
body_mass_g = mean(penguins$body_mass_g)
)
data_to_plug_in_cat_mat <- model.matrix(
~ 0 + ., data = data_to_plug_in_cat
)

The approach I typically take is to draw them as a pseudo node that points at the middle of a path between two real nodes (see Figure 7 here https://royalsocietypublishing.org/doi/10.1098/rspb.2020.2815 where Q interacts with/modifies X (and how to draw it with R: https://gist.github.com/andrewheiss/6546a22672a13a4eacd8a9a726eac88a). It’s just like Figure 3A in the Attia et al. article too (https://academic.oup.com/ije/article/51/4/1047/6607680), and Laubach, Murray, et al. have a longer appendix about it all too.

It’s a tricky solution because the regular rules of do-calculus don’t apply, since the Q node isn’t really in the DAG in normal ways.

In practice, what I’ve generally seen is that people generally don’t care about interaction nodes because interactions are a statement of functional form and DAGs are supposed to be function-agnostic.

For instance, if you have a situation like y ~ x + a + a*x, where a is binary, and your main treatment is x, the causal effect of x depends on whether or

library(tidyverse)
library(tidygeocoder)
library(sf)
library(albersusa)
places <- tribble(
~name, ~address,
"My empty GSU office", "14 Marietta Street NW, Atlanta, GA 30303",
"My old BYU office", "155 East 1230 North, Provo, UT 84604",
"My old Duke office", "201 Science Dr, Durham, NC 27708"