Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的For循环在每次迭代时替换对象值_R_Loops - Fatal编程技术网

R中的For循环在每次迭代时替换对象值

R中的For循环在每次迭代时替换对象值,r,loops,R,Loops,我正在努力弄清楚如何创建一个for循环,其中一些初始对象(u、l、h、和y)及其值在循环的每次迭代结束时更新和报告。循环将先前迭代的值作为基础(例如,在更新上述对象之后,runif函数在绘图aq中获取更新后的u和l值。我不断重复得到相同的结果,没有任何变化,我不确定解决这个问题的最佳方法是什么 提前道歉,因为我对R和编码一般来说都是新手 reset = { l = 0.1 #lower bound of belief in theta u = 0.9 #upper bound of beli

我正在努力弄清楚如何创建一个for循环,其中一些初始对象(
u
l
h
、和
y
)及其值在循环的每次迭代结束时更新和报告。循环将先前迭代的值作为基础(例如,在更新上述对象之后,
runif
函数在绘图a
q
中获取更新后的
u
l
值。我不断重复得到相同的结果,没有任何变化,我不确定解决这个问题的最佳方法是什么

提前道歉,因为我对R和编码一般来说都是新手

reset = {
l = 0.1 #lower bound of belief in theta  
u = 0.9 #upper bound of belief in theta
h = 0.2 #lower legal threshold, below which an action is not liable
y = 0.8 #upper legal threshold, above which an action is liable 
}
### need 1-u <= h <= y <= 1-l  for each t along every path of play

period = c(1:100)   ## Number of periods in the iteration of the loop.

for (t in 1:length(period)) {
q = runif(1,min = l, max = u)  ### 1 draw of q from a uniform distribution
q 
  probg = function(q,l,u){(u - (1-q))/(u-l)} ### probability of being found guilty given q in the ambiguous region
  probg(q,l,u)

  probi = function(q,l,u){1-probg(q,l,u)} ### probability of being found innocent given q in the ambiguous region
  probi(q,l,u)

    ruling = if(q>=y | probg(q,l,u) > 1){print("Guilty")              ###Strict liability

  } else if(q<=h | probi(q,l,u) > 1) {print("Innocent")               ###Permissible

  } else if(q>h & q<y) {                                              ###Ambiguous region


  discovery = sample(c('guilty','not guilty'), size=1, replace=TRUE, prob=c(probg(q,l,u),probi(q,l,u)))  ### court discovering whether a particular ambiguous q is permissible or not

  } 
  discovery
  ruling

if(ruling == "not guilty") {u = 1-q} else if (ruling == "guilty") {l = 1-q} else (print("beliefs unchanged"))      
if(ruling == "not guilty"){h = 1 - u} else if (ruling == "guilty") {y = 1 - l}  else (print("legal threshold unchanged"))              #### legal adjustment and updating of beliefs in ambiguous region after discovery of liability


probg(q,l,u)
probi(q,l,u)
modelparam = c(l,u,h,y)
show(modelparam)

}
reset={
l=0.1#θ的置信下限
u=0.9#θ的置信上限
h=0.2#较低的法律阈值,低于该阈值,诉讼不承担责任
y=0.8#法律上限,超过该上限,诉讼应承担责任
}

###需要1-u h&Q您的预期结果是什么?看起来问题在于您如何处理
裁决
,目前它从不需要“有罪”或“无罪”,因此您的脚本将不会像您希望的那样工作。如果您将“有罪”替换为“有罪”,将“无罪”替换为“无罪”它应该会起作用。我的预期结果是,边界
l
u
h
y
在每次迭代时都会更新并收敛,并且每次迭代时绘制的
q
在更新的边界之间具有一个值。谢谢@KevinCazelles,这似乎成功了!