This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Parse a codebook file with variable and level information. | |
#' | |
#' Parses a codebook file where lines starting at column zero (far left) represet | |
#' variable information (e.g. name, description, type) and indented lines | |
#' (i.e. lines beginning with white space, either tabs or spaces, etc.) represent factor | |
#' levels and labels. | |
#' | |
#' Note that white space at the beginning and end of each line is stripped before | |
#' processing that line. | |
#' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#=============================================================================== | |
# 2019-07-19-- ikashnitsky.github.io | |
# Reproduce Figure 2 from http://doi.org/10.1007/s10708-018-9953-5 | |
# Ilya Kashnitsky, ilya.kashnitsky@gmail.com | |
#=============================================================================== | |
library(tidyverse) | |
library(hrbrthemes); import_roboto_condensed() | |
# the data as tribble |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/Rscript --vanilla | |
########################################################### | |
## ## | |
## elastic.R ## | |
## ## | |
## Author: Tony Fischetti ## | |
## tony.fischetti@gmail.com ## | |
## ## | |
########################################################### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
library(magick) | |
image_read("logo:") %>% | |
image_convert('png', colorspace = 'gray') %>% | |
image_edge(radius = 1) %>% | |
image_negate() %>% | |
image_transparent('white', 10000) %>% | |
image_background("white") %>% | |
image_browse() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# removes characters from multiple data frame columns | |
# example removes * , % characters | |
dw <- data.frame(gsub("\\*|,|%", "", as.matrix(dw)), stringsAsFactors = FALSE) |