[英]Speed up a loop in R
I am using the following function to estimate the Kernel density of some data 我正在使用以下函数来估计某些数据的内核密度
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) }
I know this is not the first question about speeding up a loop. 我知道这不是关于加快循环速度的第一个问题。 I checked around in the site, I found that sometimes using some
apply
function is faster, but this is not always the case if you manage to correctly set the loop. 我在该站点中进行了检查,发现有时使用一些
apply
功能会更快,但是如果您能够正确设置循环,这种情况并非总是如此。
In the above code, every "not needed thing" is left out of the loop, as - if I understood correctly - suggested to speed up computation. 在上面的代码中,每个“不需要的东西”都被排除在循环之外,因为-如果我理解正确的话-建议加快计算速度。 However, I made a comparison between the above
KDens
function and the density
function implemented in R by default. 但是,我将上述
KDens
函数与默认情况下在R中实现的density
函数进行了比较。 Well, density
needs 1 or 2 seconds, while KDens
needs ~30 on my machine. 好吧,
density
需要1或2秒,而我的机器上KDens
需要30秒。
trywiththis <- rnorm(4800)
x = trywiththis
N = length(trywiththis)
h = 1.059*sd(trywiththis , na.rm=T)*(N^(-0.2))
EDIT: the information I provided was not complete 编辑:我提供的信息不完整
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) }
Suppose I want to speed up the KDens function, how could I do that ? 假设我想加快KDens功能,我该怎么做?
Try this... For your original 4800 length dataset it takes 2.5 seconds. 尝试一下...对于原始的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
And when N=4800
... 当
N=4800
...
system.time( KDens2(x,h,N) )
user system elapsed
2.33 0.19 2.51
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.