Skip to content

Instantly share code, notes, and snippets.

View cmrnp's full-sized avatar

Cameron Patrick cmrnp

View GitHub Profile
@cmrnp
cmrnp / multiple_models_tidy.R
Last active October 27, 2021 06:06
Tidyverse approach to fitting multiple models
library(tidyverse)
library(palmerpenguins) # for `penguins` data set
# fit a model for each variable (bill length, bill depth, flipper length)
penguin_models <- penguins %>%
pivot_longer(c(bill_length_mm, bill_depth_mm, flipper_length_mm),
names_to = "outcome_name",
values_to = "outcome") %>%
group_by(outcome_name) %>%
summarise(model = list(lm(outcome ~ species, data = cur_data())))
@cmrnp
cmrnp / multiple_models_tidy_2.R
Created October 27, 2021 06:16
Tidyverse approach to fitting multiple models using data frame of formulas
library(tidyverse)
library(palmerpenguins) # for `penguins` data set
penguin_model_formulas <- tribble(
~name, ~formula,
"depth only", "bill_length_mm ~ bill_depth_mm",
"species only", "bill_length_mm ~ species",
"depth and species", "bill_length_mm ~ bill_depth_mm + species",
)
@cmrnp
cmrnp / example_geo_data.R
Created January 21, 2022 13:46
example of spatial data which may be desirable to # overlay on a map (for Twitter thread)
# example_geo_data.R: example of spatial data which may be desirable to
# overlay on a map.
# Cameron Patrick <cameron.patrick@unimelb.edu.au>, 22 Jan 2022.
library(tidyverse)
library(rnaturalearth)
library(ragg)
# You will also need the natural earth data:
# remotes::install_github("ropensci/rnaturalearthhires")
library(emmeans)
data(mtcars)
# in the example here, all models give the same point estimates and similar
# SEs because there is only a single, categorical, variable in the model.
# if you add a continuous predictor, they will no longer, because the relationship
# assumed by the model will be different for the three models!
m <- glm(am ~ vs, data = mtcars, family = binomial)
em1 <- emmeans(m, "vs")
library(tidyverse)
library(lubridate)
library(glue)
library(jsonlite)
library(cowplot)
# thanks to u/doubleunplussed on reddit for this dataset
json_url <- "https://pastebin.com/raw/gxZAUJwd"
ib_rate_data_orig <- fromJSON(json_url)
@cmrnp
cmrnp / qdrg_metafor.R
Created August 31, 2022 05:36
Using emmeans with metafor objects - "quick and dirty reference grid"
# Using emmeans with metafor objects - "quick and dirty reference grid"
# usage:
# emmeans(qdrg_metafor(my_metafor_object), "categorical_var")
qdrg_metafor <- function(object) {
coefs <- coef(object)
names(coefs)[1] <- "(Intercept)"
vcovs <- vcov(object)
rownames(vcovs)[1] <- "(Intercept)"
colnames(vcovs)[1] <- "(Intercept)"
qdrg(
library(dplyr)
library(glmmTMB)
nobs <- 100
ngroup <- 5
dat <- tibble(
x = sample(1:3, size = nobs, replace = TRUE, prob = c(0.15, 0.5, 0.35)),
y = runif(nobs),
z = case_when(
x == 1 ~ 0,
@cmrnp
cmrnp / ggplot_supermongo.R
Last active April 16, 2023 03:56
An example of creating a plot in ggplot somewhat matching the style of SuperMongo
library(tidyverse)
library(ggrepel)
library(ragg)
library(showtext)
showtext_auto()
font_add("AVHershey Complex Medium", "AVHersheyComplexMedium.ttf")
data(Animals, package = "MASS")
@cmrnp
cmrnp / CLT_30_demo.Rmd
Created March 15, 2024 03:23
Code for simulation of convergence due to Central Limit Theorem: is n = 30 a good rule of thumb?
---
title: "CLT rule of thumb demo"
author: "Cameron Patrick"
date: "2024-03-15"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,