Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/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 运行函数100次_R_Function_Loops - Fatal编程技术网

R 运行函数100次

R 运行函数100次,r,function,loops,R,Function,Loops,a是我的数据帧。 如何通过更新pi\u hat,theta\u hat,lambda\u hat的值来运行ziu hat函数100次?每次显示pi_hat,theta_hat,lambda_hat Zi <- function(x){ x <- zi_hat=(pi_hat*theta_hat*exp(-theta_hat*a))/(pi_hat*theta_hat*exp(-theta_hat*a)+(1-pi_hat)*lambda_hat*exp(-lambda_hat*a

a
是我的数据帧。 如何通过更新
pi\u hat
theta\u hat
lambda\u hat
的值来运行
ziu hat
函数100次?每次显示
pi_hat
theta_hat
lambda_hat

Zi <- function(x){
  x <- zi_hat=(pi_hat*theta_hat*exp(-theta_hat*a))/(pi_hat*theta_hat*exp(-theta_hat*a)+(1-pi_hat)*lambda_hat*exp(-lambda_hat*a))


  pi_hat=(1/n)*sum(zi_hat)
  theta_hat=sum(zi_hat)/(sum(zi_hat*a))
  lambda_hat=(n*sum(zi_hat))/(n*sum(a)-sum(zi_hat)*sum(a))

  c(pi_hat,theta_hat,lambda_hat)  #print out the updated data#
  if (?>100) break
}
Zi你可以试试这个

Zi <- function(x){
counter = 1
while(counter <= 100)
{
  x <- zi_hat=(pi_hat*theta_hat*exp(-theta_hat*a))/(pi_hat*theta_hat*exp(-theta_hat*a)+(1-pi_hat)*lambda_hat*exp(-lambda_hat*a))
  pi_hat=(1/n)*sum(zi_hat)
  theta_hat=sum(zi_hat)/(sum(zi_hat*a))
  lambda_hat=(n*sum(zi_hat))/(n*sum(a)-sum(zi_hat)*sum(a))
  c(pi_hat,theta_hat,lambda_hat)  #print out the updated data#

  counter = counter + 1
 }
}

Zi谢谢@cccmir。很好的例子。
它必须分别返回zi、pi_hat、lambda_hat和theta_hat

Zi <- function(pi_hat,theta_hat,lambda_hat){
  counter = 1
  while(counter <= 10000)
  {
    zi_hat<-(pi_hat*theta_hat*exp(-theta_hat*a))/(pi_hat*theta_hat*exp(-theta_hat*a)+(1-pi_hat)*lambda_hat*exp(-lambda_hat*a))
    pi_hat=(1/n)*sum(zi_hat)
    theta_hat=sum(zi_hat)/(sum(zi_hat*a))
    lambda_hat=(n*sum(zi_hat))/
    (n*sum(a)-sum(zi_hat)*sum(a))
    counter = counter + 1
  }
  return(lambda_hat)
}

Zi查看
中的
循环或
while