Skip to content

Instantly share code, notes, and snippets.

View briatte's full-sized avatar

François Briatte briatte

View GitHub Profile
@jbryer
jbryer / parse.codebook.r
Last active March 9, 2023 15:19
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.
#' 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.
#'
@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 / 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()
#===============================================================================
# 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
@tonyfischetti
tonyfischetti / elastic-net-regression-evaluation.R
Last active April 27, 2021 13:24
Code for my blog post "Kickin' it with elastic net regression"
#!/usr/bin/Rscript --vanilla
###########################################################
## ##
## elastic.R ##
## ##
## Author: Tony Fischetti ##
## tony.fischetti@gmail.com ##
## ##
###########################################################
@jeroen
jeroen / canny.R
Last active April 1, 2021 14:20
Magick canny edge detector
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()
@dsparks
dsparks / colors.R
Last active December 23, 2020 22:12
Sunlight Foundation palettes, based on the Wes Anderson code
# 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 / Making k-folds.R
Created September 11, 2012 01:55
Random, equally-sized partitions
# 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)
@briatte
briatte / cleaning_characters.r
Last active August 12, 2020 18:13
a few lines of code to load packages, download and scrape data, convert, sort, etc.
# removes characters from multiple data frame columns
# example removes * , % characters
dw <- data.frame(gsub("\\*|,|%", "", as.matrix(dw)), stringsAsFactors = FALSE)