violin plot

ggplot2 0.9.0版本将在3月1号发布,改动较大,最近bioc mailing list也有诸多讨论,因为它导致了某些包编译出错(估计是NAMESPACE引起的)。

按照hadley的说法是这是为了把他个人的项目变成一个社区项目。

大多数的改动对用户来说是invisible的,用roxygen2去注释函数,核心代码使用S3重写,分离出一些特性(scales包)等。

最明显的是现在画图速度变快了,不过我觉得最让人兴奋的是guide_legend()guide_colorbar()两个函数,可以修改legend.

画图的话,加了四个函数geom_map(), geom_raster(),geom_dotplot()geom_violin().

Continue reading

MC积分

Monte Carlo方法由冯·诺伊曼于二战时提出,1777年法国人布丰用此思路估计pi值被认为是Monte Carlo的起源,这个方法简单又快速,通过产生随机数,将数值计算问题变成随机变量的某些特征值(比如期望值)。

积分运算,和估计pi值一样,用hit and miss法估计。

hit_miss <- function(fun, lower, upper, n=500) {
    # Monte Carlo integration using the hit and miss method
    x <- runif(n, lower, upper)
    f.value <- sapply(seq(lower, upper, 0.001), fun)
    f.min <- min(f.value)
    f.max <- max(f.value)
    y <- runif(n, f.min, f.max)
    hits <- sum(y <= fun(x))
    area <- (upper-lower) * f.min + hits/n * (upper-lower) * (f.max-f.min)
    return(area)
}

hit and miss方法收敛太慢,效率并不高,通常所说的MC积分是指下面这个方法。

Continue reading

圆和外接正方形的面积比,是$ \frac{\pi r^2}{(2r)^2} = \frac{\pi}{4}$.

通过这一比值,可以使用蒙特-卡罗方法来估计Pi,这是Monte Carlo方法的最经典的一个例子。

getPI <- function(N) {
    x <- runif(N)
    y <- runif(N)
    hits <- sum(sqrt(x^2+y^2) < 1)
    pi <- 4*hits/N
    return(pi)
}

Continue reading

Author's picture

Guangchuang Yu

Bioinformatics Professor @ SMU

Bioinformatics Professor

Guangzhou