Skip to content

Instantly share code, notes, and snippets.

@dsparks
dsparks / coefficent_plot_walkthrough.R
Created December 18, 2012 22:31
A coefficient plot for multiple models
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 / image_manipulation_1.R
Created October 30, 2012 12:25
Image Manipulation, Part 1
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ReadImages", "reshape", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Image URL:
allImageURLs <- c("http://media.charlesleifer.com/blog/photos/thumbnails/akira_940x700.jpg",
"http://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/402px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg",
"http://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Official_portrait_of_Barack_Obama.jpg/441px-Official_portrait_of_Barack_Obama.jpg",
"http://cache.boston.com/universal/site_graphics/blogs/bigpicture/obama_11_05/obama22_16604051.jpg",
@dsparks
dsparks / K-means convex hulls.R
Created September 23, 2012 17:14
From holey polygons to convex hulls
# Highlighting clusters, with chull()
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "RColorBrewer")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate some data
nn <- 500
myData <- data.frame(X = rnorm(nn),
@dsparks
dsparks / Heatmap.R
Last active August 19, 2022 06:54
ggplot2 heatmap with "spectral" palette
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "reshape2", "RColorBrewer")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate a random matrix
# This can be any type of numeric matrix,
# though we often see heatmaps of square correlation matrices.
nRow <- 9
nCol <- 16
@dsparks
dsparks / latent_class_analysis.R
Created December 19, 2012 16:24
Latent class model of ANES respondents
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 / SmoothCoefficientPlot.R
Last active April 5, 2022 19:32
ggplot2 regression coefficient plot, intervals smoothed out to transparent
SmoothCoefficientPlot <- function(models, modelnames = "", removeintercept = FALSE){
# models must be a list()
Alphas <- seq(1, 99, 2) / 100
Multiplier <- qnorm(1 - Alphas / 2)
zzTransparency <<- 1/(length(Multiplier)/4)
CoefficientTables <- lapply(models, function(x){summary(x)$coef})
TableRows <- unlist(lapply(CoefficientTables, nrow))
@dsparks
dsparks / alphahull.R
Created December 4, 2012 02:53
Alpha Hull Example
doInstall <- TRUE
toInstall <- c("alphahull")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate some sample data:
myData <- data.frame(x = rnorm(500), y = rnorm(500))
# With a unit-circle hole in the center:
myData <- myData[sqrt(rowSums(myData^2)) > 1, ]
plot(myData)
@dsparks
dsparks / tapered-intensity-curved_edges.R
Last active December 8, 2021 12:23
Beautiful tapered-intensity-curved edge network graph with ggplot2
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()
@dsparks
dsparks / dput example.R
Created September 10, 2012 03:21
Illustrating the use of dput()
# Let's say you have an R object that you'd like to share with someone else
# but, for whatever reason, it is necessary to share it in text form.
# Generate a random data.frame
set.seed(1337)
NN <- 10
theData <- data.frame(Alpha = rnorm(NN),
Beta = rnorm(NN))
theData$Gamma <- theData$Alpha * 2 + theData$Beta / 2 + rnorm(NN)
print(theData)
@dsparks
dsparks / Hmisc_bezier.R
Created December 6, 2012 23:32
Economics-style graphs
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")