R的新功能...这是着名的(可能?)概率模拟的公交车开始为空,并在每个站点接收0,1或2名乘客,概率分别为0.5,0.4和0.2。同样在每个站点,每位乘客下车的概率为0.2
我可以看到,在开始时它会跳过一部分代码作为变量'乘客'从0开始。但我不明白的是runif(1)代码 - 它在这里做了什么?另外一个问题 - 我如何跟踪向量,我想看看它如何存储信息,如表格(对R来说是新的,所以没有线索)。
(此代码模拟10次停止后空巴士的概率)
nreps <- 10000
nstops <- 10
count <- 0
for (i in 1:nreps){
passengers <- 0
for (j in 1:nstops){
if(passengers > 0)
for (k in 1:passengers)
if(runif(1) < 0.2)
passengers <- passengers - 1
newpass <- sample(0:2, 1, prob=c(0.5, 0.4, 0.1))
passengers <- passengers + newpass
}
if (passengers == 0) count <- count + 1
}
print(count/nreps)
答案 0 :(得分:1)
首先,这是每个步骤的解释:
nreps <- 10000
nstops <- 10
count <- 0
for (i in 1:nreps){ #for 10,000 iterations
passengers <- 0 #start with zero passengers
for (j in 1:nstops){ #for the number of stops (10) do...
if(passengers > 0) #for number of passengers, when number > 0...
for (k in 1:passengers) #for each passenger...
if(runif(1) < 0.2) #take 1 random val from a uniform dist, and if less than 0.2...
passengers <- passengers - 1 #subtract 1 from passengers (i.e. n-1)
newpass <- sample(0:2, 1, prob=c(0.5, 0.4, 0.1)) #take one random sample from 0,1,2, with probability weights 0.5, 0.4, 0.1, respectively
passengers <- passengers + newpass #add existing passengers to number of new passengers
}
if (passengers == 0) count <- count + 1 #if the number of passengers still equal zero by now, add 1 to the count
}
print(count/nreps) #number of times when passenger = 0 divided by number of iterations
现在,如果要跟踪每次迭代的进度,可以先设置空变量,然后根据每次迭代的索引填充这些变量,如下所示:
passengers_before <- c() #empty vector
passengers_after <- c() #empty vector
for (i in 1:nreps){ #for 10,000 iterations
passengers <- 0 #start with zero passengers
passengers_before[i] <- passengers
for (j in 1:nstops){ #for the number of stops (10) do...
if(passengers > 0) #for number of passengers, when number > 0...
for (k in 1:passengers) #for each passenger...
if(runif(1) < 0.2) #take 1 random val from a uniform dist, and if less than 0.2...
passengers <- passengers - 1 #subtract 1 from passengers (i.e. n-1)
newpass <- sample(0:2, 1, prob=c(0.5, 0.4, 0.1)) #take one random sample from 0,1,2, with probability weights 0.5, 0.4, 0.1, respectively
passengers <- passengers + newpass #add existing passengers to number of new passengers
}
passengers_after[i] <- passengers
if (passengers == 0) count <- count + 1 #if the number of passengers still equal zero by now, add 1 to the count
}
passenger_sample <- data.frame(passengers_before,passengers_after)
print(count/nreps)
现在,passenger_sample将是一个data.frame,其行数等于迭代次数(例如10,000),它将有两列:passengers_before和passengers_after。
这是第一批第一行:
head(passenger_sample)
passengers_before passengers_after
1 0 2
2 0 4
3 0 4
4 0 4
5 0 1
6 0 2