2011-09-02 56 views
13

这个问题是涉及到两个不同的问题我以前问:情节加权频率矩阵

1)Reproduce frequency matrix plot

2)Add 95% confidence limits to cumulative plot

我想重现这一情节R:boringmatrix

我有这么多,使用下面的图形代码:multiplot

#Set the number of bets and number of trials and % lines 
numbet <- 36 
numtri <- 1000 
#Fill a matrix where the rows are the cumulative bets and the columns are the trials 
xcum <- matrix(NA, nrow=numbet, ncol=numtri) 
for (i in 1:numtri) { 
x <- sample(c(0,1), numbet, prob=c(5/6,1/6), replace = TRUE) 
xcum[,i] <- cumsum(x)/(1:numbet) 
} 
#Plot the trials as transparent lines so you can see the build up 
matplot(xcum, type="l", xlab="Number of Trials", ylab="Relative Frequency", main="", col=rgb(0.01, 0.01, 0.01, 0.02), las=1) 

我的问题是:如何在一次通过中重现顶部绘图,而不绘制多个样本?

谢谢。

+0

尽管事实上您已经考虑了更多路径确定性图形,但我认为您的透明度加权图更能说明此问题的统计性质。我想这可以通过:'lines(6:36,6 /(6:36),lty = 3)'来显示极端的可能性。) –

+0

@DWin有趣的是,我现在正在努力创造我的脑袋某种密度热图(或hexbin),所以它更像透明加权版本。如果你有一个好主意如何创建它,我可以问一个新的问题?我在想[像这样](http://www.actualanalytics.com/density-plot-heatmap-using-r-a58)。 –

+0

那个链接目前不适合我,但我从你的问题中学到了很多东西,所以我鼓励你提出更多问题。 –

回答

6

可以产生这样的情节......

enter image description here

...通过使用此代码:

boring <- function(x, occ) occ/x 

boring_seq <- function(occ, length.out){ 
    x <- seq(occ, length.out=length.out) 
    data.frame(x = x, y = boring(x, occ)) 
} 

numbet <- 31 
odds <- 6 
plot(1, 0, type="n", 
    xlim=c(1, numbet + odds), ylim=c(0, 1), 
    yaxp=c(0,1,2), 
    main="Frequency matrix", 
    xlab="Successive occasions", 
    ylab="Relative frequency" 
    ) 

axis(2, at=c(0, 0.5, 1))  

for(i in 1:odds){ 
    xy <- boring_seq(i, numbet+1) 
    lines(xy$x, xy$y, type="o", cex=0.5) 
} 

for(i in 1:numbet){ 
    xy <- boring_seq(i, odds+1) 
    lines(xy$x, 1-xy$y, type="o", cex=0.5) 
} 
+1

这真的有帮助。几天来,我一直在撞墙,并在最后期限迫近。我现在可以继续做一些事情。 :) –

3

您还可以使用Koshke的方法,通过值的组合限制在Andrie的请求中添加了Ps $ n和ps $ s之差的条件以获得“尖锐”配置。

ps <- ldply(0:35, function(i)data.frame(s=0:i, n=i)) 
plot.new() 
plot.window(c(0,36), c(0,1)) 
apply(ps[ps$s<6 & ps$n - ps$s < 30, ], 1, function(x){ 
    s<-x[1]; n<-x[2]; 
    lines(c(n, n+1, n, n+1), c(s/n, s/(n+1), s/n, (s+1)/(n+1)), type="o")}) 
axis(1) 
axis(2) 
lines(6:36, 6/(6:36), type="o") 
# need to fill in the unconnected points on the upper frontier 

Resulting plot (version 2)

+0

非常有趣。谢谢。 –

+0

除了与原始问题相同,试验次数不限于31次。 (比较右侧边缘图形的形状。) – Andrie

+0

哦。好的。将添加逻辑条件来完成。 –

0

加权频率矩阵也被称为位置权重矩阵(生物信息学)。 它可以以sequence logo的形式表示。 这至少是我如何绘制加权频率矩阵。

library(cosmo) 
data(motifPWM); attributes(motifPWM) # Loads a sample position weight matrix (PWM) containing 8 positions. 
plot(motifPWM) # Plots the PWM as sequence logo.