Skip to content

Instantly share code, notes, and snippets.

View carlislerainey's full-sized avatar

Carlisle Rainey carlislerainey

View GitHub Profile
@carlislerainey
carlislerainey / fakeData.R
Created November 19, 2014 17:28
Create fake data for hierarchical models.
fakeData <- function() {
n <- 2000
n.groups <- 100
x1 <<- rnorm(n); x2 <<- rnorm(n); x3 <<- rnorm(n)
X <- cbind(1, x1, x2, x3)
beta0 <<- runif(ncol(X), -1, 1)
group <<- rep(1:n.groups, length.out = n)
@carlislerainey
carlislerainey / uninsurance_and_attitudes.R
Created July 6, 2015 01:46
Plotting uninsurance and health policy attitudes.
# load packages
library(readr) # for reading data
library(ggplot2) # for graphics
library(dplyr) # for data manipulation
library(gridExtra) # for arranging plots in grids
library(texreg) # for printing output
library(arm) # for rescale() function
# load data
@carlislerainey
carlislerainey / pair-students.R
Last active September 1, 2015 15:44
A short script to randomly pair students.
# some code to randomly pair classmates
# set seed
set.seed(413876)
# a vector of students
students <- c("a", "b", "c", "d")
# create random pairs
n_students <- length(students)
@carlislerainey
carlislerainey / soren.R
Last active October 1, 2015 13:01
code for understanding firth's bias correction for logit models
library(ggplot2)
library(MASS)
library(logistf)
set.seed(12345678)
############ FAKE DATA ############
rho <- 0.0
k <- 3
sigma <- matrix(rho, k, k); diag(sigma) <- 5
@carlislerainey
carlislerainey / switches-and-streaks.R
Created October 30, 2015 10:22
Code to simulate and calculate the number of switches and streaks in a series of coin tosses
calc_switches <- function(x) {
n <- length(x)
x0 <- x[1:(n - 1)]
x1 <- x[2:n]
switches <- sum(x0 != x1)
return(switches)
}
calc_streak <- function(x) {
@carlislerainey
carlislerainey / mass-shootings.R
Last active December 4, 2015 12:51
Downloads and plots data on mass shootings from Mother Jones (http://www.motherjones.com/politics/2012/12/mass-shootings-mother-jones-full-data)
# load packages
library(magrittr)
library(googlesheets)
library(lubridate)
library(dplyr)
library(stringr)
library(tidyr)
library(ggplot2)
library(maps)
@carlislerainey
carlislerainey / read-handedness-data.R
Last active January 30, 2017 13:47
Code to read the handedness data collected a survey.
# this code uses the googlesheets package, which you may need to install using install.packages("googlesheets")
# load packages
library(googlesheets) # used to load the google sheet data
library(ggplot2) # used to create graphics
# register google sheet
sheet <- gs_key("1_bGZNmVkT2NBxJd-vQdqmy75UgFtIJ-E0m6mK7eRXyk")
@carlislerainey
carlislerainey / r-assign-01-key.R
Last active February 5, 2017 22:40
Key for Computing Assignment 1
# Computing Assignment 1 Code
### Question 1
x <- c(3, 4, 2, 6, 1) # create a numeric vector
### Question 2
3*x # multiply the numeric vector by three
@carlislerainey
carlislerainey / r-assign-02-key.R
Last active February 5, 2017 22:38
Key for Computing Assignment 2
# Computing Assignment 2 Code
# set working directory
setwd("~/Dropbox/classes/pols-209")
### Task 1
# load packages
library(tibble) # for glimpse()
# set working directory
setwd("~/Dropbox/classes/pols-209")
# load packages
library(ggplot2) # for creating figures
library(tibble) # for glimpse()
# load data
leg <- readRDS("data/state-legislators.rds") # data are in the data subfolder