Skip to content

Instantly share code, notes, and snippets.

View jknowles's full-sized avatar

Jared Knowles jknowles

View GitHub Profile
@jknowles
jknowles / ScopingFunctions.R
Created January 19, 2012 23:13
Functions for figuring out where user specified thresholds are met in a vector using R
#######################################
# Example data
######################################
testdata<-replicate(10, rpois(100, 20))
###########################################
# This function tells us how far we have to
@jknowles
jknowles / TEXandRStudioEnv
Created January 27, 2012 17:19
Set R Sysenvironment for LaTeX and BibTex in RStudio
#Set Environment Variables
TEXINPUTS="C:\\" #Path to tex file in Windows
Sys.setenv(TEXINPUTS="C:\\~", BIBINPUTS=TEXINPUTS,BSTINPUTS=TEXINPUTS)
#Path to texfiles in Windows, set BIB files and BST files the same
#Run before clicking "Compile PDF"
@jknowles
jknowles / animatedheaRt
Created May 5, 2012 21:41
An Animated Heart in R
############################################################
## Title: Make an Animated Heart in R
## Author: Jared Knowles
## Date: May 5th, 2012
############################################################
# Make heart curve
t<-seq(-100,100,length.out=1000) # Order
x<-16*sin(t)^3 # Create the Xs from a formula
y<-(13*cos(t))-(5*cos(2*t))-(2*cos(3*t))-(cos(4*t)) # Create Ys from a formula
@jknowles
jknowles / dropbox_source.R
Created July 15, 2012 01:52
Source an R script hosted in Dropbox from R
# Made by Jared Knowles
# Not necessarily going to work for scripts outside of the Public folder
# Not recommended for production use
# myurl must be a character string with the full public link of the script file
# Problem--we can't use URL shorteners
dropbox_source<-function(myurl){
s<-str_extract(myurl,"[/][a-z][/]\\d+[/][a-z]*.*")
new_url<-paste("http://dl.dropbox.com",s,sep="")
@jknowles
jknowles / FRLmap.R
Created August 26, 2012 21:31
FRL map
#######################################
## FRPL MAP ##
## Create a series of maps plotting ##
## FRPL proportions from Wisconsin ##
## Publicly available data ##
######################################
# Version 0.7
# Date: 08/21/2012
# Author: Jared E. Knowles, Policy Research Advisor DPI
@jknowles
jknowles / gen_lotto
Created November 27, 2012 04:17
Making lottery numbers that are less likely to results in splitting the winnings.
# Generate lotto numbers
# Avoid a split pot
# Author: Jared Knowles
# Date: 11/26/2012
# All numbers are equally likely to be selected to win in PowerBall,
# but what fun is winning if we have to share the pot?
# Therefore, we will only select numbers that minimize our
# likelihood of sharing the pot.
@jknowles
jknowles / server.R
Created November 27, 2012 16:37
Election Simulations
library(shiny)
# data from : http://is-r.tumblr.com/post/35266021903/five-thirty-hate
election.data <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/election2012.csv")
shinyServer(function(input,output){
output$voteplot<-reactivePlot(function(){
Mode <- function(X) {
@jknowles
jknowles / server.R
Created January 8, 2013 15:40
A shiny app to demonstrate the relationship between field goal kicking and distance for two NFL kickers. Useful for demonstrating some statistical properties.
library(shiny)
library(scales)
# obs
# length
dat<-data.frame(dist=c("all","under20","twenties","thirties","forties","over50"),
att=c(266,6,79,74,85,22),
made=c(230,6,76,67,66,15))
dat$per<-dat$made/dat$att
dat$distprob<-dat$att/266
@jknowles
jknowles / server.R
Created January 8, 2013 15:50
Showing different sampling methods graphically in a `shiny` app.
library(shiny)
library(ggplot2)
library(eeptools)
library(plyr)
# Sampling examples
probs<-c(.05,.15,.4,.4)
samprobs<-c(.4,.4,.15,.05)
###############
# Convenience functions
@jknowles
jknowles / server.R
Created January 8, 2013 15:52
Draw a normal distribution with a given number of observations. Demonstrate what sample sizes and approximations can mean.
library(shiny)
shinyServer(function(input,output){
output$distPlot<-reactivePlot(function(){
dist<-rnorm(input$obs)
p<-qplot(dist,binwidth=0.1)+geom_vline(xintercept=mean(dist))+theme_dpi()
p<-p+coord_cartesian(xlim=c(-4,4))+geom_vline(xintercept=median(dist),color=I("red"))
print(p)
})