我正在尝试使用while循环设置一个进程,以便在某个特定的xd[i]变得等于x之前,让我的代码在特定的xd[i]中一致地采样。
我知道将所有内容放在一个for循环下会更有效(除了while循环),但我正在尝试一步一步地创建它。现在,我被while循环部分卡住了。我不能在不崩溃的情况下运行这部分代码,或者如果它没有崩溃,它似乎会继续不间断地采样,直到我手动停止它。如何更改while循环,使其在xd向量上采样,直到xd的一个元素与x匹配?
谢谢
reset1 = {
a = 0.3 #lower legal threshold
b = 0.9 #upper legal threshold
x = 0
theta = runif(1,min = a, max = b)
theta
A = 5 ## monetary value of harm from
maxw = 2*A
minw = 0
wbar = (maxw+minw)/2 ##average cost
wbar
xd = c(1,2,3)
w = c(1,2,3)
}for (i in 1:length(xd)){w[i] = runif(1, min = 0, max = 2)} #trying to make it create a w for each person
##Drivers problem: pick the x that will minimize your cost
for(i in 1:length(xd)){xd[i] = min(c(1-(w[i]/(2*A)),((2+b)-sqrt(b^2-2*b+1+3*(w[i]/A)*(b-a)))/3,b))}
xd
for(i in 1:length(xd)){proba = function(xd){(xd-1)^2}}
proba(xd) #ith individual probability of getting in an accident given their xd[i]
proba(xd[c(1:3)])
probn = 1 - proba(xd) #probability of not getting in an accident given driveri's effort level
probn
while (any(x!=xd)) {x = sample(c(xd[c(1,2,3)],0,0,0),size = 1, replace = TRUE, prob = c(proba(xd), probn)) ###the x is selected based on which ever x resulted in an accident
}
show(x) 发布于 2020-03-03 08:52:23
也许吧
while(sum(xd!=x)==3){}只要xd的任何元素都不等于x,这个循环就会运行
https://stackoverflow.com/questions/60498411
复制相似问题