Skip to content

Instantly share code, notes, and snippets.

View gu-mi's full-sized avatar

Gu Mi gu-mi

  • Sanofi
  • Cambridge, MA
View GitHub Profile
@gu-mi
gu-mi / nested-loops.cpp
Last active December 11, 2015 06:09
Using nested loops to iterate elements in a 2-dim array of integers
#include <iostream>
using namespace std;
int main()
{
const int MAX_ROWS = 3;
const int MAX_COLS = 4;
// 2-D array of integers
int MyInts[MAX_ROWS][MAX_COLS] = { {34, -1, 879, 22},
@gu-mi
gu-mi / Spirograph.R
Created December 30, 2012 23:22
This R snippet is retrieved from http://menugget.blogspot.com/2012/12/spirograph-with-r.html on Dec. 30, 2012
#spirographR() will produce either a hypotrochoid or an epitrochoid.
#'A' is a circle of radius 'A.RADIUS'
#'B' is a circle of radius 'B.RADIUS' travelling around 'A'
#'C' is a point relative to the center of 'B' which rotates with the turning of 'B'.
#'BC' is the distance from the center of 'B' to 'C'
#'A.REV' is the number of revolutions that 'B' should travel around 'A'
#'N.PER.A.REV' is the number of radial increments to be calculated per revolution
#'A.CEN' is the position of the center of 'A'
#
@gu-mi
gu-mi / voronoi_raster.R
Created November 28, 2012 19:14 — forked from dsparks/voronoi_raster.R
Image Manipulation, Part 3
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",
@gu-mi
gu-mi / kmeans_palette.R
Created November 27, 2012 16:24 — forked from dsparks/kmeans_palette.R
Image Manipulation, Part 2
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",
@gu-mi
gu-mi / Pearson_Fisher.R
Created October 30, 2012 06:32
What's the power cost of using the exact test instead of the Chi-square for 2-by-2 table?
# http://www.r-bloggers.com/example-10-7-fisher-vs-pearson/
# Author: Ken Kleinman
# In R, we'll simulate observations from a multinomial distribution
# with the desired cell probabilities, and assemble the result into
# a table to calculate the p-values. This will make it easier to simulate
# tables under the alternative, as we need to do to assess power.
# If there are empty rows or columns, the chisq.test() function produces
# a p-value of "NaN", which will create problems later. To avoid this,
# we'll put the table generation inside a while() function. This operates
@gu-mi
gu-mi / heatmap3.R
Created October 26, 2012 04:46 — forked from nachocab/heatmap3.R
heatmap 3 revised
heatmap.3=function (x, Rowv = TRUE, Colv = if (symm) "Rowv" else TRUE,
distfun = dist, hclustfun = hclust, dendrogram = c("both",
"row", "column", "none"), symm = FALSE, scale = c("none",
"row", "column"), na.rm = TRUE, revC = identical(Colv,
"Rowv"), add.expr, breaks, symbreaks = max(x < 0, na.rm = TRUE) ||
scale != "none", col = "heat.colors", colsep, rowsep,
sepcolor = "white", sepwidth = c(0.05, 0.05), cellnote, notecex = 1,
notecol = "cyan", na.color = par("bg"), trace = c("column",
"row", "both", "none"), tracecol = "cyan", hline = median(breaks),
vline = median(breaks), linecol = tracecol, margins = c(5,
@gu-mi
gu-mi / seriation.R
Created October 25, 2012 22:56 — forked from dsparks/seriation.R
Optimal matrix seriation
# Simple ggplot2 heatmap, with optimal seriation
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "reshape2", "RColorBrewer", "seriation")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Using U.S. Judge Rating Data
myData <- as.matrix(USJudgeRatings)
@gu-mi
gu-mi / EmailClass.R
Created October 24, 2012 21:16 — forked from jbryer/EmailClass.R
Example of object oriented programming in R
#' Constructor
EmailClass <- function(name, email) {
nc = list(
name = name,
email = email,
get = function(x) nc[[x]],
set = function(x, value) nc[[x]] <<- value,
props = list(),
history = list(),
getHistory = function() return(nc$history),
@gu-mi
gu-mi / Making k-folds.R
Created October 1, 2012 17:29 — forked from dsparks/Making k-folds.R
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)
@gu-mi
gu-mi / Heatmap.R
Created September 27, 2012 16:25 — forked from dsparks/Heatmap.R
ggplot2 heatmap with "spectral" palette
# Simple ggplot2 heatmap
# with colorBrewer "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,