【问题标题】:Plot weighted frequency matrix绘制加权频率矩阵
【发布时间】:2011-11-08 23:22:35
【问题描述】:

这个问题与我之前提出的两个不同的问题有关:

1) Reproduce frequency matrix plot

2) Add 95% confidence limits to cumulative plot

我希望在 R: 中重现这个情节

我已经走到这一步了,使用图形下方的代码:

#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)

我的问题是:如何在不绘制多个样本的情况下一次性重现顶部图?

谢谢。

【问题讨论】:

  • 尽管您心中有一个更具路径确定性的图形,但我认为您的透明度加权图更能说明这个问题的统计性质。我想它可能已经被概述:lines(6:36, 6/(6:36), lty=3) 以显示极端的可能性。)
  • @DWin 有趣的是,我现在正在努力创建某种密度热图(或十六进制),所以它更像是透明加权版本。如果你有一个好主意如何创建它,我可以问一个新问题?我在想this之类的东西。
  • 该链接目前对我不起作用,但我从您的问题中学到了很多东西,所以我鼓励您提出更多问题。
  • @DWin 这让我很头疼。这是我的新question 的链接。

标签: r matrix probability frequency weighted


【解决方案1】:

加权频率矩阵也称为位置权重矩阵(在生物信息学中)。 它可以以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. 

【讨论】:

    【解决方案2】:

    您还可以使用 Koshke 的方法,将值的组合限制为 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
    

    【讨论】:

    • 除了试验次数不限于 31 次,如原始问题所示。 (比较右侧边缘的图形形状。)
    • 哦。好的。将添加逻辑条件来完成。
    • 安德烈:感谢您的投票。回了人情。当我第一次解决这个问题时,我确实尝试过使用你的 boring 函数,但我承认我不理解它以及 koshke 使用的 plyr 方法。我不太明白 4 元组如何与 lines 一起工作,但我可以更好地看到 @koshke 的“ps”对象的结构。
    • @Dwin 同意。我对无聊功能的第一次尝试(在上一个问题中)相当混乱并且不容易扩展。我不得不从头开始重新设计它来制作这个新情节。我认为它的新形式更容易理解。
    【解决方案3】:

    你可以制作这个情节...

    ...通过使用此代码:

    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)
    }
    

    【讨论】:

    • 这真的很有帮助。几天来,我一直在用头撞砖墙,最后期限迫在眉睫。我现在可以继续做一些事情了。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 2020-03-20
    相关资源
    最近更新 更多