Skip to content

Instantly share code, notes, and snippets.

View jknowles's full-sized avatar

Jared Knowles jknowles

View GitHub Profile
@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 / 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 / barplots.R
Created February 20, 2020 19:33
How to order bar charts in ggplot2
# Load ggplot2
library(ggplot2)
# Load example data
data(mtcars)
# Create a character vector of car names
mtcars$name <- row.names(mtcars)
# Plot car names by mpg
ggplot(mtcars, aes(x = name, y = mpg)) +
@jknowles
jknowles / server.R
Created January 8, 2013 16:01
Making a shiny app of the 538 demo on the excellent is.R() blog.
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")
#election.data <- read.csv("electiondata.csv")
shinyServer(function(input,output){
output$voteplot<-reactivePlot(function(){
Mode <- function(X) {
@jknowles
jknowles / gist:4674473
Created January 30, 2013 16:34
lavaan SEM plots with semPlot package
library(lavaan)
model <- '
# latent variable definitions
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + a*y2 + b*y3 + c*y4
dem65 =~ y5 + a*y6 + b*y7 + c*y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
@jknowles
jknowles / sdp_reg_exercise_1_example_2019.R
Created June 17, 2019 15:50
Exploring student composition effects on test score growth of school average scores. Example for SDP 2019 Regression course.
###################################################################################################
## Title: Regression Module Assignment 1
## Exploring Student Composition Effects on Test Score Growth
## Author: Jared E. Knowles, Civilytics Consulting
## Date: 6/12/2019
## Last Updated: 6/17/2019
###################################################################################################
# ----------------------------------------------------------------------------
# Load the data
@jknowles
jknowles / MWEblogpost.Rmd
Created May 27, 2013 22:30
R Markdown of blog post on R minimal working examples (MWE).
How to Ask for Help using R
========================================================
The key to getting good help with an R problem is to provide a minimally working
reproducible example (MWRE). Making an MWRE is really easy with R, and it will
help ensure that those helping you can identify the source of the error, and
ideally submit to you back the corrected code to fix the error instead of sending
you hunting for code that works. To have an MWRE you need the following items:
- a minimal dataset that produces the error
@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 / cpe_functions.R
Last active December 4, 2018 22:46
Functions to Support CPE and Census Data Alignment
################################################################################
# Functions to find the data
################################################################################
# Finders
# Simple functions that take the ID code from CPE (e.g. 49-00039) and look up
# the respective data for it in the structure provided in teh competition
find_police_shape <- function(dept_id, kaggle_kernel = FALSE) {
if(kaggle_kernel == TRUE) {
prefix = "../input/cpe-data/"
###############################################################################
## SDP Fall Workshop Predictive Analytics
## Advanced / Additional Code Snippets for Working with PA Data and Models
## Author: Jared E. Knowles
## Date: 09/14/2018
## You do not need to use all or even any of this code. The code does not need to
## be run together. This is just a survey of some additional techniques/tricks you
## can do in R to make explaining predictive models and complex data easier.
## As always - your needs and approaches may different.
################################################################################