如何在R中加速以下函数?

如何在R中加速以下函数?,r,loops,for-loop,R,Loops,For Loop,我用R编写了以下函数。我想迭代它,比如50000次。我在函数中使用了“sapply”,但在R中运行缓慢。我的电脑现在仍在工作约20小时,我不知道运行时间。有没有办法加快这项行动?谢谢 data=matrix(c(0.01132162,1,0.04056053,1,0.11676735,0,0.12029087,1, 0.16197702,1,0.17190980,1,0.20386841,1,0.21251687,0, 0.36536492,0,0.4

我用R编写了以下函数。我想迭代它,比如50000次。我在函数中使用了“sapply”,但在R中运行缓慢。我的电脑现在仍在工作约20小时,我不知道运行时间。有没有办法加快这项行动?谢谢

data=matrix(c(0.01132162,1,0.04056053,1,0.11676735,0,0.12029087,1,
           0.16197702,1,0.17190980,1,0.20386841,1,0.21251687,0,
           0.36536492,0,0.40256414,1),ncol=2,byrow=T)

 GIBS=function(data,a,b,beta,R)
    {
       m=length(R)
       n1=sum(data[,2]==1)
       n2=m-n1
       n=m+sum(R)
       N=c(n1,n2)
       R1=c(0,R)
       nstar=c()
       for(i in 1:m) nstar[i]=n-(i-1)-sum(R1[1:i])
       Z=c(data[1,1],data[2:m,1]-data[1:(m-1),1])
       f=function(x)
        {
          A=0
          for(i in 1:m) A=A+x^i*nstar[i]*Z[i]
          FR=1
          for(j in 1:2) FR=FR*(A+b[j])^(N[j]+a[j])
          return(x^(m*(m+1)/2)*exp(-beta*(x-1))/FR)
        }
      INT=integrate(f,1,Inf)$value
      SG=function(it)
       {
        uu=runif(1)
        g0=function(t) integrate(f,1,t)$value/INT-uu
        aa=5
        if(g0(1)>0) {while(g0(aa)>0) aa=aa+1} else {while(g0(aa)<0) aa=aa+1}
        ra=uniroot(g0,c(1,aa))$root
        A1=sum(ra^(1:m)*nstar*Z)
        rl1=rgamma(1,n1+a[1],A1+b[1])
        rl2=rgamma(1,n2+a[2],A1+b[2])
        return(c(ra,rl1,rl2))
      }
     return(colMeans(t(sapply(1:10000,SG,simplify = "array"))))
  }
########
BGI=matrix(NA,ncol=3,nrow=50000)
for(iter in 1:50000)
  {
    BGI[iter,]=GIBS(data,c(2,1.6),c(2,2),5,c(10,rep(0,9)))
    cat(iter, "of 50000\r") 
   flush.console()
 }
data=矩阵(c(0.01132162,1,0.04056053,1,0.11676735,0,0.12029087,1,
0.16197702,1,0.17190980,1,0.20386841,1,0.21251687,0,
0.36536492,0,0.40256414,1),ncol=2,byrow=T)
GIBS=功能(数据、a、b、β、R)
{
m=长度(R)
n1=总和(数据[,2]==1)
n2=m-n1
n=m+和(R)
N=c(n1,n2)
R1=c(0,R)
nstar=c()
对于(1:m中的i)nstar[i]=n-(i-1)-和(R1[1:i])
Z=c(数据[1,1],数据[2:m,1]-数据[1:(m-1),1])
f=函数(x)
{
A=0
对于(1:m中的i)A=A+x^i*nstar[i]*Z[i]
FR=1
对于(1:2中的j)FR=FR*(A+b[j])^(N[j]+A[j])
返回(x^(m*(m+1)/2)*exp(-beta*(x-1))/FR)
}
INT=integrate(f,1,Inf)$value
SG=功能(it)
{
uu=runif(1)
g0=函数(t)积分(f,1,t)$value/INT uu
aa=5

如果(g0(1)>0{while(g0(aa)>0)aa=aa+1}或者{while(g0(aa)关于函数的更新版本,如果您需要那么多迭代,我建议您利用它来运行它

以下是对函数原始版本的评论:

这部分函数(及其输入,
f
和数据对象)存在问题:

f
行开始

FR=prod((A+b)^(N+a))
它给出以下警告,这些警告会导致后续问题,并有效地形成无限循环:


我不明白这个函数试图完成什么,以便我可以帮助您修复它,但如果您更清楚地说明这一部分,我将尝试。

它没有完成的原因是代码中存在错误。对象长度在许多实例中都不一致。由于函数名为
GIBS
,我可以问一下这是否是支持的吗ed想成为Gibbs采样器?如果是的话,你可以找到一个预先存在的包/函数来满足你的需要。谢谢,我的函数不完全是Gibbs采样器。如果你把
打印出来(iter)
在for循环中,那么你就知道它有多远,进而可以猜测它需要多长时间。至于问题本身,R在循环中和
之间的速度不是很快,而
循环中,你的代码中有很多。假设这足够重要,可以证明这一点这项工作将是使用Rcpp,不幸的是,这将要求您在cpp@K.Ahmadi:Concernings speed中重写大部分代码helps@K.Ahmadi好的,太好了。如果这解决了您的问题,请将其标记为解决方案。如果不是,请用注释的更改更新您的问题。
FR=prod((A+b)^(N+a))
Warning messages:
1: In x^(1:m) : longer object length is not a multiple of shorter object length
2: In x^(1:m) * nstar :
  longer object length is not a multiple of shorter object length
3: In x^(1:m) * nstar * Z :
  longer object length is not a multiple of shorter object length
4: In A + b : longer object length is not a multiple of shorter object length
5: In N + a : longer object length is not a multiple of shorter object length
6: In x^(1:m) : longer object length is not a multiple of shorter object length
7: In x^(1:m) * nstar :
  longer object length is not a multiple of shorter object length
8: In x^(1:m) * nstar * Z :
  longer object length is not a multiple of shorter object length
9: In A + b : longer object length is not a multiple of shorter object length
10: In N + a : longer object length is not a multiple of shorter object length
11: In x^(1:m) : longer object length is not a multiple of shorter object length
12: In x^(1:m) * nstar :
  longer object length is not a multiple of shorter object length
13: In x^(1:m) * nstar * Z :
  longer object length is not a multiple of shorter object length
14: In A + b : longer object length is not a multiple of shorter object length
15: In N + a : longer object length is not a multiple of shorter object length