Матрица взвешенной частотной матрицы - PullRequest
13 голосов
/ 02 сентября 2011

Этот вопрос связан с двумя разными вопросами, которые я задавал ранее:

1) Воспроизвести частотный матричный график

2) Добавить 95% доверительные пределы к совокупному графику

Я хочу воспроизвести этот сюжет в 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)

Мой вопрос: Как я могу воспроизвести верхний график за один проход, не нанося несколько образцов?

Спасибо.

Ответы [ 3 ]

6 голосов
/ 04 сентября 2011

Вы можете произвести этот участок ...

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)
}
3 голосов
/ 04 сентября 2011

Вы также можете использовать метод Кошке, ограничивая комбинации значений значениями с s <6, и по запросу Андри добавили условие разности Ps $ n и ps $ s, чтобы получить «остроконечную» конфигурацию. </p>

 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 голосов
/ 06 мая 2014

Матрица взвешенной частоты также называется Матрица веса позиции (в биоинформатике).Он может быть представлен в виде последовательности логотипа .Это как минимум то, как я строю матрицу взвешенных частот.

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