English 中文(简体)
Simulate Random 人民步入会议室
原标题:Simulate Random People Walking into a Room

这是我所思熟虑并试图指出的问题。

  • 共有100人:个人1、个人2、......人100。

  • 选择随机人员进入房间。

  • 另一步步步步入会议室的人根据最后走进会议室的人,任意选定其余人群。

  • 更接近刚刚步行的人的人的个人更有可能被选中。 更远的个人的概率较低(概率分配不一定是不对称)

  • 一旦一个人走进房间,他们就无法离开。

例如,如果17人步入房间,那么18人和16人就更愿意选择下班,而99人则选择下班的可能性较小。 这一可能性使你越走越了最后的人步入会议室。

这里是我写到的一个职能(https://math.stack Exchange.com/questions/4900283/randomly-ordering-people-waling-into-a-room):

calculate_probability <- function(S, L, x) {
      S <- S[S != L]
      

  if (!(x %in% S)) {
    return(0)
  }
 
  numerator <- 1 / (abs(x - L) + 1)

  denominator <- sum(1 / (abs(S - L) + 1))
  
  probability <- numerator / denominator
  
  return(probability)
}

# sanity check

S <- c(1:10)
L <- 8
x <- 8
probability <- calculate_probability(S, L, x)
print(probability) 

# probabilities sum to 1


S <- c(1:10)
L <- 8

S <- S[S != L]

probabilities <- sapply(S, function(x) calculate_probability(S, L, x))

print(probabilities)
sum(probabilities)

我现在试图做以下工作:

  • Imagine there are 100 people. A randomly selected person walks into the room.
  • I want to use this function to simulate the other 99 people walking into the room
  • I want to plot (i.e. ggplot2) the results (i.e. x-axis is the index and the y-axis is the number corresponding to each person)

最终结果应当包含按进入房间顺序排列的人员名单,如个人1、个人3、个人5、个人8、个人2等。

谁能帮助我这样做?

问题回答

这里试图利用一种排外程序模拟任意命令:

library(ggplot2)


calculate_probability <- function(S, L, x) {
    S <- S[S != L]
    
    if (!(x %in% S)) {
        return(0)
    }
    
    numerator <- 1 / (abs(x - L) + 1)
    denominator <- sum(1 / (abs(S - L) + 1))
    probability <- numerator / denominator
    
    return(probability)
}


simulate <- function(n) {
    S <- c(1:n)
    L <- sample(S, 1)
    result <- c(L)
    
    for (i in 2:n) {
        probabilities <- sapply(S, function(x) calculate_probability(S, L, x))
        L <- sample(S, 1, prob = probabilities)
        result <- c(result, L)
        S <- S[S != L]
    }
    
    return(result)
}

result <- simulate(100)


df <- data.frame(index = 1:100, person = result)
ggplot(df, aes(x = index, y = person)) +
    geom_line() +
    labs(x = "Index", y = "Person") +
    theme_minimal()

https://i.stack.imgur.com/R7PNg.png” rel=“nofollow noreferer”>。

然而,这里似乎有重复之处,这表明任意命令是不正确的:

 result[duplicated(result) | duplicated(result, fromLast = TRUE)]
[1] 25 25

最后一个人将具有确定性,因此,你可以到<代码>(n - 1),而不是n/code>。 另外,S <-S[S !=L]应当先在你计算概率之前,因为你已经选择了排外的第一人,而你不想在你为第二人进行的抽样框架中包含这一gu。 这可能说明为什么要重复。

simulate <- function(n) {
  S <- 1:n
  L <- sample(S, 1)
  result <- L
  
  for (i in 2:(n-1)) {
    # Remove the previous person from the sampling frame
    S <- S[S != L]
    probabilities <- sapply(S, function(x) calculate_probability(S, L, x))
    L <- sample(S, 1, prob = probabilities)
    result <- c(result, L)
  }
  
  # The last guy must be selected with certainty
  result <- c(result, setdiff(1:n, result))
  
  return(result)
}

result <- simulate(100)

any(duplicated(result))
[1] FALSE




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...