Skip to content

Instantly share code, notes, and snippets.

View jknowles's full-sized avatar

Jared Knowles jknowles

View GitHub Profile
@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: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 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 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 / 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 / s_dplyr.R
Last active August 29, 2015 13:59 — forked from skranz/s_dplyr
# Helper functions that allow string arguments for dplyr's data modification functions like arrange, select etc.
# Author: Sebastian Kranz
# Examples are below
#' Modified version of dplyr's filter that uses string arguments
#' @export
s_filter = function(.data, ...) {
eval.string.dplyr(.data,"filter", ...)
}
@jknowles
jknowles / 0_reuse_code.js
Created April 27, 2014 02:40
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@jknowles
jknowles / helper_funcs.R
Last active September 10, 2017 04:30
R Helper functions for the Philadelphia SDP Cohort 8 Predictive Analytics Workshop
# Calculate the AUC of a GLM model easily
# Jared Knowles
# model = a fitted glm in R
# newdata = an optional data.frame of new fitted values
auc.glm <- function(model, newdata = NULL){
if(missing(newdata)){
resp <- model$y
# if(class(resp) == "numeric"){
# resp <- factor(resp)
# }
@jknowles
jknowles / robust_predict.lm.R
Created March 6, 2018 20:40
Robust Prediction Intervals for LM
predict.robust <- function(model, data, robust_vcov = NULL, level = 0.95,
interval = "prediction"){
# adapted from
# https://stackoverflow.com/questions/38109501/how-does-predict-lm-compute-confidence-interval-and-prediction-interval
# model is an lm object from r
# data is the dataset to predict from
# robust_vcov must be a robust vcov matrix created by V <- sandwich::vcovHC(model, ...)
# level = the % of the confidence interval, default is 95%
# interval = either "prediction" or "confidence" - prediction includes uncertainty about the model itself
if(is.null(robust_vcov)){