Skip to content

Instantly share code, notes, and snippets.

@dsparks
dsparks / Problem 42.R
Created March 1, 2011 01:39
Of triangle numbers and word values
rm(list = ls())
# from http://projecteuler.net/index.php?section=problems&id=42
Text <- readLines("http://projecteuler.net/project/words.txt")
TextString <- gsub("\"", "", unlist(strsplit(Text, "\",\"")))
TriangleNumbers <- cumsum(1:100)
StringLetters <- strsplit(TextString, "")
LetterRank <- rank(LETTERS)
@dsparks
dsparks / Matte Density Overlap.R
Created March 25, 2011 11:04
Artificially-colored overlapping densities.
require(ggplot2)
Data <- data.frame(Group = sample(c(1, 2), 100, replace = T))
Data$Value <- rnorm(100, Data$Group * 2 - 6, 1)
Data$Group <- LETTERS[1:2][Data$Group]
Plot1 <- qplot(Value, data = Data, geom = "density", fill = Group, alpha = I(1/2))
Plot1 <- Plot1 + scale_fill_manual(values = c("#3E6995", "#953E3E"))
print(Plot1)
@dsparks
dsparks / A Ratio-Polarization Measure.R
Created April 11, 2011 21:46
Pairwise distance CPG, polarization, or segregation measure
require(cluster)
Polarizer <- function(group, data){
if((mean(is.na(group)) > 0.99) | length(group) <= 1){
Output <- c(NA, NA)
} else {
SameGroup <- try(as.matrix(daisy(data.frame(group))) == 0)
diag(SameGroup) <- NA
@dsparks
dsparks / ColorPalettes.R
Last active April 15, 2020 19:49
Color Palettes from COLOURlovers
PaletteMaker <- function(pid = "GG"){
pal <- colorRampPalette(c(gray(0), gray(1)))(5)
if(pid == "GG") pal <- c("#69D2E7", "#A7DBD8", "#E0E4CC", "#F38630", "#FA6900")
if(pid == "TP") pal <- rev(c("#ECD078", "#D95B43", "#C02942", "#542437", "#53777A"))
if(pid == "cuek") pal <- c("#556270", "#4ECDC4", "#C7F464", "#FF6B6B", "#C44D58")
if(pid == "OF") pal <- c("#00A0B0", "#6A4A3C", "#CC333F", "#EB6841", "#EDC951")
if(pid == "SP") pal <- rev(c("#BCBDAC", "#CFBE27", "#F27435", "#F02475", "#3B2D38"))
if(pid == "VM") pal <- c("#8C2318", "#5E8C6A", "#88A65E", "#BFB35A", "#F2C45A")
if(pid == "MC") pal <- rev(c("#FAD089", "#FF9C5B", "#F5634A", "#ED303C", "#3B8183"))
if(pid == "yab") pal <- c("#351330", "#424254", "#64908A", "#E8CAA4", "#CC2A41")
@dsparks
dsparks / sdfBeta.R
Created June 21, 2011 17:17
Standardized DfBeta
# See: http://stats.stackexchange.com/questions/12148/looking-for-a-name-for-a-mean-influencing-statistic
# Somewhat personalized
sdfBeta <- function(numer, denom = 1){
if(identical(1, denom)){denom <- rep(1, length(numer))}
Bj <- sum(numer, na.rm = T) / sum(denom, na.rm = T)
Bjni <- (sum(numer, na.rm = T) - numer) / (sum(denom, na.rm = T) - denom)
StdError <- sd(Bjni, na.rm = T)
Value <- (Bj - Bjni) / StdError
names(Value) <- names(numer)
@dsparks
dsparks / Logit.R
Created September 13, 2011 12:56
Logit
# http://en.wikipedia.org/wiki/Logit#Definition
# Logit: From probability to normal
# Logistic (inverse logit): From normal to probability
Logit <- function(p){log(p / (1 - p))}
Logistic <- function(x){exp(x) / (1 + exp(x))}
@dsparks
dsparks / two-dimensional density plots.R
Created October 7, 2011 18:58
Neat two-dimensional density plots
library(ggplot2)
x.centers <- c(-2, 1, 3)
x.ses <- c(1, 2, 1/2)
y.centers <- c(1/2, -2, 2)
y.ses <- c(1/4, 3/2, 1)
NN <- 5000#0
Sample1 <- cbind(rnorm(NN, x.centers[1], x.ses[1]), rnorm(NN, y.centers[1], y.ses[1]))
Sample2 <- cbind(rnorm(NN, x.centers[2], x.ses[2]), rnorm(NN, y.centers[2], y.ses[2]))
@dsparks
dsparks / eddington.R
Created February 2, 2012 14:19
Eddington Functions
Eddfinder <- function(v){
v=floor(v)
possibles=1:max(v)
count=function(w){length(v[v>w])}
which.max(lapply(possibles,count)<=possibles)
}
Eddington <- function(value,name){
temp=lapply(split(value,name),eddfinder)
unlist(temp)
@dsparks
dsparks / Install and load a vector of package names.R
Created August 28, 2012 12:49
Install and load a vector of package names
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("foreign", "ggplot2", "reshape2", "plyr", "devtools")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
@dsparks
dsparks / Transformed color scale.R
Created August 30, 2012 15:59
Transformed color scale with nice guide in ggplot2
library(ggplot2)
library(scales)
# Generate data
myData <- data.frame(x = rnorm(1000),
y = rnorm(1000))
myData$z <- with(myData, x * y)
# Make an untransformed plot
badVersion <- ggplot(myData,