Skip to content

Instantly share code, notes, and snippets.

dsparks

Block or report user

Report or block dsparks

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@dsparks
dsparks / lsos.R
Last active Jul 14, 2017
Improved list of objects, with size
View lsos.R
# improved list of objects
# From http://stackoverflow.com/a/4827843/479554
.ls.objects <- function (pos = 1, pattern, order.by,
decreasing=FALSE, head=FALSE, n=5) {
napply <- function(names, fn) sapply(names, function(x)
fn(get(x, pos = pos)))
names <- ls(pos = pos, pattern = pattern)
obj.class <- napply(names, function(x) as.character(class(x))[1])
obj.mode <- napply(names, mode)
@dsparks
dsparks / drawing_from_hypersphere.R
Created Jun 26, 2013
Random points on a hypersphere
View drawing_from_hypersphere.R
N <- 1000
nDims <- 2
randomNumbers <- rnorm(nDims * N, 0, 1)
randomNumbers <- matrix(randomNumbers, ncol = nDims)
plot(randomNumbers)
radius = sqrt(rowSums(randomNumbers ^ 2))
randomSphere <- 1/radius * randomNumbers
plot(randomSphere)
@dsparks
dsparks / whiteStrokeText.R
Created May 23, 2013
Hack for white strokes around text
View whiteStrokeText.R
doInstall <- TRUE
toInstall <- c("ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Make some random data:
randPoints <- data.frame(x = runif(1000), y = runif(1000))
randPoints$color <- hsv(runif(1000), runif(1000), runif(1000))
zp1 <- ggplot(randPoints,
View hashing_function.R
library(digest)
set.seed(1)
(x <- sample(1e9, size=6))
# [1] 265508664 372123900 572853364 908207790 201681932 898389685
## To hash R's internal representation of these numbers
strtoi(substr(sapply(x, digest), 28, 32), 16L) %% 1e3
# [1] 552 511 233 293 607 819
View find_peaks.r
#' Finds the local maxima (peaks) in the given vector after smoothing the data
#' with a kernel density estimator.
#'
#' First, we smooth the data using kernel density estimation (KDE) with the
#' \code{\link{density}} function. Then, we find all the local maxima such that
#' the density is concave (downward).
#'
#' Effectively, we find the local maxima with a discrete analogue to a second
#' derivative applied to the KDE. For details, see this StackOverflow post:
#' \url{http://bit.ly/Zbl7LV}.
@dsparks
dsparks / simplest_parallel.R
Created Mar 4, 2013
Another attempt at simplest possible parallel computation in Windows
View simplest_parallel.R
myFunction <- function(x){prod(1:x)}
myFunction(10)
system.time(lapply(1:10000, myFunction))
library(parallel)
myCluster <- makeCluster(detectCores())
clusterExport(myCluster, ls())
system.time(parSapply(myCluster, 1:10000, myFunction))
stopCluster(myCluster)
@dsparks
dsparks / latent_class_analysis.R
Created Dec 19, 2012
Latent class model of ANES respondents
View latent_class_analysis.R
doInstall <- TRUE
toInstall <- c("ggplot2", "poLCA", "reshape2")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
ANES <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/ANES.csv")
ANES <- ANES[ANES$year == 2008, -c(1, 11, 17)] # Limit to just 2008 respondents,
head(ANES) # remove some non-helpful variables
# Adjust so that 1 is the minimum value for each variable:
ANES <- data.frame(apply(ANES, 2, function(cc){ cc - min(cc, na.rm = T) + 1 }))
@dsparks
dsparks / district_compactness.R
Created Dec 19, 2012
Calculating and mapping district compactness
View district_compactness.R
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("maptools", "rgdal", "ggplot2", "spatstat", "RColorBrewer")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Taking an online compressed shapefile, and opening it in R
# From http://stackoverflow.com/a/3053883
temp <- tempfile() # 110th & 111th Congressional District Shapefiles
download.file("http://www.census.gov/geo/cob/bdy/cd/cd110shp/cd99_110_shp.zip",
temp) # See http://www.census.gov/geo/www/cob/cd110.html#shp
@dsparks
dsparks / coefficent_plot_walkthrough.R
Created Dec 18, 2012
A coefficient plot for multiple models
View coefficent_plot_walkthrough.R
doInstall <- TRUE
toInstall <- c("ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
ANES <- read.csv("http://www.oberlin.edu/faculty/cdesante/assets/downloads/ANES.csv")
ANES <- ANES[ANES$year == 2008, -c(1, 11, 17)] # Limit to just 2008 respondents,
head(ANES) # remove some non-helpful variables
# Fit several models with the same DV:
@dsparks
dsparks / tapered-intensity-curved_edges.R
Last active Aug 17, 2019
Beautiful tapered-intensity-curved edge network graph with ggplot2
View tapered-intensity-curved_edges.R
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("sna", "ggplot2", "Hmisc", "reshape2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Empty ggplot2 theme
new_theme_empty <- theme_bw()
new_theme_empty$line <- element_blank()
new_theme_empty$rect <- element_blank()
new_theme_empty$strip.text <- element_blank()
You can’t perform that action at this time.