Skip to content

Instantly share code, notes, and snippets.

View samclifford's full-sized avatar

Sam Clifford samclifford

View GitHub Profile
@samclifford
samclifford / tidy_nfl.R
Created April 9, 2018 14:31
Tidy Tuesday NFL challenge
library(tidyverse)
library(hrbrthemes)
# download https://github.com/rfordatascience/tidytuesday/blob/master/data/tidy_tuesday_week2.xlsx
football <- read_xlsx("data/tidy_tuesday_week2.xlsx")
# get the top 16 paid players in each position for each year
to_plot <- football %>%
mutate(Team = 1:nrow(.)) %>%
gather(position, salary, -c(year, Team)) %>%
@samclifford
samclifford / inla_prior_poisson.R
Created November 22, 2019 11:42
Run a Poisson regression model with varying prior precisions on the linear effect
if (!require(INLA)){
install.packages("INLA",
repos=c(getOption("repos"),
INLA="https://inla.r-inla-download.org/R/testing"),
dep=TRUE)
}
library(INLA)
library(tidyverse)
library(broom)
@samclifford
samclifford / trim_percent.R
Created November 20, 2020 09:53
Trim trailing zeros from percentages
trim_percent <- function(x, ...){
require(magrittr)
require(scales)
require(stringr)
scales::percent(x, ...) %>%
stringr::str_replace(string = ., pattern = "0+\\%", replacement = "\\%") %>%
stringr::str_replace(string = ., pattern = "\\.\\%", replacement = "\\%")
}
library(tidyverse)
expand.grid(day = 1:7, week = 1:13) %>%
mutate(mu = 3*sin(day*2*pi/7)) %>%
mutate(p = boot::inv.logit(mu)) %>%
mutate(obs = rbinom(n(), size = 1, p = p),
pred = rbinom(n(), size = 1, p = p)) %>%
gather(key, value, obs, pred) %>%
mutate(value = factor(value)) %>%
ggplot(data =., aes(x = day, y = key)) +
geom_tile(aes(fill = value)) +
@samclifford
samclifford / NHANESre.R
Created March 10, 2021 22:15
Generate heatmaps of mixed effect model estimates using a model of poverty regressed on age, gender and random effects for education and sexual orientation by gender
library(NHANES)
data(NHANES)
library(tidyverse)
library(extrafont)
library(lme4)
library(RColorBrewer)
NHANES <- mutate(NHANES,
SexOrientation = fct_relevel(SexOrientation, "Heterosexual"),
Education = fct_relevel(Education, "High School"))