mapply 与将先前计算的输出作为输入的函数

mapply with function that takes previously computed output as input

我不确定如何在 R 中有效地执行此操作:

B <- matrix(0,p,L+1)
  for(jj in 1:L){
    B[,jj+1] <- enet(y, X, B[,jj], lamgrid[jj+1], alpha, printitn) # enet returns a p-dim vector
  }

我尝试使用 mapply,但我不知道如何将最近计算的 B[jj] 作为参数传递。

B[,2:] <- mapply(function(b,l)<- enet(y, X, b, l, alpha, printitn), ???, lamgrid[2:])

mapply 不这样做,它是广义的 map。您需要的算法称为 scanprefix (or cumulative) sum. Unfortunately it isn’t built into R (except in the specific case of an actual sum and product),我也不知道实现该操作的主流包。

不管怎样,你写的循环可能不是很优雅,但它的效率不是问题,使用扫描算法不会使它更高效。