Skip to content

Instantly share code, notes, and snippets.

@dsparks
dsparks / CorrelationEllipses.R
Created February 9, 2011 19:39
correlation plots with ellipses
# Correlation ellipses
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ellipse")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Function to plot colored correlation ellipses
correlationEllipses <- function(cor){
require(ellipse)
# Illustrative use of plyr
# LOAD LIBRARIES REQUIRED
library(plyr);
library(XML)
# FIGURE OUT PATTERN OF URL FOR EACH SEASON
url.b1 = 'http://ca.sports.yahoo.com/nhl/stats/byposition?pos=C,RW,LW,D';
@dsparks
dsparks / Built-in Functions.R
Created February 10, 2011 00:49
A couple of functions whose names I have trouble remembering
# run length encoding, or counting streaks
rle
# combinations, combinatorics, n choose k
combn
# allows you to dump an object in the form of R code
dput
# modulo
@dsparks
dsparks / Test for Dimensionality.R
Created February 12, 2011 21:23
Bootstrap simulations from data, to compare to empirical PCA, "checkplots"?
require(ggplot2)
### Boot ###
DimensionTester <- function(object, niterations = 50, dimstotest = 0){
if(length(dimstotest) == 1){dimstotest <- 1:(ncol(object)*2)}
Parameters <- expand.grid(1:niterations, dimstotest)
DoPCA <- function(dims){
@dsparks
dsparks / Jaccard Similarity Coefficient.R
Created February 14, 2011 03:24
Jaccard similarity for person-to-group-like matrices
Jaccard <- function(matrix){
Intersection <- matrix %*% t(matrix)
Union <- ncol(matrix) - (!matrix) %*% t(!matrix)
Jaccard <- Intersection / Union
return(Jaccard)
}
@dsparks
dsparks / Nonparametric Dimensionality Test for PCA.R
Created February 14, 2011 13:39
Bootstrap simulations from data, to compare to empirical PCA, functions only.
require(ggplot2)
### Boot ###
DimensionTester <- function(object, niterations = 50, dimstotest = 0){
if(length(dimstotest) == 1){dimstotest <- 1:(ncol(object)*2)}
Parameters <- expand.grid(1:niterations, dimstotest)
DoPCA <- function(dims){
@dsparks
dsparks / Gist source reader.R
Created February 14, 2011 14:54
Uses RCurl to get past https, saves and loads code locally
Gister <- function(rawurl){
require(RCurl)
write.table(unlist(strsplit(getURL(rawurl, ssl.verifypeer = FALSE), "\n")),
"GistCode.txt", row.names = F, quote = F)
source("GistCode.txt")
}
@dsparks
dsparks / Parallel Computing.R
Created February 15, 2011 00:03
Example of parallel vectorized functions using snow
require(snow)
VariabletoExport <- 5
TheFunction <- function(x){
TimeWaster <- mean(rnorm(100000, 0, 1))
x^2 + TimeWaster + VariabletoExport
}
FunctionInput <- 1:1000
#@author Michael J Bommarito
#@contact michael.bommarito@gmail.com
#@date Feb 20, 2011
#@ip Simplified BSD, (C) 2011.
# This is a simple example of an R script that will retrieve
# public tweets from a given hashtag.
library(RJSONIO)
# This function loads stored tag data to determine the current max_id.
@dsparks
dsparks / Text Progress Bar.R
Last active September 25, 2015 01:17
Example of a text progress bar.
total <- 100
# create progress bar
pb <- txtProgressBar(min = 0, max = total, style = 3)
TheFunction <- function(i){
Sys.sleep(0.05)
setTxtProgressBar(pb, i)
}
lapply(1:total, TheFunction)