Skip to content

Instantly share code, notes, and snippets.

View carlislerainey's full-sized avatar

Carlisle Rainey carlislerainey

View GitHub Profile
@carlislerainey
carlislerainey / cis-for-percents.R
Created August 2, 2017 16:22
Code to illustrate CIs for Percents
# simulation parameters
n <- 25
pop_proportion <- 1/6
n_reps <- 100
# simulate data
y <- rbinom(n_reps, n, pop_proportion)
# compute sample percent, estimated SE, and 95% CI
@carlislerainey
carlislerainey / evaluate-pres-election-models.R
Last active October 24, 2017 00:35
Evaluation presidential election forecasting models
# evaluate model fit by predicting years, one-by-one, out-of-sample
evaluate_models <- function(..., data, group, model_names) {
require(dplyr, warn.conflicts = FALSE, quietly = TRUE)
formulas <- list(...)
# in-sample rmse
eval1 <- NULL
for (i in 1:length(formulas)) {
f <- formulas[[i]]
fit0 <- lm(f, data = data)
df0 <- data.frame(model = model_names[i],
# clear workspace
rm(list = ls())
# load packages
library(ggplot2)
# box
number_of_draws <- 5
box <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0)
@carlislerainey
carlislerainey / clt.R
Created November 27, 2017 14:15
code illustrating central limit theorem using box model
# load packages
library(tidyverse) # includes ggplot
library(foreach) # for times()
library(magrittr)
# set up box and number of draws
box <- c(1, 3, 5, 7)
n_draws <- 400
events <- 1500:10000
@carlislerainey
carlislerainey / probit-product-term.R
Created December 1, 2017 23:05
code to understand product terms and probit models
# code to understand product terms and probit models
# clear workspace
rm(list = ls())
# load packages
library(tidyverse)
library(plotly)
# parameters
@carlislerainey
carlislerainey / gbread.R
Created December 24, 2017 17:13
Code for plotting the votes in the 2017 McDonald Gingerbread House Competition.
library(tidyverse)
library(googlesheets)
library(forcats)
sheet_key <- "10qrPd-2RJ2HXsEi1fIYm1QeCVXWnGWVgV8iq-Ph7CRs"
sheet <- gs_key(sheet_key) # register google sheet
votes_raw <- gs_read(sheet) %>%
glimpse()
@carlislerainey
carlislerainey / list.md
Last active December 27, 2017 12:56
testing GitHub-flavored markdown task lists
  1. task 1
  2. task 2
# load packages -----------------------------------------------------------
library(tidyverse)
library(magrittr)
library(blme)
library(rstanarm); options(mc.cores = parallel::detectCores())
# also need the package `arm` installed
@carlislerainey
carlislerainey / berry-replication.R
Created May 8, 2018 14:31
Code attempting to replicate a simulation finding from Berry et al.
# note this code is written for clarity, not speed, so be careful before setting
# n_sims too large. when we feel it's correct, both loops are embarrassingly
# parrellel, so we can hit the outer loop with foreach.
# in the meantime, just set n_sims to 10,000 or 50,000 (and give it a minute) when
# you are ready
# load packages
library(tidyverse)
@carlislerainey
carlislerainey / makefile-dag.md
Last active October 12, 2022 10:18
Drawing the Makefile DAG