即使在 R 中 n=1.000.000.000,每个实验的结果也不同,这是怎么回事?
Different result for each experiment even when n=1.000.000.000 in R, what's wrong?
每次我 运行 下面的代码,我都会得到不同的答案,主要是 1、3、4 或 5。但是,由于我将 n 设置为一毫,我觉得这很奇怪,所以我认为我的代码一定有问题。第一次用R,不知道是数学错误还是编码错误
我要模拟的是:
一个群40个人,其中一个是医生
第0
天,1人感染;然后 for i = 1, 2, ..., T
:
- 天
i
:所有剩余的健康人被感染的概率为0.15;
- 天
i + 1
:医生最多治愈5人(包括他自己);
以上停止直到第 T
天,届时不再有感染者。
我现在的代码(怀疑有误):
SampleT <- function(n)
{
for (k in 1:n)
{
T <- 0 # day 0
X <- 39 # amount of healthy people
Y <- 1 # amount of infected people
cured <- 0 # amount of people cured by doctor
while(cured < Y)
{
T <- T+1 # increase day by 1 in each loop
infected <- sum((runif(X)<0.15)) # amount of people infected on day T
X <- X-infected # remaining healthy people
Y <- 40-X # total amount of people who have been infected at least once so far
cured <- cured + 5
}
return(T)
}
}
n <- 1000000000
T <- SampleT(n)
mean(T)
您的 for 循环仅 运行 是第一次迭代。一旦 'return' 在函数内部被击中即结束 - 它 return 是结果并退出函数。如果您计划获得一百万个结果,则需要将这些结果存储在某个地方,然后 return 在循环 运行 之后得到该结果。下面举例说明:
f <- function(n){
for(i in 1:n){
print(i)
return(i)
}
# were you thinking it would return all 'n' values?
}
然后当我们运行它...
> f(30)
[1] 1
[1] 1
也尝试使用 apply
函数:
N <- 10000
SampleT <- sapply(1:N, function(n) {
T <- 0 # day 0
X <- 39 # amount of healthy people
Y <- 1 # amount of infected people
cured <- 0 # amount of people cured by doctor
while(cured < Y)
{
T <- T + 1 # increase day by 1 in each loop
infected <- sum((runif(X) < 0.15)) # amount of people infected on day T
print(infected)
X <- X - infected # remaining healthy people
Y <- Y + infected # total amount of people who have been infected at least once so far
cured <- cured + 5
}
return (T)
})
mean(SampleT)
每次我 运行 下面的代码,我都会得到不同的答案,主要是 1、3、4 或 5。但是,由于我将 n 设置为一毫,我觉得这很奇怪,所以我认为我的代码一定有问题。第一次用R,不知道是数学错误还是编码错误
我要模拟的是:
一个群40个人,其中一个是医生
第0
天,1人感染;然后 for i = 1, 2, ..., T
:
- 天
i
:所有剩余的健康人被感染的概率为0.15; - 天
i + 1
:医生最多治愈5人(包括他自己);
以上停止直到第 T
天,届时不再有感染者。
我现在的代码(怀疑有误):
SampleT <- function(n)
{
for (k in 1:n)
{
T <- 0 # day 0
X <- 39 # amount of healthy people
Y <- 1 # amount of infected people
cured <- 0 # amount of people cured by doctor
while(cured < Y)
{
T <- T+1 # increase day by 1 in each loop
infected <- sum((runif(X)<0.15)) # amount of people infected on day T
X <- X-infected # remaining healthy people
Y <- 40-X # total amount of people who have been infected at least once so far
cured <- cured + 5
}
return(T)
}
}
n <- 1000000000
T <- SampleT(n)
mean(T)
您的 for 循环仅 运行 是第一次迭代。一旦 'return' 在函数内部被击中即结束 - 它 return 是结果并退出函数。如果您计划获得一百万个结果,则需要将这些结果存储在某个地方,然后 return 在循环 运行 之后得到该结果。下面举例说明:
f <- function(n){
for(i in 1:n){
print(i)
return(i)
}
# were you thinking it would return all 'n' values?
}
然后当我们运行它...
> f(30)
[1] 1
[1] 1
也尝试使用 apply
函数:
N <- 10000
SampleT <- sapply(1:N, function(n) {
T <- 0 # day 0
X <- 39 # amount of healthy people
Y <- 1 # amount of infected people
cured <- 0 # amount of people cured by doctor
while(cured < Y)
{
T <- T + 1 # increase day by 1 in each loop
infected <- sum((runif(X) < 0.15)) # amount of people infected on day T
print(infected)
X <- X - infected # remaining healthy people
Y <- Y + infected # total amount of people who have been infected at least once so far
cured <- cured + 5
}
return (T)
})
mean(SampleT)