如何在R

时间:2018-01-06 21:47:46

标签: r probability

如何在R中的prob命令中使用sample参数,以便它也包含条件概率?例如,字母表中的字母:生成字母的概率' b'取决于之前生成的字母。

谢谢!

2 个答案:

答案 0 :(得分:1)

这个问题仍然有点难以回答,如果你指定一个条件概率的例子,它会有所帮助,参见here

但是如果我们假设我们有a,b和c,并且a和b很可能在彼此之后发生,我们可以继续here的答案,我们可以将函数修改为如下:

# n = number of elements
# sample_from = draw random numbers from this range
random_non_consecutive <- function(n=10,sample_from = seq(1,5))
{
  y= rep(NA, n)
  prev=-1 # change this if -1 is in your range, to e.g. max(sample_from)+1
  probs = rep(1,length(sample_from));names(probs)=sample_from
  for(i in seq(n)){

    # your conditional probability rules should go here.
    if(prev=="a")
    {
      probs = c('a'=0,'b'=0.9,'c'=0.1)
    }
    if(prev=="b")
    {
      probs = c('a'=0.9,'b'=0,'c'=0.1)
    }
    if(prev=="c")
    {
      probs = c('a'=0.9,'b'=0.1 ,'c'=0)
    }

    y[i]=sample(setdiff(sample_from,prev),1,prob = probs[names(probs) %in% setdiff(sample_from,prev)])
    prev = y[i]
  }
  return(y)
}

在这种情况下,a和b很可能在彼此之后发生。事实上:

random_non_consecutive(40,letters[1:3])
 [1] "c" "a" "b" "a" "b" "a" "b" "c" "b" "a" "b" "a" "b" "a" "b" "a" "b" "a" "b" "a" "b" "a" "b" "a" "c" "a" "b" "a" "b" "c" "a" "b" "a" "b" "a" "b" "a" "b" "a" "c"

希望这有帮助。

答案 1 :(得分:1)

如果你试图将一系列随机字母串在一个长字符向量中,当前字母以前一个为条件,那么你只能通过sample调用prob来完成它,因为library(tibble) library(dplyr) library(magrittr) letters <- c('a', 'b', 'c') marg <- c(11.29, 4.68, 4.39) cond <- tribble( ~letters, ~a, ~b, ~c, 'a', 0.02, 2.05, 3.85, 'b', 8.47, 0.90, 0.06, 'c', 13.19, 0.02, 1.76) n <- 1000 s <- rep(NULL, n) s[1] <- sample(x = letters, size = 1, prob = marg) for (i in (2:n)) { p <- cond %>% filter(letters == s[i-1]) %>% select(-letters) s[i] <- sample(x = letters, size = 1, prob = p) } head(s) # [1] "a" "c" "c" "a" "c" "a" 是将使用相同的权重向量对权重向量和每个绘制进行采样。然而,可以这样做:

{{1}}

这不是解决问题的有效方法,因为创建1000个字符串char矢量需要几秒钟。