1

我正在使用以下函数来估计一些数据的内核密度

KDens = function(x,h,N) {
         fx = matrix(0,N,1)
         Kx = AK(x,h,N)
         for (i in 1:N) {
         fx[i] = sum(Kx[i,], na.rm=T)/N
         }
         return(fx) }

我知道这不是关于加速循环的第一个问题。我在网站上进行了检查,发现有时使用某些apply功能更快,但如果您设法正确设置循环,情况并非总是如此。

在上面的代码中,每个“不需要的东西”都被排除在循环之外,因为 - 如果我理解正确 - 建议加快计算速度。KDens但是,我对上面的函数和densityR 默认实现的函数做了一个比较。好吧,density需要 1 或 2 秒,而KDens在我的机器上需要 ~30 秒。

trywiththis <- rnorm(4800)
x = trywiththis
N = length(trywiththis)
h = 1.059*sd(trywiththis , na.rm=T)*(N^(-0.2))

编辑:我提供的信息不完整

kerf = function(x){ return(dnorm(x)) }
ker = function(x,x0,h){
       temp = kerf((x-x0)/h)/h
       return(temp)
       }

AK = function(x,h,N) {
      K = array(0,c(N,N))                 
         for (i in 1:N) {
         for (j in 1:N) {
         K[i,j] = ker(x[i],x[j],h) 
       }}
       return(K) }

假设我想加快 KDens 功能,我该怎么做?

4

1 回答 1

4

试试这个……对于你原来的 4800 长度的数据集,它需要 2.5 秒。

KDens2 = function(x,h,N) {
Kx <- outer( x , x , FUN = function(x,y) dnorm( ( x-y ) / h ) / h )
fx <- as.matrix( rowSums( Kx ) / N , ncol = 1 )
return( fx )
}

测试

set.seed(1)
trywiththis <- rnorm(480)
x = trywiththis
N = length(trywiththis)
h = 1.059*sd(trywiththis , na.rm=T)*(N^(-0.2))

#Produce same result? (posibly not identical because of 'dnorm' function)
all.equal( KDens(x,h,N) , KDens2(x,h,N) )
[1] TRUE

#Rough timing on N=480 length data...
system.time( KDens2(x,h,N) )
#   user  system elapsed 
#   0.01    0.00    0.02 

system.time( KDens(x,h,N) )
#   user  system elapsed 
#    2.7     0.0     2.7 

而当N=4800...

system.time( KDens2(x,h,N) )
   user  system elapsed 
   2.33    0.19    2.51
于 2014-04-22T11:02:46.257 回答