我认为@MrFlick 的评论为我指明了正确的方向。除了建议的approxfun
方法和我的示例for
循环方法之外,我还意识到我可以使用mapply
. 请注意,我使用的approxfun
不会与使用 的其他两种方法的结果完全匹配which.min
,但我不太关心输出的差异,尽管其他方法可能会如此。
First, reproducing the sample data from the question:
set.seed(1)
x.sample <- rnorm(100)
sample.density <- density(x.sample)
G <- seq(-2,2, length.out=20)
现在,为循环版本创建一个函数
环形()
loop <- function(x, ref){
if(class(ref)!="density"){
ref <- density(ref)
}
ref.y <- ref$y
ref.x <- ref$x
G.dens <- c()
for(i in 1:length(x)){
t.G <- x[i]
G.dens[i] <- ref.y[which.min(abs(ref.x-t.G))]
}
G.dens
}
接下来,我将使用我想出的方法mapply
采样()
dsample <- function(x, ref){
if(class(ref)!="density"){
ref <- density(ref)
}
XisY <- function(x,y){ # which of several X values most closely matches a single Y value?
which.min(abs(y-x))
}
ref.y <- ref$y
ref.x <- ref$x
# ds <- approxfun(ref)
# ds(x)
ref.y[mapply(XisY, x, MoreArgs=list(y=ref.x))]
}
最后,approxfun
@MrFlick 建议的利用方法:
af()
af <- function(x, ref){
if(class(ref)!="density"){
ref <- density(ref)
}
# XisY <- function(x,y){ # which of several X values most closely matches a single Y value?
# which.min(abs(y-x))
# }
ref.y <- ref$y
ref.x <- ref$x
ds <- approxfun(ref)
ds(x)
# ref.y[mapply(XisY, x, MoreArgs=list(y=ref.x))]
}
现在比较它们的速度:
microbenchmark(
loop(G, sample.density),
dsample(G, sample.density),
af(G, sample.density)
)
# Unit: microseconds
# expr min lq mean median uq max neval
# loop(G, sample.density) 221.801 286.6675 360.3698 348.065 409.9785 942.071 100
# dsample(G, sample.density) 252.641 290.7900 359.0432 368.388 417.1510 520.960 100
# af(G, sample.density) 201.331 227.8740 276.0425 253.339 273.6225 2545.081 100
现在比较 G 大小增加时的速度:
speed.loop <- c()
speed.dsample <- c()
speed.af <- c()
lengths <- seq(20, 5E3, by=200)
for(i in 1:length(lengths)){
G <- seq(-2,2, length.out=lengths[i])
bm <- microbenchmark(
loop(G, sample.density),
dsample(G, sample.density),
af(G, sample.density), times=10
)
means <- aggregate(bm$time, by=list(bm$expr), FUN=mean)[,"x"]/1E6 # in milliseconds
speed.loop[i] <- means[1]
speed.dsample[i] <- means[2]
speed.af[i] <- means[3]
}
speed.ylim <- range(c(speed.loop, speed.dsample, speed.af))
plot(lengths, (speed.loop), ylim=(speed.ylim), type="l", ylab="Time (milliseconds)", xlab="# Elements in G")
lines(lengths, (speed.dsample), col="red")
lines(lengths, (speed.af), col="blue")