您正在查看 "R" 分类下的文章 2009-08-30 20:41 之前,corrplot包( 部分效果见此)只能通过Rforge下载:
install.packages("corrplot", repos="http://R-Forge.R-project.org")
目前小bug都找的差不多了,加上近来比较忙碌,故打算提交到CRAN(大约五天之内会到CRAN上露脸吧),需要此包的朋友们就不用发email给我原始数据让我代劳了
此包以后的更新方向主要是变量的重排序方法:
1. Robinsonian
2. Dimension reduction
3. Heuristics
4. Block modeling
5. TSP
现在已经实现了主成分排序和各种系统聚类排序,其他的还得边学边卖,慢慢更新。相关矩阵可视化竟然能扯出这么多数学、统计甚至图论的东西,之前从没想到过,真是好玩。
注1:最初是在R会议上看见bjt大哥用椭圆图来表示相关矩阵,那时觉得很新鲜、很好玩,记忆很深刻。后来随便想了一阵子,写了个 小函数来娱乐,却没想到滚雪球滚成了一个小package。
注2:曾经觉得自己折腾得太久了,很无聊,不过现在又觉得很好玩了,因为还有很多有趣的工作要做。
注3:corrplot包在Rforge上最近不太好用,等我忙完手头的事立即更新。 |
2009-05-29 11:02 发现第一届 R 中国会议上了 The R Journal第一期(Conference Review: The 1st Chinese R Conference,第69页),很有历史意义,谢兄辛苦了啊。
此外,还发现文中还介绍各个演讲者和对应的演讲课题,我的也放了进去(Statistical Computation :optimization in R by Taiyun Wei),这是我的名字第一次上正式学术期刊,呵呵。
不知第二届 R 中国会议何时何地举办,消息灵通的看客了不妨透漏一下  |
2009-05-14 0:27 下午拿 R 去忽悠师兄师姐们,主题是回归和分类在R中的实现,讲得乱七八糟,幸好许青松老师宽宏大量,师兄师姐们都很给面子。不过可能由于我耿耿于怀没有讲好,晚上吃饭时闹了个笑话:
晚上去吃蒸菜,愣是要份 回归肉(这两天回归树、回归机说得多了些,可怎么又冒出了个回归肉啊  ),搞得服务员摸不着头脑,整个半天才弄明白我要的是梅菜扣肉(不是回锅肉)。 |
2009-04-21 22:34 
新图一张,还是关于相关阵的。 |
2009-03-26 0:48 一直觉得圆圈图听来不雅,也没有“闪电图”那么响亮的名字,不料却受到了许多前辈们的关怀议论,叫好的不少,提出批评意见的也不少。从中,我发现了一个秘密:人们的视觉系统是有差异的!同一个图,在某些人眼里好,而在其他人眼里就不一定舒服了。所以,我把原先的函数升级了,添加了背景颜色和填充颜色两个参数,用户可以自行选择适当的参数,当然后果自负。
许多朋友反映负相关系数的白色圈子淹没在白色背景里了,不怎么显眼。受中国围棋的启发,把背景设置为鹅黄色,发现效果不错。这样圆圈图就有个扬眉吐气的名字了:围棋图。

更多关于相关阵的图片参见我的 Picasa 图片集,欢迎评价!
跑题一下:Google 万岁。Google的Serch、Gmail、Groups、Blog、Maps、Reader、Docs、Picasa、Gtalk都非常好用,并且都免费。太喜欢google了!
函数代码分享如下:
#' Represents Correlation circles
#'
#' @author Taiyun Wei
#' @param corr Correlation matrix to represent
#' @col vector the fill color of circles form 1 to -1
#' @bg background color of graph
#' @param cex numeric, for the variable names
#' @param axes logical, TRUE if axes are to be drawn
#' @param xlab label for the x axis
#' @param ylab label for the y axis
#' @param title title of the graph
#' @param ... extra parameters, currenlty ignored
circle.corr <- function(corr, col=c("black","white"), bg = "white", cex = 1,
axes = FALSE, xlab = "", ylab = "",
title = "correlation matrix circles", ...){
if (is.null(corr))
return(invisible())
if ((!is.matrix(corr)) || (round(min(corr, na.rm = TRUE),
6) < -1) || (round(max(corr, na.rm = TRUE), 6) > 1))
stop("Need a correlation matrix!")
n <- nrow(corr)
m <- ncol(corr)
## set up variable names
rname <- rownames(corr)
cname <- colnames(corr)
if (is.null(rname))
rname <- 1:n
if (is.null(cname))
cname <- 1:m
rname <- as.character(rname)
cname <- as.character(cname)
## calculate label-text width approximately
plot(c(0, m), c(0, n), type = "n")
xlabwidth <- max(strwidth(rname, cex = cex))
ylabwidth <- max(strwidth(cname, cex = cex))
## set up an empty plot with the appropriate dimensions
par(mar = c(0, 0, 2, 0), bg = "white")
plot(c(-xlabwidth + 0.5, m + 0.5), c(0, n + 1 + ylabwidth),
type = "n", axes = axes, xlab = "", ylab = "", asp = 1)
rect(0+0.5, 0+0.5, m+0.5, n+0.5, col=bg) ##background color
## add variable names and title
text(rep(-xlabwidth/2, n), n:1, rname, col = "red", cex = cex)
text(1:m, rep(n + 1 + ylabwidth/2, m), cname, srt = 90, col = "red",
cex = cex)
title(title)
## add grid
segments(rep(0.5, n + 1), 0.5 + 0:n, rep(m + 0.5, n + 1),
0.5 + 0:n, col = "gray")
segments(0.5 + 0:m, rep(0.5, m + 1), 0.5 + 0:m, rep(n + 0.5,
m), col = "gray")
## assign circles' fill color
nc <- length(col)
if(nc==1)
bg <- rep(col, n*m)
else{
ff <- seq(-1,1, length=nc+1)
bg2 = rep(0, n * m)
for (i in 1:(n * m)) {
bg2[i] <- rank(c(ff[2:nc], as.vector(corr)[i]),
ties.method = "random")[nc]
}
bg <- (col[nc:1])[bg2]
}
## plot n*m circles using vector language, suggested by Yihui Xie
## the area of circles denotes the absolute value of coefficient
symbols(rep(1:m, each = n), rep(n:1, m), add = TRUE, inches = F,
circles = as.vector(sqrt(abs(corr))/2), bg = as.vector(bg))
}
最后,抱怨一下百度,博客里面连HTML语言都不能用,真是落后!
|
2009-03-22 18:18 截一个游戏结束时的图如下:
目前该函数已被fun包收录:
install.packages("fun", repos="http://R-Forge.R-project.org")
代码分享如下:
#' Play sliding puzzles
#' @author Taiyun Wei
#' @param size vector, the size of puzzle
#' @param bg, the color of number-cases
#' @param z, the puzzle matrix, can be generated automaticly
playNums <- function(size = c(4,4), bg = "lightblue",z = NULL, ...) {
n <- size[1]
m <- size[2]
z.right <- matrix(1:(n*m), n, byrow = TRUE)
z.right[n,m]<- 0
##calculate inverse number
neg_seq.length <- function(x){
len <- 0
for(i in 1:(length(x) - 1)){
tmp <- x[(i+1):length(x)] - x[i]
len <- len + sum(tmp < 0)
}
}
len.right <- neg_seq.length(as.vector(z.right)) +n+m
if (is.null(z))
z <- matrix(sample(0:(n*m - 1)), n)
len.z <- neg_seq.length(as.vector(z)) + sum(which(z==0, arr.ind = TRUE))
####guarantee the game can be solved
while((len.right%%2)!=(len.z%%2)){
z <- matrix(sample(0:(n*m - 1)), n)
len.z <- neg_seq.length(as.vector(z)) + sum(which(z==0, arr.ind = TRUE))
}
z[!z]<-NA
##plot puzzles
replot <- function(z) {
bg <- ifelse(z, bg, "white")
fg <- ifelse(z, bg, "white")
par(mar = c(0, 0, 0, 0), bg = "white")
plot(c(0, m), c(0, n), type = "n",axes = FALSE, asp = 1, xlab = "",
ylab = "")
segments(0:m, rep(0, m + 1), 0:m, rep(n, m + 1), col = "grey",
lwd = 2)
segments(rep(0, n + 1), 0:n, rep(m, n + 1), 0:n, col = "grey",
lwd = 2)
symbols(0.5 + rep(0:(m - 1), each = n), 0.5 + rep((n -
1):0, m), squares = rep(0.9, n*m), add = TRUE, inches = FALSE,
fg = as.vector(fg), bg = as.vector(bg))
text(0.5 + rep(0:(m - 1), each = n), 0.5 + rep((n -
1):0, m), as.vector(z), cex = 3)
}
##push function
push <- function(x, begin, space) {
tmp <- x[space]
if (begin < space) {
x[(begin + 1):space] <- x[begin:(space - 1)]
x[begin] <- tmp
}
if (begin > space) {
x[(begin - 1):space] <- x[begin:(space + 1)]
x[begin] <- tmp
}
x
}
count <- 0
mousedown <- function(buttons, x, y) {
plx <- grconvertX(x, "ndc", "user")
ply <- grconvertY(y, "ndc", "user")
m.col <- ceiling(plx)
m.row <- n - floor(ply)
ind.NA <- which(is.na(z), arr.ind = TRUE)
if (!xor(m.row == ind.NA[1], m.col == ind.NA[2]))
cat("Warning: Cannot push any case!\n")
##row push
ind.NA <- which(is.na(z), arr.ind = TRUE)
if (ind.NA[1] == m.row & ind.NA[2] != m.col) {
z[m.row, ] <<- push(z[m.row, ], m.col, ind.NA[2])
cat("step = ", count <<- count + 1, "\n")
}
##col push
if (ind.NA[1] != m.row & ind.NA[2] == m.col) {
z[, m.col] <<- push(z[, m.col], m.row, ind.NA[1])
cat("step = ", count <<- count + 1, "\n")
}
replot(z)
flag <- z == z.right
if (all(flag[!is.na(flag)])){
"You win!"
}
}
replot(z)
getGraphicsEvent("Game begin!", onMouseDown = mousedown)
} |
2009-03-14 21:57 英语很差的我居然开了个英文博客,大家可要多多捧场啊:)
是google的免费博客 http://weitaiyun.blogspot.com/
此外,我新创的圆圈图受到了 Romain François的关注,要将其放在R graph gallery上去,在他的建议下,我在代码里面加了点注释。代码共享如下:
circle.cor = function(cor, axes = FALSE, xlab = "",
ylab = "", asp = 1, title = "Taiyun's cor-matrix circles",
...) {
n = nrow(cor)
par(mar = c(0, 0, 2, 0), bg = "white")
plot(c(0, n + 0.8), c(0, n + 0.8), axes = axes, xlab = "",
ylab = "", asp = 1, type = "n")
##add grid
segments(rep(0.5, n + 1), 0.5 + 0:n, rep(n + 0.5, n + 1),
0.5 + 0:n, col = "gray")
segments(0.5 + 0:n, rep(0.5, n + 1), 0.5 + 0:n, rep(n + 0.5,
n), col = "gray")
##define circles' background color.
##black for positive correlation coefficient and white for negative
bg = cor
bg[cor > 0] = "black"
bg[cor <= 0] = "white"
##plot n*n circles using vector language, suggested by Yihui Xie
##the area of circles denotes the absolute value of coefficient
symbols(rep(1:n, each = n), rep(n:1, n), add = TRUE, inches = F,
circles = as.vector(sqrt(abs(cor))/2), bg = as.vector(bg))
text(rep(0, n), 1:n, n:1, col = "red")
text(1:n, rep(n + 1), 1:n, col = "red")
title(title)
}
## an example
data(mtcars)
fit = lm(mpg ~ ., mtcars)
cor = summary(fit, correlation = TRUE)$correlation
circle.cor(cor)
|
2009-02-05 22:21
R有个sudoku包,不过有些粗糙,不能保证产生的数独有唯一解(看看源代码就知道它的生成机理,对一个已知的数独做变换而已),对于多解问题也只是仅仅得到一个结果(其实一般数独不允许多解)。下面是玩数独时一个问题的正确结果,但是软件包判错:
其实本题至少有两个正确结果,sudoku包的结果:
|
2008-09-23 20:42 计划写写R软件在数学建模方面的应用,初步选择最优化和统计两块。 |
| | |