Skip to content

Instantly share code, notes, and snippets.

View carlislerainey's full-sized avatar

Carlisle Rainey carlislerainey

View GitHub Profile
# set wd
setwd("~/Dropbox/classes/pols-209")
# load packages
library(dplyr)
library(ggplot2)
# load data
# set wd
setwd("~/Dropbox/classes/pols-209")
# load packages
library(dplyr)
library(ggplot2)
# load data
nominate <- readRDS("data/nominate.rds") # read data set
@carlislerainey
carlislerainey / load-runs.R
Created February 22, 2017 18:33
Code to load my run logs from Google Sheets
# data loading and tidying
##########################
# Note: we haven't talked about how to do this, and I don't expect you to
# understand the code below.
library(dplyr) # useful function for cleaning up data
library(lubridate) # useful for working with dates and durations
library(magrittr) # useful for data manipulation
library(googlesheets) # used to load the google sheet data
sheet <- gs_key("19aZA5_xnMiNfrpJpVur1FxNWGxUe3l-LlJ8vPp7_uBc") # register google sheet
runs <- gs_read(sheet) # load sheet
@carlislerainey
carlislerainey / predicting-predictions.R
Last active February 23, 2017 17:10
Code to load data from survey on predicting height using other body measurements
# data loading and tidying
##########################
# Note: we haven't talked about how to do this, and I don't expect you to
# understand the code below.
library(dplyr) # useful function for cleaning up data
library(lubridate) # useful for working with dates and durations
library(magrittr) # useful for data manipulation
library(tidyr) # to gather the data
library(googlesheets) # used to load the google sheet data
sheet <- gs_key("11pXvnrDfygcaMp6iI-30BasveKYLZ4Ce9Z0-cuuKLww") # register google sheet
@carlislerainey
carlislerainey / evaluate-models.R
Last active April 3, 2017 13:02
Code to calculate in-sample rms error, BIC, and out-of-sample (by group) rms error for linear 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],
# note: for this code to work, you need to have devtools, dplyr, and
# ggplot2 install, as well as the data sets in your data folder.
# set working directory **CHANGE THIS**
setwd("~/Dropbox/classes/pols-209")
# load packages
library(dplyr)
@carlislerainey
carlislerainey / box-model.R
Last active April 10, 2017 13:06
code to repeat a box model
# box model
box <- c(0, 1) # the box
N <- 10 # the number of draws to take from the box
n_reps <- 10000 # the number of times to take and sum N draws from the box
# create a function to take and sum N draws from the box n_reps times
box_model <- function(box, N, n_reps) {
sums <- numeric(n_reps)
# load packages
library(tidyverse)
# change these values
n_1s <- 10 # number of 1s in the box
n_0s <- 10 # number of 0s in the box
sample_size <- 25 # number of times to draw from the box (w/ replacement)
# number of times to repeat the study
@carlislerainey
carlislerainey / wa1-plots.R
Last active July 13, 2017 16:20
Create the plots to motivate Writing Assignment 1 for POLS 209 (Summer 2017).
# remember to set working directory!
# load packages
library(ggplot2)
library(dplyr)
# load data
d <- readRDS("data/state-legislators.rds")
@carlislerainey
carlislerainey / chs. 11-12 code
Created July 19, 2017 16:36
code for help teaching chs. 11 and 12 of FPP
# data loading and tidying
##########################
# Note: we haven't talked about how to do this, and I don't expect you to
# understand the code below. But trust me that it grabs the data you submitted
# in the forms and loads it as a data frame.
library(googlesheets) # used to load the google sheet data
sheet <- gs_key("1mJVzIpolvJ5XPROMCXg1A5iU_mlWXwHBlXQwG5t_QVk") # register google sheet
bodies <- gs_read(sheet) # load sheet
saveRDS(bodies, "data/bodies.rds") # save the former google sheet as a .rds
rm(sheet, bodies) # remove the sheet to have a clean workspace