Skip to content

Instantly share code, notes, and snippets.

@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 / tm_example.R
Created Dec 11, 2012
Denver debate analysis I
View tm_example.R
rm(list = ls())
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("zoo", "tm", "ggplot2", "Snowball")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# From: http://www.cnn.com/2012/10/03/politics/debate-transcript/index.html
Transcript <- readLines("https://raw.github.com/dsparks/Test_image/master/Denver_Debate_Transcript.txt")
head(Transcript, 20)
@dsparks
dsparks / colors.R
Last active Dec 23, 2020
Sunlight Foundation palettes, based on the Wes Anderson code
View colors.R
# Sunlight Foundation style guide: http://design.sunlightlabs.com/projects/Sunlight-StyleGuide-DataViz.pdf
# Ram's original Wes Anderson code: https://github.com/karthik/wesanderson/blob/master/R/colors.R
#' A Wes Anderson palette generator
#'
#' These are a handful of color palettes from Wes Anderson movies.
#' @param n Number of colors desired. Unfortunately most palettes now only have 4 or 5 colors. But hopefully we'll add more palettes soon. All color schemes are derived from the most excellent Tumblr blog: \href{http://wesandersonpalettes.tumblr.com/}{Wes Anderson Palettes}
#' @param name Name of desired palette. Choices are: \code{GrandBudapest}, \code{Moonrise1}, \code{Royal1}, \code{Moonrise2}, \code{Cavalcanti}, \code{Royal2}, \code{GrandBudapest2}, \code{Moonrise3}, \code{Chevalier}, \code{BottleRocket}, \code{darjeeling}, \code{darjeeling2}
#' @param type Set to continuous if you require a gradient of colors similar to how heat map works.
#' @export
@dsparks
dsparks / sna_example.R
Created Dec 11, 2012
Flag similarity network
View sna_example.R
# Drawing a scatter plot of raster images
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("png", "sna")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Go to https://www.gosquared.com/resources/2400-flags, download the ZIP,
# and put the 64 x 64 files into a directory of your choosing.
# Then setwd() to that directory:
setwd("C:/Dropbox/isDotR_Files/flagIcons")
@dsparks
dsparks / Hmisc_bezier.R
Created Dec 6, 2012
Economics-style graphs
View Hmisc_bezier.R
doInstall <- TRUE
toInstall <- c("Hmisc", "ggplot2", "proxy", "grid")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Example usage
x <- c(4,6,4,5,6,7)
y <- 1:6
plot(x, y, "o", pch=20) # bezier() generates smoothed curves from these points
points(bezier(x, y), type="l", col="red")
@dsparks
dsparks / Making k-folds.R
Created Sep 11, 2012
Random, equally-sized partitions
View Making k-folds.R
# Randomly allocating observations into groups, for, e.g. cross-validation
kk <- 10 # Number of partitions, as in "kk-fold cross-validation."
# Here is a data.frame full of good data:
nn <- 1003
myData <- data.frame(matrix(rnorm(nn * 3), ncol = 3))
colnames(myData) <- LETTERS[1:3]
# This does not work:
whichK <- sample(LETTERS[1:kk], nrow(myData), replace = T)
@dsparks
dsparks / geocoded_Tweets.R
Created Dec 18, 2012
Gathering Tweets, geocoding users, and plotting them
View geocoded_Tweets.R
doInstall <- TRUE
toInstall <- c("twitteR", "dismo", "maps", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
searchTerm <- "#rstats"
searchResults <- searchTwitter(searchTerm, n = 1000) # Gather Tweets
tweetFrame <- twListToDF(searchResults) # Convert to a nice dF
userInfo <- lookupUsers(tweetFrame$screenName) # Batch lookup of user info
@dsparks
dsparks / Avoiding a loop.R
Created Sep 12, 2012
lapply() as an alternative to a multiply-nested loop
View Avoiding a loop.R
# Alternative to a doubly-nested loop
# Imagine I want to perform an operation on a data frame
# once for each combination of two variables, such as Country and Year
# I can do this with a nested loop, or I can do this with (among other
# things) lapply()
# Generate random data:
allCountries <- LETTERS[1:10]
allYears <- 1990:2012
@dsparks
dsparks / ColorPalettes.R
Last active Apr 15, 2020
Color Palettes from COLOURlovers
View ColorPalettes.R
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 / 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 }))