Skip to content

Instantly share code, notes, and snippets.

View jknowles's full-sized avatar

Jared Knowles jknowles

View GitHub Profile
@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 / 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 / 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 / server.R
Created January 8, 2013 16:00
Simulating fitting a coin and receiving payoffs. A shiny app.
library(shiny)
library(scales)
shinyServer(function(input,output){
trialInput<-reactive(function(){
bias<-input$coin
sims<-input$obs
reps<-input$reps
trials<-rbinom(reps,sims,0.5+bias)
})
@jknowles
jknowles / server.R
Created January 8, 2013 15:57
Demonstrating bi-variate correlations using simulation.
# Script to demonstrate distributions
library(eeptools)
library(shiny)
library(ggplot2)
rnormcor <- function(x,rho) rnorm(1,rho*x,sqrt(1-rho^2))
shinyServer(function(input,output){
output$distPlot<-reactivePlot(function(){
@jknowles
jknowles / server.R
Created January 8, 2013 15:54
Show how different moments of a distribution can shift it away from a normal distribution.
# Script to demonstrate distributions
library(VGAM)
library(eeptools)
library(shiny)
library(ggplot2)
shinyServer(function(input,output){
@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)
})
@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: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 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) {