Skip to content

Instantly share code, notes, and snippets.

View even4void's full-sized avatar

chl even4void

View GitHub Profile
@even4void
even4void / sweave_ggplot.Rnw
Created March 22, 2011 23:12
How to include ggplot graphics within Sweave
% compile with
% R CMD Sweave sweave_ggplot.Rnw
% pdflatex sweave_ggplot.tex
\documentclass[t,ucs,12pt,xcolor=dvipsnames]{beamer}
\usepackage{fancyvrb}
\title{A sample Sweave demo}
\author{Author name}
\date{}
library(mixOmics)
library(RColorbrewer)
X <- replicate(6, rnorm(50))
X.row.mds <- cmdscale(dist(X), k=1)
X.col.mds <- cmdscale(dist(t(X)), k=1)
cim(cor(X)[order(X.col.mds),order(X.col.mds)], col=rev(brewer.pal(8, "RdBu")))
cim(cor(t(X))[order(X.row.mds),order(X.row.mds)], col=rev(brewer.pal(8, "RdBu")))
cim(X, col=rev(brewer.pal(8, "RdBu"))
@even4void
even4void / table2plot.R
Created March 13, 2011 19:59
A fake example for mixing text and graphic
display.cell <- function(x, bgcol="#DAE6F2", ...) {
opar <- par(bg=bgcol, mar=rep(0,4))
plot(c(0,1), c(0,1), type="n", axes=FALSE, xlab="", ylab="")
text(.5, .5, as.character(x), ...)
lines(c(-0.1,1.1), c(0,0))
par(opar)
}
format.digits <- function(x) as.character(paste("$", as.character(x), sep=" "))
@even4void
even4void / CVJC.bib
Created March 10, 2011 19:38
Articles proposed for CrossValidated Journal Club
@article{arlot2010survey,
Author = {Arlot, S. and Celisse, A.},
Journal = {Statistics Surveys},
Pages = {40--79},
Title = {A survey of cross-validation procedures for model selection},
Volume = {4},
Year = {2010}}
@article{feinerer2008text,
Author = {Feinerer, I. and Hornik, K. and Meyer, D.},
@even4void
even4void / levelplot_annot.R
Created February 16, 2011 21:47
Lattice annotation
x <- seq(0,10)
y <- seq(0,10)
df <- expand.grid(x=x, y=y)
df$z <- rnorm(nrow(df), mean=5)
library(lattice)
levelplot(z ~ x+y, data=df,
panel=function(x, y, ...) {
panel.levelplot(x, y, ...)
panel.abline(v=1)
@even4void
even4void / venn_sql.r
Created January 7, 2011 11:58
Toy illustration of SQL joins with Venn diagrams in R
# Time-stamp: <2011-01-07 12:41:47 chl>
#
# Some illustrations of SQL joins with R.
# Inspiration: Jeff Atwood, http://bit.ly/eMhJEp
#
tableA <- data.frame(id=1:4, name=c("Pirate","Monkey","Ninja","Spaghetti"))
tableB <- data.frame(id=1:4, name=c("Rutabaga","Pirate","Darth Vader","Ninja"))
@even4void
even4void / 23_designAB.mp
Created December 10, 2010 13:51
Illustration of two factors interaction
prologues := 2;
input geometriesyr16;
figure(-u,-u,6.5u,6u);
pair A,B,C,D,E,F,G,H,I,J,K,L,M,N;
picture depart;
numeric i,j,r,v;
r:=.5;
# Time-stamp: <2010-12-10 14:28:19 chl>
#
# A toy example to illustrate SS idea in the Linear Model.
#
do.it <- function(x, g) {
stopifnot(is.factor(g))
stripchart(x ~ g, method="jitter", pch=19, col="darkgrey",
vertical=TRUE, ylab="")
# Some examples of graphical displays for Likert items
#
# Time-stamp: <2010-10-24 20:04:59 chl>
data(Environment, package="ltm")
Environment[sample(1:nrow(Environment), 10),1] <- NA
na.count <- apply(Environment, 2, function(x) sum(is.na(x)))
tab <- apply(Environment, 2, table)/apply(apply(Environment, 2, table), 2, sum)*100
# Cleveland dot plot
dotchart(tab, xlim=c(0,100), xlab="Frequency (%)", sub=paste("N", nrow(Environment), sep="="), pch=19)
itan <- function(x, keys=NULL, digits=3, no.resp=4) {
# x is an n subjects by k items matrix
# keys is a vector with the correct keys (A, B, C, D)
stopifnot(ncol(x)>1)
if (is.null(keys)) keys <- rep("A", ncol(x))
require(ltm)
raw.resp <- matrix(nr=ncol(x), nc=no.resp)
colnames(raw.resp) <- LETTERS[1:no.resp]
for (i in 1:ncol(x)) {
tmp <- table(x[,i])